Java

Embed
Preview of the card
<a href="https://forumulate.com/p/watchtower/docs/onboarding-java"><img src="https://forumulate.com/api/public/orgs/watchtower/spaces/docs/pages/f2bee642-7492-4654-880b-1c0d16994317/social-card.svg?v=3e7ae33eec8b" alt="Java" width="600" height="315"></a>

<!-- status: ga language: java -->

Java

Send traces from a Java service to Watchtower over OTLP/HTTP with the standard OpenTelemetry SDK. Nothing Watchtower-specific is installed: the SDK reads the standard OTEL_* variables.

  1. Create a project, then an ingestion key (wti_...) with the ingest:traces scope. The key is shown once.

  2. Point the SDK at the gateway and pass the key in the x-watchtower-key header (Authorization: Bearer wti_... works too):

export OTEL_EXPORTER_OTLP_ENDPOINT=https://ingest.example.com
export OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
export OTEL_EXPORTER_OTLP_HEADERS=x-watchtower-key=wti_YOUR_KEY
export OTEL_SERVICE_NAME=my-service
  1. Add the SDK and run this once. The endpoint above is the gateway's base URL; the SDK appends /v1/traces.

implementation("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure")
implementation("io.opentelemetry:opentelemetry-exporter-otlp")
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;

public class Onboarding {
    public static void main(String[] args) {
        // reads OTEL_EXPORTER_OTLP_*
        OpenTelemetrySdk sdk = AutoConfiguredOpenTelemetrySdk.initialize().getOpenTelemetrySdk();
        Tracer tracer = sdk.getTracer("onboarding");
        tracer.spanBuilder("hello-watchtower").startSpan().end();
        sdk.close();
    }
}
  1. Open the project's onboarding screen. It polls GET /v1/projects/{prj}/onboarding/first-event and turns green when the first span arrives. Then open the trace in the trace explorer.

For an existing app you can skip the code: run it with the OpenTelemetry Java agent (-javaagent:opentelemetry-javaagent.jar) and the same variables.

Updated