goroutine safety guarantees for jaeger + opentracing-go

502 views
Skip to first unread message

Hechen Gao

unread,
Feb 13, 2018, 1:23:31 PM2/13/18
to Jaeger Tracing
Hi jaeger community,

I have a question about the goroutine safety guarantees for jaeger + opentracing-go.

In my code I am using jaeger and set jaeger tracer as global tracer for opentracing-go API. And I have a following code block.

func xyz(ctx context.Context, ...) {
    span, spanctx := opentracing.StartSpanFromContext(ctx, "xyz")
    defer span.Finish()
    span.LogFields(
        log.String("event", "soft error"),
        log.String("type", "cache timeout"),
        log.Int("waited.millis", 1500))
    ...

    go taskProcess(spanctx, ...)
}

func taskProcess(ctx context.Context, ...) {
span, spanctx := opentracing.StartSpanFromContext(ctx, "taskProcess")
        defer span.Finish()
        ...
}

I am passing the span context to a goroutine function, and start a new span from that context. I am wondering if this is safe to do.

Looking forward to your reply.
Best regards

Yuri Shkuro

unread,
Feb 13, 2018, 2:38:01 PM2/13/18
to Hechen Gao, Jaeger Tracing
Good question. Yes, it is safe to do so, but I often seen people get burned by the fact that if the original context comes from an RPC framework it might contain a deadline, which doesn't work great when you transfer said context to a goroutine. Also, the context might get cancelled once the RPC is completed. So you might want to do this in teh goroutine
  1. create a new child span from the original context
  2. create a new Background context for the goroutine
  3. save the span to the next context

--
You received this message because you are subscribed to the Google Groups "Jaeger Tracing" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jaeger-tracing+unsubscribe@googlegroups.com.
To post to this group, send email to jaeger-tracing@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jaeger-tracing/c37dd45f-e7d7-478e-8943-0e31dbe0c34e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Yuri Shkuro

unread,
Feb 13, 2018, 4:27:10 PM2/13/18
to Hechen Gao, Jaeger Tracing
So basically add this to your example: 

func taskProcess(ctx context.Context, ...) {
callerSpan := opentracing.SpanFromContext(ctx)
ctx = opentracing.ContextWithSpan(Background(), callerSpan)
// now ctx is independent of the caller but has access to its span

span, spanctx := opentracing.StartSpanFromContext(ctx, "taskProcess")
        defer span.Finish()
        ...
}


On Tue, Feb 13, 2018 at 2:59 PM, Hechen Gao <zere...@gmail.com> wrote:
I see, could you please add a more specific example? I feel this would be very useful to add in opentracing and jaeger doc.

Best regards,


To post to this group, send email to jaeger-...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages