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 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@
// 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 @@
}

// SpanSampled updates the sampling flag for a given span.
//
// Deprecated: Use WithSpanSampled() instead

Check failure on line 837 in tracing.go

View workflow job for this annotation

GitHub Actions / Lint

Comment should end in a period (godot)
func SpanSampled(sampled Sampled) SpanOption {
return WithSpanSampled(sampled)
}

// SpanSampled 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 @@
return currentTransaction
}

options = append(options, TransactionName(name))
options = append(options, WithTransactionName(name))
return StartSpan(
ctx,
"",
Expand Down
31 changes: 23 additions & 8 deletions tracing_test.go
Expand Up @@ -106,7 +106,7 @@
"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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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,18 @@

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

// This test should be the only thing to fail when deprecated TransactionName is removed

Check failure on line 874 in tracing_test.go

View workflow job for this annotation

GitHub Actions / Lint

Comment should end in a period (godot)
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

Check failure on line 879 in tracing_test.go

View workflow job for this annotation

GitHub Actions / Lint

Comment should end in a period (godot)
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))
}