diff --git a/tracing.go b/tracing.go index f8fbe85..3098a6f 100644 --- a/tracing.go +++ b/tracing.go @@ -18,6 +18,8 @@ var ( 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 { otelStartOpts := make([]trace.SpanStartOption, 0) 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 { return func(ctx context.Context) (context.Context, []trace.SpanStartOption, []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{} } } + +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...) + } +}