Skip to content

Commit

Permalink
creating function to create startSpanOption and update config links f…
Browse files Browse the repository at this point in the history
…ield
  • Loading branch information
mhlidd committed Oct 30, 2024
1 parent ea7ff6c commit 79e9a24
Show file tree
Hide file tree
Showing 14 changed files with 25 additions and 18 deletions.
4 changes: 2 additions & 2 deletions contrib/IBM/sarama.v1/sarama.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func WrapPartitionConsumer(pc sarama.PartitionConsumer, opts ...Option) sarama.P
// kafka supports headers, so try to extract a span context
carrier := NewConsumerMessageCarrier(msg)
if spanctx, err := tracer.Extract(carrier); err == nil {
opts = append(opts, tracer.ChildOf(spanctx))
opts = append(opts, tracer.ChildOfWithExtractedSpanLinks(spanctx))
}
next := tracer.StartSpan(cfg.consumerSpanName, opts...)
// reinject the span context so consumers can pick it up
Expand Down Expand Up @@ -298,7 +298,7 @@ func startProducerSpan(cfg *config, version sarama.KafkaVersion, msg *sarama.Pro
}
// if there's a span context in the headers, use that as the parent
if spanctx, err := tracer.Extract(carrier); err == nil {
opts = append(opts, tracer.ChildOf(spanctx))
opts = append(opts, tracer.ChildOfWithExtractedSpanLinks(spanctx))
}
span := tracer.StartSpan(cfg.producerSpanName, opts...)
if version.IsAtLeast(sarama.V0_11_0_0) {
Expand Down
4 changes: 2 additions & 2 deletions contrib/Shopify/sarama/sarama.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func WrapPartitionConsumer(pc sarama.PartitionConsumer, opts ...Option) sarama.P
// kafka supports headers, so try to extract a span context
carrier := NewConsumerMessageCarrier(msg)
if spanctx, err := tracer.Extract(carrier); err == nil {
opts = append(opts, tracer.ChildOf(spanctx))
opts = append(opts, tracer.ChildOfWithExtractedSpanLinks(spanctx))
}
next := tracer.StartSpan(cfg.consumerSpanName, opts...)
// reinject the span context so consumers can pick it up
Expand Down Expand Up @@ -301,7 +301,7 @@ func startProducerSpan(cfg *config, version sarama.KafkaVersion, msg *sarama.Pro
}
// if there's a span context in the headers, use that as the parent
if spanctx, err := tracer.Extract(carrier); err == nil {
opts = append(opts, tracer.ChildOf(spanctx))
opts = append(opts, tracer.ChildOfWithExtractedSpanLinks(spanctx))
}
span := tracer.StartSpan(cfg.producerSpanName, opts...)
if version.IsAtLeast(sarama.V0_11_0_0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func TraceReceiveFunc(s Subscription, opts ...Option) func(ctx context.Context,
tracer.Tag(ext.Component, componentName),
tracer.Tag(ext.SpanKind, ext.SpanKindConsumer),
tracer.Tag(ext.MessagingSystem, ext.MessagingSystemGCPPubsub),
tracer.ChildOf(parentSpanCtx),
tracer.ChildOfWithExtractedSpanLinks(parentSpanCtx),
}
if cfg.serviceName != "" {
opts = append(opts, tracer.ServiceName(cfg.serviceName))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (tr *KafkaTracer) StartConsumeSpan(msg Message) ddtrace.Span {
// kafka supports headers, so try to extract a span context
carrier := MessageCarrier{msg: msg}
if spanctx, err := tracer.Extract(carrier); err == nil {
opts = append(opts, tracer.ChildOf(spanctx))
opts = append(opts, tracer.ChildOfWithExtractedSpanLinks(spanctx))
}
span, _ := tracer.StartSpanFromContext(tr.ctx, tr.consumerSpanName, opts...)
// reinject the span context so consumers can pick it up
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (tr *KafkaTracer) StartProduceSpan(msg Message) ddtrace.Span {
// if there's a span context in the headers, use that as the parent
carrier := NewMessageCarrier(msg)
if spanctx, err := tracer.Extract(carrier); err == nil {
opts = append(opts, tracer.ChildOf(spanctx))
opts = append(opts, tracer.ChildOfWithExtractedSpanLinks(spanctx))
}
span, _ := tracer.StartSpanFromContext(tr.ctx, tr.producerSpanName, opts...)
// inject the span context so consumers can pick it up
Expand Down
2 changes: 1 addition & 1 deletion contrib/gofiber/fiber.v2/fiber.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func Middleware(opts ...Option) func(c *fiber.Ctx) error {
}
}
if spanctx, err := tracer.Extract(tracer.HTTPHeadersCarrier(h)); err == nil {
opts = append(opts, tracer.ChildOf(spanctx))
opts = append(opts, tracer.ChildOfWithExtractedSpanLinks(spanctx))
}
opts = append(opts, cfg.spanOpts...)
opts = append(opts,
Expand Down
2 changes: 1 addition & 1 deletion contrib/google.golang.org/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func startSpanFromContext(
)
md, _ := metadata.FromIncomingContext(ctx) // nil is ok
if sctx, err := tracer.Extract(grpcutil.MDCarrier(md)); err == nil {
opts = append(opts, tracer.ChildOf(sctx))
opts = append(opts, tracer.ChildOfWithExtractedSpanLinks(sctx))
}
return tracer.StartSpanFromContext(ctx, operation, opts...)
}
Expand Down
2 changes: 1 addition & 1 deletion contrib/internal/httptrace/httptrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func StartRequestSpan(r *http.Request, opts ...ddtrace.StartSpanOption) (tracer.
cfg.Tags["http.host"] = r.Host
}
if spanctx, err := tracer.Extract(tracer.HTTPHeadersCarrier(r.Header)); err == nil {
cfg.Parent = spanctx
tracer.ChildOfWithExtractedSpanLinks(spanctx)(cfg) //TODO: ensure that this line is performing the expected functionality
}
for k, v := range ipTags {
cfg.Tags[k] = v
Expand Down
2 changes: 1 addition & 1 deletion contrib/segmentio/kafka.go.v0/internal/tracing/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (tr *Tracer) StartConsumeSpan(ctx context.Context, msg Message) ddtrace.Spa
// kafka supports headers, so try to extract a span context
carrier := NewMessageCarrier(msg)
if spanctx, err := tracer.Extract(carrier); err == nil {
opts = append(opts, tracer.ChildOf(spanctx))
opts = append(opts, tracer.ChildOfWithExtractedSpanLinks(spanctx))
}
span, _ := tracer.StartSpanFromContext(ctx, tr.consumerSpanName, opts...)
// reinject the span context so consumers can pick it up
Expand Down
4 changes: 2 additions & 2 deletions contrib/twitchtv/twirp/twirp.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (wc *wrappedClient) Do(req *http.Request) (*http.Response, error) {
opts = append(opts, tracer.Tag(ext.EventSampleRate, wc.cfg.analyticsRate))
}
if spanctx, err := tracer.Extract(tracer.HTTPHeadersCarrier(req.Header)); err == nil {
opts = append(opts, tracer.ChildOf(spanctx))
opts = append(opts, tracer.ChildOfWithExtractedSpanLinks(spanctx))
}

span, ctx := tracer.StartSpanFromContext(req.Context(), wc.cfg.spanName, opts...)
Expand Down Expand Up @@ -139,7 +139,7 @@ func WrapServer(h http.Handler, opts ...Option) http.Handler {
spanOpts = append(spanOpts, tracer.Tag(ext.EventSampleRate, cfg.analyticsRate))
}
if spanctx, err := tracer.Extract(tracer.HTTPHeadersCarrier(r.Header)); err == nil {
spanOpts = append(spanOpts, tracer.ChildOf(spanctx))
spanOpts = append(spanOpts, tracer.ChildOfWithExtractedSpanLinks(spanctx))
}
span, ctx := tracer.StartSpanFromContext(r.Context(), "twirp.handler", spanOpts...)
defer span.Finish()
Expand Down
2 changes: 1 addition & 1 deletion contrib/valyala/fasthttp.v1/fasthttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func WrapHandler(h fasthttp.RequestHandler, opts ...Option) fasthttp.RequestHand
ReqHeader: &fctx.Request.Header,
}
if sctx, err := tracer.Extract(fcc); err == nil {
spanOpts = append(spanOpts, tracer.ChildOf(sctx))
spanOpts = append(spanOpts, tracer.ChildOfWithExtractedSpanLinks(sctx))
}
span := fasthttptrace.StartSpanFromContext(fctx, "http.request", spanOpts...)
defer span.Finish()
Expand Down
2 changes: 1 addition & 1 deletion ddtrace/tracer/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestStartSpanFromContextWithSpanLinks(t *testing.T) {
child, ctx := StartSpanFromContext(
context.Background(),
"http.request",
ChildOf(spanLinkContext),
ChildOfWithExtractedSpanLinks(spanLinkContext),
)
assert := assert.New(t)

Expand Down
10 changes: 10 additions & 0 deletions ddtrace/tracer/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,16 @@ func ChildOf(ctx ddtrace.SpanContext) StartSpanOption {
}
}

func ChildOfWithExtractedSpanLinks(ctx ddtrace.SpanContext) StartSpanOption {
return func(cfg *ddtrace.StartSpanConfig) {
if spanCtx, ok := ctx.(*spanContext); ok && spanCtx != nil {
cfg.SpanLinks = spanCtx.spanLinks
spanCtx.spanLinks = nil
}
cfg.Parent = ctx
}
}

// withContext associates the ctx with the span.
func withContext(ctx context.Context) StartSpanOption {
return func(cfg *ddtrace.StartSpanConfig) {
Expand Down
3 changes: 0 additions & 3 deletions ddtrace/tracer/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,9 +544,6 @@ func (t *tracer) StartSpan(operationName string, options ...ddtrace.StartSpanOpt
}

span.SpanLinks = append(span.SpanLinks, opts.SpanLinks...)
if parentCtx, ok := opts.Parent.(*spanContext); ok {
span.SpanLinks = append(span.SpanLinks, parentCtx.spanLinks...)
}

if t.config.hostname != "" {
span.setMeta(keyHostname, t.config.hostname)
Expand Down

0 comments on commit 79e9a24

Please sign in to comment.