Tutorial: Instrumenting a Custom Scheduler with the Instana Tracing SDK
Schedulers are tools to run jobs periodically. The most popular Java scheduler is quartz, which is supported out-of-the-box by the Instana.
However, in case you have a custom in-house scheduler that is not supported, you can use the Java Trace SDK to add tracing for your scheduler.
This tutorial shows how to do this.
- Example Code
- Source Not Monitored By Instana
- Why is the source not monitored?
- Enabling Tracing for the Custom Scheduler
- Adding Tags
- Summary and Outlook
Example Code
The example code for the custom-scheduler-example
tutorial can be found on github.com. It implements a simple
scheduler, that calls a Hello World service on http://localhost:8080/ every 4 seconds and prints the response to the console.
Source Not Monitored By Instana
If you run the example, you will see that the HTTP calls are coming from an unmonitored source.
Why is the source not monitored?
The example code uses the Apache HTTP Client for the outgoing call. The Apache HTTP Client is supported by the Instana agent. So why do we not see the source of the request in Instana?
The reason is that Instana instruments outgoing calls only if they happen in an existing trace context. In the example, the outgoing call is triggered by the custom scheduler which is not instrumented. Therefore, the call is not instrumented because no existing trace context is found.
Enabling Tracing for the Custom Scheduler
In order to enable tracing for the custom scheduler, you first need to add the Java Trace SDK as a dependency. For Maven, it looks like this:
<dependency>
<groupId>com.instana</groupId>
<artifactId>instana-java-sdk</artifactId>
<version>1.2.0</version>
</dependency>
In order to create a new trace context whenever the HttpClientJob
is called, add a @Span
annotation to the job's run()
method:
@Span(type = Span.Type.ENTRY, value = "my-custom-scheduler")
public void run() {
// ...
}
This annotation tells the Java agent to generate an ENTRY
span whenever the run()
method is called.
Third, add the following to the Instana agents configuration.yaml
file to tell it that Java classes in package com.instana.sample.*
should be scanned for the @Span
annotation:
com.instana.plugin.javatrace:
instrumentation:
sdk:
packages:
- 'com.instana.sample'
Now, each call to HelloWorldJob.run()
creates a trace context, and the outgoing HTTP request is performed within this trace context. You see this in Instana as follows:
The source of the HTTP GET request is now traced, including all metadata gathered from the automatic instrumentation of the Apache HTTP Client:
The scheduler job itself is visualized as an empty call with an unmonitored source:
Summary and Outlook
In this tutorial, we created a new trace context for our custom scheduler, thus enabling the batch job to be recognized as a source for outgoing HTTP calls in Instana.
For a full reference on our SDK, view the SDK's JavaDoc. For example, the @SpanParam
and @SpanReturn
annotations make it really easy to capture parameters or return values as additional information far spans, which
might be more convenient than explicitly calling SpanSupport.annotate()
in some scenarios.