Skip to content

Commit eb22fb5

Browse files
authored
ref: Add span options aliases (WithSpanSampled, WithOpName, WithTransactionName) (#624)
1 parent e70db81 commit eb22fb5

File tree

5 files changed

+57
-12
lines changed

5 files changed

+57
-12
lines changed

http/sentryhttp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (h *Handler) handle(handler http.Handler) http.HandlerFunc {
8787
ctx = sentry.SetHubOnContext(ctx, hub)
8888
}
8989
options := []sentry.SpanOption{
90-
sentry.OpName("http.server"),
90+
sentry.WithOpName("http.server"),
9191
sentry.ContinueFromRequest(r),
9292
sentry.WithTransactionSource(sentry.SourceURL),
9393
}

otel/propagator_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func createTransactionAndMaybeSpan(transactionContext transactionTestContext, wi
3232
transaction := sentry.StartTransaction(
3333
emptyContextWithSentry(),
3434
transactionContext.name,
35-
sentry.SpanSampled(transactionContext.sampled),
35+
sentry.WithSpanSampled(transactionContext.sampled),
3636
)
3737
transaction.TraceID = TraceIDFromHex(transactionContext.traceID)
3838
transaction.SpanID = SpanIDFromHex(transactionContext.spanID)

otel/span_processor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (ssp *sentrySpanProcessor) OnStart(parent context.Context, s otelSdkTrace.R
5050
transaction := sentry.StartTransaction(
5151
parent,
5252
s.Name(),
53-
sentry.SpanSampled(traceParentContext.Sampled),
53+
sentry.WithSpanSampled(traceParentContext.Sampled),
5454
)
5555
transaction.SpanID = sentry.SpanID(otelSpanID)
5656
transaction.TraceID = sentry.TraceID(otelTraceID)

tracing.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,14 +787,32 @@ type SpanOption func(s *Span)
787787
// A span tree has a single transaction name, therefore using this option when
788788
// starting a span affects the span tree as a whole, potentially overwriting a
789789
// name set previously.
790+
//
791+
// Deprecated: Use WithTransactionSource() instead.
790792
func TransactionName(name string) SpanOption {
793+
return WithTransactionName(name)
794+
}
795+
796+
// WithTransactionName option sets the name of the current transaction.
797+
//
798+
// A span tree has a single transaction name, therefore using this option when
799+
// starting a span affects the span tree as a whole, potentially overwriting a
800+
// name set previously.
801+
func WithTransactionName(name string) SpanOption {
791802
return func(s *Span) {
792803
s.Name = name
793804
}
794805
}
795806

796807
// OpName sets the operation name for a given span.
808+
//
809+
// Deprecated: Use WithOpName() instead.
797810
func OpName(name string) SpanOption {
811+
return WithOpName(name)
812+
}
813+
814+
// WithOpName sets the operation name for a given span.
815+
func WithOpName(name string) SpanOption {
798816
return func(s *Span) {
799817
s.Op = name
800818
}
@@ -815,7 +833,14 @@ func WithTransactionSource(source TransactionSource) SpanOption {
815833
}
816834

817835
// SpanSampled updates the sampling flag for a given span.
836+
//
837+
// Deprecated: Use WithSpanSampled() instead.
818838
func SpanSampled(sampled Sampled) SpanOption {
839+
return WithSpanSampled(sampled)
840+
}
841+
842+
// WithSpanSampled updates the sampling flag for a given span.
843+
func WithSpanSampled(sampled Sampled) SpanOption {
819844
return func(s *Span) {
820845
s.Sampled = sampled
821846
}
@@ -911,7 +936,7 @@ func StartTransaction(ctx context.Context, name string, options ...SpanOption) *
911936
return currentTransaction
912937
}
913938

914-
options = append(options, TransactionName(name))
939+
options = append(options, WithTransactionName(name))
915940
return StartSpan(
916941
ctx,
917942
"",

tracing_test.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func TestStartSpan(t *testing.T) {
106106
"k": "v",
107107
}
108108
span := StartSpan(ctx, op,
109-
TransactionName(transaction),
109+
WithTransactionName(transaction),
110110
func(s *Span) {
111111
s.Description = description
112112
s.Status = status
@@ -177,7 +177,7 @@ func TestStartChild(t *testing.T) {
177177
TracesSampleRate: 1.0,
178178
Transport: transport,
179179
})
180-
span := StartSpan(ctx, "top", TransactionName("Test Transaction"))
180+
span := StartSpan(ctx, "top", WithTransactionName("Test Transaction"))
181181
child := span.StartChild("child")
182182
child.Finish()
183183
span.Finish()
@@ -611,7 +611,7 @@ func TestDoubleSampling(t *testing.T) {
611611
TracesSampleRate: 1.0,
612612
Transport: transport,
613613
})
614-
span := StartSpan(ctx, "op", TransactionName("name"))
614+
span := StartSpan(ctx, "op", WithTransactionName("name"))
615615

616616
// CaptureException should not send any event because of SampleRate.
617617
GetHubFromContext(ctx).CaptureException(errors.New("ignored"))
@@ -638,7 +638,7 @@ func TestSample(t *testing.T) {
638638
ctx = NewTestContext(ClientOptions{
639639
EnableTracing: false,
640640
})
641-
span = StartSpan(ctx, "op", TransactionName("name"))
641+
span = StartSpan(ctx, "op", WithTransactionName("name"))
642642
if got := span.Sampled; got != SampledFalse {
643643
t.Fatalf("got %s, want %s", got, SampledFalse)
644644
}
@@ -648,7 +648,7 @@ func TestSample(t *testing.T) {
648648
EnableTracing: true,
649649
TracesSampleRate: 0.0,
650650
})
651-
span = StartSpan(ctx, "op", TransactionName("name"), SpanSampled(SampledTrue))
651+
span = StartSpan(ctx, "op", WithTransactionName("name"), WithSpanSampled(SampledTrue))
652652
if got := span.Sampled; got != SampledTrue {
653653
t.Fatalf("got %s, want %s", got, SampledTrue)
654654
}
@@ -660,7 +660,7 @@ func TestSample(t *testing.T) {
660660
return 1.0
661661
},
662662
})
663-
span = StartSpan(ctx, "op", TransactionName("name"))
663+
span = StartSpan(ctx, "op", WithTransactionName("name"))
664664
if got := span.Sampled; got != SampledTrue {
665665
t.Fatalf("got %s, want %s", got, SampledTrue)
666666
}
@@ -670,7 +670,7 @@ func TestSample(t *testing.T) {
670670
EnableTracing: true,
671671
TracesSampleRate: 1.0,
672672
})
673-
span = StartSpan(ctx, "op", TransactionName("name"))
673+
span = StartSpan(ctx, "op", WithTransactionName("name"))
674674
childSpan := span.StartChild("child")
675675
if got := childSpan.Sampled; got != SampledTrue {
676676
t.Fatalf("got %s, want %s", got, SampledTrue)
@@ -681,7 +681,7 @@ func TestSample(t *testing.T) {
681681
EnableTracing: true,
682682
TracesSampleRate: 1.0,
683683
})
684-
span = StartSpan(ctx, "op", TransactionName("name"))
684+
span = StartSpan(ctx, "op", WithTransactionName("name"))
685685
if got := span.Sampled; got != SampledTrue {
686686
t.Fatalf("got %s, want %s", got, SampledTrue)
687687
}
@@ -870,3 +870,23 @@ func TestSpanSetContextOverrides(t *testing.T) {
870870

871871
assertEqual(t, map[string]Context{"a": {"foo": 2}}, transaction.contexts)
872872
}
873+
874+
// This test should be the only thing to fail when deprecated TransactionName is removed.
875+
func TestDeprecatedSpanOptionTransactionName(t *testing.T) {
876+
StartSpan(context.Background(), "op", TransactionName("name"))
877+
}
878+
879+
// This test should be the only thing to fail when deprecated OpName is removed.
880+
func TestDeprecatedSpanOptionOpName(t *testing.T) {
881+
StartSpan(context.Background(), "op", OpName("name"))
882+
}
883+
884+
// This test should be the only thing to fail when deprecated SpanSampled is removed.
885+
func TestDeprecatedSpanOptionSpanSampled(t *testing.T) {
886+
StartSpan(context.Background(), "op", SpanSampled(SampledTrue))
887+
}
888+
889+
// This test should be the only thing to fail when deprecated TransctionSource is removed.
890+
func TestDeprecatedSpanOptionTransctionSource(t *testing.T) {
891+
StartSpan(context.Background(), "op", TransctionSource("src"))
892+
}

0 commit comments

Comments
 (0)