From a641ab134dab6718aa930a2a10ff04bf952e7109 Mon Sep 17 00:00:00 2001 From: Michael Brooks Date: Tue, 24 Nov 2026 12:10:16 -0700 Subject: [PATCH 1/2] refactor: use OpenTelemetry spans in internal/iostreams printers --- internal/iostreams/printer.go | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/internal/iostreams/printer.go b/internal/iostreams/printer.go index 4d94dd73..280257e1 100644 --- a/internal/iostreams/printer.go +++ b/internal/iostreams/printer.go @@ -20,8 +20,9 @@ import ( "strings" "time" - "github.com/opentracing/opentracing-go" "github.com/slackapi/slack-cli/internal/style" + "github.com/slackapi/slack-cli/internal/tracer" + "go.opentelemetry.io/otel/attribute" ) // Printer contains implementations of printers that output to the display or @@ -46,8 +47,9 @@ type Printer interface { // PrintDebug prints a debug message to stdout if debug is enabled func (io *IOStreams) PrintDebug(ctx context.Context, format string, a ...any) { message := strings.TrimSpace(style.RemoveANSI(sprintF(format, a...))) - span, _ := opentracing.StartSpanFromContext(ctx, "printDebug", opentracing.Tag{Key: "debug_log", Value: message}) - defer span.Finish() + _, span := tracer.Start(ctx, "printDebug") + span.SetAttributes(attribute.String("debug_log", message)) + defer span.End() lines := strings.SplitSeq(message, "\n") for line := range lines { _ = io.FlushToLogFile(ctx, "debug", line) @@ -62,8 +64,9 @@ func (io *IOStreams) PrintDebug(ctx context.Context, format string, a ...any) { // PrintError logs and prints an error message to stderr func (io *IOStreams) PrintError(ctx context.Context, format string, a ...any) { message := sprintF(format, a...) - span, _ := opentracing.StartSpanFromContext(ctx, "printError", opentracing.Tag{Key: "error_log", Value: message}) - defer span.Finish() + _, span := tracer.Start(ctx, "printError") + span.SetAttributes(attribute.String("error_log", message)) + defer span.End() _ = io.FlushToLogFile(ctx, "error", message) io.FinishLogFile(ctx) if !strings.Contains(message, style.Emoji("prohibited")) { @@ -76,8 +79,9 @@ func (io *IOStreams) PrintError(ctx context.Context, format string, a ...any) { // PrintWarning logs and prints a warning message to stdout func (io *IOStreams) PrintWarning(ctx context.Context, format string, a ...any) { message := sprintF(format, a...) - span, _ := opentracing.StartSpanFromContext(ctx, "printWarning", opentracing.Tag{Key: "warning_log", Value: message}) - defer span.Finish() + _, span := tracer.Start(ctx, "printWarning") + span.SetAttributes(attribute.String("warning_log", message)) + defer span.End() _ = io.FlushToLogFile(ctx, "warning", message) io.Stderr.Println("\n" + style.Emoji("warning") + message) } @@ -86,8 +90,9 @@ func (io *IOStreams) PrintWarning(ctx context.Context, format string, a ...any) func (io *IOStreams) PrintInfo(ctx context.Context, shouldTrace bool, format string, a ...any) { message := sprintF(format, a...) if shouldTrace { - span, _ := opentracing.StartSpanFromContext(ctx, "printInfo", opentracing.Tag{Key: "printInfo", Value: message}) - defer span.Finish() + _, span := tracer.Start(ctx, "printInfo") + span.SetAttributes(attribute.String("printInfo", message)) + defer span.End() } if style.IsLipglossEnabled() { io.Stdout.Println(message) From 81ac66bab9d08845b4923cb483832554cb107f4b Mon Sep 17 00:00:00 2001 From: Michael Brooks Date: Tue, 24 Nov 2026 12:28:04 -0700 Subject: [PATCH 2/2] test: check each printer's span name and log attribute --- internal/iostreams/printer_test.go | 62 ++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/internal/iostreams/printer_test.go b/internal/iostreams/printer_test.go index 54b2c409..329e2b27 100644 --- a/internal/iostreams/printer_test.go +++ b/internal/iostreams/printer_test.go @@ -16,6 +16,7 @@ package iostreams import ( "bytes" + "context" "fmt" "log" "regexp" @@ -27,6 +28,10 @@ import ( "github.com/slackapi/slack-cli/internal/slackdeps" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/attribute" + sdktrace "go.opentelemetry.io/otel/sdk/trace" + "go.opentelemetry.io/otel/sdk/trace/tracetest" ) func Test_PrintDebug(t *testing.T) { @@ -177,6 +182,63 @@ func Test_PrintInfo(t *testing.T) { } } +func Test_IOStreams_printSpans(t *testing.T) { + tests := map[string]struct { + print func(ctx context.Context, io *IOStreams) + expectedName string + expectedKey string + expectedValue string + }{ + "PrintDebug": { + print: func(ctx context.Context, io *IOStreams) { io.PrintDebug(ctx, "debug %d", 1) }, + expectedName: "printDebug", + expectedKey: "debug_log", + expectedValue: "debug 1", + }, + "PrintError": { + print: func(ctx context.Context, io *IOStreams) { io.PrintError(ctx, "error %d", 2) }, + expectedName: "printError", + expectedKey: "error_log", + expectedValue: "error 2", + }, + "PrintWarning": { + print: func(ctx context.Context, io *IOStreams) { io.PrintWarning(ctx, "warning %d", 3) }, + expectedName: "printWarning", + expectedKey: "warning_log", + expectedValue: "warning 3", + }, + "PrintInfo when tracing": { + print: func(ctx context.Context, io *IOStreams) { io.PrintInfo(ctx, true, "info %d", 4) }, + expectedName: "printInfo", + expectedKey: "printInfo", + expectedValue: "info 4", + }, + } + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + recorder := tracetest.NewSpanRecorder() + previous := otel.GetTracerProvider() + otel.SetTracerProvider(sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(recorder))) + t.Cleanup(func() { otel.SetTracerProvider(previous) }) + fsMock := slackdeps.NewFsMock() + osMock := slackdeps.NewOsMock() + osMock.AddDefaultMocks() + io := NewIOStreams(config.NewConfig(fsMock, osMock), fsMock, osMock) + io.Stdout = log.New(&bytes.Buffer{}, "", 0) + io.Stderr = log.New(&bytes.Buffer{}, "", 0) + tc.print(slackcontext.MockContext(t.Context()), io) + var found sdktrace.ReadOnlySpan + for _, span := range recorder.Ended() { + if span.Name() == tc.expectedName { + found = span + } + } + require.NotNil(t, found) + assert.Contains(t, found.Attributes(), attribute.String(tc.expectedKey, tc.expectedValue)) + }) + } +} + func Test_IOStreams_PrintTrace(t *testing.T) { tests := map[string]struct { traceID string