Skip to content

Commit

Permalink
feat(tracing): Introduce span.SetContext (#599)
Browse files Browse the repository at this point in the history
  • Loading branch information
AbhiPrasad committed Mar 14, 2023
1 parent 1ffabde commit 27e50ad
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 10 deletions.
3 changes: 1 addition & 2 deletions otel/span_processor.go
Expand Up @@ -133,8 +133,7 @@ func updateTransactionWithOtelData(transaction *sentry.Span, s otelSdkTrace.Read
resource[kv.Key] = kv.Value.AsString()
}

// TODO(michi) We might need to set this somewhere else than on the scope
hub.Scope().SetContext("otel", map[string]interface{}{
transaction.SetContext("otel", map[string]interface{}{
"attributes": attributes,
"resource": resource,
})
Expand Down
29 changes: 21 additions & 8 deletions tracing.go
Expand Up @@ -52,6 +52,8 @@ type Span struct { //nolint: maligned // prefer readability over optimal memory
isTransaction bool
// recorder stores all spans in a transaction. Guaranteed to be non-nil.
recorder *spanRecorder
// span context, can only be set on transactions
contexts map[string]Context
}

// TraceParentContext describes the context of a (remote) parent span.
Expand Down Expand Up @@ -107,7 +109,10 @@ func StartSpan(ctx context.Context, operation string, options ...SpanOption) *Sp
ctx: context.WithValue(ctx, spanContextKey{}, &span),
parent: parent,
isTransaction: !hasParent,

contexts: make(map[string]Context),
}

if hasParent {
span.TraceID = parent.TraceID
} else {
Expand Down Expand Up @@ -236,6 +241,10 @@ func (s *Span) SetData(name, value string) {
s.Data[name] = value
}

func (s *Span) SetContext(key string, value Context) {
s.contexts[key] = value
}

// IsTransaction checks if the given span is a transaction.
func (s *Span) IsTransaction() bool {
return s.isTransaction
Expand Down Expand Up @@ -484,17 +493,21 @@ func (s *Span) toEvent() *Event {
s.dynamicSamplingContext = DynamicSamplingContextFromTransaction(s)
}

contexts := map[string]Context{}
for k, v := range s.contexts {
contexts[k] = v
}
contexts["trace"] = s.traceContext().Map()

return &Event{
Type: transactionType,
Transaction: hub.Scope().Transaction(),
Contexts: map[string]Context{
"trace": s.traceContext().Map(),
},
Tags: s.Tags,
Extra: s.Data,
Timestamp: s.EndTime,
StartTime: s.StartTime,
Spans: finished,
Contexts: contexts,
Tags: s.Tags,
Extra: s.Data,
Timestamp: s.EndTime,
StartTime: s.StartTime,
Spans: finished,
TransactionInfo: &TransactionInfo{
Source: s.Source,
},
Expand Down
35 changes: 35 additions & 0 deletions tracing_test.go
Expand Up @@ -260,6 +260,7 @@ func TestStartTransaction(t *testing.T) {
s.StartTime = startTime
s.EndTime = endTime
s.Data = data
s.SetContext("otel", Context{"k": "v"})
},
)
transaction.Finish()
Expand All @@ -283,6 +284,7 @@ func TestStartTransaction(t *testing.T) {
Description: description,
Status: status,
}.Map(),
"otel": {"k": "v"},
},
Tags: nil,
// TODO(tracing): the root span / transaction data field is
Expand Down Expand Up @@ -834,3 +836,36 @@ func TestToBaggage(t *testing.T) {
"sentry-trace_id=f1a4c5c9071eca1cdf04e4132527ed16,sentry-release=test-release,sentry-transaction=transaction-name",
)
}

func TestSpanSetContext(t *testing.T) {
ctx := NewTestContext(ClientOptions{
EnableTracing: true,
})
transaction := StartTransaction(ctx, "Test Transaction")

transaction.SetContext("a", Context{"b": 1})

assertEqual(t, map[string]Context{"a": {"b": 1}}, transaction.contexts)
}

func TestSpanSetContextMerges(t *testing.T) {
ctx := NewTestContext(ClientOptions{
EnableTracing: true,
})
transaction := StartTransaction(ctx, "Test Transaction")
transaction.SetContext("a", Context{"foo": "bar"})
transaction.SetContext("b", Context{"b": 2})

assertEqual(t, map[string]Context{"a": {"foo": "bar"}, "b": {"b": 2}}, transaction.contexts)
}

func TestSpanSetContextOverrides(t *testing.T) {
ctx := NewTestContext(ClientOptions{
EnableTracing: true,
})
transaction := StartTransaction(ctx, "Test Transaction")
transaction.SetContext("a", Context{"foo": "bar"})
transaction.SetContext("a", Context{"foo": 2})

assertEqual(t, map[string]Context{"a": {"foo": 2}}, transaction.contexts)
}

0 comments on commit 27e50ad

Please sign in to comment.