Skip to content
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

ref: Add span options aliases (WithSpanSampled, WithOpName, WithTransactionName) #624

Merged
merged 6 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion http/sentryhttp.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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"))
}