From e316695e0faf9f73922b10bacf01da4781b7eb63 Mon Sep 17 00:00:00 2001 From: Ron Federman <73110295+RonFed@users.noreply.github.com> Date: Mon, 18 Nov 2024 12:08:26 +0200 Subject: [PATCH] Handle nil sampling config (#1292) * Apply default sampling config when none is provided * Provide default sampling config in NewSamplingManager if non is provided * changelog entry * Update CHANGELOG.md Co-authored-by: Tyler Yahn --------- Co-authored-by: Tyler Yahn --- CHANGELOG.md | 1 + .../probe/sampling/sampling.go | 8 +++++++ .../probe/sampling/sampling_linux_test.go | 24 +++++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 99b80dbd8..334a94585 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ OpenTelemetry Go Automatic Instrumentation adheres to [Semantic Versioning](http ### Fixed - Don't include `db.query.text` attribute in `database/sql` if the query string is empty or not collected. ([#1246](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/1246)) +- Handle a `ConfigProvider` which doesn't provide a sampling config in the initial configuration by applying the default sampler. ([#1292](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/1292)) ### Removed diff --git a/internal/pkg/instrumentation/probe/sampling/sampling.go b/internal/pkg/instrumentation/probe/sampling/sampling.go index 5299e7d7e..6bd09f537 100644 --- a/internal/pkg/instrumentation/probe/sampling/sampling.go +++ b/internal/pkg/instrumentation/probe/sampling/sampling.go @@ -206,6 +206,10 @@ func NewSamplingManager(c *ebpf.Collection, conf *Config) (*Manager, error) { ActiveSamplerMap: probeActiveSampler, } + if conf == nil { + conf = DefaultConfig() + } + err := m.applyConfig(conf) if err != nil { return nil, err @@ -215,6 +219,10 @@ func NewSamplingManager(c *ebpf.Collection, conf *Config) (*Manager, error) { } func (m *Manager) applyConfig(conf *Config) error { + if conf == nil { + return fmt.Errorf("cannot apply nil config") + } + samplerIDs := make([]SamplerID, 0, len(conf.Samplers)) configs := make([]SamplerConfig, 0, len(conf.Samplers)) for id, samplerConfig := range conf.Samplers { diff --git a/internal/pkg/instrumentation/probe/sampling/sampling_linux_test.go b/internal/pkg/instrumentation/probe/sampling/sampling_linux_test.go index 15bb9c7b6..f36d72ed8 100644 --- a/internal/pkg/instrumentation/probe/sampling/sampling_linux_test.go +++ b/internal/pkg/instrumentation/probe/sampling/sampling_linux_test.go @@ -127,4 +127,28 @@ func TestEBPFNewSamplingConfigDefault(t *testing.T) { assert.Equal(t, SamplerTraceIDRatio, samplersConfigInMap.SamplerType) assert.Equal(t, TraceIDRatioConfig{42}, samplersConfigInMap.Config) }) + + t.Run("nil config", func(t *testing.T) { + m, err := NewSamplingManager(c, nil) + if !assert.NoError(t, err) { + return + } + + assert.Equal(t, m.currentSamplerID, ParentBasedID) + + var activeSamplerIDInMap SamplerID + err = m.ActiveSamplerMap.Lookup(uint32(0), &activeSamplerIDInMap) + if !assert.NoError(t, err) { + return + } + assert.Equal(t, activeSamplerIDInMap, ParentBasedID) + + var samplersConfigInMap SamplerConfig + err = m.samplersConfigMap.Lookup(uint32(ParentBasedID), &samplersConfigInMap) + if !assert.NoError(t, err) { + return + } + assert.Equal(t, samplersConfigInMap.SamplerType, SamplerParentBased) + assert.Equal(t, samplersConfigInMap.Config, DefaultParentBasedSampler()) + }) }