Skip to content

Commit

Permalink
probabilisticsampler: add filterspan config
Browse files Browse the repository at this point in the history
  • Loading branch information
tanner-bruce authored and sveiss committed Aug 2, 2022
1 parent cb9227f commit 8f1d9cc
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions processor/probabilisticsamplerprocessor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ package probabilisticsamplerprocessor // import "github.com/open-telemetry/opent

import (
"go.opentelemetry.io/collector/config"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/processor/filterconfig"
)

// Config has the configuration guiding the trace sampler processor.
type Config struct {
config.ProcessorSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct

filterconfig.MatchConfig `mapstructure:",squash"`

// SamplingPercentage is the percentage rate at which traces are going to be sampled. Defaults to zero, i.e.: no sample.
// Values greater or equal 100 are treated as "sample all traces".
SamplingPercentage float32 `mapstructure:"sampling_percentage"`
Expand Down
2 changes: 2 additions & 0 deletions processor/probabilisticsamplerprocessor/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/knadh/koanf v1.4.2 // indirect
Expand All @@ -23,6 +24,7 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/cast v1.5.0 // indirect
go.opencensus.io v0.23.0 // indirect
go.opentelemetry.io/otel v1.8.0 // indirect
go.opentelemetry.io/otel/metric v0.31.0 // indirect
Expand Down
3 changes: 3 additions & 0 deletions processor/probabilisticsamplerprocessor/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions processor/probabilisticsamplerprocessor/probabilisticsampler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"
"go.opentelemetry.io/collector/processor/processorhelper"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/processor/filterspan"
)

// samplingPriority has the semantic result of parsing the "sampling.priority"
Expand Down Expand Up @@ -52,15 +54,28 @@ const (
type tracesamplerprocessor struct {
scaledSamplingRate uint32
hashSeed uint32
include filterspan.Matcher
exclude filterspan.Matcher
}

// newTracesProcessor returns a processor.TracesProcessor that will perform head sampling according to the given
// configuration.
func newTracesProcessor(nextConsumer consumer.Traces, cfg *Config) (component.TracesProcessor, error) {
include, err := filterspan.NewMatcher(cfg.Include)
if err != nil {
return nil, err
}
exclude, err := filterspan.NewMatcher(cfg.Exclude)
if err != nil {
return nil, err
}

tsp := &tracesamplerprocessor{
// Adjust sampling percentage on private so recalculations are avoided.
scaledSamplingRate: uint32(cfg.SamplingPercentage * percentageScaleFactor),
hashSeed: cfg.HashSeed,
include: include,
exclude: exclude,
}

return processorhelper.NewTracesProcessor(
Expand All @@ -74,6 +89,9 @@ func (tsp *tracesamplerprocessor) processTraces(_ context.Context, td ptrace.Tra
td.ResourceSpans().RemoveIf(func(rs ptrace.ResourceSpans) bool {
rs.ScopeSpans().RemoveIf(func(ils ptrace.ScopeSpans) bool {
ils.Spans().RemoveIf(func(s ptrace.Span) bool {
if filterspan.SkipSpan(tsp.include, tsp.exclude, s, rs.Resource(), ils.Scope()) {
return false
}
sp := parseSpanSamplingPriority(s)
if sp == doNotSampleSpan {
// The OpenTelemetry mentions this as a "hint" we take a stronger
Expand Down

0 comments on commit 8f1d9cc

Please sign in to comment.