Skip to content

Commit

Permalink
ref: Add span options aliases (WithSpanSampled, WithOpName, WithTrans…
Browse files Browse the repository at this point in the history
…actionName) (#624)
  • Loading branch information
tonyo committed Apr 13, 2023
1 parent e70db81 commit eb22fb5
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 12 deletions.
2 changes: 1 addition & 1 deletion http/sentryhttp.go
Expand Up @@ -87,7 +87,7 @@ func (h *Handler) handle(handler http.Handler) http.HandlerFunc {
ctx = sentry.SetHubOnContext(ctx, hub)
}
options := []sentry.SpanOption{
sentry.OpName("http.server"),
sentry.WithOpName("http.server"),
sentry.ContinueFromRequest(r),
sentry.WithTransactionSource(sentry.SourceURL),
}
Expand Down
2 changes: 1 addition & 1 deletion otel/propagator_test.go
Expand Up @@ -32,7 +32,7 @@ func createTransactionAndMaybeSpan(transactionContext transactionTestContext, wi
transaction := sentry.StartTransaction(
emptyContextWithSentry(),
transactionContext.name,
sentry.SpanSampled(transactionContext.sampled),
sentry.WithSpanSampled(transactionContext.sampled),
)
transaction.TraceID = TraceIDFromHex(transactionContext.traceID)
transaction.SpanID = SpanIDFromHex(transactionContext.spanID)
Expand Down
2 changes: 1 addition & 1 deletion otel/span_processor.go
Expand Up @@ -50,7 +50,7 @@ func (ssp *sentrySpanProcessor) OnStart(parent context.Context, s otelSdkTrace.R
transaction := sentry.StartTransaction(
parent,
s.Name(),
sentry.SpanSampled(traceParentContext.Sampled),
sentry.WithSpanSampled(traceParentContext.Sampled),
)
transaction.SpanID = sentry.SpanID(otelSpanID)
transaction.TraceID = sentry.TraceID(otelTraceID)
Expand Down
27 changes: 26 additions & 1 deletion tracing.go
Expand Up @@ -787,14 +787,32 @@ type SpanOption func(s *Span)
// A span tree has a single transaction name, therefore using this option when
// starting a span affects the span tree as a whole, potentially overwriting a
// name set previously.
//
// Deprecated: Use WithTransactionSource() instead.
func TransactionName(name string) SpanOption {
return WithTransactionName(name)
}

// WithTransactionName option sets the name of the current transaction.
//
// A span tree has a single transaction name, therefore using this option when
// starting a span affects the span tree as a whole, potentially overwriting a
// name set previously.
func WithTransactionName(name string) SpanOption {
return func(s *Span) {
s.Name = name
}
}

// OpName sets the operation name for a given span.
//
// Deprecated: Use WithOpName() instead.
func OpName(name string) SpanOption {
return WithOpName(name)
}

// WithOpName sets the operation name for a given span.
func WithOpName(name string) SpanOption {
return func(s *Span) {
s.Op = name
}
Expand All @@ -815,7 +833,14 @@ func WithTransactionSource(source TransactionSource) SpanOption {
}

// SpanSampled updates the sampling flag for a given span.
//
// Deprecated: Use WithSpanSampled() instead.
func SpanSampled(sampled Sampled) SpanOption {
return WithSpanSampled(sampled)
}

// WithSpanSampled updates the sampling flag for a given span.
func WithSpanSampled(sampled Sampled) SpanOption {
return func(s *Span) {
s.Sampled = sampled
}
Expand Down Expand Up @@ -911,7 +936,7 @@ func StartTransaction(ctx context.Context, name string, options ...SpanOption) *
return currentTransaction
}

options = append(options, TransactionName(name))
options = append(options, WithTransactionName(name))
return StartSpan(
ctx,
"",
Expand Down
36 changes: 28 additions & 8 deletions tracing_test.go
Expand Up @@ -106,7 +106,7 @@ func TestStartSpan(t *testing.T) {
"k": "v",
}
span := StartSpan(ctx, op,
TransactionName(transaction),
WithTransactionName(transaction),
func(s *Span) {
s.Description = description
s.Status = status
Expand Down Expand Up @@ -177,7 +177,7 @@ func TestStartChild(t *testing.T) {
TracesSampleRate: 1.0,
Transport: transport,
})
span := StartSpan(ctx, "top", TransactionName("Test Transaction"))
span := StartSpan(ctx, "top", WithTransactionName("Test Transaction"))
child := span.StartChild("child")
child.Finish()
span.Finish()
Expand Down Expand Up @@ -611,7 +611,7 @@ func TestDoubleSampling(t *testing.T) {
TracesSampleRate: 1.0,
Transport: transport,
})
span := StartSpan(ctx, "op", TransactionName("name"))
span := StartSpan(ctx, "op", WithTransactionName("name"))

// CaptureException should not send any event because of SampleRate.
GetHubFromContext(ctx).CaptureException(errors.New("ignored"))
Expand All @@ -638,7 +638,7 @@ func TestSample(t *testing.T) {
ctx = NewTestContext(ClientOptions{
EnableTracing: false,
})
span = StartSpan(ctx, "op", TransactionName("name"))
span = StartSpan(ctx, "op", WithTransactionName("name"))
if got := span.Sampled; got != SampledFalse {
t.Fatalf("got %s, want %s", got, SampledFalse)
}
Expand All @@ -648,7 +648,7 @@ func TestSample(t *testing.T) {
EnableTracing: true,
TracesSampleRate: 0.0,
})
span = StartSpan(ctx, "op", TransactionName("name"), SpanSampled(SampledTrue))
span = StartSpan(ctx, "op", WithTransactionName("name"), WithSpanSampled(SampledTrue))
if got := span.Sampled; got != SampledTrue {
t.Fatalf("got %s, want %s", got, SampledTrue)
}
Expand All @@ -660,7 +660,7 @@ func TestSample(t *testing.T) {
return 1.0
},
})
span = StartSpan(ctx, "op", TransactionName("name"))
span = StartSpan(ctx, "op", WithTransactionName("name"))
if got := span.Sampled; got != SampledTrue {
t.Fatalf("got %s, want %s", got, SampledTrue)
}
Expand All @@ -670,7 +670,7 @@ func TestSample(t *testing.T) {
EnableTracing: true,
TracesSampleRate: 1.0,
})
span = StartSpan(ctx, "op", TransactionName("name"))
span = StartSpan(ctx, "op", WithTransactionName("name"))
childSpan := span.StartChild("child")
if got := childSpan.Sampled; got != SampledTrue {
t.Fatalf("got %s, want %s", got, SampledTrue)
Expand All @@ -681,7 +681,7 @@ func TestSample(t *testing.T) {
EnableTracing: true,
TracesSampleRate: 1.0,
})
span = StartSpan(ctx, "op", TransactionName("name"))
span = StartSpan(ctx, "op", WithTransactionName("name"))
if got := span.Sampled; got != SampledTrue {
t.Fatalf("got %s, want %s", got, SampledTrue)
}
Expand Down Expand Up @@ -870,3 +870,23 @@ func TestSpanSetContextOverrides(t *testing.T) {

assertEqual(t, map[string]Context{"a": {"foo": 2}}, transaction.contexts)
}

// This test should be the only thing to fail when deprecated TransactionName is removed.
func TestDeprecatedSpanOptionTransactionName(t *testing.T) {
StartSpan(context.Background(), "op", TransactionName("name"))
}

// This test should be the only thing to fail when deprecated OpName is removed.
func TestDeprecatedSpanOptionOpName(t *testing.T) {
StartSpan(context.Background(), "op", OpName("name"))
}

// This test should be the only thing to fail when deprecated SpanSampled is removed.
func TestDeprecatedSpanOptionSpanSampled(t *testing.T) {
StartSpan(context.Background(), "op", SpanSampled(SampledTrue))
}

// This test should be the only thing to fail when deprecated TransctionSource is removed.
func TestDeprecatedSpanOptionTransctionSource(t *testing.T) {
StartSpan(context.Background(), "op", TransctionSource("src"))
}

0 comments on commit eb22fb5

Please sign in to comment.