-
Notifications
You must be signed in to change notification settings - Fork 220
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -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 { | ||
|
@@ -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 | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might overwrite the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope, we want to make sure the |
||
|
||
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, | ||
}, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we guard against this?
There was a problem hiding this comment.
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()
.