Skip to content

Commit

Permalink
fix(cloudtrace): re-add trace logger fields for zap
Browse files Browse the repository at this point in the history
Commit 0aeada6 accidentally removed
trace field on log statements written with the request logger.

This commit reverts parts of that commit, such that zap request logger
still has the trace field populated.
  • Loading branch information
ericwenn committed Oct 31, 2024
1 parent a3f2e6c commit d359901
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions cloudtrace/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"net/http"

"go.einride.tech/cloudrunner/cloudstream"
"go.einride.tech/cloudrunner/cloudzap"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
Expand Down Expand Up @@ -34,6 +36,7 @@ func (i *Middleware) GRPCServerUnaryInterceptor(
}
ctx = i.withOutgoingRequestTracing(ctx, values[0])
ctx = i.withInternalContext(ctx, values[0])
ctx = i.withLogTracing(ctx, values[0])
return handler(ctx, req)
}

Expand All @@ -55,6 +58,7 @@ func (i *Middleware) GRPCStreamServerInterceptor(
ctx := ss.Context()
ctx = i.withOutgoingRequestTracing(ctx, values[0])
ctx = i.withInternalContext(ctx, values[0])
ctx = i.withLogTracing(ctx, values[0])
return handler(srv, cloudstream.NewContextualServerStream(ctx, ss))
}

Expand All @@ -69,6 +73,7 @@ func (i *Middleware) HTTPServer(next http.Handler) http.Handler {
w.Header().Set(ContextHeader, header)
ctx := i.withOutgoingRequestTracing(r.Context(), header)
ctx = i.withInternalContext(ctx, header)
ctx = i.withLogTracing(ctx, header)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
Expand All @@ -84,3 +89,22 @@ func (i *Middleware) withInternalContext(ctx context.Context, header string) con
}
return SetContext(ctx, result)
}

func (i *Middleware) withLogTracing(ctx context.Context, header string) context.Context {
var traceContext Context
if err := traceContext.UnmarshalString(header); err != nil {
return ctx
}
if i.TraceHook != nil {
ctx = i.TraceHook(ctx, traceContext)
}
fields := make([]zap.Field, 0, 3)
fields = append(fields, cloudzap.Trace(i.ProjectID, traceContext.TraceID))
if traceContext.SpanID != "" {
fields = append(fields, cloudzap.SpanID(traceContext.SpanID))
}
if traceContext.Sampled {
fields = append(fields, cloudzap.TraceSampled(traceContext.Sampled))
}
return cloudzap.WithLoggerFields(ctx, fields...)
}

0 comments on commit d359901

Please sign in to comment.