config_env.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // Copyright (c) 2018 The Jaeger 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 config
  15. import (
  16. "fmt"
  17. "os"
  18. "strconv"
  19. "strings"
  20. "time"
  21. opentracing "github.com/opentracing/opentracing-go"
  22. "github.com/pkg/errors"
  23. "github.com/uber/jaeger-client-go"
  24. )
  25. const (
  26. // environment variable names
  27. envServiceName = "JAEGER_SERVICE_NAME"
  28. envDisabled = "JAEGER_DISABLED"
  29. envRPCMetrics = "JAEGER_RPC_METRICS"
  30. envTags = "JAEGER_TAGS"
  31. envSamplerType = "JAEGER_SAMPLER_TYPE"
  32. envSamplerParam = "JAEGER_SAMPLER_PARAM"
  33. envSamplerManagerHostPort = "JAEGER_SAMPLER_MANAGER_HOST_PORT"
  34. envSamplerMaxOperations = "JAEGER_SAMPLER_MAX_OPERATIONS"
  35. envSamplerRefreshInterval = "JAEGER_SAMPLER_REFRESH_INTERVAL"
  36. envReporterMaxQueueSize = "JAEGER_REPORTER_MAX_QUEUE_SIZE"
  37. envReporterFlushInterval = "JAEGER_REPORTER_FLUSH_INTERVAL"
  38. envReporterLogSpans = "JAEGER_REPORTER_LOG_SPANS"
  39. envAgentHost = "JAEGER_AGENT_HOST"
  40. envAgentPort = "JAEGER_AGENT_PORT"
  41. )
  42. // FromEnv uses environment variables to set the tracer's Configuration
  43. func FromEnv() (*Configuration, error) {
  44. c := &Configuration{}
  45. if e := os.Getenv(envServiceName); e != "" {
  46. c.ServiceName = e
  47. }
  48. if e := os.Getenv(envRPCMetrics); e != "" {
  49. if value, err := strconv.ParseBool(e); err == nil {
  50. c.RPCMetrics = value
  51. } else {
  52. return nil, errors.Wrapf(err, "cannot parse env var %s=%s", envRPCMetrics, e)
  53. }
  54. }
  55. if e := os.Getenv(envDisabled); e != "" {
  56. if value, err := strconv.ParseBool(e); err == nil {
  57. c.Disabled = value
  58. } else {
  59. return nil, errors.Wrapf(err, "cannot parse env var %s=%s", envDisabled, e)
  60. }
  61. }
  62. if e := os.Getenv(envTags); e != "" {
  63. c.Tags = parseTags(e)
  64. }
  65. if s, err := samplerConfigFromEnv(); err == nil {
  66. c.Sampler = s
  67. } else {
  68. return nil, errors.Wrap(err, "cannot obtain sampler config from env")
  69. }
  70. if r, err := reporterConfigFromEnv(); err == nil {
  71. c.Reporter = r
  72. } else {
  73. return nil, errors.Wrap(err, "cannot obtain reporter config from env")
  74. }
  75. return c, nil
  76. }
  77. // samplerConfigFromEnv creates a new SamplerConfig based on the environment variables
  78. func samplerConfigFromEnv() (*SamplerConfig, error) {
  79. sc := &SamplerConfig{}
  80. if e := os.Getenv(envSamplerType); e != "" {
  81. sc.Type = e
  82. }
  83. if e := os.Getenv(envSamplerParam); e != "" {
  84. if value, err := strconv.ParseFloat(e, 64); err == nil {
  85. sc.Param = value
  86. } else {
  87. return nil, errors.Wrapf(err, "cannot parse env var %s=%s", envSamplerParam, e)
  88. }
  89. }
  90. if e := os.Getenv(envSamplerManagerHostPort); e != "" {
  91. sc.SamplingServerURL = e
  92. }
  93. if e := os.Getenv(envSamplerMaxOperations); e != "" {
  94. if value, err := strconv.ParseInt(e, 10, 0); err == nil {
  95. sc.MaxOperations = int(value)
  96. } else {
  97. return nil, errors.Wrapf(err, "cannot parse env var %s=%s", envSamplerMaxOperations, e)
  98. }
  99. }
  100. if e := os.Getenv(envSamplerRefreshInterval); e != "" {
  101. if value, err := time.ParseDuration(e); err == nil {
  102. sc.SamplingRefreshInterval = value
  103. } else {
  104. return nil, errors.Wrapf(err, "cannot parse env var %s=%s", envSamplerRefreshInterval, e)
  105. }
  106. }
  107. return sc, nil
  108. }
  109. // reporterConfigFromEnv creates a new ReporterConfig based on the environment variables
  110. func reporterConfigFromEnv() (*ReporterConfig, error) {
  111. rc := &ReporterConfig{}
  112. if e := os.Getenv(envReporterMaxQueueSize); e != "" {
  113. if value, err := strconv.ParseInt(e, 10, 0); err == nil {
  114. rc.QueueSize = int(value)
  115. } else {
  116. return nil, errors.Wrapf(err, "cannot parse env var %s=%s", envReporterMaxQueueSize, e)
  117. }
  118. }
  119. if e := os.Getenv(envReporterFlushInterval); e != "" {
  120. if value, err := time.ParseDuration(e); err == nil {
  121. rc.BufferFlushInterval = value
  122. } else {
  123. return nil, errors.Wrapf(err, "cannot parse env var %s=%s", envReporterFlushInterval, e)
  124. }
  125. }
  126. if e := os.Getenv(envReporterLogSpans); e != "" {
  127. if value, err := strconv.ParseBool(e); err == nil {
  128. rc.LogSpans = value
  129. } else {
  130. return nil, errors.Wrapf(err, "cannot parse env var %s=%s", envReporterLogSpans, e)
  131. }
  132. }
  133. host := jaeger.DefaultUDPSpanServerHost
  134. if e := os.Getenv(envAgentHost); e != "" {
  135. host = e
  136. }
  137. port := jaeger.DefaultUDPSpanServerPort
  138. if e := os.Getenv(envAgentPort); e != "" {
  139. if value, err := strconv.ParseInt(e, 10, 0); err == nil {
  140. port = int(value)
  141. } else {
  142. return nil, errors.Wrapf(err, "cannot parse env var %s=%s", envAgentPort, e)
  143. }
  144. }
  145. // the side effect of this is that we are building the default value, even if none of the env vars
  146. // were not explicitly passed
  147. rc.LocalAgentHostPort = fmt.Sprintf("%s:%d", host, port)
  148. return rc, nil
  149. }
  150. // parseTags parses the given string into a collection of Tags.
  151. // Spec for this value:
  152. // - comma separated list of key=value
  153. // - value can be specified using the notation ${envVar:defaultValue}, where `envVar`
  154. // is an environment variable and `defaultValue` is the value to use in case the env var is not set
  155. func parseTags(sTags string) []opentracing.Tag {
  156. pairs := strings.Split(sTags, ",")
  157. tags := make([]opentracing.Tag, 0)
  158. for _, p := range pairs {
  159. kv := strings.SplitN(p, "=", 2)
  160. k, v := strings.TrimSpace(kv[0]), strings.TrimSpace(kv[1])
  161. if strings.HasPrefix(v, "${") && strings.HasSuffix(v, "}") {
  162. ed := strings.SplitN(v[2:len(v)-1], ":", 2)
  163. e, d := ed[0], ed[1]
  164. v = os.Getenv(e)
  165. if v == "" && d != "" {
  166. v = d
  167. }
  168. }
  169. tag := opentracing.Tag{Key: k, Value: v}
  170. tags = append(tags, tag)
  171. }
  172. return tags
  173. }