Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Span Link support for compound header tag extractions with conflicting trace IDs #2948

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
17fef62
initial code for adding spanlinks for span context
mhlidd Oct 24, 2024
6b1593e
adding span links in startspan
mhlidd Oct 24, 2024
6c2b25c
printing flags
mhlidd Oct 25, 2024
eec17a2
editing flags
mhlidd Oct 25, 2024
92b6415
omitting empty spanlinks in spans
mhlidd Oct 25, 2024
fba3190
changing lclctx to localCtx
mhlidd Oct 29, 2024
452d4d7
creating function to create startSpanOption and update config links f…
mhlidd Oct 30, 2024
dc6c762
updating WithExtractedSpanLinks
mhlidd Oct 31, 2024
cab4ff2
adding copyright
mhlidd Nov 4, 2024
6968211
removing stats files
mhlidd Nov 4, 2024
06f893e
removing unwanted files on remote
mhlidd Nov 4, 2024
3b3968f
addressing PR comments
mhlidd Nov 6, 2024
3487509
refactoring extract and instrumentations
mhlidd Nov 7, 2024
7701611
updating PR comments and unit tests
mhlidd Nov 8, 2024
538d25a
logging
mhlidd Nov 11, 2024
cdc6f40
updating PR comments
mhlidd Nov 19, 2024
279246e
Update ddtrace/tracer/textmap.go
mtoffl01 Nov 20, 2024
14a69f2
Restructure Extract method and clean up tests for idiomiatc Golang TDD
mtoffl01 Nov 20, 2024
6936c46
cleanup overrideDatadogParentID case
mtoffl01 Nov 20, 2024
c7eb900
revert recent changes to overridedatadogparentid case
mtoffl01 Nov 20, 2024
44ff629
updating code to correctly add span links as start span option
mhlidd Nov 20, 2024
990a98a
Make spanLink.context_headers more dynamic and refactor tests
mtoffl01 Nov 21, 2024
1574d11
small change to propagator names for cross-library consistency
mhlidd Nov 21, 2024
7584ed7
updating test
mhlidd Nov 22, 2024
2847f4c
adding valid SpanID check to make consistent with other libraries
mhlidd Nov 22, 2024
a1fce54
Update ddtrace/tracer/textmap.go
mtoffl01 Nov 22, 2024
59e1504
Update ddtrace/tracer/textmap.go
mtoffl01 Nov 22, 2024
036efda
nits: clean up comments and simplify code
mtoffl01 Nov 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions contrib/IBM/sarama.v1/sarama.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ 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 {
// If there are span links as a result of context extraction, add them as a StartSpanOption
if linksCtx, ok := spanctx.(ddtrace.SpanContextWithLinks); ok && linksCtx.SpanLinks() != nil {
opts = append(opts, tracer.WithSpanLinks(linksCtx.SpanLinks()))
}
opts = append(opts, tracer.ChildOf(spanctx))
}
next := tracer.StartSpan(cfg.consumerSpanName, opts...)
Expand Down Expand Up @@ -298,6 +302,10 @@ 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 {
// If there are span links as a result of context extraction, add them as a StartSpanOption
if linksCtx, ok := spanctx.(ddtrace.SpanContextWithLinks); ok && linksCtx.SpanLinks() != nil {
opts = append(opts, tracer.WithSpanLinks(linksCtx.SpanLinks()))
}
opts = append(opts, tracer.ChildOf(spanctx))
}
span := tracer.StartSpan(cfg.producerSpanName, opts...)
Expand Down
8 changes: 8 additions & 0 deletions contrib/Shopify/sarama/sarama.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ 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 {
// If there are span links as a result of context extraction, add them as a StartSpanOption
if linksCtx, ok := spanctx.(ddtrace.SpanContextWithLinks); ok && linksCtx.SpanLinks() != nil {
opts = append(opts, tracer.WithSpanLinks(linksCtx.SpanLinks()))
}
opts = append(opts, tracer.ChildOf(spanctx))
}
next := tracer.StartSpan(cfg.consumerSpanName, opts...)
Expand Down Expand Up @@ -301,6 +305,10 @@ 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 {
// If there are span links as a result of context extraction, add them as a StartSpanOption
if linksCtx, ok := spanctx.(ddtrace.SpanContextWithLinks); ok && linksCtx.SpanLinks() != nil {
opts = append(opts, tracer.WithSpanLinks(linksCtx.SpanLinks()))
}
opts = append(opts, tracer.ChildOf(spanctx))
}
span := tracer.StartSpan(cfg.producerSpanName, opts...)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ func TraceReceiveFunc(s Subscription, opts ...Option) func(ctx context.Context,
if cfg.measured {
opts = append(opts, tracer.Measured())
}
// If there are span links as a result of context extraction, add them as a StartSpanOption
if linksCtx, ok := parentSpanCtx.(ddtrace.SpanContextWithLinks); ok && linksCtx.SpanLinks() != nil {
opts = append(opts, tracer.WithSpanLinks(linksCtx.SpanLinks()))
}
span, ctx := tracer.StartSpanFromContext(ctx, cfg.receiveSpanName, opts...)
if msg.DeliveryAttempt != nil {
span.SetTag("delivery_attempt", *msg.DeliveryAttempt)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ 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 {
// If there are span links as a result of context extraction, add them as a StartSpanOption
if linksCtx, ok := spanctx.(ddtrace.SpanContextWithLinks); ok && linksCtx.SpanLinks() != nil {
opts = append(opts, tracer.WithSpanLinks(linksCtx.SpanLinks()))
}
opts = append(opts, tracer.ChildOf(spanctx))
}
span, _ := tracer.StartSpanFromContext(tr.ctx, tr.consumerSpanName, opts...)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ 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 {
// If there are span links as a result of context extraction, add them as a StartSpanOption
if linksCtx, ok := spanctx.(ddtrace.SpanContextWithLinks); ok && linksCtx.SpanLinks() != nil {
opts = append(opts, tracer.WithSpanLinks(linksCtx.SpanLinks()))
}
opts = append(opts, tracer.ChildOf(spanctx))
}
span, _ := tracer.StartSpanFromContext(tr.ctx, tr.producerSpanName, opts...)
Expand Down
4 changes: 4 additions & 0 deletions contrib/gofiber/fiber.v2/fiber.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ func Middleware(opts ...Option) func(c *fiber.Ctx) error {
}
}
if spanctx, err := tracer.Extract(tracer.HTTPHeadersCarrier(h)); err == nil {
// If there are span links as a result of context extraction, add them as a StartSpanOption
if linksCtx, ok := spanctx.(ddtrace.SpanContextWithLinks); ok && linksCtx.SpanLinks() != nil {
opts = append(opts, tracer.WithSpanLinks(linksCtx.SpanLinks()))
}
opts = append(opts, tracer.ChildOf(spanctx))
}
opts = append(opts, cfg.spanOpts...)
Expand Down
4 changes: 4 additions & 0 deletions contrib/google.golang.org/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ func startSpanFromContext(
)
md, _ := metadata.FromIncomingContext(ctx) // nil is ok
if sctx, err := tracer.Extract(grpcutil.MDCarrier(md)); err == nil {
// If there are span links as a result of context extraction, add them as a StartSpanOption
if linksCtx, ok := sctx.(ddtrace.SpanContextWithLinks); ok && linksCtx.SpanLinks() != nil {
opts = append(opts, tracer.WithSpanLinks(linksCtx.SpanLinks()))
}
opts = append(opts, tracer.ChildOf(sctx))
}
return tracer.StartSpanFromContext(ctx, operation, opts...)
Expand Down
6 changes: 5 additions & 1 deletion contrib/internal/httptrace/httptrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ 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
// If there are span links as a result of context extraction, add them as a StartSpanOption
if linksCtx, ok := spanctx.(ddtrace.SpanContextWithLinks); ok && linksCtx.SpanLinks() != nil {
opts = append(opts, tracer.WithSpanLinks(linksCtx.SpanLinks()))
}
tracer.ChildOf(spanctx)(cfg)
}
for k, v := range ipTags {
cfg.Tags[k] = v
Expand Down
4 changes: 4 additions & 0 deletions contrib/segmentio/kafka.go.v0/internal/tracing/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ 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 {
// If there are span links as a result of context extraction, add them as a StartSpanOption
if linksCtx, ok := spanctx.(ddtrace.SpanContextWithLinks); ok && linksCtx.SpanLinks() != nil {
opts = append(opts, tracer.WithSpanLinks(linksCtx.SpanLinks()))
}
opts = append(opts, tracer.ChildOf(spanctx))
}
span, _ := tracer.StartSpanFromContext(ctx, tr.consumerSpanName, opts...)
Expand Down
9 changes: 9 additions & 0 deletions contrib/twitchtv/twirp/twirp.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"net/http"
"strconv"

"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
"gopkg.in/DataDog/dd-trace-go.v1/internal/log"
Expand Down Expand Up @@ -90,6 +91,10 @@ 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 {
// If there are span links as a result of context extraction, add them as a StartSpanOption
if linksCtx, ok := spanctx.(ddtrace.SpanContextWithLinks); ok && linksCtx.SpanLinks() != nil {
opts = append(opts, tracer.WithSpanLinks(linksCtx.SpanLinks()))
}
opts = append(opts, tracer.ChildOf(spanctx))
}

Expand Down Expand Up @@ -139,6 +144,10 @@ 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 {
// If there are span links as a result of context extraction, add them as a StartSpanOption
if linksCtx, ok := spanctx.(ddtrace.SpanContextWithLinks); ok && linksCtx.SpanLinks() != nil {
spanOpts = append(spanOpts, tracer.WithSpanLinks(linksCtx.SpanLinks()))
}
spanOpts = append(spanOpts, tracer.ChildOf(spanctx))
}
span, ctx := tracer.StartSpanFromContext(r.Context(), "twirp.handler", spanOpts...)
Expand Down
4 changes: 4 additions & 0 deletions contrib/valyala/fasthttp.v1/fasthttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func WrapHandler(h fasthttp.RequestHandler, opts ...Option) fasthttp.RequestHand
ReqHeader: &fctx.Request.Header,
}
if sctx, err := tracer.Extract(fcc); err == nil {
// If there are span links as a result of context extraction, add them as a StartSpanOption
if linksCtx, ok := sctx.(ddtrace.SpanContextWithLinks); ok && linksCtx.SpanLinks() != nil {
spanOpts = append(spanOpts, tracer.WithSpanLinks(linksCtx.SpanLinks()))
}
spanOpts = append(spanOpts, tracer.ChildOf(sctx))
}
span := fasthttptrace.StartSpanFromContext(fctx, "http.request", spanOpts...)
Expand Down
28 changes: 14 additions & 14 deletions ddtrace/tracer/civisibility_tslv_msgp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ddtrace/tracer/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ type span struct {
TraceID uint64 `msg:"trace_id"` // lower 64-bits of the root span identifier
ParentID uint64 `msg:"parent_id"` // identifier of the span's direct parent
Error int32 `msg:"error"` // error status of the span; 0 means no errors
SpanLinks []ddtrace.SpanLink `msg:"span_links"` // links to other spans
SpanLinks []ddtrace.SpanLink `msg:"span_links,omitempty"` // links to other spans
mhlidd marked this conversation as resolved.
Show resolved Hide resolved

goExecTraced bool `msg:"-"`
noDebugStack bool `msg:"-"` // disables debug stack traces
Expand Down
39 changes: 22 additions & 17 deletions ddtrace/tracer/span_msgp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading