Skip to content

Commit

Permalink
serialize span links in meta before finishing span
Browse files Browse the repository at this point in the history
  • Loading branch information
nhulston committed Jan 27, 2025
1 parent fbbfdda commit e1c23c8
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions ddtrace/tracer/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package tracer
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"os"
"reflect"
Expand Down Expand Up @@ -469,6 +470,21 @@ func (s *span) AddSpanLinks(spanLinks ...ddtrace.SpanLink) {
s.SpanLinks = append(s.SpanLinks, spanLinks...)
}

// serializeSpanLinksInMeta saves span links as a JSON string under `Span[meta][_dd.span_links]`.
func (s *span) serializeSpanLinksInMeta() {
if len(s.SpanLinks) == 0 {
return
}
spanLinkBytes, err := json.Marshal(s.SpanLinks)
if err != nil {
return
}
if s.Meta == nil {
s.Meta = make(map[string]string)
}
s.Meta["_dd.span_links"] = string(spanLinkBytes)
}

// Finish closes this Span (but not its children) providing the duration
// of its part of the tracing session.
func (s *span) Finish(opts ...ddtrace.FinishOption) {
Expand Down Expand Up @@ -519,6 +535,8 @@ func (s *span) Finish(opts ...ddtrace.FinishOption) {
}
}

s.serializeSpanLinksInMeta()

s.finish(t)
orchestrion.GLSPopValue(sharedinternal.ActiveSpanKey)
}
Expand Down

0 comments on commit e1c23c8

Please sign in to comment.