opentracing-sparkjava
OpenTracing Instrumentation for SparkJava
This repo contains tracing filters and an exception handler for SparkJava. The filters extract tracing headers from incoming requests to create OpenTracing spans. These spans can be exported to a variety of backends including Jaeger, Hawkular, and others.
Dependencies
The Spark dependency is marked as compileOnly and must be supplied by the application.
compile group: 'com.sparkjava', name: 'spark-core', version: '2.7.+'
Usage
To enable tracing you need to add before
, exception
and afterAfter
hooks:
OpenTracingSparkFilters sparkTracingFilters = new OpenTracingSparkFilters(tracer);
Spark.before(sparkTracingFilters.before());
Spark.afterAfter(sparkTracingFilters.afterAfter());
Spark.exception(sparkTracingFilters.exception());
// tracing is added for all routes
Spark.get("/hello", (req, res) -> "hello world");
To access the current span in a resource retrieve it from the request attributes using OpenTracingSparkFilters.SERVER_SPAN
:
Spark.get("/path", (req, res) -> {
Span span = req.attribute(OpenTracingSparkFilters.SERVER_SPAN);
tracer.buildSpan("child").asChildOf(span).withTag("test", "value").start().finish();
//do stuff
return "hello world";
});