Skip to content

Commit fdbcf71

Browse files
vibhavpjmacdken8203bvwellsjeremy-stytch
authored
Allow setting the Sampler via environment variables (#2517)
* Allow setting the Sampler via environment variables (#2305) * Add changelog entry. * Replace t.Setenv with internaltest/SetEnvVariables for Go <= 1.6. * Handle the lack of a sampler argument without logging errors. * Add additional test cases and error checks. * Refactor documentation. Co-authored-by: Joshua MacDonald <[email protected]> * emitBatchOverhead should only be used for splitting spans into batches (#2512) * emitBatchOverhead should only be used for splitting spans into batches (#2503) * limit max packet size parameter * Add additional errors types, simplify abstractions and error handling * Make error comparisons less fragile. * Fix typo in jaeger example (#2524) * Fix some typos in docs for Go libraries (#2520) Co-authored-by: Tyler Yahn <[email protected]> * Fix getting-started.md Run function (#2527) * Fix getting-started.md Run function, it assigns this new context to a variable shared between connections in to accept loop. Thus creating a growing chain of contexts. so every calculate fibonacci request, all spans in a trace. * add a comment explaining the reason for that new variable * update example fib * Bump github.com/google/go-cmp from 0.5.6 to 0.5.7 across the project (#2545) * update go-cmp to 0.5.7 * fixes go.sums Co-authored-by: Aaron Clawson <[email protected]> * Un-escape url coding when parsing baggage. (#2529) * un-escape url coding when parsing baggage. * Added changelog Co-authored-by: Aaron Clawson <[email protected]> Co-authored-by: Tyler Yahn <[email protected]> * Bump go.opentelemetry.io/proto/otlp from 0.11.0 to 0.12.0 (#2546) * Update go.opentelemetry.io/proto/otlp to v0.12.0 * Changelog * Update CHANGELOG.md Fix's md linting Co-authored-by: Tyler Yahn <[email protected]> Co-authored-by: Aaron Clawson <[email protected]> Co-authored-by: Tyler Yahn <[email protected]> * Remove unused sdk/internal/santize (#2549) * Add links to code examples and docs (#2551) * Bump github.com/prometheus/client_golang from 1.11.0 to 1.12.0 in /exporters/prometheus (#2541) * Bump github.com/prometheus/client_golang in /exporters/prometheus Bumps [github.com/prometheus/client_golang](https://github.com/prometheus/client_golang) from 1.11.0 to 1.12.0. - [Release notes](https://github.com/prometheus/client_golang/releases) - [Changelog](https://github.com/prometheus/client_golang/blob/main/CHANGELOG.md) - [Commits](prometheus/client_golang@v1.11.0...v1.12.0) --- updated-dependencies: - dependency-name: github.com/prometheus/client_golang dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <[email protected]> * go mod tidy Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Tyler Yahn <[email protected]> Co-authored-by: Tyler Yahn <[email protected]> * Optimize evictedQueue implementation and use (#2556) * Optimize evictedQueue impl and use Avoid unnecessary allocations in the recordingSpan by using an evictedQueue type instead of a pointer to one. Lazy allocate the evictedQueue queue to prevent unnecessary operations for spans without any use of the queue. Document the evictedQueue * Fix grammar * Add env support for batch span processor (#2515) * Add env support for batch span processor * Update changelog * lint * Bump golang.org/x/tools from 0.1.8 to 0.1.9 in /internal/tools (#2566) * Bump golang.org/x/tools from 0.1.8 to 0.1.9 in /internal/tools Bumps [golang.org/x/tools](https://github.com/golang/tools) from 0.1.8 to 0.1.9. - [Release notes](https://github.com/golang/tools/releases) - [Commits](golang/tools@v0.1.8...v0.1.9) --- updated-dependencies: - dependency-name: golang.org/x/tools dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <[email protected]> * Auto-fix go.sum changes in dependent modules Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: MrAlias <[email protected]> * Bump github.com/golangci/golangci-lint from 1.43.0 to 1.44.0 in /internal/tools (#2567) * Bump github.com/golangci/golangci-lint in /internal/tools Bumps [github.com/golangci/golangci-lint](https://github.com/golangci/golangci-lint) from 1.43.0 to 1.44.0. - [Release notes](https://github.com/golangci/golangci-lint/releases) - [Changelog](https://github.com/golangci/golangci-lint/blob/master/CHANGELOG.md) - [Commits](golangci/golangci-lint@v1.43.0...v1.44.0) --- updated-dependencies: - dependency-name: github.com/golangci/golangci-lint dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <[email protected]> * Auto-fix go.sum changes in dependent modules Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: MrAlias <[email protected]> * Bump github.com/prometheus/client_golang from 1.12.0 to 1.12.1 in /exporters/prometheus (#2570) * Bump github.com/prometheus/client_golang in /exporters/prometheus Bumps [github.com/prometheus/client_golang](https://github.com/prometheus/client_golang) from 1.12.0 to 1.12.1. - [Release notes](https://github.com/prometheus/client_golang/releases) - [Changelog](https://github.com/prometheus/client_golang/blob/main/CHANGELOG.md) - [Commits](prometheus/client_golang@v1.12.0...v1.12.1) --- updated-dependencies: - dependency-name: github.com/prometheus/client_golang dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <[email protected]> * Auto-fix go.sum changes in dependent modules Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: MrAlias <[email protected]> * Fix TestBackoffRetry in otlp/internal/retry package (#2562) * Fix TestBackoffRetry in otlp retry pkg The delay of the retry is within two times a randomization factor (the back-off time is delay * random number within [1 - factor, 1 + factor]. This means the waitFunc in TestBackoffRetry needs to check the delay is within an appropriate delta, not equal to configure initial delay. * Fix delta value * Fix delta Co-authored-by: Aaron Clawson <[email protected]> * Bump google.golang.org/grpc from 1.43.0 to 1.44.0 in /exporters/otlp/otlptrace (#2568) * Bump google.golang.org/grpc in /exporters/otlp/otlptrace Bumps [google.golang.org/grpc](https://github.com/grpc/grpc-go) from 1.43.0 to 1.44.0. - [Release notes](https://github.com/grpc/grpc-go/releases) - [Commits](grpc/grpc-go@v1.43.0...v1.44.0) --- updated-dependencies: - dependency-name: google.golang.org/grpc dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <[email protected]> * Auto-fix go.sum changes in dependent modules Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: MrAlias <[email protected]> * Bump google.golang.org/grpc from 1.43.0 to 1.44.0 in /example/otel-collector (#2565) * Bump google.golang.org/grpc in /example/otel-collector Bumps [google.golang.org/grpc](https://github.com/grpc/grpc-go) from 1.43.0 to 1.44.0. - [Release notes](https://github.com/grpc/grpc-go/releases) - [Commits](grpc/grpc-go@v1.43.0...v1.44.0) --- updated-dependencies: - dependency-name: google.golang.org/grpc dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <[email protected]> * Auto-fix go.sum changes in dependent modules Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: MrAlias <[email protected]> * Bump google.golang.org/grpc from 1.43.0 to 1.44.0 in /exporters/otlp/otlpmetric (#2572) * Bump google.golang.org/grpc in /exporters/otlp/otlpmetric Bumps [google.golang.org/grpc](https://github.com/grpc/grpc-go) from 1.43.0 to 1.44.0. - [Release notes](https://github.com/grpc/grpc-go/releases) - [Commits](grpc/grpc-go@v1.43.0...v1.44.0) --- updated-dependencies: - dependency-name: google.golang.org/grpc dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <[email protected]> * Auto-fix go.sum changes in dependent modules Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: MrAlias <[email protected]> * Change Options to accept type not pointer (#2558) * Change trace options to accept type not pointer Add benchmark to show allocation improvement. * Update CONTRIBUTING.md guidelines * Update all Option iface * Fix grammar in CONTRIBUTING * Do not store TracerProvider or Tracer fields in SDK recordingSpan (#2575) * Do not store TracerProvider fields in span Instead of keeping a reference to the span's Tracer, and therefore also it's TracerProvider, and the associated resource and spanLimits just keep the reference to the Tracer. Refer to the TracerProvider fields when needed instead. * Make span refer to the inst lib via the Tracer Instead of holding a field in the span, refer to the field in the parent Tracer. * [website_docs] fix page meta-links (#2580) Contributes to open-telemetry/opentelemetry.io#1096 /cc @cartermp @austinlparker * Validate members once, in `NewMember` (#2522) * use NewMember, or specify if the member is not validated when creating new ones * expect members to already be validated when creating a new package * add changelog entry * add an isEmpty field to member and property for quick validation * rename isEmpty to hasData So by default, an empty struct really is marked as having no data * Update baggage/baggage_test.go Co-authored-by: Aaron Clawson <[email protected]> * don't validate the member in parseMember, we alredy ran that validation We also don't want to use NewMember, as that runs the property validation again, making the benchmark quite slower * move changelog entry to the fixed section * provide the member/property data when returning an invalid error Co-authored-by: Aaron Clawson <[email protected]> * Fix link to Zipkin exporter (#2581) Currently it is linked to the old package that was moved. * Unexport EnvBatchSpanProcessor* constants (#2583) * Move BSP env support to internal * Use pkg name * Update env test * Use internal/env in sdk/trace * Avoid an extra allocation in applyTracerProviderEnvConfigs. * Add additional errors for ratio > 1.0. * Add test cases for ratio > 1.0. * Update CHANGELOG.md Co-authored-by: Joshua MacDonald <[email protected]> Co-authored-by: jaychung <[email protected]> Co-authored-by: Ben Wells <[email protected]> Co-authored-by: Jeremy Kaplan <[email protected]> Co-authored-by: Tyler Yahn <[email protected]> Co-authored-by: thinkgo <[email protected]> Co-authored-by: Aaron Clawson <[email protected]> Co-authored-by: Aaron Clawson <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Tyler Yahn <[email protected]> Co-authored-by: Chao Weng <[email protected]> Co-authored-by: Patrice Chalin <[email protected]> Co-authored-by: Damien Mathieu <[email protected]>
1 parent 2f8698c commit fdbcf71

File tree

4 files changed

+301
-1
lines changed

4 files changed

+301
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Code instrumented with the `go.opentelemetry.io/otel/metric` will need to be mod
1616
### Added
1717

1818
- Add go 1.18 to our compatibility tests. (#2679)
19+
- Allow configuring the Sampler with the `OTEL_TRACES_SAMPLER` and `OTEL_TRACES_SAMPLER_ARG` environment variables. (#2305, #2517)
1920

2021
### Changed
2122

sdk/trace/provider.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ func NewTracerProvider(opts ...TracerProviderOption) *TracerProvider {
9999
o := tracerProviderConfig{
100100
spanLimits: NewSpanLimits(),
101101
}
102+
o = applyTracerProviderEnvConfigs(o)
102103

103104
for _, opt := range opts {
104105
o = opt.apply(o)
@@ -335,7 +336,10 @@ func WithIDGenerator(g IDGenerator) TracerProviderOption {
335336
// Tracers the TracerProvider creates to make their sampling decisions for the
336337
// Spans they create.
337338
//
338-
// If this option is not used, the TracerProvider will use a
339+
// This option overrides the Sampler configured through the OTEL_TRACES_SAMPLER
340+
// and OTEL_TRACES_SAMPLER_ARG environment variables. If this option is not used
341+
// and the sampler is not configured through environment variables or the environment
342+
// contains invalid/unsupported configuration, the TracerProvider will use a
339343
// ParentBased(AlwaysSample) Sampler by default.
340344
func WithSampler(s Sampler) TracerProviderOption {
341345
return traceProviderOptionFunc(func(cfg tracerProviderConfig) tracerProviderConfig {
@@ -408,6 +412,29 @@ func WithRawSpanLimits(limits SpanLimits) TracerProviderOption {
408412
})
409413
}
410414

415+
func applyTracerProviderEnvConfigs(cfg tracerProviderConfig) tracerProviderConfig {
416+
for _, opt := range tracerProviderOptionsFromEnv() {
417+
cfg = opt.apply(cfg)
418+
}
419+
420+
return cfg
421+
}
422+
423+
func tracerProviderOptionsFromEnv() []TracerProviderOption {
424+
var opts []TracerProviderOption
425+
426+
sampler, err := samplerFromEnv()
427+
if err != nil {
428+
otel.Handle(err)
429+
}
430+
431+
if sampler != nil {
432+
opts = append(opts, WithSampler(sampler))
433+
}
434+
435+
return opts
436+
}
437+
411438
// ensureValidTracerProviderConfig ensures that given TracerProviderConfig is valid.
412439
func ensureValidTracerProviderConfig(cfg tracerProviderConfig) tracerProviderConfig {
413440
if cfg.sampler == nil {

sdk/trace/provider_test.go

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@ package trace
1717
import (
1818
"context"
1919
"errors"
20+
"fmt"
21+
"math/rand"
2022
"testing"
2123

2224
"github.com/stretchr/testify/assert"
25+
"github.com/stretchr/testify/require"
2326

27+
ottest "go.opentelemetry.io/otel/internal/internaltest"
2428
"go.opentelemetry.io/otel/trace"
2529
)
2630

@@ -94,3 +98,164 @@ func TestSchemaURL(t *testing.T) {
9498
tracerStruct := tracerIface.(*tracer)
9599
assert.EqualValues(t, schemaURL, tracerStruct.instrumentationLibrary.SchemaURL)
96100
}
101+
102+
func TestTracerProviderSamplerConfigFromEnv(t *testing.T) {
103+
type testCase struct {
104+
sampler string
105+
samplerArg string
106+
argOptional bool
107+
description string
108+
errorType error
109+
invalidArgErrorType interface{}
110+
}
111+
112+
randFloat := rand.Float64()
113+
114+
tests := []testCase{
115+
{
116+
sampler: "invalid-sampler",
117+
argOptional: true,
118+
description: ParentBased(AlwaysSample()).Description(),
119+
errorType: errUnsupportedSampler("invalid-sampler"),
120+
invalidArgErrorType: func() *errUnsupportedSampler { e := errUnsupportedSampler("invalid-sampler"); return &e }(),
121+
},
122+
{
123+
sampler: "always_on",
124+
argOptional: true,
125+
description: AlwaysSample().Description(),
126+
},
127+
{
128+
sampler: "always_off",
129+
argOptional: true,
130+
description: NeverSample().Description(),
131+
},
132+
{
133+
sampler: "traceidratio",
134+
samplerArg: fmt.Sprintf("%g", randFloat),
135+
description: TraceIDRatioBased(randFloat).Description(),
136+
},
137+
{
138+
sampler: "traceidratio",
139+
samplerArg: fmt.Sprintf("%g", -randFloat),
140+
description: TraceIDRatioBased(1.0).Description(),
141+
errorType: errNegativeTraceIDRatio,
142+
},
143+
{
144+
sampler: "traceidratio",
145+
samplerArg: fmt.Sprintf("%g", 1+randFloat),
146+
description: TraceIDRatioBased(1.0).Description(),
147+
errorType: errGreaterThanOneTraceIDRatio,
148+
},
149+
{
150+
sampler: "traceidratio",
151+
argOptional: true,
152+
description: TraceIDRatioBased(1.0).Description(),
153+
invalidArgErrorType: new(samplerArgParseError),
154+
},
155+
{
156+
sampler: "parentbased_always_on",
157+
argOptional: true,
158+
description: ParentBased(AlwaysSample()).Description(),
159+
},
160+
{
161+
sampler: "parentbased_always_off",
162+
argOptional: true,
163+
description: ParentBased(NeverSample()).Description(),
164+
},
165+
{
166+
sampler: "parentbased_traceidratio",
167+
samplerArg: fmt.Sprintf("%g", randFloat),
168+
description: ParentBased(TraceIDRatioBased(randFloat)).Description(),
169+
},
170+
{
171+
sampler: "parentbased_traceidratio",
172+
samplerArg: fmt.Sprintf("%g", -randFloat),
173+
description: ParentBased(TraceIDRatioBased(1.0)).Description(),
174+
errorType: errNegativeTraceIDRatio,
175+
},
176+
{
177+
sampler: "parentbased_traceidratio",
178+
samplerArg: fmt.Sprintf("%g", 1+randFloat),
179+
description: ParentBased(TraceIDRatioBased(1.0)).Description(),
180+
errorType: errGreaterThanOneTraceIDRatio,
181+
},
182+
{
183+
sampler: "parentbased_traceidratio",
184+
argOptional: true,
185+
description: ParentBased(TraceIDRatioBased(1.0)).Description(),
186+
invalidArgErrorType: new(samplerArgParseError),
187+
},
188+
}
189+
190+
handler.Reset()
191+
192+
for _, test := range tests {
193+
t.Run(test.sampler, func(t *testing.T) {
194+
envVars := map[string]string{
195+
"OTEL_TRACES_SAMPLER": test.sampler,
196+
}
197+
198+
if test.samplerArg != "" {
199+
envVars["OTEL_TRACES_SAMPLER_ARG"] = test.samplerArg
200+
}
201+
envStore, err := ottest.SetEnvVariables(envVars)
202+
require.NoError(t, err)
203+
t.Cleanup(func() {
204+
handler.Reset()
205+
require.NoError(t, envStore.Restore())
206+
})
207+
208+
stp := NewTracerProvider(WithSyncer(NewTestExporter()))
209+
assert.Equal(t, test.description, stp.sampler.Description())
210+
if test.errorType != nil {
211+
testStoredError(t, test.errorType)
212+
} else {
213+
assert.Empty(t, handler.errs)
214+
}
215+
216+
if test.argOptional {
217+
t.Run("invalid sampler arg", func(t *testing.T) {
218+
envStore, err := ottest.SetEnvVariables(map[string]string{
219+
"OTEL_TRACES_SAMPLER": test.sampler,
220+
"OTEL_TRACES_SAMPLER_ARG": "invalid-ignored-string",
221+
})
222+
require.NoError(t, err)
223+
t.Cleanup(func() {
224+
handler.Reset()
225+
require.NoError(t, envStore.Restore())
226+
})
227+
228+
stp := NewTracerProvider(WithSyncer(NewTestExporter()))
229+
t.Cleanup(func() {
230+
require.NoError(t, stp.Shutdown(context.Background()))
231+
})
232+
assert.Equal(t, test.description, stp.sampler.Description())
233+
234+
if test.invalidArgErrorType != nil {
235+
testStoredError(t, test.invalidArgErrorType)
236+
} else {
237+
assert.Empty(t, handler.errs)
238+
}
239+
})
240+
}
241+
})
242+
}
243+
}
244+
245+
func testStoredError(t *testing.T, target interface{}) {
246+
t.Helper()
247+
248+
if assert.Len(t, handler.errs, 1) && assert.Error(t, handler.errs[0]) {
249+
err := handler.errs[0]
250+
251+
require.Implements(t, (*error)(nil), target)
252+
require.NotNil(t, target.(error))
253+
254+
defer handler.Reset()
255+
if errors.Is(err, target.(error)) {
256+
return
257+
}
258+
259+
assert.ErrorAs(t, err, target)
260+
}
261+
}

sdk/trace/sampler_env.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package trace // import "go.opentelemetry.io/otel/sdk/trace"
16+
17+
import (
18+
"errors"
19+
"fmt"
20+
"os"
21+
"strconv"
22+
"strings"
23+
)
24+
25+
const (
26+
tracesSamplerKey = "OTEL_TRACES_SAMPLER"
27+
tracesSamplerArgKey = "OTEL_TRACES_SAMPLER_ARG"
28+
29+
samplerAlwaysOn = "always_on"
30+
samplerAlwaysOff = "always_off"
31+
samplerTraceIDRatio = "traceidratio"
32+
samplerParentBasedAlwaysOn = "parentbased_always_on"
33+
samplerParsedBasedAlwaysOff = "parentbased_always_off"
34+
samplerParentBasedTraceIDRatio = "parentbased_traceidratio"
35+
)
36+
37+
type errUnsupportedSampler string
38+
39+
func (e errUnsupportedSampler) Error() string {
40+
return fmt.Sprintf("unsupported sampler: %s", string(e))
41+
}
42+
43+
var (
44+
errNegativeTraceIDRatio = errors.New("invalid trace ID ratio: less than 0.0")
45+
errGreaterThanOneTraceIDRatio = errors.New("invalid trace ID ratio: greater than 1.0")
46+
)
47+
48+
type samplerArgParseError struct {
49+
parseErr error
50+
}
51+
52+
func (e samplerArgParseError) Error() string {
53+
return fmt.Sprintf("parsing sampler argument: %s", e.parseErr.Error())
54+
}
55+
56+
func (e samplerArgParseError) Unwrap() error {
57+
return e.parseErr
58+
}
59+
60+
func samplerFromEnv() (Sampler, error) {
61+
sampler, ok := os.LookupEnv(tracesSamplerKey)
62+
if !ok {
63+
return nil, nil
64+
}
65+
66+
sampler = strings.ToLower(strings.TrimSpace(sampler))
67+
samplerArg, hasSamplerArg := os.LookupEnv(tracesSamplerArgKey)
68+
samplerArg = strings.TrimSpace(samplerArg)
69+
70+
switch sampler {
71+
case samplerAlwaysOn:
72+
return AlwaysSample(), nil
73+
case samplerAlwaysOff:
74+
return NeverSample(), nil
75+
case samplerTraceIDRatio:
76+
ratio, err := parseTraceIDRatio(samplerArg, hasSamplerArg)
77+
return ratio, err
78+
case samplerParentBasedAlwaysOn:
79+
return ParentBased(AlwaysSample()), nil
80+
case samplerParsedBasedAlwaysOff:
81+
return ParentBased(NeverSample()), nil
82+
case samplerParentBasedTraceIDRatio:
83+
ratio, err := parseTraceIDRatio(samplerArg, hasSamplerArg)
84+
return ParentBased(ratio), err
85+
default:
86+
return nil, errUnsupportedSampler(sampler)
87+
}
88+
89+
}
90+
91+
func parseTraceIDRatio(arg string, hasSamplerArg bool) (Sampler, error) {
92+
if !hasSamplerArg {
93+
return TraceIDRatioBased(1.0), nil
94+
}
95+
v, err := strconv.ParseFloat(arg, 64)
96+
if err != nil {
97+
return TraceIDRatioBased(1.0), samplerArgParseError{err}
98+
}
99+
if v < 0.0 {
100+
return TraceIDRatioBased(1.0), errNegativeTraceIDRatio
101+
}
102+
if v > 1.0 {
103+
return TraceIDRatioBased(1.0), errGreaterThanOneTraceIDRatio
104+
}
105+
106+
return TraceIDRatioBased(v), nil
107+
}

0 commit comments

Comments
 (0)