From 1323eeb689d866c6fe218479f3b0810bd8d28089 Mon Sep 17 00:00:00 2001 From: Eliott Bouhana Date: Fri, 12 Jul 2024 22:37:48 +0200 Subject: [PATCH] ddtrace/mocktracer: fix SetUser Signed-off-by: Eliott Bouhana --- ddtrace/mocktracer/mockspan.go | 8 +++++++- ddtrace/mocktracer/mocktracer_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/ddtrace/mocktracer/mockspan.go b/ddtrace/mocktracer/mockspan.go index 7609fa30c7..4701d96c7f 100644 --- a/ddtrace/mocktracer/mockspan.go +++ b/ddtrace/mocktracer/mockspan.go @@ -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) } @@ -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. diff --git a/ddtrace/mocktracer/mocktracer_test.go b/ddtrace/mocktracer/mocktracer_test.go index 1b761f94e7..8c863f60c5 100644 --- a/ddtrace/mocktracer/mocktracer_test.go +++ b/ddtrace/mocktracer/mocktracer_test.go @@ -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()