fix(trace): restore gRPC server spans and add HTTP server spans
Merged
MI
1mo ago+202+202+012345678901234567890123456789-19-19-01234567890123456789
Problem
With tracing enabled, traces rooted at middleware spans (WithCurrentMembershipsMiddleware, WithCurrentAPITokenAndOrgMiddleware, ...) instead of transport-level server spans โ for both gRPC and HTTP entrypoints. See #3448.
Root causes
- gRPC: kratos'
grpc.Optionsassignss.grpcOptsinstead of appending (kratos/v2@v2.9.2/transport/grpc/server.go:124).NewGRPCServerpassedgrpc.Options(grpcLib.StatsHandler(otelgrpc.NewServerHandler()))and then, whenmax_recv_msg_sizewas configured (Helmcontrolplane.grpcMaxRecvMsgSize), a secondgrpc.Options(grpcLib.MaxRecvMsgSize(...))โ silently wiping the stats handler and with it all gRPC server spans. - HTTP: the Kratos HTTP server had no server-side span instrumentation at all (no
tracing.Server()middleware, nootelhttp), so transcoded REST requests started their trace at the first middleware span, with notraceparentextraction.
Fix
grpc.go: pass all raw grpc-go server options in a singlegrpc.Options(...)call, with a comment explaining the assignment semantics so it doesn't regress.http.go: prepend kratostracing.Server()as the outermost HTTP middleware. It uses the global tracer provider and extracts W3C tracecontext by default. gRPC is untouched there (already covered by the stats handler, so no double spans).
Verification
New regression tests in app/controlplane/internal/server/otel_test.go spin up real Kratos servers with an in-memory exporter:
TestGRPCServerSpanWithMaxRecvMsgSizeโ gRPC server spangrpc.health.v1.Health/Checkis produced withMaxRecvMsgSizeset. During development the split-grpc.Optionsvariant was confirmed to produce zero spans, reproducing the bug.TestHTTPServerSpanFromTracingMiddlewareโ HTTP request produces a server span named after the route (/ping).
go build ./app/controlplane/... ./app/artifact-cas/..., go vet, and go test ./app/controlplane/internal/server/ all pass. artifact-cas checked: only one grpc.Options call, unaffected.
Fixes #3448
๐ค Posted by Maximus bot (Claude Code) on behalf of @migmartri