This repository has been archived by the owner on May 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 289
All spans of a trace share sampling state #443
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
86c8398
[WIP] Share span state
vprithvi 3673079
Address Feedback
vprithvi d889df3
Address Feedback
vprithvi a1c7bb7
Remove unused
vprithvi 8010c58
Cleanup
vprithvi f6cbdd4
Remove unused var
vprithvi 4183593
Address feedback
vprithvi df4bee4
Address feedback
vprithvi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,14 +17,16 @@ package jaeger | |
import ( | ||
"errors" | ||
"fmt" | ||
"go.uber.org/atomic" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
const ( | ||
flagSampled = byte(1) | ||
flagDebug = byte(2) | ||
flagFirehose = byte(8) | ||
flagUnsampled = 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
flagSampled = 1 | ||
flagDebug = 2 | ||
flagFirehose = 8 | ||
) | ||
|
||
var ( | ||
|
@@ -56,9 +58,6 @@ type SpanContext struct { | |
// Should be 0 if the current span is a root span. | ||
parentID SpanID | ||
|
||
// flags is a bitmap containing such bits as 'sampled' and 'debug'. | ||
flags byte | ||
|
||
// Distributed Context baggage. The is a snapshot in time. | ||
baggage map[string]string | ||
|
||
|
@@ -67,6 +66,92 @@ type SpanContext struct { | |
// | ||
// See JaegerDebugHeader in constants.go | ||
debugID string | ||
|
||
// samplingState is shared across all spans | ||
samplingState *samplingState | ||
} | ||
|
||
type samplingState struct { | ||
flags atomic.Int32 // Only lower 8 bits are used. We use an int32 instead of a byte to use CAS operations | ||
} | ||
|
||
func (s *samplingState) setSampled() { | ||
// TODO: add tests for all these transitions | ||
swapped := s.flags.CAS(flagUnsampled, flagSampled) | ||
vprithvi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if swapped { | ||
return | ||
} | ||
swapped = s.flags.CAS(flagDebug, flagDebug|flagSampled) | ||
if swapped { | ||
return | ||
} | ||
swapped = s.flags.CAS(flagFirehose, flagFirehose|flagSampled) | ||
if swapped { | ||
return | ||
} | ||
s.flags.CAS(flagFirehose|flagDebug, flagDebug|flagFirehose|flagSampled) | ||
} | ||
|
||
func (s *samplingState) setDebug() { | ||
swapped := s.flags.CAS(flagUnsampled, flagSampled|flagDebug) | ||
if swapped { | ||
return | ||
} | ||
|
||
swapped = s.flags.CAS(flagSampled, flagSampled|flagDebug) | ||
if swapped { | ||
return | ||
} | ||
|
||
swapped = s.flags.CAS(flagFirehose, flagFirehose|flagDebug) | ||
if swapped { | ||
return | ||
} | ||
|
||
s.flags.CAS(flagFirehose|flagSampled, flagFirehose|flagSampled|flagDebug) | ||
} | ||
|
||
func (s *samplingState) setFirehose() { | ||
swapped := s.flags.CAS(flagUnsampled, flagUnsampled|flagFirehose) | ||
if swapped { | ||
return | ||
} | ||
|
||
swapped = s.flags.CAS(flagSampled, flagSampled|flagFirehose) | ||
if swapped { | ||
return | ||
} | ||
|
||
swapped = s.flags.CAS(flagDebug, flagDebug|flagFirehose) | ||
if swapped { | ||
return | ||
} | ||
|
||
s.flags.CAS(flagDebug|flagSampled, flagDebug|flagUnsampled|flagFirehose) | ||
} | ||
|
||
func (s *samplingState) resetFlags() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. func |
||
s.flags.Store(flagUnsampled) | ||
} | ||
|
||
func (s *samplingState) setFromFlags(flags byte) { | ||
vprithvi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
s.flags.Store(int32(flags)) | ||
} | ||
|
||
func (s *samplingState) getFlags() byte { | ||
vprithvi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return byte(s.flags.Load()) | ||
} | ||
|
||
func (s *samplingState) isSampled() bool { | ||
return s.flags.Load()&flagSampled == flagSampled | ||
} | ||
|
||
func (s *samplingState) isDebug() bool { | ||
return s.flags.Load()&flagDebug == flagDebug | ||
} | ||
|
||
func (s *samplingState) isFirehose() bool { | ||
return s.flags.Load()&flagFirehose == flagFirehose | ||
} | ||
|
||
// ForeachBaggageItem implements ForeachBaggageItem() of opentracing.SpanContext | ||
|
@@ -81,17 +166,17 @@ func (c SpanContext) ForeachBaggageItem(handler func(k, v string) bool) { | |
// IsSampled returns whether this trace was chosen for permanent storage | ||
// by the sampling mechanism of the tracer. | ||
func (c SpanContext) IsSampled() bool { | ||
return (c.flags & flagSampled) == flagSampled | ||
return c.samplingState.isSampled() | ||
} | ||
|
||
// IsDebug indicates whether sampling was explicitly requested by the service. | ||
func (c SpanContext) IsDebug() bool { | ||
return (c.flags & flagDebug) == flagDebug | ||
return c.samplingState.isDebug() | ||
} | ||
|
||
// IsFirehose indicates whether the firehose flag was set | ||
func (c SpanContext) IsFirehose() bool { | ||
return (c.flags & flagFirehose) == flagFirehose | ||
return c.samplingState.isFirehose() | ||
} | ||
|
||
// IsValid indicates whether this context actually represents a valid trace. | ||
|
@@ -101,9 +186,9 @@ func (c SpanContext) IsValid() bool { | |
|
||
func (c SpanContext) String() string { | ||
if c.traceID.High == 0 { | ||
return fmt.Sprintf("%x:%x:%x:%x", c.traceID.Low, uint64(c.spanID), uint64(c.parentID), c.flags) | ||
return fmt.Sprintf("%x:%x:%x:%x", c.traceID.Low, uint64(c.spanID), uint64(c.parentID), c.samplingState.flags.Load()) | ||
} | ||
return fmt.Sprintf("%x%016x:%x:%x:%x", c.traceID.High, c.traceID.Low, uint64(c.spanID), uint64(c.parentID), c.flags) | ||
return fmt.Sprintf("%x%016x:%x:%x:%x", c.traceID.High, c.traceID.Low, uint64(c.spanID), uint64(c.parentID), c.samplingState.flags.Load()) | ||
} | ||
|
||
// ContextFromString reconstructs the Context encoded in a string | ||
|
@@ -130,7 +215,8 @@ func ContextFromString(value string) (SpanContext, error) { | |
if err != nil { | ||
return emptyContext, err | ||
} | ||
context.flags = byte(flags) | ||
context.samplingState = &samplingState{} | ||
context.samplingState.setFromFlags(byte(flags)) | ||
return context, nil | ||
} | ||
|
||
|
@@ -151,16 +237,17 @@ func (c SpanContext) ParentID() SpanID { | |
|
||
// NewSpanContext creates a new instance of SpanContext | ||
func NewSpanContext(traceID TraceID, spanID, parentID SpanID, sampled bool, baggage map[string]string) SpanContext { | ||
flags := byte(0) | ||
samplingState := &samplingState{} | ||
if sampled { | ||
flags = flagSampled | ||
samplingState.setSampled() | ||
} | ||
|
||
return SpanContext{ | ||
traceID: traceID, | ||
spanID: spanID, | ||
parentID: parentID, | ||
flags: flags, | ||
baggage: baggage} | ||
traceID: traceID, | ||
spanID: spanID, | ||
parentID: parentID, | ||
samplingState: samplingState, | ||
baggage: baggage} | ||
} | ||
|
||
// CopyFrom copies data from ctx into this context, including span identity and baggage. | ||
|
@@ -169,7 +256,7 @@ func (c *SpanContext) CopyFrom(ctx *SpanContext) { | |
c.traceID = ctx.traceID | ||
c.spanID = ctx.spanID | ||
c.parentID = ctx.parentID | ||
c.flags = ctx.flags | ||
c.samplingState = ctx.samplingState | ||
if l := len(ctx.baggage); l > 0 { | ||
c.baggage = make(map[string]string, l) | ||
for k, v := range ctx.baggage { | ||
|
@@ -193,7 +280,7 @@ func (c SpanContext) WithBaggageItem(key, value string) SpanContext { | |
newBaggage[key] = value | ||
} | ||
// Use positional parameters so the compiler will help catch new fields. | ||
return SpanContext{c.traceID, c.spanID, c.parentID, c.flags, newBaggage, ""} | ||
return SpanContext{c.traceID, c.spanID, c.parentID, newBaggage, "", c.samplingState} | ||
} | ||
|
||
// isDebugIDContainerOnly returns true when the instance of the context is only | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
flagUnsampled
is unused (fromvarcheck
)