sampler_env.go (3020B)
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 if !hasSamplerArg { 77 return TraceIDRatioBased(1.0), nil 78 } 79 return parseTraceIDRatio(samplerArg) 80 case samplerParentBasedAlwaysOn: 81 return ParentBased(AlwaysSample()), nil 82 case samplerParsedBasedAlwaysOff: 83 return ParentBased(NeverSample()), nil 84 case samplerParentBasedTraceIDRatio: 85 if !hasSamplerArg { 86 return ParentBased(TraceIDRatioBased(1.0)), nil 87 } 88 ratio, err := parseTraceIDRatio(samplerArg) 89 return ParentBased(ratio), err 90 default: 91 return nil, errUnsupportedSampler(sampler) 92 } 93 } 94 95 func parseTraceIDRatio(arg string) (Sampler, error) { 96 v, err := strconv.ParseFloat(arg, 64) 97 if err != nil { 98 return TraceIDRatioBased(1.0), samplerArgParseError{err} 99 } 100 if v < 0.0 { 101 return TraceIDRatioBased(1.0), errNegativeTraceIDRatio 102 } 103 if v > 1.0 { 104 return TraceIDRatioBased(1.0), errGreaterThanOneTraceIDRatio 105 } 106 107 return TraceIDRatioBased(v), nil 108 }