options.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 otlptracehttp // import "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
  15. import (
  16. "crypto/tls"
  17. "time"
  18. "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig"
  19. "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp/internal/retry"
  20. )
  21. // Compression describes the compression used for payloads sent to the
  22. // collector.
  23. type Compression otlpconfig.Compression
  24. const (
  25. // NoCompression tells the driver to send payloads without
  26. // compression.
  27. NoCompression = Compression(otlpconfig.NoCompression)
  28. // GzipCompression tells the driver to send payloads after
  29. // compressing them with gzip.
  30. GzipCompression = Compression(otlpconfig.GzipCompression)
  31. ZstdCompression = Compression(otlpconfig.ZstdCompression)
  32. )
  33. // Option applies an option to the HTTP client.
  34. type Option interface {
  35. applyHTTPOption(otlpconfig.Config) otlpconfig.Config
  36. }
  37. func asHTTPOptions(opts []Option) []otlpconfig.HTTPOption {
  38. converted := make([]otlpconfig.HTTPOption, len(opts))
  39. for i, o := range opts {
  40. converted[i] = otlpconfig.NewHTTPOption(o.applyHTTPOption)
  41. }
  42. return converted
  43. }
  44. // RetryConfig defines configuration for retrying batches in case of export
  45. // failure using an exponential backoff.
  46. type RetryConfig retry.Config
  47. type wrappedOption struct {
  48. otlpconfig.HTTPOption
  49. }
  50. func (w wrappedOption) applyHTTPOption(cfg otlpconfig.Config) otlpconfig.Config {
  51. return w.ApplyHTTPOption(cfg)
  52. }
  53. // WithEndpoint allows one to set the address of the collector
  54. // endpoint that the driver will use to send spans. If
  55. // unset, it will instead try to use
  56. // the default endpoint (localhost:4318). Note that the endpoint
  57. // must not contain any URL path.
  58. func WithEndpoint(endpoint string) Option {
  59. return wrappedOption{otlpconfig.WithEndpoint(endpoint)}
  60. }
  61. // WithCompression tells the driver to compress the sent data.
  62. func WithCompression(compression Compression) Option {
  63. return wrappedOption{otlpconfig.WithCompression(otlpconfig.Compression(compression))}
  64. }
  65. // WithURLPath allows one to override the default URL path used
  66. // for sending traces. If unset, default ("/v1/traces") will be used.
  67. func WithURLPath(urlPath string) Option {
  68. return wrappedOption{otlpconfig.WithURLPath(urlPath)}
  69. }
  70. // WithTLSClientConfig can be used to set up a custom TLS
  71. // configuration for the client used to send payloads to the
  72. // collector. Use it if you want to use a custom certificate.
  73. func WithTLSClientConfig(tlsCfg *tls.Config) Option {
  74. return wrappedOption{otlpconfig.WithTLSClientConfig(tlsCfg)}
  75. }
  76. // WithInsecure tells the driver to connect to the collector using the
  77. // HTTP scheme, instead of HTTPS.
  78. func WithInsecure() Option {
  79. return wrappedOption{otlpconfig.WithInsecure()}
  80. }
  81. // WithHeaders allows one to tell the driver to send additional HTTP
  82. // headers with the payloads. Specifying headers like Content-Length,
  83. // Content-Encoding and Content-Type may result in a broken driver.
  84. func WithHeaders(headers map[string]string) Option {
  85. return wrappedOption{otlpconfig.WithHeaders(headers)}
  86. }
  87. // WithTimeout tells the driver the max waiting time for the backend to process
  88. // each spans batch. If unset, the default will be 10 seconds.
  89. func WithTimeout(duration time.Duration) Option {
  90. return wrappedOption{otlpconfig.WithTimeout(duration)}
  91. }
  92. // WithRetry configures the retry policy for transient errors that may occurs
  93. // when exporting traces. An exponential back-off algorithm is used to ensure
  94. // endpoints are not overwhelmed with retries. If unset, the default retry
  95. // policy will retry after 5 seconds and increase exponentially after each
  96. // error for a total of 1 minute.
  97. func WithRetry(rc RetryConfig) Option {
  98. return wrappedOption{otlpconfig.WithRetry(retry.Config(rc))}
  99. }