Skip to content

Implement OpenTelemetry for Go service #452

@davideme

Description

@davideme

Overview

Implement the OpenTelemetry integration for the Go (Chi + sqlc) service as specified in src/go/adr/008-opentelemetry.md.

Parent issue: #14

Tasks

  • Add required Go modules (go.opentelemetry.io/otel, otel/sdk, otlptracegrpc, otlpmetricgrpc, otelhttp, contrib/instrumentation/runtime, semconv/v1.26.0)
  • Initialize SDK at startup — use no-op providers when OTEL_EXPORTER_OTLP_ENDPOINT is absent
  • Wrap Chi router with otelhttp.NewHandler for inbound HTTP traces and metrics
  • Wrap outbound HTTP clients with otelhttp.NewTransport
  • Add manual span wrapping around sqlc repository calls (no auto-instrumentation available)
  • Enable Go runtime metrics via go.opentelemetry.io/contrib/instrumentation/runtime (goroutines, heap, GC)
  • Inject trace/span IDs into log/slog structured log entries
  • Verify metrics baseline: http.server.request.duration, http.server.active_requests, go.goroutine.count, go.memory.used, go.gc.duration
  • Set resource attributes: service.name=lamp-control-api-go, service.version, deployment.environment
  • Ensure W3C TraceContext + Baggage propagation is enabled
  • Add/update tests to verify instrumentation wires up correctly

Instrumentation summary

Signal Mechanism Type
Traces – Inbound HTTP otelhttp.NewHandler Config only
Traces – Outbound HTTP otelhttp.NewTransport Config only
Traces – Database (sqlc) Manual span wrapping via OTel Tracer API Custom code
Metrics – HTTP server otelhttp (auto-emitted) Config only
Metrics – Go runtime contrib/instrumentation/runtime Config only
Logs Manual trace.SpanFromContext + log/slog injection Custom code

Acceptance criteria

  • SDK is a no-op when OTEL_EXPORTER_OTLP_ENDPOINT is not set
  • All three pillars (traces, metrics, logs) emit data when collector is configured
  • Existing tests continue to pass

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions