Skip to main content
Dual export from a Go service on net/http. Built from the example examples/otel-go-nethttp: Go 1.22 routing patterns, otelhttp v0.72.0 and the OpenTelemetry Go SDK v1.47.0. tests/otelapps runs it against stock collectors in every export mode, the apipath tag runs it against the real platform collector, and tests/collector uses the same SDK version for the collector’s own integration suite. Read the API quickstart first for the token and the environment variables.

Modules

The example has its own go.mod, so you can copy the directory as it is. Its direct dependencies:

The SDK setup

One tracer provider and one meter provider, each with two exporters, so both backends see the same data from the same SDK state. Only the platform exporters carry the token:

Instrumenting the server

Wrap the whole mux with otelhttp.NewHandler. Health checks are filtered out so probes do not count as API traffic:
otelhttp names the server span after the mux pattern and puts http.route on its metrics, but not on the span. Copy the pattern onto the span as well, so a traces-only export and your own backend group requests by route too (http-route.md):

Notes

  • Shut down cleanly. Call the returned shutdown function after http.Server.Shutdown, so the last interval’s metrics are exported. The meter provider is shut down before the tracer provider.
  • Endpoints are given as full URLs with WithEndpointURL (.../v1/metrics, .../v1/traces). The standard OTEL_EXPORTER_OTLP_* variables would apply to both exporters alike, so the example does not use them for the platform copy.

Export modes the tests cover