-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to OTel ParentSampler for correct sampling behavior (dapr#7573)
* Switch to OTel ParentSampler for correct sampling behavior Signed-off-by: Andrej Kyselica <[email protected]> * fix formatting Signed-off-by: Andrej Kyselica <[email protected]> * fix lint error Signed-off-by: Andrej Kyselica <[email protected]> --------- Signed-off-by: Andrej Kyselica <[email protected]> Signed-off-by: Andrej Kyselica <[email protected]> Co-authored-by: Yaron Schneider <[email protected]> Co-authored-by: Loong Dai <[email protected]> Co-authored-by: Dapr Bot <[email protected]>
- Loading branch information
1 parent
6ffe177
commit e08e673
Showing
2 changed files
with
40 additions
and
50 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,44 +1,12 @@ | ||
package diagnostics | ||
|
||
import ( | ||
"fmt" | ||
|
||
sdktrace "go.opentelemetry.io/otel/sdk/trace" | ||
"go.opentelemetry.io/otel/trace" | ||
|
||
diagUtils "github.com/dapr/dapr/pkg/diagnostics/utils" | ||
) | ||
|
||
type DaprTraceSampler struct { | ||
sdktrace.Sampler | ||
ProbabilitySampler sdktrace.Sampler | ||
SamplingRate float64 | ||
} | ||
|
||
/** | ||
* Decisions for the Dapr sampler are as follows: | ||
* - parent has sample flag turned on -> sample | ||
* - parent has sample turned off -> fall back to probability-based sampling | ||
*/ | ||
func (d *DaprTraceSampler) ShouldSample(p sdktrace.SamplingParameters) sdktrace.SamplingResult { | ||
psc := trace.SpanContextFromContext(p.ParentContext) | ||
if psc.IsValid() && psc.IsSampled() { | ||
// Parent is valid and specifies sampling flag is on -> sample | ||
return sdktrace.AlwaysSample().ShouldSample(p) | ||
} | ||
|
||
// Parent is invalid or does not have sampling enabled -> sample probabilistically | ||
return d.ProbabilitySampler.ShouldSample(p) | ||
} | ||
|
||
func (d *DaprTraceSampler) Description() string { | ||
return fmt.Sprintf("DaprTraceSampler(P=%f)", d.SamplingRate) | ||
} | ||
|
||
func NewDaprTraceSampler(samplingRateString string) *DaprTraceSampler { | ||
func NewDaprTraceSampler(samplingRateString string) sdktrace.Sampler { | ||
samplingRate := diagUtils.GetTraceSamplingRate(samplingRateString) | ||
return &DaprTraceSampler{ | ||
SamplingRate: samplingRate, | ||
ProbabilitySampler: sdktrace.TraceIDRatioBased(samplingRate), | ||
} | ||
return sdktrace.ParentBased(sdktrace.TraceIDRatioBased(samplingRate)) | ||
} |
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