telemetry.go69 lines, 2.5 KB
| 1 | // Package telemetry sets up OpenTelemetry tracing, and carries trace context |
| 2 | // across the hops that aren't HTTP or gRPC calls. |
| 3 | package telemetry |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "fmt" |
| 8 | "log/slog" |
| 9 | "os" |
| 10 | |
| 11 | "go.opentelemetry.io/otel" |
| 12 | "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp" |
| 13 | "go.opentelemetry.io/otel/propagation" |
| 14 | "go.opentelemetry.io/otel/sdk/resource" |
| 15 | sdktrace "go.opentelemetry.io/otel/sdk/trace" |
| 16 | semconv "go.opentelemetry.io/otel/semconv/v1.40.0" |
| 17 | ) |
| 18 | |
| 19 | // Options says which service is being traced. |
| 20 | type Options struct { |
| 21 | Service string |
| 22 | Version string |
| 23 | Env string // development or production |
| 24 | } |
| 25 | |
| 26 | // Setup sends traces to the collector in OTEL_EXPORTER_OTLP_ENDPOINT, the |
| 27 | // standard variable. Without one, tracing is off: spans cost next to nothing, |
| 28 | // and tests need no collector. Call shutdown before exiting, to send the |
| 29 | // spans still waiting in the batch. |
| 30 | func Setup(ctx context.Context, o Options) (shutdown func(context.Context) error, err error) { |
| 31 | // Trace context travels in the W3C headers, traceparent and tracestate, |
| 32 | // and so does baggage. With tracing off, we still pass them on. |
| 33 | otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator( |
| 34 | propagation.TraceContext{}, propagation.Baggage{}, |
| 35 | )) |
| 36 | if os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT") == "" { |
| 37 | return func(context.Context) error { return nil }, nil |
| 38 | } |
| 39 | exporter, err := otlptracehttp.New(ctx) |
| 40 | if err != nil { |
| 41 | return nil, fmt.Errorf("telemetry: %w", err) |
| 42 | } |
| 43 | // Errors the SDK can't return to anyone, like an export that failed. |
| 44 | otel.SetErrorHandler(otel.ErrorHandlerFunc(func(err error) { |
| 45 | slog.Warn("telemetry", "err", err) |
| 46 | })) |
| 47 | |
| 48 | // What every span says about where it came from. We list it ourselves, |
| 49 | // instead of detecting it: only what we choose goes into our traces. |
| 50 | res := resource.NewWithAttributes(semconv.SchemaURL, |
| 51 | semconv.ServiceName(o.Service), |
| 52 | semconv.ServiceVersion(o.Version), |
| 53 | semconv.DeploymentEnvironmentName(o.Env), |
| 54 | ) |
| 55 | // Keep every trace in development, and one in ten in production. The |
| 56 | // decision is made once, where a trace starts: everyone downstream |
| 57 | // follows their parent, so a trace is kept whole, or not at all. |
| 58 | ratio := 1.0 |
| 59 | if o.Env == "production" { |
| 60 | ratio = 0.1 |
| 61 | } |
| 62 | provider := sdktrace.NewTracerProvider( |
| 63 | sdktrace.WithBatcher(exporter), // in the background, a batch at a time |
| 64 | sdktrace.WithResource(res), |
| 65 | sdktrace.WithSampler(sdktrace.ParentBased(sdktrace.TraceIDRatioBased(ratio))), |
| 66 | ) |
| 67 | otel.SetTracerProvider(provider) |
| 68 | return provider.Shutdown, nil |
| 69 | } |