Skip to content

Commit

Permalink
Honor sampler environment variable if set (#724)
Browse files Browse the repository at this point in the history
* Honor sampler envar if set

* Add entry to changelog
  • Loading branch information
MrAlias authored Mar 30, 2022
1 parent 8ff26ac commit af4eabc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Update `go.opentelemetry.io/contrib*` dependencies from
[`v1.4.0`/`v0.30.0`][contrib-v1.4.0] to [`v1.6.0`/`v0.31.0`][contrib-v1.6.0].
(#720)
- The `OTEL_TRACES_SAMPLER` environment variable is now honored instead of only
defaulting to an always-on sampler. (#724)

### Fixed

Expand Down
12 changes: 9 additions & 3 deletions distro/otel.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import (

var distroVerAttr = attribute.String("splunk.distro.version", splunkotel.Version())

const tracesSamplerKey = "OTEL_TRACES_SAMPLER"

const noServiceWarn = `service.name attribute is not set. Your service is unnamed and might be difficult to identify. Set your service name using the OTEL_SERVICE_NAME environment variable. For example, OTEL_SERVICE_NAME="<YOUR_SERVICE_NAME_HERE>")`

// SDK contains all OpenTelemetry SDK state and provides access to this state.
Expand Down Expand Up @@ -100,11 +102,15 @@ func Run(opts ...Option) (SDK, error) {
c.Logger.Info(noServiceWarn)
}

traceProvider := trace.NewTracerProvider(
o := []trace.TracerProviderOption{
trace.WithResource(res),
trace.WithSampler(trace.AlwaysSample()),
trace.WithSpanProcessor(trace.NewBatchSpanProcessor(exp)),
)
}
if _, ok := os.LookupEnv(tracesSamplerKey); !ok {
o = append(o, trace.WithSampler(trace.AlwaysSample()))
}

traceProvider := trace.NewTracerProvider(o...)
otel.SetTracerProvider(traceProvider)

return SDK{
Expand Down

0 comments on commit af4eabc

Please sign in to comment.