Skip to content

Commit

Permalink
ddtrace/mocktracer: fix SetUser
Browse files Browse the repository at this point in the history
Signed-off-by: Eliott Bouhana <[email protected]>
  • Loading branch information
eliottness committed Jul 12, 2024
1 parent be3543a commit 1323eeb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
8 changes: 7 additions & 1 deletion ddtrace/mocktracer/mockspan.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,9 @@ func (s *mockspan) SetUser(id string, opts ...tracer.UserMonitoringOption) {
return
}

var cfg tracer.UserMonitoringConfig
cfg := tracer.UserMonitoringConfig{
Metadata: make(map[string]string),
}
for _, fn := range opts {
fn(&cfg)
}
Expand All @@ -257,6 +259,10 @@ func (s *mockspan) SetUser(id string, opts ...tracer.UserMonitoringOption) {
root.SetTag("usr.role", cfg.Role)
root.SetTag("usr.scope", cfg.Scope)
root.SetTag("usr.session_id", cfg.SessionID)

for k, v := range cfg.Metadata {
root.SetTag(fmt.Sprintf("usr.%s", k), v)
}
}

// Root walks the span up to the root parent span and returns it.
Expand Down
24 changes: 24 additions & 0 deletions ddtrace/mocktracer/mocktracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,30 @@ func TestTracerOpenSpans(t *testing.T) {
assert.Empty(t, mt.OpenSpans())
}

func TestTracerSetUser(t *testing.T) {
mt := newMockTracer()
span := mt.StartSpan("http.request")
tracer.SetUser(span, "test-user",
tracer.WithUserEmail("email"),
tracer.WithUserName("name"),
tracer.WithUserRole("role"),
tracer.WithUserScope("scope"),
tracer.WithUserSessionID("session"),
tracer.WithUserMetadata("key", "value"),
)

span.Finish()

finishedSpan := mt.FinishedSpans()[0]
assert.Equal(t, "test-user", finishedSpan.Tag("usr.id"))
assert.Equal(t, "email", finishedSpan.Tag("usr.email"))
assert.Equal(t, "name", finishedSpan.Tag("usr.name"))
assert.Equal(t, "role", finishedSpan.Tag("usr.role"))
assert.Equal(t, "scope", finishedSpan.Tag("usr.scope"))
assert.Equal(t, "session", finishedSpan.Tag("usr.session_id"))
assert.Equal(t, "value", finishedSpan.Tag("usr.key"))
}

func TestTracerReset(t *testing.T) {
assert := assert.New(t)
mt := newMockTracer()
Expand Down

0 comments on commit 1323eeb

Please sign in to comment.