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

feat(tracing): Introduce span.SetContext #599

Merged
merged 2 commits into from Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// span context, can only be set on transactions

Should we guard against this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about it, but decided against this for now since this field is a) private and b) only used when calling .toEvent().

}

// 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe worth adding a test here as well.

}
contexts["trace"] = s.traceContext().Map()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might overwrite the trace key, is this a concern?

Copy link
Member Author

@AbhiPrasad AbhiPrasad Mar 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, we want to make sure the trace key is always being set by the transaction itself, and not overwritten by setContext.


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)
}