Debian Codegraph @

exporters/otlp/otlptrace/otlptracehttp/options.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package otlptracehttp // import "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
5
6 import (
7 "crypto/tls"
8 "net/http"
9 "net/url"
10 "time"
11
12 "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig"
13 "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp/internal/retry"
14 )
15
16 // Compression describes the compression used for payloads sent to the
17 // collector.
18 type Compression otlpconfig.Compression
19
20 // HTTPTransportProxyFunc is a function that resolves which URL to use as proxy for a given request.
21 // This type is compatible with http.Transport.Proxy and can be used to set a custom proxy function
22 // to the OTLP HTTP client.
23 type HTTPTransportProxyFunc func(*http.Request) (*url.URL, error)
24
25 const (
26 // NoCompression tells the driver to send payloads without
27 // compression.
28 NoCompression = Compression(otlpconfig.NoCompression)
29 // GzipCompression tells the driver to send payloads after
30 // compressing them with gzip.
31 GzipCompression = Compression(otlpconfig.GzipCompression)
32 )
33
34 // Option applies an option to the HTTP client.
35 type Option interface {
36 applyHTTPOption(otlpconfig.Config) otlpconfig.Config
37 }
38
39 func asHTTPOptions(opts []Option) []otlpconfig.HTTPOption {
40 converted := make([]otlpconfig.HTTPOption, len(opts))
41 for i, o := range opts {
42 converted[i] = otlpconfig.NewHTTPOption(o.applyHTTPOption)
43 }
44 return converted
45 }
46
47 // RetryConfig defines configuration for retrying batches in case of export
48 // failure using an exponential backoff.
49 type RetryConfig retry.Config
50
51 type wrappedOption struct {
52 otlpconfig.HTTPOption
53 }
54
55 func (w wrappedOption) applyHTTPOption(cfg otlpconfig.Config) otlpconfig.Config {
56 return w.ApplyHTTPOption(cfg)
57 }
58
59 // WithEndpoint sets the target endpoint (host and port) the Exporter will
60 // connect to. The provided endpoint should resemble "example.com:4318" (no
61 // scheme or path).
62 //
63 // If the OTEL_EXPORTER_OTLP_ENDPOINT or OTEL_EXPORTER_OTLP_TRACES_ENDPOINT
64 // environment variable is set, and this option is not passed, that variable
65 // value will be used. If both environment variables are set,
66 // OTEL_EXPORTER_OTLP_TRACES_ENDPOINT will take precedence. If an environment
67 // variable is set, and this option is passed, this option will take precedence.
68 // Note, both environment variables include the full
69 // scheme and path, while WithEndpoint sets only the host and port.
70 //
71 // If both this option and WithEndpointURL are used, the last used option will
72 // take precedence.
73 //
74 // By default, if an environment variable is not set, and this option is not
75 // passed, "localhost:4318" will be used.
76 //
77 // This option has no effect if WithGRPCConn is used.
78 func WithEndpoint(endpoint string) Option {
79 return wrappedOption{otlpconfig.WithEndpoint(endpoint)}
80 }
81
82 // WithEndpointURL sets the target endpoint URL (scheme, host, port, path) the
83 // Exporter will connect to.
84 //
85 // If the OTEL_EXPORTER_OTLP_ENDPOINT or OTEL_EXPORTER_OTLP_TRACES_ENDPOINT
86 // environment variable is set, and this option is not passed, that variable
87 // value will be used. If both environment variables are set,
88 // OTEL_EXPORTER_OTLP_TRACES_ENDPOINT will take precedence. If an environment
89 // variable is set, and this option is passed, this option will take precedence.
90 //
91 // If both this option and WithEndpoint are used, the last used option will
92 // take precedence.
93 //
94 // If an invalid URL is provided, the default value will be kept.
95 //
96 // By default, if an environment variable is not set, and this option is not
97 // passed, "localhost:4318" will be used.
98 //
99 // This option has no effect if WithGRPCConn is used.
100 func WithEndpointURL(u string) Option {
101 return wrappedOption{otlpconfig.WithEndpointURL(u)}
102 }
103
104 // WithCompression tells the driver to compress the sent data.
105 func WithCompression(compression Compression) Option {
106 return wrappedOption{otlpconfig.WithCompression(otlpconfig.Compression(compression))}
107 }
108
109 // WithURLPath allows one to override the default URL path used
110 // for sending traces. If unset, default ("/v1/traces") will be used.
111 func WithURLPath(urlPath string) Option {
112 return wrappedOption{otlpconfig.WithURLPath(urlPath)}
113 }
114
115 // WithTLSClientConfig can be used to set up a custom TLS
116 // configuration for the client used to send payloads to the
117 // collector. Use it if you want to use a custom certificate.
118 func WithTLSClientConfig(tlsCfg *tls.Config) Option {
119 return wrappedOption{otlpconfig.WithTLSClientConfig(tlsCfg)}
120 }
121
122 // WithInsecure tells the driver to connect to the collector using the
123 // HTTP scheme, instead of HTTPS.
124 func WithInsecure() Option {
125 return wrappedOption{otlpconfig.WithInsecure()}
126 }
127
128 // WithHeaders allows one to tell the driver to send additional HTTP
129 // headers with the payloads. Specifying headers like Content-Length,
130 // Content-Encoding and Content-Type may result in a broken driver.
131 func WithHeaders(headers map[string]string) Option {
132 return wrappedOption{otlpconfig.WithHeaders(headers)}
133 }
134
135 // WithTimeout tells the driver the max waiting time for the backend to process
136 // each spans batch. If unset, the default will be 10 seconds.
137 func WithTimeout(duration time.Duration) Option {
138 return wrappedOption{otlpconfig.WithTimeout(duration)}
139 }
140
141 // WithRetry configures the retry policy for transient errors that may occurs
142 // when exporting traces. An exponential back-off algorithm is used to ensure
143 // endpoints are not overwhelmed with retries. If unset, the default retry
144 // policy will retry after 5 seconds and increase exponentially after each
145 // error for a total of 1 minute.
146 func WithRetry(rc RetryConfig) Option {
147 return wrappedOption{otlpconfig.WithRetry(retry.Config(rc))}
148 }
149
150 // WithProxy sets the Proxy function the client will use to determine the
151 // proxy to use for an HTTP request. If this option is not used, the client
152 // will use [http.ProxyFromEnvironment].
153 func WithProxy(pf HTTPTransportProxyFunc) Option {
154 return wrappedOption{otlpconfig.WithProxy(otlpconfig.HTTPTransportProxyFunc(pf))}
155 }
156
157 // WithHTTPClient sets the HTTP client to used by the exporter.
158 //
159 // This option will take precedence over [WithProxy], [WithTimeout],
160 // [WithTLSClientConfig] options as well as OTEL_EXPORTER_OTLP_CERTIFICATE,
161 // OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE, OTEL_EXPORTER_OTLP_TIMEOUT,
162 // OTEL_EXPORTER_OTLP_TRACES_TIMEOUT environment variables.
163 //
164 // Timeout and all other fields of the passed [http.Client] are left intact.
165 //
166 // Be aware that passing an HTTP client with transport like
167 // [go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp.NewTransport] can
168 // cause the client to be instrumented twice and cause infinite recursion.
169 func WithHTTPClient(c *http.Client) Option {
170 return wrappedOption{otlpconfig.WithHTTPClient(c)}
171 }