feat(tracing): add RunTraced() function
This commit is contained in:
parent
0338be0407
commit
8bf2fa122d
1 changed files with 12 additions and 2 deletions
14
tracing.go
14
tracing.go
|
@ -18,6 +18,8 @@ var (
|
||||||
tracerContextKey = contextKey{"tracer"}
|
tracerContextKey = contextKey{"tracer"}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ConfigureSpanStartFunc = func(context.Context) (context.Context, []trace.SpanStartOption, []sentry.SpanOption)
|
||||||
|
|
||||||
func (t *Telemetry) StartSpan(ctx context.Context, operation, name string, cfgs ...ConfigureSpanStartFunc) *Span {
|
func (t *Telemetry) StartSpan(ctx context.Context, operation, name string, cfgs ...ConfigureSpanStartFunc) *Span {
|
||||||
otelStartOpts := make([]trace.SpanStartOption, 0)
|
otelStartOpts := make([]trace.SpanStartOption, 0)
|
||||||
sentryStartOpts := []sentry.SpanOption{sentry.WithTransactionName(name), sentry.WithDescription(name)}
|
sentryStartOpts := []sentry.SpanOption{sentry.WithTransactionName(name), sentry.WithDescription(name)}
|
||||||
|
@ -48,8 +50,6 @@ func (t *Telemetry) StartSpan(ctx context.Context, operation, name string, cfgs
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type ConfigureSpanStartFunc = func(context.Context) (context.Context, []trace.SpanStartOption, []sentry.SpanOption)
|
|
||||||
|
|
||||||
func WithOtelOptions(opts ...trace.SpanStartOption) ConfigureSpanStartFunc {
|
func WithOtelOptions(opts ...trace.SpanStartOption) ConfigureSpanStartFunc {
|
||||||
return func(ctx context.Context) (context.Context, []trace.SpanStartOption, []sentry.SpanOption) {
|
return func(ctx context.Context) (context.Context, []trace.SpanStartOption, []sentry.SpanOption) {
|
||||||
return ctx, opts, []sentry.SpanOption{}
|
return ctx, opts, []sentry.SpanOption{}
|
||||||
|
@ -81,3 +81,13 @@ func WithOtelTracer(tracer trace.Tracer) ConfigureSpanStartFunc {
|
||||||
return context.WithValue(ctx, tracerContextKey, tracer), []trace.SpanStartOption{}, []sentry.SpanOption{}
|
return context.WithValue(ctx, tracerContextKey, tracer), []trace.SpanStartOption{}, []sentry.SpanOption{}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func RunTraced[T any](t *Telemetry, op string, fn func(context.Context, ...any) T) func(context.Context, string, ...any) T {
|
||||||
|
return func(ctx context.Context, name string, args ...any) T {
|
||||||
|
span := t.StartSpan(ctx, op, name)
|
||||||
|
defer span.End()
|
||||||
|
ctx = span.Context()
|
||||||
|
|
||||||
|
return fn(ctx, args...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue