-
Notifications
You must be signed in to change notification settings - Fork 599
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add functions for attaching errors to spans
The "spancheck" linter reminds us to call "Span.RecordError" when returning an error. Two functions help with that: "tracing.Check" and "tracing.Escape".
- Loading branch information
Showing
9 changed files
with
172 additions
and
42 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright 2021 - 2024 Crunchy Data Solutions, Inc. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package tracing | ||
|
||
import ( | ||
semconv "go.opentelemetry.io/otel/semconv/v1.27.0" | ||
"go.opentelemetry.io/otel/trace" | ||
) | ||
|
||
// Check returns true when err is nil. Otherwise, it adds err as an exception | ||
// event on s and returns false. If you intend to return err, consider using | ||
// [Escape] instead. | ||
// | ||
// See: https://opentelemetry.io/docs/specs/semconv/exceptions/exceptions-spans | ||
func Check(s Span, err error) bool { | ||
if err == nil { | ||
return true | ||
} | ||
if s.IsRecording() { | ||
s.RecordError(err) | ||
} | ||
return false | ||
} | ||
|
||
// Escape adds non-nil err as an escaped exception event on s and returns err. | ||
// See: https://opentelemetry.io/docs/specs/semconv/exceptions/exceptions-spans | ||
func Escape(s Span, err error) error { | ||
if err != nil && s.IsRecording() { | ||
s.RecordError(err, trace.WithAttributes(semconv.ExceptionEscaped(true))) | ||
} | ||
return err | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright 2021 - 2024 Crunchy Data Solutions, Inc. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package tracing | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"go.opentelemetry.io/otel/sdk/trace" | ||
"go.opentelemetry.io/otel/sdk/trace/tracetest" | ||
semconv "go.opentelemetry.io/otel/semconv/v1.27.0" | ||
"gotest.tools/v3/assert" | ||
) | ||
|
||
func TestCheck(t *testing.T) { | ||
recorder := tracetest.NewSpanRecorder() | ||
tracer := trace.NewTracerProvider( | ||
trace.WithSpanProcessor(recorder), | ||
).Tracer("") | ||
|
||
{ | ||
_, span := tracer.Start(context.Background(), "") | ||
assert.Assert(t, Check(span, nil)) | ||
span.End() | ||
|
||
spans := recorder.Ended() | ||
assert.Equal(t, len(spans), 1) | ||
assert.Equal(t, len(spans[0].Events()), 0, "expected no events") | ||
} | ||
|
||
{ | ||
_, span := tracer.Start(context.Background(), "") | ||
assert.Assert(t, !Check(span, errors.New("msg"))) | ||
span.End() | ||
|
||
spans := recorder.Ended() | ||
assert.Equal(t, len(spans), 2) | ||
assert.Equal(t, len(spans[1].Events()), 1, "expected one event") | ||
|
||
event := spans[1].Events()[0] | ||
assert.Equal(t, event.Name, semconv.ExceptionEventName) | ||
|
||
attrs := event.Attributes | ||
assert.Equal(t, len(attrs), 2) | ||
assert.Equal(t, string(attrs[0].Key), "exception.type") | ||
assert.Equal(t, string(attrs[1].Key), "exception.message") | ||
assert.Equal(t, attrs[0].Value.AsInterface(), "*errors.errorString") | ||
assert.Equal(t, attrs[1].Value.AsInterface(), "msg") | ||
} | ||
} | ||
|
||
func TestEscape(t *testing.T) { | ||
recorder := tracetest.NewSpanRecorder() | ||
tracer := trace.NewTracerProvider( | ||
trace.WithSpanProcessor(recorder), | ||
).Tracer("") | ||
|
||
{ | ||
_, span := tracer.Start(context.Background(), "") | ||
assert.NilError(t, Escape(span, nil)) | ||
span.End() | ||
|
||
spans := recorder.Ended() | ||
assert.Equal(t, len(spans), 1) | ||
assert.Equal(t, len(spans[0].Events()), 0, "expected no events") | ||
} | ||
|
||
{ | ||
_, span := tracer.Start(context.Background(), "") | ||
expected := errors.New("somesuch") | ||
assert.Assert(t, errors.Is(Escape(span, expected), expected), | ||
"expected to unwrap the original error") | ||
span.End() | ||
|
||
spans := recorder.Ended() | ||
assert.Equal(t, len(spans), 2) | ||
assert.Equal(t, len(spans[1].Events()), 1, "expected one event") | ||
|
||
event := spans[1].Events()[0] | ||
assert.Equal(t, event.Name, semconv.ExceptionEventName) | ||
|
||
attrs := event.Attributes | ||
assert.Equal(t, len(attrs), 3) | ||
assert.Equal(t, string(attrs[0].Key), "exception.escaped") | ||
assert.Equal(t, string(attrs[1].Key), "exception.type") | ||
assert.Equal(t, string(attrs[2].Key), "exception.message") | ||
assert.Equal(t, attrs[0].Value.AsInterface(), true) | ||
assert.Equal(t, attrs[1].Value.AsInterface(), "*errors.errorString") | ||
assert.Equal(t, attrs[2].Value.AsInterface(), "somesuch") | ||
} | ||
} |