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

feat: Add "transaction" pointer on Span #552

Closed
wants to merge 2 commits into from
Closed
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
42 changes: 29 additions & 13 deletions tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ type Span struct { //nolint: maligned // prefer readability over optimal memory
// parent refers to the immediate local parent span. A remote parent span is
// only referenced by setting ParentSpanID.
parent *Span
// isTransaction is true only for the root span of a local span tree. The
// root span is the first span started in a context. Note that a local root
// span may have a remote parent belonging to the same trace, therefore
// isTransaction depends on ctx and not on parent.
isTransaction bool
// transaction refers to the root span of a local span tree. For the root span
// itself it is set to nil. The root span is the first span started in a context.
// Note that a local root span may have a remote parent belonging to the same trace,
// therefore the transaction pointer depends on ctx and not on parent.
transaction *Span
// recorder stores all spans in a transaction. Guaranteed to be non-nil.
recorder *spanRecorder
}
Expand Down Expand Up @@ -104,9 +104,8 @@ func StartSpan(ctx context.Context, operation string, options ...SpanOption) *Sp
StartTime: time.Now(),
Sampled: SampledUndefined,

ctx: context.WithValue(ctx, spanContextKey{}, &span),
parent: parent,
isTransaction: !hasParent,
ctx: context.WithValue(ctx, spanContextKey{}, &span),
parent: parent,
}
if hasParent {
span.TraceID = parent.TraceID
Expand Down Expand Up @@ -153,8 +152,16 @@ func StartSpan(ctx context.Context, operation string, options ...SpanOption) *Sp
if err != nil {
panic(err)
}

if hasParent {
span.ParentSpanID = parent.SpanID
if parent.IsTransaction() {
span.transaction = parent
} else {
span.transaction = parent.transaction
}
} else {
span.transaction = nil
}

// Apply options to override defaults.
Expand Down Expand Up @@ -238,7 +245,7 @@ func (s *Span) SetData(name, value string) {

// IsTransaction checks if the given span is a transaction.
func (s *Span) IsTransaction() bool {
return s.isTransaction
return s.parent == nil
}

// TODO(tracing): maybe add shortcuts to get/set transaction name. Right now the
Expand Down Expand Up @@ -274,7 +281,7 @@ func (s *Span) ToBaggage() string {
// SetDynamicSamplingContext sets the given dynamic sampling context on the
// current transaction.
func (s *Span) SetDynamicSamplingContext(dsc DynamicSamplingContext) {
if s.isTransaction {
if s.IsTransaction() {
s.dynamicSamplingContext = dsc
}
}
Expand Down Expand Up @@ -314,7 +321,7 @@ func (s *Span) updateFromSentryTrace(header []byte) (updated bool) {
}

func (s *Span) updateFromBaggage(header []byte) {
if s.isTransaction {
if s.IsTransaction() {
dsc, err := DynamicSamplingContextFromHeader(header)
if err != nil {
return
Expand Down Expand Up @@ -373,7 +380,7 @@ func (s *Span) sample() Sampled {
// Note: non-transaction should always have a parent, but we check both
// conditions anyway -- the first for semantic meaning, the second to
// avoid a nil pointer dereference.
if !s.isTransaction && s.parent != nil {
if !s.IsTransaction() && s.parent != nil {
return s.parent.Sampled
}

Expand Down Expand Up @@ -430,7 +437,7 @@ func (s *Span) sample() Sampled {
}

func (s *Span) toEvent() *Event {
if !s.isTransaction {
if !s.IsTransaction() {
return nil // only transactions can be transformed into events
}
hub := hubFromContext(s.ctx)
Expand Down Expand Up @@ -485,6 +492,15 @@ func (s *Span) traceContext() *TraceContext {
// spanRecorder stores the span tree. Guaranteed to be non-nil.
func (s *Span) spanRecorder() *spanRecorder { return s.recorder }

// GetTransaction returns the root span (transaction span) for
// the given span.
func (s *Span) GetTransaction() *Span {
if s.transaction == nil {
return s
}
return s.transaction
}

// ParseTraceParentContext parses a sentry-trace header and builds a TraceParentContext from the
// parsed values. If the header was parsed correctly, the second returned argument
// ("valid") will be set to true, otherwise (e.g., empty or malformed header) it will
Expand Down
108 changes: 82 additions & 26 deletions tracing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ func TestStartSpan(t *testing.T) {
RecorderLen: 1,
}.Check(t, span)

if span.transaction != nil {
t.Fatal("'transaction' field on a transaction should be empty")
}

events := transport.Events()
if got := len(events); got != 1 {
t.Fatalf("sent %d events, want 1", got)
Expand Down Expand Up @@ -189,6 +193,13 @@ func TestStartChild(t *testing.T) {
c.Check(t, span)
c.Check(t, child)

if span.transaction != nil {
t.Fatalf("'transaction' field on a transaction should be empty")
}
if child.transaction != span {
t.Fatalf("'transaction' field on a child span should point to the transaction")
}

events := transport.Events()
if got := len(events); got != 1 {
t.Fatalf("sent %d events, want 1", got)
Expand Down Expand Up @@ -269,6 +280,10 @@ func TestStartTransaction(t *testing.T) {
RecorderLen: 1,
}.Check(t, transaction)

if transaction.transaction != nil {
t.Fatalf("'transaction' field on a transaction should be empty")
}

events := transport.Events()
if got := len(events); got != 1 {
t.Fatalf("sent %d events, want 1", got)
Expand Down Expand Up @@ -313,6 +328,48 @@ func TestStartTransaction(t *testing.T) {
}
}

func TestChildShouldInheritParentTransaction(t *testing.T) {
ctx := NewTestContext(ClientOptions{
EnableTracing: true,
Transport: &TransportMock{},
})

transaction := StartTransaction(ctx, "transaction")
child := transaction.StartChild("child")
grandchild := child.StartChild("grandchild")

if transaction.transaction != nil {
t.Fatalf("'transaction' field on a transaction should be empty")
}
if child.transaction != transaction {
t.Fatalf("'transaction' field on a child span should point to the root transaction")
}
if grandchild.transaction != transaction {
t.Fatalf("'transaction' field on a grandchild span should point to the root transaction")
}
}

func TestGetTransaction(t *testing.T) {
ctx := NewTestContext(ClientOptions{
EnableTracing: true,
Transport: &TransportMock{},
})

transaction := StartTransaction(ctx, "transaction")
child := transaction.StartChild("child")
grandchild := child.StartChild("grandchild")

if transaction.GetTransaction() != transaction {
t.Fatalf("GetTransaction() should return self for a transaction span")
}
if child.GetTransaction() != transaction {
t.Fatalf("GetTransaction() should return the root level span for immediate children")
}
if grandchild.GetTransaction() != transaction {
t.Fatalf("GetTransaction() should return the root level span for nested spans")
}
}

func TestSetTag(t *testing.T) {
ctx := NewTestContext(ClientOptions{
EnableTracing: true,
Expand Down Expand Up @@ -481,8 +538,7 @@ func TestContinueTransactionFromHeaders(t *testing.T) {
traceStr: "",
baggageStr: "",
wantSpan: Span{
isTransaction: true,
Sampled: 0,
Sampled: 0,
dynamicSamplingContext: DynamicSamplingContext{
Frozen: false,
Entries: nil,
Expand All @@ -494,8 +550,7 @@ func TestContinueTransactionFromHeaders(t *testing.T) {
traceStr: "",
baggageStr: "other-vendor-key1=value1;value2, other-vendor-key2=value3",
wantSpan: Span{
isTransaction: true,
Sampled: 0,
Sampled: 0,
dynamicSamplingContext: DynamicSamplingContext{
Frozen: false,
Entries: map[string]string{},
Expand All @@ -508,10 +563,9 @@ func TestContinueTransactionFromHeaders(t *testing.T) {
traceStr: "bc6d53f15eb88f4320054569b8c553d4-b72fa28504b07285-1",
baggageStr: "",
wantSpan: Span{
isTransaction: true,
TraceID: TraceIDFromHex("bc6d53f15eb88f4320054569b8c553d4"),
ParentSpanID: SpanIDFromHex("b72fa28504b07285"),
Sampled: 1,
TraceID: TraceIDFromHex("bc6d53f15eb88f4320054569b8c553d4"),
ParentSpanID: SpanIDFromHex("b72fa28504b07285"),
Sampled: 1,
dynamicSamplingContext: DynamicSamplingContext{
Frozen: true,
},
Expand All @@ -522,10 +576,9 @@ func TestContinueTransactionFromHeaders(t *testing.T) {
traceStr: "bc6d53f15eb88f4320054569b8c553d4-b72fa28504b07285-1",
baggageStr: "sentry-trace_id=d49d9bf66f13450b81f65bc51cf49c03,sentry-public_key=public,sentry-sample_rate=1",
wantSpan: Span{
isTransaction: true,
TraceID: TraceIDFromHex("bc6d53f15eb88f4320054569b8c553d4"),
ParentSpanID: SpanIDFromHex("b72fa28504b07285"),
Sampled: 1,
TraceID: TraceIDFromHex("bc6d53f15eb88f4320054569b8c553d4"),
ParentSpanID: SpanIDFromHex("b72fa28504b07285"),
Sampled: 1,
dynamicSamplingContext: DynamicSamplingContext{
Frozen: true,
Entries: map[string]string{
Expand All @@ -539,7 +592,7 @@ func TestContinueTransactionFromHeaders(t *testing.T) {
}

for _, tt := range tests {
s := Span{isTransaction: true}
s := Span{}
spanOption := ContinueFromHeaders(tt.traceStr, tt.baggageStr)
spanOption(&s)

Expand Down Expand Up @@ -694,36 +747,39 @@ func TestDoesNotCrashWithEmptyContext(t *testing.T) {
}

func TestSetDynamicSamplingContextWorksOnTransaction(t *testing.T) {
s := Span{
isTransaction: true,
dynamicSamplingContext: DynamicSamplingContext{Frozen: false},
}
ctx := NewTestContext(ClientOptions{
EnableTracing: true,
TracesSampleRate: 1.0,
})
transaction := StartTransaction(ctx, "transaction")
newDsc := DynamicSamplingContext{
Entries: map[string]string{"environment": "dev"},
Frozen: true,
}

s.SetDynamicSamplingContext(newDsc)
transaction.SetDynamicSamplingContext(newDsc)

if diff := cmp.Diff(newDsc, s.dynamicSamplingContext); diff != "" {
if diff := cmp.Diff(newDsc, transaction.dynamicSamplingContext); diff != "" {
t.Errorf("DynamicSamplingContext mismatch (-want +got):\n%s", diff)
}
}

func TestSetDynamicSamplingContextDoesNothingOnSpan(t *testing.T) {
func TestSetDynamicSamplingContextDoesNothingOnChildSpan(t *testing.T) {
// SetDynamicSamplingContext should do nothing on non-transaction spans
s := Span{
isTransaction: false,
dynamicSamplingContext: DynamicSamplingContext{},
}
ctx := NewTestContext(ClientOptions{
EnableTracing: true,
TracesSampleRate: 1.0,
})
transaction := StartTransaction(ctx, "transaction")
span := transaction.StartChild("span")
newDsc := DynamicSamplingContext{
Entries: map[string]string{"environment": "dev"},
Frozen: true,
}

s.SetDynamicSamplingContext(newDsc)
span.SetDynamicSamplingContext(newDsc)

if diff := cmp.Diff(DynamicSamplingContext{}, s.dynamicSamplingContext); diff != "" {
if diff := cmp.Diff(DynamicSamplingContext{}, span.dynamicSamplingContext); diff != "" {
t.Errorf("DynamicSamplingContext mismatch (-want +got):\n%s", diff)
}
}
Expand Down