Java tracing installation
- 1
Install the OpenTelemetry Java agent
RequiredFor the complete SDK reference, see the OpenTelemetry Java docs.
The zero-code Java agent is the recommended way to instrument a Java app. Download the latest agent jar:
TerminalThe agent instruments your application at runtime, so you don't need to add any dependencies to get started. If you'd rather instrument manually, see the SDK alternative in the configuration step.
- 2
Get your project token
RequiredYou'll need your PostHog project token to authenticate trace requests. This is the same token you use for capturing events with the PostHog SDK.
Important: Use your project token which starts with
phc_. Do not use a personal API key (which starts withphx_).You can find your project token in Project settings.
- 3
Configure the agent
RequiredSet the OpenTelemetry environment variables to export spans to PostHog over OTLP HTTP, then start your app with the agent attached.
TerminalTerminalTo instrument manually instead, add the SDK dependencies (
opentelemetry-api,opentelemetry-sdk, andopentelemetry-exporter-otlpvia theopentelemetry-bom) and build the exporter in code:JavaThe
buildAndRegisterGlobal()call registers the SDK as the global instance, soGlobalOpenTelemetry.getTracer(...)in the next step works. (The Java agent does this for you, which is why the zero-code path needs no registration.)Note: Pass the full
/i/v1/tracespath to the traces endpoint.OtlpHttpSpanExporteralready sends OTLP over HTTP/protobuf, so no protocol setting is needed in code. If you configure via the environment variables above instead, setOTEL_EXPORTER_OTLP_TRACES_PROTOCOL=http/protobuf– the SDK's autoconfigure path defaults to gRPC. - 4
Create spans
RequiredWrap the operations you want to measure in spans, and attach attributes for context.
Java - 5
Next steps
CheckpointWhat you can do with your tracesAction Description Why you need distributed tracing What a trace shows you that nothing else does Explore traces Read a trace as a waterfall to see where time goes Filter spans Narrow down by service, status, duration, and attributes Propagate context Pass trace context across services so spans join the same trace