Skip to content

Commit

Permalink
feat: Add Span.SetData() (#542)
Browse files Browse the repository at this point in the history
  • Loading branch information
cleptric committed Jan 23, 2023
1 parent 52e1547 commit 946b556
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,7 @@
- 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))
- Add Span.SetData() ([#542](https://github.com/getsentry/sentry-go/pull/542))

## 0.17.0

Expand Down
10 changes: 10 additions & 0 deletions tracing.go
Expand Up @@ -211,6 +211,16 @@ func (s *Span) SetTag(name, value string) {
s.Tags[name] = value
}

// SetData sets a data on the span. It is recommended to use SetData instead of
// accessing the data map directly as SetData takes care of initializing the map
// when necessary.
func (s *Span) SetData(name, value string) {
if s.Data == nil {
s.Data = make(map[string]interface{})
}
s.Data[name] = value
}

// TODO(tracing): maybe add shortcuts to get/set transaction name. Right now the
// transaction name is in the Scope, as it has existed there historically, prior
// to tracing.
Expand Down
24 changes: 24 additions & 0 deletions tracing_test.go
Expand Up @@ -313,6 +313,30 @@ func TestStartTransaction(t *testing.T) {
}
}

func TestSetTag(t *testing.T) {
ctx := NewTestContext(ClientOptions{
EnableTracing: true,
})
span := StartSpan(ctx, "Test Span")
span.SetTag("key", "value")

if (span.Tags == nil) || (span.Tags["key"] != "value") {
t.Fatalf("Tags mismatch, got %v", span.Tags)
}
}

func TestSetData(t *testing.T) {
ctx := NewTestContext(ClientOptions{
EnableTracing: true,
})
span := StartSpan(ctx, "Test Span")
span.SetData("key", "value")

if (span.Data == nil) || (span.Data["key"] != "value") {
t.Fatalf("Data mismatch, got %v", span.Data)
}
}

// testContextKey is used to store a value in a context so that we can check
// that SDK operations on that context preserve the original context values.
type testContextKey struct{}
Expand Down

0 comments on commit 946b556

Please sign in to comment.