Skip to content

Commit

Permalink
feat(tracing): Add Span.SetDynamicSamplingContext method (#539)
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyo committed Jan 19, 2023
1 parent d787fe5 commit 52e1547
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
### Misc

- Add SentryTraceHeader and SentryBaggageHeader constants ([#538](https://github.com/getsentry/sentry-go/pull/538/))
- Add `Span.SetDynamicSamplingContext` method ([#539](https://github.com/getsentry/sentry-go/pull/539/))
- Add DSN getters ([#540](https://github.com/getsentry/sentry-go/pull/540))

## 0.17.0
Expand Down
8 changes: 8 additions & 0 deletions tracing.go
Expand Up @@ -240,6 +240,14 @@ func (s *Span) ToBaggage() string {
return s.dynamicSamplingContext.String()
}

// SetDynamicSamplingContext sets the given dynamic sampling context on the
// current transaction.
func (s *Span) SetDynamicSamplingContext(dsc DynamicSamplingContext) {
if s.isTransaction {
s.dynamicSamplingContext = dsc
}
}

// sentryTracePattern matches either
//
// TRACE_ID - SPAN_ID
Expand Down
35 changes: 35 additions & 0 deletions tracing_test.go
Expand Up @@ -654,3 +654,38 @@ func TestDoesNotCrashWithEmptyContext(t *testing.T) {
tx.Sampled = SampledTrue
tx.Finish()
}

func TestSetDynamicSamplingContextWorksOnTransaction(t *testing.T) {
s := Span{
isTransaction: true,
dynamicSamplingContext: DynamicSamplingContext{Frozen: false},
}
newDsc := DynamicSamplingContext{
Entries: map[string]string{"environment": "dev"},
Frozen: true,
}

s.SetDynamicSamplingContext(newDsc)

if diff := cmp.Diff(newDsc, s.dynamicSamplingContext); diff != "" {
t.Errorf("DynamicSamplingContext mismatch (-want +got):\n%s", diff)
}
}

func TestSetDynamicSamplingContextDoesNothingOnSpan(t *testing.T) {
// SetDynamicSamplingContext should do nothing on non-transaction spans
s := Span{
isTransaction: false,
dynamicSamplingContext: DynamicSamplingContext{},
}
newDsc := DynamicSamplingContext{
Entries: map[string]string{"environment": "dev"},
Frozen: true,
}

s.SetDynamicSamplingContext(newDsc)

if diff := cmp.Diff(DynamicSamplingContext{}, s.dynamicSamplingContext); diff != "" {
t.Errorf("DynamicSamplingContext mismatch (-want +got):\n%s", diff)
}
}

0 comments on commit 52e1547

Please sign in to comment.