hydrate_trace.go 763 B

123456789101112131415161718192021
  1. package middleware
  2. import (
  3. "net/http"
  4. "github.com/porter-dev/porter/internal/telemetry"
  5. "go.opentelemetry.io/otel/trace"
  6. )
  7. // HydrateTraces pulls related IDs from requests, and puts them into a span which already exists.
  8. // If no span already exists, these attibutes will not be populated. This should not be used as a replacement for creating your own spans.
  9. // This should be added as the last middleware in the chain, so that it can pull IDs from the request context.
  10. func HydrateTraces(next http.Handler) http.Handler {
  11. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  12. ctx := r.Context()
  13. span := trace.SpanFromContext(ctx)
  14. telemetry.AddKnownContextVariablesToSpan(ctx, span)
  15. r = r.Clone(ctx)
  16. next.ServeHTTP(w, r)
  17. })
  18. }