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

Tracing without performance improvements #862

Merged
merged 17 commits into from
Sep 4, 2024
9 changes: 3 additions & 6 deletions dynamic_sampling_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ func DynamicSamplingContextFromHeader(header []byte) (DynamicSamplingContext, er
}

func DynamicSamplingContextFromTransaction(span *Span) DynamicSamplingContext {
entries := map[string]string{}

hub := hubFromContext(span.Context())
scope := hub.Scope()
client := hub.Client()
Expand All @@ -52,6 +50,8 @@ func DynamicSamplingContextFromTransaction(span *Span) DynamicSamplingContext {
}
}

entries := make(map[string]string)

if traceID := span.TraceID.String(); traceID != "" {
entries["trace_id"] = traceID
}
Expand Down Expand Up @@ -80,10 +80,7 @@ func DynamicSamplingContextFromTransaction(span *Span) DynamicSamplingContext {

entries["sampled"] = strconv.FormatBool(span.Sampled.Bool())

return DynamicSamplingContext{
Entries: entries,
Frozen: true,
}
return DynamicSamplingContext{Entries: entries, Frozen: true}
}

func (d DynamicSamplingContext) HasEntries() bool {
Expand Down
3 changes: 2 additions & 1 deletion hub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,11 +461,12 @@ func TestGetBaggage(t *testing.T) {
ctx: context.Background(),
Sampled: SampledTrue,
}

s.span.spanRecorder().record(s.span)

return h
}(),
expected: "sentry-sample_rate=1,sentry-environment=production,sentry-release=1.0.0",
expected: "sentry-environment=production,sentry-release=1.0.0,sentry-sample_rate=1",
},
"Without span": {
hub: func() *Hub {
Expand Down
25 changes: 15 additions & 10 deletions tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,17 +324,22 @@ func (s *Span) ToSentryTrace() string {
// Use this function to propagate the DynamicSamplingContext to a downstream SDK,
// either as the value of the "baggage" HTTP header, or as an html "baggage" meta tag.
func (s *Span) ToBaggage() string {
if containingTransaction := s.GetTransaction(); containingTransaction != nil {
// In case there is currently no frozen DynamicSamplingContext attached to the transaction,
// create one from the properties of the transaction.
if !s.dynamicSamplingContext.IsFrozen() {
// This will return a frozen DynamicSamplingContext.
s.dynamicSamplingContext = DynamicSamplingContextFromTransaction(containingTransaction)
}
t := s.GetTransaction()
if t == nil {
return ""
}

return containingTransaction.dynamicSamplingContext.String()
// In case there is currently no frozen DynamicSamplingContext attached to the transaction,
// create one from the properties of the transaction.
if !s.dynamicSamplingContext.IsFrozen() {
// This will return a frozen DynamicSamplingContext.
if dsc := DynamicSamplingContextFromTransaction(t); dsc.HasEntries() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

@cleptric Not sure if this will cause unwanted changes, but it did fix the failing test because it was setting the dynamicSamplingContext to "" due to nil client.

t.dynamicSamplingContext = dsc
}
}
return ""

return t.dynamicSamplingContext.String()

}

// SetDynamicSamplingContext sets the given dynamic sampling context on the
Expand Down Expand Up @@ -550,7 +555,7 @@ func (s *Span) toEvent() *Event {
s.dynamicSamplingContext = DynamicSamplingContextFromTransaction(s)
}

contexts := map[string]Context{}
contexts := make(map[string]Context, len(s.contexts))
for k, v := range s.contexts {
contexts[k] = cloneContext(v)
}
Expand Down
Loading