Skip to content

Lack of convenient API to start span with a parent context in v2 #3598

Open
@Gilthoniel

Description

@Gilthoniel

Tracer Version(s)

v2.0.0

Go Version(s)

go v1.24.3

Bug Report

We used to use the ChildOf option to inject a parent context extracted from a message or an HTTP request but the option is not deprecated in favor of StartChild but we cannot instantiate the parent span from the parent context and we end up doing this:

if parentSpan, _ := tracer.Extract(MessageCarrier{msg}); parentSpan != nil {
	opts = append(opts, tracer.WithStartSpanConfig(&tracer.StartSpanConfig{
		Parent: parentSpan,
	}))
}

But that seems overkill. I can't find a different way so I'm assuming that's not the right approach but then I have no idea what the right one is.

Or a Parent option when starting the span would be possible ?

Reproduction Code

opts := []tracer.StartSpanOption{
	tracer.Tag("messaging.topic", topic),
	tracer.Tag(ext.SpanKind, ext.SpanKindConsumer),
	tracer.SpanType(ext.SpanTypeMessageConsumer),
}

if parentSpan, _ := tracer.Extract(MessageCarrier{msg}); parentSpan != nil {
	opts = append(opts, tracer.WithStartSpanConfig(&tracer.StartSpanConfig{
		Parent: parentSpan,
	}))
}

span, ctx := tracer.StartSpanFromContext(ctx, "queue.consume", opts...)
defer func() {
	span.Finish(tracer.WithError(err))
}()

Error Logs

No response

Go Env Output

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementquick change/addition that does not need full team approval

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions