|
| 1 | +package cloudotel |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + |
| 7 | + gcppropagator "github.com/GoogleCloudPlatform/opentelemetry-operations-go/propagator" |
| 8 | + "go.einride.tech/cloudrunner/cloudstream" |
| 9 | + "go.einride.tech/cloudrunner/cloudzap" |
| 10 | + "go.opentelemetry.io/otel/propagation" |
| 11 | + "go.opentelemetry.io/otel/trace" |
| 12 | + "go.uber.org/zap" |
| 13 | + "google.golang.org/grpc" |
| 14 | + "google.golang.org/grpc/metadata" |
| 15 | +) |
| 16 | + |
| 17 | +type TraceHook func(context.Context, trace.SpanContext) context.Context |
| 18 | + |
| 19 | +// TraceMiddleware that ensures incoming traces are forwarded and included in logging. |
| 20 | +type TraceMiddleware struct { |
| 21 | + // ProjectID of the project the service is running in. |
| 22 | + ProjectID string |
| 23 | + // TraceHook is an optional callback that gets called with the parsed trace context. |
| 24 | + TraceHook TraceHook |
| 25 | + // propagator is a opentelemetry trace propagator |
| 26 | + propagator propagation.TextMapPropagator |
| 27 | +} |
| 28 | + |
| 29 | +func NewTraceMiddleware() TraceMiddleware { |
| 30 | + propagator := propagation.NewCompositeTextMapPropagator( |
| 31 | + gcppropagator.CloudTraceFormatPropagator{}, |
| 32 | + propagation.TraceContext{}, |
| 33 | + propagation.Baggage{}, |
| 34 | + ) |
| 35 | + return TraceMiddleware{ |
| 36 | + TraceHook: TraceIDHook, |
| 37 | + propagator: propagator, |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +// GRPCServerUnaryInterceptor provides unary RPC middleware for gRPC servers. |
| 42 | +func (i *TraceMiddleware) GRPCServerUnaryInterceptor( |
| 43 | + ctx context.Context, |
| 44 | + req interface{}, |
| 45 | + _ *grpc.UnaryServerInfo, |
| 46 | + handler grpc.UnaryHandler, |
| 47 | +) (resp interface{}, err error) { |
| 48 | + md, ok := metadata.FromIncomingContext(ctx) |
| 49 | + if !ok { |
| 50 | + return handler(ctx, req) |
| 51 | + } |
| 52 | + carrier := propagation.HeaderCarrier(md) |
| 53 | + ctx = i.propagator.Extract(ctx, carrier) |
| 54 | + ctx = i.withLogTracing(ctx, trace.SpanContextFromContext(ctx)) |
| 55 | + return handler(ctx, req) |
| 56 | +} |
| 57 | + |
| 58 | +// GRPCStreamServerInterceptor adds tracing metadata to streaming RPCs. |
| 59 | +func (i *TraceMiddleware) GRPCStreamServerInterceptor( |
| 60 | + srv interface{}, |
| 61 | + ss grpc.ServerStream, |
| 62 | + _ *grpc.StreamServerInfo, |
| 63 | + handler grpc.StreamHandler, |
| 64 | +) (err error) { |
| 65 | + md, ok := metadata.FromIncomingContext(ss.Context()) |
| 66 | + if !ok { |
| 67 | + return handler(srv, ss) |
| 68 | + } |
| 69 | + ctx := ss.Context() |
| 70 | + carrier := propagation.HeaderCarrier(md) |
| 71 | + ctx = i.propagator.Extract(ctx, carrier) |
| 72 | + ctx = i.withLogTracing(ctx, trace.SpanContextFromContext(ctx)) |
| 73 | + return handler(srv, cloudstream.NewContextualServerStream(ctx, ss)) |
| 74 | +} |
| 75 | + |
| 76 | +// HTTPServer provides middleware for HTTP servers. |
| 77 | +func (i *TraceMiddleware) HTTPServer(next http.Handler) http.Handler { |
| 78 | + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 79 | + carrier := propagation.HeaderCarrier(r.Header) |
| 80 | + ctx := i.propagator.Extract(r.Context(), carrier) |
| 81 | + ctx = i.withLogTracing(ctx, trace.SpanContextFromContext(ctx)) |
| 82 | + next.ServeHTTP(w, r.WithContext(ctx)) |
| 83 | + }) |
| 84 | +} |
| 85 | + |
| 86 | +func (i *TraceMiddleware) withLogTracing(ctx context.Context, spanCtx trace.SpanContext) context.Context { |
| 87 | + if i.TraceHook != nil { |
| 88 | + ctx = i.TraceHook(ctx, spanCtx) |
| 89 | + } |
| 90 | + fields := make([]zap.Field, 0, 3) |
| 91 | + fields = append(fields, cloudzap.Trace(i.ProjectID, spanCtx.TraceID().String())) |
| 92 | + if spanCtx.SpanID().String() != "" { |
| 93 | + fields = append(fields, cloudzap.SpanID(spanCtx.SpanID().String())) |
| 94 | + } |
| 95 | + if spanCtx.IsSampled() { |
| 96 | + fields = append(fields, cloudzap.TraceSampled(spanCtx.IsSampled())) |
| 97 | + } |
| 98 | + return cloudzap.WithLoggerFields(ctx, fields...) |
| 99 | +} |
0 commit comments