sampler_env.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. package trace // import "go.opentelemetry.io/otel/sdk/trace"
  15. import (
  16. "errors"
  17. "fmt"
  18. "os"
  19. "strconv"
  20. "strings"
  21. )
  22. const (
  23. tracesSamplerKey = "OTEL_TRACES_SAMPLER"
  24. tracesSamplerArgKey = "OTEL_TRACES_SAMPLER_ARG"
  25. samplerAlwaysOn = "always_on"
  26. samplerAlwaysOff = "always_off"
  27. samplerTraceIDRatio = "traceidratio"
  28. samplerParentBasedAlwaysOn = "parentbased_always_on"
  29. samplerParsedBasedAlwaysOff = "parentbased_always_off"
  30. samplerParentBasedTraceIDRatio = "parentbased_traceidratio"
  31. )
  32. type errUnsupportedSampler string
  33. func (e errUnsupportedSampler) Error() string {
  34. return fmt.Sprintf("unsupported sampler: %s", string(e))
  35. }
  36. var (
  37. errNegativeTraceIDRatio = errors.New("invalid trace ID ratio: less than 0.0")
  38. errGreaterThanOneTraceIDRatio = errors.New("invalid trace ID ratio: greater than 1.0")
  39. )
  40. type samplerArgParseError struct {
  41. parseErr error
  42. }
  43. func (e samplerArgParseError) Error() string {
  44. return fmt.Sprintf("parsing sampler argument: %s", e.parseErr.Error())
  45. }
  46. func (e samplerArgParseError) Unwrap() error {
  47. return e.parseErr
  48. }
  49. func samplerFromEnv() (Sampler, error) {
  50. sampler, ok := os.LookupEnv(tracesSamplerKey)
  51. if !ok {
  52. return nil, nil
  53. }
  54. sampler = strings.ToLower(strings.TrimSpace(sampler))
  55. samplerArg, hasSamplerArg := os.LookupEnv(tracesSamplerArgKey)
  56. samplerArg = strings.TrimSpace(samplerArg)
  57. switch sampler {
  58. case samplerAlwaysOn:
  59. return AlwaysSample(), nil
  60. case samplerAlwaysOff:
  61. return NeverSample(), nil
  62. case samplerTraceIDRatio:
  63. if !hasSamplerArg {
  64. return TraceIDRatioBased(1.0), nil
  65. }
  66. return parseTraceIDRatio(samplerArg)
  67. case samplerParentBasedAlwaysOn:
  68. return ParentBased(AlwaysSample()), nil
  69. case samplerParsedBasedAlwaysOff:
  70. return ParentBased(NeverSample()), nil
  71. case samplerParentBasedTraceIDRatio:
  72. if !hasSamplerArg {
  73. return ParentBased(TraceIDRatioBased(1.0)), nil
  74. }
  75. ratio, err := parseTraceIDRatio(samplerArg)
  76. return ParentBased(ratio), err
  77. default:
  78. return nil, errUnsupportedSampler(sampler)
  79. }
  80. }
  81. func parseTraceIDRatio(arg string) (Sampler, error) {
  82. v, err := strconv.ParseFloat(arg, 64)
  83. if err != nil {
  84. return TraceIDRatioBased(1.0), samplerArgParseError{err}
  85. }
  86. if v < 0.0 {
  87. return TraceIDRatioBased(1.0), errNegativeTraceIDRatio
  88. }
  89. if v > 1.0 {
  90. return TraceIDRatioBased(1.0), errGreaterThanOneTraceIDRatio
  91. }
  92. return TraceIDRatioBased(v), nil
  93. }