Technology Blog Posts by SAP
cancel
Showing results for 
Search instead for 
Did you mean: 

Introduction

When troubleshooting application issues, developers often start with logs. While logs provide valuable information, they may not always provide the complete journey of a request across different application layers.

In modern cloud applications, a single request can flow through multiple components such as APIs, application services, and databases. Understanding where a request spends time or where a failure occurs requires deeper visibility into the request lifecycle.

This is where distributed tracing helps. Tracing provides insights into the complete execution flow of a request by capturing individual operations and correlating them together.

OpenTelemetry is an open-source observability framework that enables applications to generate telemetry data such as traces, metrics, and logs.

In this blog, we will explore how to enable tracing in an SAP CAP Java application using the OpenTelemetry Java Agent.

The key advantage of using the Java Agent is zero-code instrumentation. The agent attaches to the application at runtime and automatically instruments supported frameworks and libraries, capturing telemetry data without requiring any changes to the CAP application code.

We will use a simple SAP CAP Java application and enable tracing to observe how a single OData request is captured across different layers, including HTTP processing, SAP CAP execution, and database interactions. We will also explore the generated Trace ID, Span IDs, and request attributes.

Sample SAP CAP Java Application

For this example, we use a simple SAP CAP Java application exposing a CatalogService with a Books entity.

db/schema.cds

namespace sap.capire.bookshop;

entity Books {
    key ID    : Integer;
        title : String;
}

srv/catalog-service.cds

using sap.capire.bookshop as db;

service CatalogService {
    entity Books as projection on db.Books;
}

The service is exposed through the OData endpoint:

GET http://localhost:8080/odata/v4/CatalogService/Books

The above endpoint can be used to retrieve Books data. For demonstrating distributed tracing, we will use a POST request to create a new Book entry and observe how the OpenTelemetry Java Agent captures the complete request flow across different application layers.

The CAP application remains unchanged. The OpenTelemetry Java Agent will be attached at runtime to capture tracing information.

Understanding OpenTelemetry Java Agent

The traditional approach to adding observability requires developers to:

  • Add OpenTelemetry dependencies
  • Configure instrumentation
  • Add tracing logic in application code

The OpenTelemetry Java Agent provides a simpler approach. The agent attaches to the JVM during application startup and automatically instruments supported frameworks and libraries.

For a CAP Java application, this allows automatic capture of:

  • Incoming HTTP requests
  • Database calls
  • Framework interactions

The application remains unchanged while telemetry data is generated automatically.

Enabling OpenTelemetry Java Agent

Download the Java Agent
Download the OpenTelemetry Java Agent JAR and place it in your project directory.

otel-cap-demo
│
├── opentelemetry-javaagent.jar
│
└── srv

Start the CAP Application with the Agent

mvn spring-boot:run \
-Dspring-boot.run.jvmArguments="-javaagent:/path/to/opentelemetry-javaagent.jar \
-Dotel.service.name=otel-cap-demo \
-Dotel.traces.exporter=logging \
-Dotel.metrics.exporter=none \
-Dotel.logs.exporter=none"

In this setup, we enable only the trace exporter and disable metrics and logs to keep the output focused and easier to understand.

Once the application starts, the agent automatically begins capturing trace data for incoming requests - without any changes to the CAP application code.

Generating a Trace

Invoke the OData endpoint:

curl --request POST \
  --url http://localhost:8080/odata/v4/CatalogService/Books \
  --header 'Content-Type: application/json' \
  --data '{
    "ID": 1,
    "title": "OpenTelemetry Guide"
  }'

Once the request is triggered, the OpenTelemetry Java Agent automatically captures the complete execution flow and generates a trace with multiple spans across different application layers.

The following log output shows the trace generated by the OpenTelemetry Java Agent. Each entry represents an individual span created during request processing, along with the associated Trace ID, Span ID, span type, and captured attributes.

2026-08-06T13:07:20.092+05:30  INFO 52419 --- [nio-8080-exec-1] c.s.c.a.odata.v4.metadata.MetadataInfo   : Loading OData V4 metadata for service 'CatalogService'
[otel.javaagent 2026-08-06 13:07:20:296 +0530] [http-nio-8080-exec-1] INFO io.opentelemetry.exporter.logging.LoggingSpanExporter - 'c65327bf-6d2d-4585-ae7f-af29c5185126' : 815936e6ae357c12bd7a2493f7833343 eb4ba2a612228683 CLIENT [tracer: io.opentelemetry.jdbc:2.30.0-alpha] AttributesMap{data={thread.name=http-nio-8080-exec-1, db.name=c65327bf-6d2d-4585-ae7f-af29c5185126, thread.id=51, db.statement=SET @valid_to = NULL; SET @locale = NULL; SET @applicationuser = NULL; SET @valid_from = NULL; SET @tenant = NULL, db.system=h2, db.connection_string=h2:mem:, db.user=sa}, capacity=128, totalAddedValues=7}
[otel.javaagent 2026-08-06 13:07:20:300 +0530] [http-nio-8080-exec-1] INFO io.opentelemetry.exporter.logging.LoggingSpanExporter - 'c65327bf-6d2d-4585-ae7f-af29c5185126' : 815936e6ae357c12bd7a2493f7833343 8369c802764b1b93 CLIENT [tracer: io.opentelemetry.jdbc:2.30.0-alpha] AttributesMap{data={thread.name=http-nio-8080-exec-1, db.name=c65327bf-6d2d-4585-ae7f-af29c5185126, thread.id=51, db.statement=SET @valid_to = TIMESTAMP WITH TIME ZONE?; SET @applicationuser = ?; SET @valid_from = TIMESTAMP WITH TIME ZONE?, db.system=h2, db.connection_string=h2:mem:, db.user=sa}, capacity=128, totalAddedValues=7}
[otel.javaagent 2026-08-06 13:07:20:361 +0530] [http-nio-8080-exec-1] INFO io.opentelemetry.exporter.logging.LoggingSpanExporter - 'INSERT c65327bf-6d2d-4585-ae7f-af29c5185126.SAP_CAPIRE_BOOKSHOP_BOOKS' : 815936e6ae357c12bd7a2493f7833343 1ef4904262677a14 CLIENT [tracer: io.opentelemetry.jdbc:2.30.0-alpha] AttributesMap{data={thread.name=http-nio-8080-exec-1, db.name=c65327bf-6d2d-4585-ae7f-af29c5185126, db.operation=INSERT, db.sql.table=SAP_CAPIRE_BOOKSHOP_BOOKS, thread.id=51, db.statement=INSERT INTO ? (?, ?) VALUES (?, ?), db.system=h2, db.connection_string=h2:mem:, db.user=sa}, capacity=128, totalAddedValues=9}
[otel.javaagent 2026-08-06 13:07:20:362 +0530] [http-nio-8080-exec-1] INFO io.opentelemetry.exporter.logging.LoggingSpanExporter - 'CQN INSERT Books' : 815936e6ae357c12bd7a2493f7833343 d1d6b9127270622b INTERNAL [tracer: com.sap.cds:] AttributesMap{data={thread.name=http-nio-8080-exec-1, thread.id=51, cds.cqn.target_entity=CatalogService.Books, cds.cqn.to_protocol=sql}, capacity=128, totalAddedValues=4}
[otel.javaagent 2026-08-06 13:07:20:381 +0530] [http-nio-8080-exec-1] INFO io.opentelemetry.exporter.logging.LoggingSpanExporter - 'POST /odata/v4/CatalogService/**' : 815936e6ae357c12bd7a2493f7833343 70860b0f76119b32 SERVER [tracer: io.opentelemetry.tomcat-10.0:2.30.0-alpha] AttributesMap{data={server.port=8080, http.response.status_code=201, thread.id=51, http.request.method=POST, http.route=/odata/v4/CatalogService/**, network.peer.address=0:0:0:0:0:0:0:1, server.address=localhost, client.address=0:0:0:0:0:0:0:1, url.path=/odata/v4/CatalogService/Books, network.peer.port=63772, url.scheme=http, thread.name=http-nio-8080-exec-1, network.protocol.version=1.1, user_agent.original=bruno-runtime/2.7.0}, capacity=128, totalAddedValues=14}

For this request, the generated Trace ID is:

815936e6ae357c12bd7a2493f7833343

The trace contains multiple spans representing different operations involved in processing the request:

| Layer          | Operation                             | Span ID                                |
| -------------- | ------------------------------------- | -------------------------------------- |
| HTTP Layer     | POST `/odata/v4/CatalogService/Books` | `70860b0f76119b32`                     |
| SAP CAP Layer  | CQN INSERT Books                      | `d1d6b9127270622b`                     |
| Database Layer | INSERT Books                          | `1ef4904262677a14`                     |

The HTTP span captures the incoming request details, the SAP CAP span captures the framework-level processing, and the JDBC spans capture the database operations triggered during request execution.

All spans are correlated using the same Trace ID along with the timestamp, providing an end-to-end view of the request lifecycle across application layers.

Benefits of Trace and Span Visibility

The trace and span information generated by the OpenTelemetry Java Agent provides deeper insights into application execution:

  • End-to-end request visibility: Follow a request across HTTP, SAP CAP, and database layers using a single Trace ID.
  • Faster troubleshooting: Identify the exact layer where failures or performance delays occur.
  • Performance analysis: Analyze individual operations using span-level execution details.
  • Improved observability: Understand application behavior without adding custom logging or tracing code.
  • Enhanced security monitoring: Trace and span data help detect unusual patterns, unauthorized access attempts, or anomalies across services, enabling quicker identification of potential security issues.

Conclusion

The OpenTelemetry Java Agent provides a simple and effective approach to enable distributed tracing in SAP CAP Java applications through zero-code instrumentation.

By attaching the agent at runtime, we were able to automatically capture the complete request flow - from the incoming OData call, through SAP CAP processing, and finally to database execution - without making any changes to the application code.

The generated traces and spans provide valuable visibility into application behavior, making it easier to monitor performance, troubleshoot issues, and understand interactions between different application layers.

With minimal configuration and no additional instrumentation effort, the OpenTelemetry Java Agent enables SAP CAP applications to achieve enhanced observability and improved operational insight.

 

Labels in this area