Azure Application Insights: Open Telemetry based Auto Instrumentation in spring-boot
Recently, I have used Azure Application Insights: Open Telemetry-based Auto Instrumentation in Spring Boot to enable Distributed Tracing. Thus, I was able to achieve the following things
- The trace ID and span ID were auto-generated and displayed in the console logs
- The Azure Monitor was able to collect, analyze and act on the telemetry data received from the application
Distributed Tracing is used to track the end-to-end application execution flow across multiple microservices. Open Telemetry is used for End-to-End Distributed Tracing. It helps us in enabling Observability for our application.
By Definition,
Open Telemetry is a collection of tools, APIs, and SDKs. Use it to the instrument, generate, collect, and export telemetry data (metrics, logs, and traces) to help you analyze your software’s performance and behavior.
Traces and Spans are the crucial components of Open Telemetry. TraceID is an ID assigned to each user-initiated web request, and it remains the same throughout the flow. Thus, whenever you see the same traceID at multiple places across multiple microservices, You can assume that it’s the same request flowing across all of them.
SpanID represents a basic unit of work, and it is printed along with a trace ID. For every operation, a unique Span ID is assigned. Thus, whenever a glitch happens, the developer should be able to trace which service actually faults during the web request.

In the above picture, You can see the traceID is the same for service A, service B, and Service C while the span ID changes for every operation.
How to enable this with a No Code Approach?
Step 1:
Add the following Gradle libraries as dependencies.
implementation ‘com.microsoft.azure:applicationinsights-runtime-attach:3.4.2’
implementation ‘com.microsoft.azure:applicationinsights-agent:3.4.2’
Step 2:
Then invoke the attach method n the first line of main method
@SpringBootApplication
public class SpringBootApp {public static void main(String[] args) {
ApplicationInsights.attach();
SpringApplication.run(SpringBootApp.class, args);
}
}
Step 3:
Create an applicationinsights.json file in the src/main/resources folder. It should look something like below
{
"connectionString": "...",
"role": {
"name": "my cloud role name"
},
"sampling": {
"percentage": 100
},
"jmxMetrics": [
],
"customDimensions": {
},
"instrumentation": {
"logging": {
"level": "INFO"
},
"micrometer": {
"enabled": true
}
},
"proxy": {
},
"preview": {
"processors": [
]
},
"selfDiagnostics": {
"destination": "file+console",
"level": "INFO",
"file": {
"path": "applicationinsights.log",
"maxSizeMb": 5,
"maxHistory": 1
}
}
}
That’s it !! You have enabled Open Telemetry-based Auto Instrumentation in your spring-boot application.
Other potential tools like Spring Cloud Sleuth + OpenTelmetry + Jaeger /Zipkin can also be used to achieve distributed tracing. Feel free to explore them !!