Repository navigation
refactor(iostreams): use OpenTelemetry spans in the printers #864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a641ab1
refactor: use OpenTelemetry spans in internal/iostreams printers
mwbrooks 81ac66b
test: check each printer's span name and log attribute
mwbrooks 5f02387
Merge remote-tracking branch 'origin/main' into mwbrooks-otel-iostreams
mwbrooks 8b31394
Merge remote-tracking branch 'origin/main' into mwbrooks-otel-iostreams
mwbrooks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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))) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.SpanRecordertest, likeinternal/pkg/create/create_test.go, that checks each printer's span name and log attribute? Nothing asserts them today,PrintErrorhas no test, andPrintInfonever runs withshouldTracetrue.There was a problem hiding this comment.
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_printSpanschecks the span name and log attribute for each printer, includingPrintErrorandPrintInfowithshouldTrace🙇