Skip to content

Commit

Permalink
Remove the boolean and go for maximum magic
Browse files Browse the repository at this point in the history
  • Loading branch information
phacops committed Oct 21, 2022
1 parent 68faf86 commit 8027243
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 22 deletions.
2 changes: 1 addition & 1 deletion http/sentryhttp.go
Expand Up @@ -92,7 +92,7 @@ func (h *Handler) handle(handler http.Handler) http.HandlerFunc {
}
// We don't mind getting an existing transaction back so we don't need to
// check if it is.
transaction, _ := sentry.StartTransaction(ctx,
transaction := sentry.StartTransaction(ctx,
fmt.Sprintf("%s %s", r.Method, r.URL.Path),
options...,
)
Expand Down
11 changes: 5 additions & 6 deletions tracing.go
Expand Up @@ -636,11 +636,10 @@ func spanFromContext(ctx context.Context) *Span {

// StartTransaction will create a transaction (root span) if there's no existing
// transaction in the context otherwise, it will return the existing transaction.
// The boolean indicates if it returned the existing transaction or not.
func StartTransaction(ctx context.Context, name string, options ...SpanOption) (*Span, bool) {
currentTransaction := ctx.Value(spanContextKey{})
if currentTransaction != nil {
return currentTransaction.(*Span), true
func StartTransaction(ctx context.Context, name string, options ...SpanOption) *Span {
currentTransaction, exists := ctx.Value(spanContextKey{}).(*Span)
if exists {
return currentTransaction
}
hub := GetHubFromContext(ctx)
if hub == nil {
Expand All @@ -652,5 +651,5 @@ func StartTransaction(ctx context.Context, name string, options ...SpanOption) (
ctx,
"",
options...,
), false
)
}
16 changes: 1 addition & 15 deletions tracing_test.go
Expand Up @@ -240,7 +240,7 @@ func TestStartTransaction(t *testing.T) {
data := map[string]interface{}{
"k": "v",
}
transaction, _ := StartTransaction(ctx,
transaction := StartTransaction(ctx,
transactionName,
func(s *Span) {
s.Description = description
Expand Down Expand Up @@ -298,20 +298,6 @@ func TestStartTransaction(t *testing.T) {
}
}

func TestStartTransactionAlreadyInProgress(t *testing.T) {
transport := &TransportMock{}
ctx := NewTestContext(ClientOptions{
Transport: transport,
})
// Simulate a transaction already in progress
ctx = context.WithValue(ctx, spanContextKey{}, &Span{})
transactionName := "Test Transaction"
_, existing := StartTransaction(ctx, transactionName)
if !existing {
t.Fatal("StartTransaction should return true if a transaction is already in progress")
}
}

// testContextKey is used to store a value in a context so that we can check
// that SDK operations on that context preserve the original context values.
type testContextKey struct{}
Expand Down

0 comments on commit 8027243

Please sign in to comment.