Prompt
How do I trace HTTP and gRPC calls together
Latest observation
To trace HTTP and gRPC calls together, the key is to use one distributed tracing context across both protocols and make sure it’s propagated in headers/metadata end-to-end.
Core idea
Both HTTP and gRPC can carry trace context:
- HTTP: via headers like
traceparent,tracestate(W3C Trace Context), or vendor headers likex-b3-* - gRPC: via metadata with the same trace context keys
If your services are instrumented consistently, a request can start as HTTP, call gRPC downstream, and all spans will appear in the same trace.
Recommended approach
1) Pick a tracing standard
Use one of these:
- W3C Trace Context (
traceparent,tracestate) — recommended - OpenTelemetry — best overall choice for new systems
- B3 headers — common in some older ecosystems
If possible, use OpenTelemetry + W3C.
2) Instrument both clients and servers
You need tracing on:
- HTTP server
- HTTP client
- gRPC server
- gRPC client
This allows the trace context to be extracted from incoming requests and injected into outgoing calls.
3) Propagate context automatically
Most OpenTelemetry SDKs do this automatically once middleware/interceptors are installed.
HTTP
- Incoming request middleware extracts trace headers
- Outgoing HTTP client injects headers
gRPC
- Server interceptor extracts metadata
- Client interceptor injects metadata
Example flow
- User sends HTTP request to Service A
- Service A creates/continues a trace span
- Service A makes gRPC call to Service B, passing the same context
- Service B extracts context and creates a child span
- Both spans appear in the same trace in your observability backend
Language-agnostic pattern
HTTP request handler
- Extract trace context from headers
- Start server span
- Do work
- On gRPC call, use the same context
gRPC call
- Start client span
- Inject trace context into metadata
- Downstream server extracts it
Example with OpenTelemetry concepts
Pseudo-code:
incomingContext = extractTraceContext(httpHeaders)
span = startServerSpan(incomingContext)
grpcCtx = span.context
injectTraceContext(grpcMetadata, grpcCtx)
callGrpcService(grpcMetadata)
end(span)
Practical setup examples
Java
- Use OpenTelemetry Java agent or manual SDK
- HTTP: Servlet filter / Spring Web filter
- gRPC:
grpc-javainterceptors - Works well if you enable auto-instrumentation
Go
- Use OpenTelemetry Go SDK
- HTTP:
otelhttp - gRPC:
otelgrpc - Use the same context passed through
context.Context
Node.js
- Use OpenTelemetry SDK
- HTTP: instrument
http/framework - gRPC: instrument
@grpc/grpc-js - Propagation handled by context APIs
Python
- Use OpenTelemetry Python SDK
- HTTP: instrument Flask/FastAPI/Django
- gRPC:
opentelemetry-instrumentation-grpc
Important details
Use context propagation correctly
Do not create a new trace for the gRPC call. Pass the parent context through.
Ensure both sides understand the same propagator
If one side uses W3C and the other expects B3, traces won’t connect unless you configure compatibility.
Sampling
If you sample too aggressively, you may see only part of the flow. Make sure sampling is consistent across services.
Async boundaries
If requests hop across threads/tasks/queues, make sure the trace context is preserved.
If you want to trace logs too
Add trace/span IDs to logs so you can correlate:
- HTTP request logs
- gRPC call logs
- Trace spans
Best practice stack
If you’re starting fresh:
- OpenTelemetry SDK
- W3C Trace Context
- OTLP exporter
- A backend like Jaeger, Grafana Tempo, Honeycomb, or Datadog
If you tell me your stack (language + framework + whether you use OpenTelemetry already), I can give you exact setup code for HTTP + gRPC tracing.