From dbf9db0390f3e7beb2c9b7c995c15bfe1c4484fc Mon Sep 17 00:00:00 2001 From: Michael Hoffmann Date: Mon, 20 Mar 2023 15:45:56 +0100 Subject: [PATCH] ref: Align SetContext --- tracing.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tracing.go b/tracing.go index c5645fa4d..346c4c176 100644 --- a/tracing.go +++ b/tracing.go @@ -109,8 +109,6 @@ func StartSpan(ctx context.Context, operation string, options ...SpanOption) *Sp ctx: context.WithValue(ctx, spanContextKey{}, &span), parent: parent, isTransaction: !hasParent, - - contexts: make(map[string]Context), } if hasParent { @@ -241,7 +239,13 @@ func (s *Span) SetData(name, value string) { s.Data[name] = value } +// SetContext sets a context on the span. It is recommended to use SetContext instead of +// accessing the contexts map directly as SetContext takes care of initializing the map +// when necessary. func (s *Span) SetContext(key string, value Context) { + if s.contexts == nil { + s.contexts = make(map[string]Context) + } s.contexts[key] = value }