Skip to main content

Java Tracing Integration

OpenTelemetry support#

Pyroscope can integrate with distributed tracing systems supporting OpenTelemetry standard which allows you to link traces with the profiling data, and find specific lines of code related to a performance issue.

  • Because of how sampling profilers work, spans shorter than the sample interval may not be captured. By default pyroscope CPU profiler probes stack traces 100 times per second, meaning that spans shorter than 10ms may not be captured.

Java code can be easily instrumented with otel-profiling-java package package - a SpanProcessor implementation, that annotates profiling data with span IDs which makes it possible to filter out profile of a particular trace span in Pyroscope.

Running as otel-java-instrumentation extension#

Download latest opentelemetry-javaagent.jar and pyroscope-otel.jar

java -jar ./build/libs/rideshare-1.0-SNAPSHOT.jar \    -javaagent:./opentelemetry-javaagent.jar \    -Dotel.javaagent.extensions=./pyroscope-otel.jar \    -Dotel.pyroscope.start.profiling=true \  \    -Dpyroscope.format=jfr \    -Dpyroscope.profiler.event=itimer \    -Dpyroscope.server.address=$PYROSCOPE_SERVER_ADDRESS \    # rest of your otel-java-instrumentation configuration

Running as manually configured otel#

Add pyroscope otel depedency


Now we can create and configure the tracer provider:

// obtain SdkTracerProviderBuilderSdkTracerProviderBuilder tpBuilder = ...
// Add PyroscopeOtelSpanProcessor to SdkTracerProviderBuilderPyroscopeOtelConfiguration pyroscopeTelemetryConfig = new PyroscopeOtelConfiguration.Builder()  .setAppName("" +  .setPyroscopeEndpoint(System.getenv("PYROSCOPE_PROFILE_URL"))  .setAddProfileURL(true)  .setAddSpanName(true)  .setRootSpanOnly(true)  .setAddProfileBaselineURLs(true)  .build();tpBuilder.addSpanProcessor(new PyroscopeOtelSpanProcessor(pyroscopeOtelConfig));

Now that we set up the tracer, we can create a new trace from anywhere:

Span span = tracer.spanBuilder("findNearestVehicle").startSpan();try (Scope s = span.makeCurrent()){    // Your code goes here.} finally {    span.end();}

Configuration options#

Configuration options may be set as a system property -Dotel.pyroscope.start.profiling=true or as environment variable OTEL_PYROSCOPE_START_PROFILING=true

  • otel.pyroscope.start.profiling - Boolean flag to start PyroscopeAgent. Set to false if you want to start the PyroscopeAgent manually. Default: true.
  • - Application name for profiler/baseline urls. if otel.pyroscope.start.profiling=true then it is ignored and app name is taken from or from the generated name if the pyroscope app name was not configured.
  • otel.pyroscope.endpoint - Pyroscope server address url. if otel.pyroscope.start.profiling=true then it is ignored and server address is taken from pyroscope.server.address or set to http://localhost:4040 by pyroscope configuration
  • otel.pyroscope.root.span.only - Boolean flag. When enabled, the tracer will annotate only the first span created locally (the root span), but the profile will include samples of all the nested spans. This may be helpful in case if the trace consists of multiple spans shorter than 10ms and profiler can't collect and annotate samples properly. Default: true.
  • - Boolean flag. Controls whether the span name added to profile labels. Default: true. automatically. If enabled, please make sure span names do not contain unique values, for example, a URL. Otherwise, this can increase data cardinality and slow down queries.
  • otel.pyroscope.add.profile.url - Boolean flag to attach pyroscope profile urls to spans. Default: true.
  • otel.pyroscope.add.profile.baseline.url - Boolean flag to attach pyroscope profile diff/comparison urls to spans. Default: true.


Collected profiles can be viewed in Pyroscope UI using FlameQL:

  •{profile_id="<spanID>"} - Shows flamegraph for a particular span.
  •{span_name="ExampleSpan"} - Shows aggregated profile for all spans named ExampleSpan.

For convenience, the tracer annotates profiled spans with extra attributes:

  • - is set to span ID to indicate that profile was captured for a span.
  • pyroscope.profile.url - contains the URL to the flamegraph in Pyroscope UI.
  • pyroscope.profile.baseline.url - contains the URL to the baseline comparison view in Pyroscope UI.

A baseline profile is an aggregate of all span profiles. For example, consider two exemplars (the number here replaces profiling data):

  •{region="us=east",env="dev",span_name="FetchData",profile_id="abc"} 100
  •{region="us=east",env="dev",span_name="FetchData",profile_id="zyx"} 200

Then, the baseline profile for each of them is:

  •{region="us=east",env="dev",span_name="FetchData"} 300


Check out the examples directory in our repository to find a complete example application that demonstrates tracing integration features (using a custom jaeger ui) and learn more!

jaeger example