Skip to content

Commit

Permalink
feat: add WithSampler option (open-telemetry#454)
Browse files Browse the repository at this point in the history
Add a new `WithSampler` method allowing end-users to provide their own implementation of OpenTelemetry sampler directly through the package API.

Signed-off-by: thomasgouveia <gouveia.thomas@outlook.fr>
  • Loading branch information
thomasgouveia committed Nov 6, 2023
1 parent 68220b0 commit 803f528
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ OpenTelemetry Go Automatic Instrumentation adheres to [Semantic Versioning](http
- Add the `WithTraceExporter` `InstrumentationOption` to configure the trace `SpanExporter` used by an `Instrumentation`. ([#426](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/426))
- Add HTTP status code attribute to `net/http` server instrumentation. ([#428](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/428))
- The instrumentation scope now includes the version of the auto-instrumentation project. ([#442](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/442))
- Add a new `WithSampler` method allowing end-users to provide their own implementation of OpenTelemetry sampler directly through the package API. ([#468](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/468)).

### Changed

Expand Down
16 changes: 15 additions & 1 deletion instrumentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ type InstrumentationOption interface {
}

type instConfig struct {
sampler trace.Sampler
traceExp trace.SpanExporter
target process.TargetArgs
serviceName string
Expand Down Expand Up @@ -192,6 +193,10 @@ func newInstConfig(ctx context.Context, opts []InstrumentationOption) (instConfi
err = errors.Join(err, e)
}

if c.sampler == nil {
c.sampler = trace.AlwaysSample()
}

return c, err
}

Expand All @@ -216,7 +221,7 @@ func (c instConfig) validate() error {

func (c instConfig) tracerProvider() *trace.TracerProvider {
return trace.NewTracerProvider(
trace.WithSampler(trace.AlwaysSample()),
trace.WithSampler(c.sampler),
trace.WithResource(c.res()),
trace.WithBatcher(c.traceExp),
trace.WithIDGenerator(opentelemetry.NewEBPFSourceIDGenerator()),
Expand Down Expand Up @@ -382,3 +387,12 @@ func WithTraceExporter(exp trace.SpanExporter) InstrumentationOption {
return c, nil
})
}

// WithSampler returns an [InstrumentationOption] that will configure
// an [Instrumentation] to use the provided sampler to sample OpenTelemetry traces.
func WithSampler(sampler trace.Sampler) InstrumentationOption {
return fnOpt(func(_ context.Context, c instConfig) (instConfig, error) {
c.sampler = sampler
return c, nil
})
}

0 comments on commit 803f528

Please sign in to comment.