I have a dummy example on this repo
I tried to pass current span context to a remote one so it would show the trace properly, what I've done:
go func() {
_, span := otel.Tracer("natsC").Start(context.Background(), "publish")
defer span.End()
// send current span context as header
spanCtx := span.SpanContext()
spanJson, _ := spanCtx.MarshalJSON()
log.Println(string(spanJson))
msg, err := nc.RequestMsg(&nats.Msg{
Subject: topic1, Data: []byte("whatever"), Header: nats.Header{
"otelTrace": []string{string(spanJson)},
},
}, 2*time.Second)
if L.IsError(err, `nc.Publish`) {
return
}
log.Println(`reply:`, msg)
}()
On the receiver server:
_, err = nc.QueueSubscribe(topic1, "my-queue", func(msg *nats.Msg) {
// take header and deserialize back to spanContext
rsc := msg.Header.Get(`otelTrace`)
parentSpanCtx := trace.SpanContext{}
err := json.Unmarshal([]byte(rsc), &parentSpanCtx)
L.IsError(err, `json.Unmarshal`)
// use remote context as parent context
_, span := otel.Tracer(`natsC`).Start(trace.ContextWithRemoteSpanContext(context.Background(), parentSpanCtx), topic1)
defer span.End()
data := string(msg.Data)
fmt.Println(data)
err = msg.Respond(msg.Data)
L.IsError(err, `msg.Respond`) // ignore error
})
then I run it using this command go run main.go natsC
.
both span shown on Jeager (localhost:16686) as separate spans, not correlated like in http/grpc example, what should I modify so it would considered as child span of the parent?
equivalent http/grpc example: