Request context
In Go, spans are linked to the request through the request’scontext.Context. Pass the request context, or a context derived from it, to instrumented libraries and to the SDK’s functions. The request context to pass depends on your framework:
Instrument libraries
Use OpenTelemetry instrumentation libraries to trace supported libraries. For example, to trace database queries made withdatabase/sql, install otelsql:
otelsql.Open instead of sql.Open, and pass the request context to your queries:
otelhttp:
otelhttp transport, and create requests with the request context:
Existing OpenTelemetry setup
Apitally works alongside your existing OpenTelemetry instrumentation and exporters. If your application registers a tracer provider fromgo.opentelemetry.io/otel/sdk/trace with otel.SetTracerProvider before starting the server, Apitally automatically adds its span processor to it.
otelgin or otelhttp, register its middleware before calling apitally.Init, or wrap your handler with otelhttp.NewHandler. Apitally then adds its data to the request span created by the instrumentation instead of creating a second one.
Create custom spans
For custom operations that are not covered by library instrumentation, you can create spans using the standard OpenTelemetry API. Start each span with the request context, or a context derived from it, and pass the returned context to nested operations:Capture handled errors
Apitally includes errors in request traces to help you diagnose server errors. Panics are captured automatically, as are errors that are returned to Echo or Fiber, or added to the Gin context withc.Error(), and result in a server error response.
If your application handles an error itself, for example by writing an error response directly, call apitally.CaptureError() with the request context to include it in the current request trace.