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

Tracing without performance improvements #862

Merged
merged 17 commits into from
Sep 4, 2024
22 changes: 0 additions & 22 deletions hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,28 +366,6 @@ func (hub *Hub) Flush(timeout time.Duration) bool {
return client.Flush(timeout)
}

// Continue a trace based on HTTP header values. If performance is enabled this
// returns a SpanOption that can be used to start a transaction, otherwise nil.
// TODO: remove - moved to tracing.go.
func (hub *Hub) ContinueTrace(trace, baggage string) (SpanOption, error) {
cleptric marked this conversation as resolved.
Show resolved Hide resolved
scope := hub.Scope()
propagationContext, err := PropagationContextFromHeaders(trace, baggage)
if err != nil {
return nil, err
}

scope.SetPropagationContext(propagationContext)

client := hub.Client()
if client != nil && client.options.EnableTracing {
return ContinueFromHeaders(trace, baggage), nil
}

scope.SetContext("trace", propagationContext.Map())

return nil, nil
}

// GetTraceparent returns the current Sentry traceparent string, to be used as a HTTP header value
// or HTML meta tag value.
// This function is context aware, as in it either returns the traceparent based
Expand Down
84 changes: 0 additions & 84 deletions hub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package sentry

import (
"context"
"errors"
"fmt"
"strings"
"sync"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/stretchr/testify/assert"
)

const testDsn = "http://[email protected]/1337"
Expand Down Expand Up @@ -327,88 +325,6 @@ func TestHasHubOnContextReturnsFalseIfHubIsNotThere(t *testing.T) {
assertEqual(t, false, HasHubOnContext(ctx))
}

func TestHub_ContinueTrace(t *testing.T) {
newScope := func() *Scope {
return &Scope{contexts: make(map[string]Context)}
}

mockClient := &Client{options: ClientOptions{EnableTracing: true}}

tests := map[string]struct {
hub *Hub
trace string
baggage string
expectedErr error
expectedSpan bool // Whether a SpanOption is expected to be returned
checkScope func(t *testing.T, scope *Scope) // Additional checks on the scope
}{
"Valid trace and baggage": {
hub: NewHub(mockClient, newScope()),
trace: "4fbfb1b884c8532962a3c0b7b834428e-a9f442f9330b4e09",
baggage: "sentry-release=1.0.0,sentry-environment=production",
expectedErr: nil,
expectedSpan: true,
checkScope: func(t *testing.T, scope *Scope) {
assert.Equal(t, "4fbfb1b884c8532962a3c0b7b834428e", scope.propagationContext.TraceID.String())
},
},
"Invalid trace": {
hub: NewHub(mockClient, newScope()),
trace: "invalid",
baggage: "sentry-release=1.0.0,sentry-environment=production",
expectedErr: nil,
expectedSpan: true,
checkScope: func(t *testing.T, scope *Scope) {
assert.NotEmpty(t, scope.propagationContext.TraceID.String())
},
},
"Invalid baggage": {
hub: NewHub(mockClient, newScope()),
trace: "4fbfb1b884c8532962a3c0b7b834428e-a9f442f9330b4e09",
baggage: "invalid_baggage",
expectedErr: errors.New("invalid baggage list-member: \"invalid_baggage\""),
expectedSpan: false,
checkScope: func(t *testing.T, scope *Scope) {
assert.Equal(t, "00000000000000000000000000000000", scope.propagationContext.TraceID.String())
},
},
"Tracing not enabled": {
hub: NewHub(&Client{options: ClientOptions{EnableTracing: false}}, newScope()),
trace: "4fbfb1b884c8532962a3c0b7b834428e-a9f442f9330b4e09",
baggage: "sentry-release=1.0.0,sentry-environment=production",
expectedErr: nil,
expectedSpan: false,
checkScope: func(t *testing.T, scope *Scope) {
assert.Equal(t, "4fbfb1b884c8532962a3c0b7b834428e", scope.propagationContext.TraceID.String())
assert.Contains(t, scope.contexts, "trace")
},
},
}

for name, tt := range tests {
t.Run(name, func(t *testing.T) {
opt, err := tt.hub.ContinueTrace(tt.trace, tt.baggage)

if tt.expectedErr != nil {
assert.Error(t, err, "expected error, got nil")
assert.Equal(t, tt.expectedErr.Error(), err.Error())
} else {
assert.NoError(t, err, "expected no error, got one")
}

// Check for expected SpanOption
if tt.expectedSpan {
assert.NotNil(t, opt, "expected SpanOption, got nil")
} else {
assert.Nil(t, opt, "expected no SpanOption, got one")
}

// Additional checks on the scope
tt.checkScope(t, tt.hub.Scope())
})
}
}

func TestGetTraceparent(t *testing.T) {
tests := map[string]struct {
hub *Hub
Expand Down