Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions internal/iostreams/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧪 suggestion (non-blocking): Could we add a tracetest.SpanRecorder test, like internal/pkg/create/create_test.go, that checks each printer's span name and log attribute? Nothing asserts them today, PrintError has no test, and PrintInfo never runs with shouldTrace true.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in commit 81ac66b, great catch! Test_IOStreams_printSpans checks the span name and log attribute for each printer, including PrintError and PrintInfo with shouldTrace 🙇

defer span.End()
lines := strings.SplitSeq(message, "\n")
for line := range lines {
_ = io.FlushToLogFile(ctx, "debug", line)
Expand All @@ -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")) {
Expand All @@ -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)
}
Expand All @@ -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)
Expand Down
62 changes: 62 additions & 0 deletions internal/iostreams/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package iostreams

import (
"bytes"
"context"
"fmt"
"log"
"regexp"
Expand All @@ -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) {
Expand Down Expand Up @@ -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)))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯 praise: Swapping the global tracer provider with a cleanup keeps this test from leaking spans into the rest of the suite, same as the auth and api tests.

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
Expand Down
Loading