diff --git a/cmd/root.go b/cmd/root.go index d55af1c0..00049f20 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -305,7 +305,6 @@ func InitConfig(ctx context.Context, clients *shared.ClientFactory, rootCmd *cob // Used by Logstash // TODO(slackcontext) Consolidate storing SystemID to slackcontext clients.Config.SystemID = systemID - // Used by OpenTracing ctx = slackcontext.SetSystemID(ctx, systemID) rootCmd.SetContext(ctx) // Debug logging @@ -318,7 +317,6 @@ func InitConfig(ctx context.Context, clients *shared.ClientFactory, rootCmd *cob // Used by Logstash // TODO(slackcontext) Consolidate storing ProjectID to slackcontext clients.Config.ProjectID = projectID - // Used by OpenTracing ctx = slackcontext.SetProjectID(ctx, projectID) rootCmd.SetContext(ctx) // Debug logging diff --git a/internal/slackcontext/slackcontext.go b/internal/slackcontext/slackcontext.go index 1ebd1d9e..7d45a132 100644 --- a/internal/slackcontext/slackcontext.go +++ b/internal/slackcontext/slackcontext.go @@ -29,9 +29,10 @@ package slackcontext import ( "context" - "github.com/opentracing/opentracing-go" "github.com/slackapi/slack-cli/internal/slackerror" "github.com/slackapi/slack-cli/internal/version" + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/trace" ) // contextKey is an unexported type to avoid context key collisions. @@ -39,39 +40,20 @@ import ( type contextKey int const ( - contextKeyOpenTracingTracer contextKey = iota - contextKeyProjectID + contextKeyProjectID contextKey = iota contextKeySessionID contextKeySystemID contextKeyTraceID contextKeyVersion ) -// SetOpenTracingSpan returns a new `context.Context` that holds a reference to -// the span. If span is nil, a new context without an active span is returned. -func SetOpenTracingSpan(ctx context.Context, span opentracing.Span) context.Context { - ctx = opentracing.ContextWithSpan(ctx, span) - return ctx -} - -// SetOpenTracingSpan returns a new `context.Context` that holds a reference to -// the `opentracing.Tracer`. If tracer is nil, a new context without a tracer returned. -func SetOpenTracingTracer(ctx context.Context, tracer opentracing.Tracer) context.Context { - ctx = context.WithValue(ctx, contextKeyOpenTracingTracer, tracer) - return ctx -} - // SetProjectID returns a new `context.Context` that holds a reference to -// the `projectID` and updates the `opentracing.Span` tag with the `projectID`. +// the `projectID` and sets the `project_id` attribute on the span in `ctx`. func SetProjectID(ctx context.Context, projectID string) context.Context { // Set projectID in the context ctx = context.WithValue(ctx, contextKeyProjectID, projectID) - // Set projectID in OpenTracing by extracting the span and updating the tag - if span := opentracing.SpanFromContext(ctx); span != nil { - span.SetTag("project_id", projectID) - ctx = opentracing.ContextWithSpan(ctx, span) - } + trace.SpanFromContext(ctx).SetAttributes(attribute.String("project_id", projectID)) return ctx } @@ -95,16 +77,12 @@ func SetSessionID(ctx context.Context, sessionID string) context.Context { } // SetSystemID returns a new `context.Context` that holds a reference to -// the `systemID` and updates the `opentracing.Span` tag with the `systemID`. +// the `systemID` and sets the `system_id` attribute on the span in `ctx`. func SetSystemID(ctx context.Context, systemID string) context.Context { // Set systemID in the context ctx = context.WithValue(ctx, contextKeySystemID, systemID) - // Set projectID in OpenTracing by extracting the span and updating the tag - if span := opentracing.SpanFromContext(ctx); span != nil { - span.SetTag("system_id", systemID) - ctx = opentracing.ContextWithSpan(ctx, span) - } + trace.SpanFromContext(ctx).SetAttributes(attribute.String("system_id", systemID)) return ctx } diff --git a/internal/slackcontext/slackcontext_test.go b/internal/slackcontext/slackcontext_test.go index 27d3c213..7759d7a9 100644 --- a/internal/slackcontext/slackcontext_test.go +++ b/internal/slackcontext/slackcontext_test.go @@ -19,51 +19,14 @@ import ( "testing" "github.com/google/uuid" - "github.com/opentracing/opentracing-go" "github.com/slackapi/slack-cli/internal/slackerror" - "github.com/slackapi/slack-cli/internal/tracer" "github.com/slackapi/slack-cli/internal/version" "github.com/stretchr/testify/require" + "go.opentelemetry.io/otel/attribute" + sdktrace "go.opentelemetry.io/otel/sdk/trace" + "go.opentelemetry.io/otel/sdk/trace/tracetest" ) -func Test_SlackContext_SetOpenTracingSpan(t *testing.T) { - tests := map[string]struct { - expectedSpan opentracing.Span - }{ - "sets the span in the context": { - expectedSpan: opentracing.StartSpan("test.span"), - }, - } - for name, tc := range tests { - t.Run(name, func(t *testing.T) { - ctx := t.Context() - ctx = SetOpenTracingSpan(ctx, tc.expectedSpan) - actualSpan := opentracing.SpanFromContext(ctx) - require.Equal(t, tc.expectedSpan, actualSpan) - }) - } -} - -func Test_SlackContext_SetOpenTracingTracer(t *testing.T) { - _, _tracer := tracer.SetupTracer(true) - - tests := map[string]struct { - expectedTracer opentracing.Tracer - }{ - "sets the Tracer in the context": { - expectedTracer: _tracer, - }, - } - for name, tc := range tests { - t.Run(name, func(t *testing.T) { - ctx := t.Context() - ctx = SetOpenTracingTracer(ctx, tc.expectedTracer) - actualTracer := ctx.Value(contextKeyOpenTracingTracer).(opentracing.Tracer) - require.Equal(t, tc.expectedTracer, actualTracer) - }) - } -} - func Test_SlackContext_SetProjectID(t *testing.T) { tests := map[string]struct { expectedProjectID string @@ -74,8 +37,11 @@ func Test_SlackContext_SetProjectID(t *testing.T) { } for name, tc := range tests { t.Run(name, func(t *testing.T) { - ctx := t.Context() + recorder := tracetest.NewSpanRecorder() + ctx, span := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(recorder)).Tracer("test").Start(t.Context(), "span") ctx = SetProjectID(ctx, tc.expectedProjectID) + span.End() + require.Contains(t, recorder.Ended()[0].Attributes(), attribute.String("project_id", tc.expectedProjectID)) actualProjectID := ctx.Value(contextKeyProjectID).(string) require.Equal(t, tc.expectedProjectID, actualProjectID) }) @@ -135,8 +101,11 @@ func Test_SlackContext_SetSystemID(t *testing.T) { } for name, tc := range tests { t.Run(name, func(t *testing.T) { - ctx := t.Context() + recorder := tracetest.NewSpanRecorder() + ctx, span := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(recorder)).Tracer("test").Start(t.Context(), "span") ctx = SetSystemID(ctx, tc.expectedSystemID) + span.End() + require.Contains(t, recorder.Ended()[0].Attributes(), attribute.String("system_id", tc.expectedSystemID)) actualSystemID := ctx.Value(contextKeySystemID).(string) require.Equal(t, tc.expectedSystemID, actualSystemID) }) diff --git a/internal/tracer/tracer.go b/internal/tracer/tracer.go index c6fdb335..670998d0 100644 --- a/internal/tracer/tracer.go +++ b/internal/tracer/tracer.go @@ -39,7 +39,7 @@ import ( ) // SetupTracer sets up the tracer to send data to honeycomb with OpenTelemetry -func SetupTracer(isDev bool) (io.Closer, opentracing.Tracer) { +func SetupTracer(isDev bool) io.Closer { var collectorEndpoint = "https://slackb.com/traces/v1/otlp" if isDev { collectorEndpoint = "https://dev.slackb.com/traces/v1/otlp" @@ -60,7 +60,7 @@ func SetupTracer(isDev bool) (io.Closer, opentracing.Tracer) { ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) defer cancel() return provider.Shutdown(ctx) - }), bridge + }) } func newTracerProvider(getenv func(string) string, newExporter func() (sdktrace.SpanExporter, error)) (*sdktrace.TracerProvider, error) { @@ -173,15 +173,6 @@ func SetTraceHeaders(ctx context.Context, header http.Header) { header.Set("x-b3-sampled", "0") } -// TraceID returns the trace ID of span, or "" if span has no trace ID. -func TraceID(span opentracing.Span) string { - carrier := opentracing.HTTPHeadersCarrier(http.Header{}) - if err := span.Tracer().Inject(span.Context(), opentracing.HTTPHeaders, carrier); err != nil { - return "" - } - return http.Header(carrier).Get("x-b3-traceid") -} - var ( flushMu sync.Mutex flushFunc func() diff --git a/internal/tracer/tracer_test.go b/internal/tracer/tracer_test.go index f814a562..58a0b10e 100644 --- a/internal/tracer/tracer_test.go +++ b/internal/tracer/tracer_test.go @@ -168,15 +168,19 @@ func Test_Start(t *testing.T) { require.Len(t, spans, 3) assert.Equal(t, spans["opentracing-parent"].SpanContext().SpanID(), spans["otel-child"].Parent().SpanID()) assert.Equal(t, spans["otel-child"].SpanContext().SpanID(), spans["opentracing-grandchild"].Parent().SpanID()) -} -func Test_TraceID(t *testing.T) { - recorder := tracetest.NewSpanRecorder() - tracer := newBridgeTracer(sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(recorder))) - span := tracer.StartSpan("span") - traceID := TraceID(span) - span.Finish() - assert.Len(t, traceID, 32) - assert.Equal(t, recorder.Ended()[0].SpanContext().TraceID().String(), traceID) - assert.Empty(t, TraceID(opentracing.NoopTracer{}.StartSpan("noop"))) + recorder = tracetest.NewSpanRecorder() + tracer = newBridgeTracer(sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(recorder))) + opentracing.SetGlobalTracer(tracer) + ctx, root := Start(context.Background(), "otel-root") + child2, _ := opentracing.StartSpanFromContext(ctx, "opentracing-child") + child2.Finish() + root.End() + spans = map[string]sdktrace.ReadOnlySpan{} + for _, span := range recorder.Ended() { + spans[span.Name()] = span + } + require.Len(t, spans, 2) + assert.False(t, spans["otel-root"].Parent().IsValid()) + assert.Equal(t, spans["otel-root"].SpanContext().SpanID(), spans["opentracing-child"].Parent().SpanID()) } diff --git a/main.go b/main.go index 76ffb3c3..8078ef23 100644 --- a/main.go +++ b/main.go @@ -21,7 +21,6 @@ import ( "strings" "github.com/google/uuid" - "github.com/opentracing/opentracing-go" "github.com/slackapi/slack-cli/cmd" "github.com/slackapi/slack-cli/internal/goutils" "github.com/slackapi/slack-cli/internal/iostreams" @@ -31,6 +30,7 @@ import ( "github.com/slackapi/slack-cli/internal/tracer" "github.com/slackapi/slack-cli/internal/useragent" "github.com/slackapi/slack-cli/internal/version" + "go.opentelemetry.io/otel/attribute" ) func main() { @@ -43,8 +43,8 @@ func main() { // - This would allow us to choose the correct API host based on flags // - Uncomment `isDevTarget` if we refactor to cmd/root.go and update to call `ResolveAPIHost` // var isDevTarget = shared.NewClientFactory().AuthClient().UserDefaultAuthIsProd(ctx) // TODO - hack, remove shared.clients - var jaegerCloser, jaegerTracer = tracer.SetupTracer(false) // Always setup open tracing on prod - ctx = slackcontext.SetOpenTracingTracer(ctx, jaegerTracer) + isDevTarget := false + traceCloser := tracer.SetupTracer(isDevTarget) // Set context values sessionID := uuid.New().String() @@ -55,30 +55,30 @@ func main() { osStr := os.Args[0:] processName := goutils.RedactPII(strings.Join(osStr, " ")) - var span = jaegerTracer.StartSpan("main", opentracing.Tag{Key: "version", Value: cliVersion}) - span.SetTag("slack_cli_sessionID", sessionID) - span.SetTag("hashed_hostname", ioutils.GetHostname()) - span.SetTag("slack_cli_process", processName) + ctx, span := tracer.Start(ctx, "main") + span.SetAttributes( + attribute.String("version", cliVersion), + attribute.String("slack_cli_sessionID", sessionID), + attribute.String("hashed_hostname", ioutils.GetHostname()), + attribute.String("slack_cli_process", processName), + ) if aiAgentName := useragent.GetAIAgentName(); aiAgentName != "" { - span.SetTag("ai_agent", aiAgentName) + span.SetAttributes(attribute.String("ai_agent", aiAgentName)) } // system_id is set in root.go initConfig() // project_id is set in root.go initConfig() - if traceID := tracer.TraceID(span); traceID != "" { - ctx = slackcontext.SetTraceID(ctx, traceID) + if spanContext := span.SpanContext(); spanContext.IsValid() { + ctx = slackcontext.SetTraceID(ctx, spanContext.TraceID().String()) } // Flush on return here, and before each exit in cmd/root.go, which skips these defers tracer.SetFlush(func() { - span.Finish() - _ = jaegerCloser.Close() + span.End() + _ = traceCloser.Close() }) defer tracer.Flush() - // add root span to context - ctx = slackcontext.SetOpenTracingSpan(ctx, span) - rootCmd, clients := cmd.Init(ctx) cmd.ExecuteContext(ctx, rootCmd, clients) }