Skip to content

Commit

Permalink
Handle nil sampling config (#1292)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

---------

Co-authored-by: Tyler Yahn <[email protected]>
  • Loading branch information
RonFed and MrAlias authored Nov 18, 2024
1 parent da30fcc commit e316695
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions internal/pkg/instrumentation/probe/sampling/sampling.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
24 changes: 24 additions & 0 deletions internal/pkg/instrumentation/probe/sampling/sampling_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
}

0 comments on commit e316695

Please sign in to comment.