Repository navigation
feat: add v2 version of tracex #5305
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| package v2 | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "runtime/debug" | ||
|
|
||
| "go.opentelemetry.io/otel/attribute" | ||
| otelcodes "go.opentelemetry.io/otel/codes" | ||
| "go.opentelemetry.io/otel/trace" | ||
| ) | ||
|
|
||
| type options struct { | ||
| OkStatusDescription string | ||
| } | ||
|
|
||
| var defaultSpanOptions = options{ | ||
| OkStatusDescription: "success", | ||
| } | ||
|
|
||
| // SpanOption configures the behavior of a reusable span definition. | ||
| type SpanOption interface { | ||
| apply(*options) | ||
| } | ||
|
|
||
| type spanOptionFunc func(*options) | ||
|
|
||
| func (f spanOptionFunc) apply(o *options) { | ||
| f(o) | ||
| } | ||
|
|
||
| // WithOkStatusDescription sets the description passed to SetStatus when a callback | ||
| // succeeds. The tracer implementation may discard descriptions for an OK status. | ||
| func WithOkStatusDescription(desc string) SpanOption { | ||
| return spanOptionFunc(func(o *options) { | ||
| o.OkStatusDescription = desc | ||
| }) | ||
| } | ||
|
|
||
| // Span is a reusable tracing configuration. Each Do or Do2 call starts and ends | ||
| // its own span using the caller's context. | ||
| type Span struct { | ||
| tracer trace.Tracer | ||
| name string | ||
| spanOpts []trace.SpanStartOption | ||
| opts options | ||
| } | ||
|
|
||
| // WithOption returns a copy with the supplied options applied in order, preserving | ||
| // existing configuration without modifying the receiver or starting a span. | ||
| func (s *Span) WithOption(opts ...SpanOption) *Span { | ||
| r := NewSpan(s.tracer, s.name, s.spanOpts...) | ||
| r.opts = s.opts | ||
|
|
||
| for _, o := range opts { | ||
| o.apply(&r.opts) | ||
| } | ||
|
|
||
| return r | ||
| } | ||
|
|
||
| // Do runs f with a context containing a new span and returns its error unchanged. | ||
| // It uses the same status, panic recording, and cleanup behavior as Do2. | ||
| func (s *Span) Do(ctx context.Context, f func(context.Context) error) error { | ||
| _, err := s.Do2[any](ctx, func(ctx context.Context) (any, error) { | ||
| return nil, f(ctx) | ||
| }) | ||
|
|
||
| return err | ||
| } | ||
|
|
||
| // Do2 runs f with a context containing a new span and returns its result and error | ||
| // unchanged. Callback errors are recorded with an error status; a nil error sets | ||
| // an OK status. Panics are recorded with a stack trace and rethrown unchanged. | ||
| // The span is ended even when the callback panics or calls runtime.Goexit. | ||
| func (s *Span) Do2[T any](ctx context.Context, f func(context.Context) (T, error)) (T, error) { | ||
| sCtx, span := s.tracer.Start(ctx, s.name, s.spanOpts...) | ||
|
|
||
| defer func() { | ||
| span.End() | ||
| }() | ||
|
|
||
| defer func() { | ||
| if r := recover(); r != nil { | ||
| span.RecordError( | ||
| fmt.Errorf("panic: %v", r), | ||
| trace.WithStackTrace(true), | ||
| trace.WithAttributes( | ||
| attribute.String("panic.stacktrace", fmt.Sprintf("%v", string(debug.Stack()))), | ||
| ), | ||
| ) | ||
| span.SetStatus(otelcodes.Error, "panic") | ||
|
|
||
| panic(r) | ||
|
greptile-apps[bot] marked this conversation as resolved.
|
||
| } | ||
| }() | ||
|
|
||
| result, err := f(sCtx) | ||
| if err != nil { | ||
| span.RecordError(err) | ||
| span.SetStatus(otelcodes.Error, err.Error()) | ||
| } else { | ||
| span.SetStatus(otelcodes.Ok, s.opts.OkStatusDescription) | ||
| } | ||
|
|
||
| return result, err | ||
| } | ||
|
|
||
| var defaultTraceSpanOptions = []trace.SpanStartOption{ | ||
| trace.WithSpanKind(trace.SpanKindInternal), | ||
| } | ||
|
|
||
| // NewSpan creates a reusable tracing configuration without starting a span. | ||
| // The name and start options are used for every Do or Do2 invocation. | ||
| func NewSpan(tracer trace.Tracer, name string, opts ...trace.SpanStartOption) *Span { | ||
| return &Span{tracer, name, append(defaultTraceSpanOptions, opts...), defaultSpanOptions} | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.