| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package otlpmetrichttp // import "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp" |
| 5 |
|
| 6 |
import ( |
| 7 |
"crypto/tls" |
| 8 |
"net/http" |
| 9 |
"net/url" |
| 10 |
"time" |
| 11 |
|
| 12 |
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf" |
| 13 |
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp/internal/retry" |
| 14 |
"go.opentelemetry.io/otel/sdk/metric" |
| 15 |
) |
| 16 |
|
| 17 |
// Compression describes the compression used for payloads sent to the |
| 18 |
// collector. |
| 19 |
type Compression oconf.Compression |
| 20 |
|
| 21 |
// HTTPTransportProxyFunc is a function that resolves which URL to use as proxy for a given request. |
| 22 |
// This type is compatible with http.Transport.Proxy and can be used to set a custom proxy function |
| 23 |
// to the OTLP HTTP client. |
| 24 |
type HTTPTransportProxyFunc func(*http.Request) (*url.URL, error) |
| 25 |
|
| 26 |
const ( |
| 27 |
// NoCompression tells the driver to send payloads without |
| 28 |
// compression. |
| 29 |
NoCompression = Compression(oconf.NoCompression) |
| 30 |
// GzipCompression tells the driver to send payloads after |
| 31 |
// compressing them with gzip. |
| 32 |
GzipCompression = Compression(oconf.GzipCompression) |
| 33 |
) |
| 34 |
|
| 35 |
// Option applies an option to the Exporter. |
| 36 |
type Option interface { |
| 37 |
applyHTTPOption(oconf.Config) oconf.Config |
| 38 |
} |
| 39 |
|
| 40 |
func asHTTPOptions(opts []Option) []oconf.HTTPOption { |
| 41 |
converted := make([]oconf.HTTPOption, len(opts)) |
| 42 |
for i, o := range opts { |
| 43 |
converted[i] = oconf.NewHTTPOption(o.applyHTTPOption) |
| 44 |
} |
| 45 |
return converted |
| 46 |
} |
| 47 |
|
| 48 |
// RetryConfig defines configuration for retrying the export of metric data |
| 49 |
// that failed. |
| 50 |
type RetryConfig retry.Config |
| 51 |
|
| 52 |
type wrappedOption struct { |
| 53 |
oconf.HTTPOption |
| 54 |
} |
| 55 |
|
| 56 |
func (w wrappedOption) applyHTTPOption(cfg oconf.Config) oconf.Config { |
| 57 |
return w.ApplyHTTPOption(cfg) |
| 58 |
} |
| 59 |
|
| 60 |
// WithEndpoint sets the target endpoint the Exporter will connect to. This |
| 61 |
// endpoint is specified as a host and optional port, no path or scheme should |
| 62 |
// be included (see WithInsecure and WithURLPath). |
| 63 |
// |
| 64 |
// If the OTEL_EXPORTER_OTLP_ENDPOINT or OTEL_EXPORTER_OTLP_METRICS_ENDPOINT |
| 65 |
// environment variable is set, and this option is not passed, that variable |
| 66 |
// value will be used. If both environment variables are set, |
| 67 |
// OTEL_EXPORTER_OTLP_METRICS_ENDPOINT will take precedence. If an environment |
| 68 |
// variable is set, and this option is passed, this option will take precedence. |
| 69 |
// |
| 70 |
// By default, if an environment variable is not set, and this option is not |
| 71 |
// passed, "localhost:4318" will be used. |
| 72 |
func WithEndpoint(endpoint string) Option { |
| 73 |
return wrappedOption{oconf.WithEndpoint(endpoint)} |
| 74 |
} |
| 75 |
|
| 76 |
// WithEndpointURL sets the target endpoint URL the Exporter will connect to. |
| 77 |
// |
| 78 |
// If the OTEL_EXPORTER_OTLP_ENDPOINT or OTEL_EXPORTER_OTLP_METRICS_ENDPOINT |
| 79 |
// environment variable is set, and this option is not passed, that variable |
| 80 |
// value will be used. If both environment variables are set, |
| 81 |
// OTEL_EXPORTER_OTLP_METRICS_ENDPOINT will take precedence. If an environment |
| 82 |
// variable is set, and this option is passed, this option will take precedence. |
| 83 |
// |
| 84 |
// If both this option and WithEndpoint are used, the last used option will |
| 85 |
// take precedence. |
| 86 |
// |
| 87 |
// If an invalid URL is provided, the default value will be kept. |
| 88 |
// |
| 89 |
// By default, if an environment variable is not set, and this option is not |
| 90 |
// passed, "localhost:4318" will be used. |
| 91 |
// |
| 92 |
// This option has no effect if WithGRPCConn is used. |
| 93 |
func WithEndpointURL(u string) Option { |
| 94 |
return wrappedOption{oconf.WithEndpointURL(u)} |
| 95 |
} |
| 96 |
|
| 97 |
// WithCompression sets the compression strategy the Exporter will use to |
| 98 |
// compress the HTTP body. |
| 99 |
// |
| 100 |
// If the OTEL_EXPORTER_OTLP_COMPRESSION or |
| 101 |
// OTEL_EXPORTER_OTLP_METRICS_COMPRESSION environment variable is set, and |
| 102 |
// this option is not passed, that variable value will be used. That value can |
| 103 |
// be either "none" or "gzip". If both are set, |
| 104 |
// OTEL_EXPORTER_OTLP_METRICS_COMPRESSION will take precedence. |
| 105 |
// |
| 106 |
// By default, if an environment variable is not set, and this option is not |
| 107 |
// passed, no compression strategy will be used. |
| 108 |
func WithCompression(compression Compression) Option { |
| 109 |
return wrappedOption{oconf.WithCompression(oconf.Compression(compression))} |
| 110 |
} |
| 111 |
|
| 112 |
// WithURLPath sets the URL path the Exporter will send requests to. |
| 113 |
// |
| 114 |
// If the OTEL_EXPORTER_OTLP_ENDPOINT or OTEL_EXPORTER_OTLP_METRICS_ENDPOINT |
| 115 |
// environment variable is set, and this option is not passed, the path |
| 116 |
// contained in that variable value will be used. If both are set, |
| 117 |
// OTEL_EXPORTER_OTLP_METRICS_ENDPOINT will take precedence. |
| 118 |
// |
| 119 |
// By default, if an environment variable is not set, and this option is not |
| 120 |
// passed, "/v1/metrics" will be used. |
| 121 |
func WithURLPath(urlPath string) Option { |
| 122 |
return wrappedOption{oconf.WithURLPath(urlPath)} |
| 123 |
} |
| 124 |
|
| 125 |
// WithTLSClientConfig sets the TLS configuration the Exporter will use for |
| 126 |
// HTTP requests. |
| 127 |
// |
| 128 |
// If the OTEL_EXPORTER_OTLP_CERTIFICATE or |
| 129 |
// OTEL_EXPORTER_OTLP_METRICS_CERTIFICATE environment variable is set, and |
| 130 |
// this option is not passed, that variable value will be used. The value will |
| 131 |
// be parsed the filepath of the TLS certificate chain to use. If both are |
| 132 |
// set, OTEL_EXPORTER_OTLP_METRICS_CERTIFICATE will take precedence. |
| 133 |
// |
| 134 |
// By default, if an environment variable is not set, and this option is not |
| 135 |
// passed, the system default configuration is used. |
| 136 |
func WithTLSClientConfig(tlsCfg *tls.Config) Option { |
| 137 |
return wrappedOption{oconf.WithTLSClientConfig(tlsCfg)} |
| 138 |
} |
| 139 |
|
| 140 |
// WithInsecure disables client transport security for the Exporter's HTTP |
| 141 |
// connection. |
| 142 |
// |
| 143 |
// If the OTEL_EXPORTER_OTLP_ENDPOINT or OTEL_EXPORTER_OTLP_METRICS_ENDPOINT |
| 144 |
// environment variable is set, and this option is not passed, that variable |
| 145 |
// value will be used to determine client security. If the endpoint has a |
| 146 |
// scheme of "http" or "unix" client security will be disabled. If both are |
| 147 |
// set, OTEL_EXPORTER_OTLP_METRICS_ENDPOINT will take precedence. |
| 148 |
// |
| 149 |
// By default, if an environment variable is not set, and this option is not |
| 150 |
// passed, client security will be used. |
| 151 |
func WithInsecure() Option { |
| 152 |
return wrappedOption{oconf.WithInsecure()} |
| 153 |
} |
| 154 |
|
| 155 |
// WithHeaders will send the provided headers with each HTTP requests. |
| 156 |
// |
| 157 |
// If the OTEL_EXPORTER_OTLP_HEADERS or OTEL_EXPORTER_OTLP_METRICS_HEADERS |
| 158 |
// environment variable is set, and this option is not passed, that variable |
| 159 |
// value will be used. The value will be parsed as a list of key value pairs. |
| 160 |
// These pairs are expected to be in the W3C Correlation-Context format |
| 161 |
// without additional semi-colon delimited metadata (i.e. "k1=v1,k2=v2"). If |
| 162 |
// both are set, OTEL_EXPORTER_OTLP_METRICS_HEADERS will take precedence. |
| 163 |
// |
| 164 |
// By default, if an environment variable is not set, and this option is not |
| 165 |
// passed, no user headers will be set. |
| 166 |
func WithHeaders(headers map[string]string) Option { |
| 167 |
return wrappedOption{oconf.WithHeaders(headers)} |
| 168 |
} |
| 169 |
|
| 170 |
// WithTimeout sets the max amount of time an Exporter will attempt an export. |
| 171 |
// |
| 172 |
// This takes precedence over any retry settings defined by WithRetry. Once |
| 173 |
// this time limit has been reached the export is abandoned and the metric |
| 174 |
// data is dropped. |
| 175 |
// |
| 176 |
// If the OTEL_EXPORTER_OTLP_TIMEOUT or OTEL_EXPORTER_OTLP_METRICS_TIMEOUT |
| 177 |
// environment variable is set, and this option is not passed, that variable |
| 178 |
// value will be used. The value will be parsed as an integer representing the |
| 179 |
// timeout in milliseconds. If both are set, |
| 180 |
// OTEL_EXPORTER_OTLP_METRICS_TIMEOUT will take precedence. |
| 181 |
// |
| 182 |
// By default, if an environment variable is not set, and this option is not |
| 183 |
// passed, a timeout of 10 seconds will be used. |
| 184 |
func WithTimeout(duration time.Duration) Option { |
| 185 |
return wrappedOption{oconf.WithTimeout(duration)} |
| 186 |
} |
| 187 |
|
| 188 |
// WithRetry sets the retry policy for transient retryable errors that are |
| 189 |
// returned by the target endpoint. |
| 190 |
// |
| 191 |
// If the target endpoint responds with not only a retryable error, but |
| 192 |
// explicitly returns a backoff time in the response, that time will take |
| 193 |
// precedence over these settings. |
| 194 |
// |
| 195 |
// If unset, the default retry policy will be used. It will retry the export |
| 196 |
// 5 seconds after receiving a retryable error and increase exponentially |
| 197 |
// after each error for no more than a total time of 1 minute. |
| 198 |
func WithRetry(rc RetryConfig) Option { |
| 199 |
return wrappedOption{oconf.WithRetry(retry.Config(rc))} |
| 200 |
} |
| 201 |
|
| 202 |
// WithTemporalitySelector sets the TemporalitySelector the client will use to |
| 203 |
// determine the Temporality of an instrument based on its kind. If this option |
| 204 |
// is not used, the client will use the DefaultTemporalitySelector from the |
| 205 |
// go.opentelemetry.io/otel/sdk/metric package. |
| 206 |
func WithTemporalitySelector(selector metric.TemporalitySelector) Option { |
| 207 |
return wrappedOption{oconf.WithTemporalitySelector(selector)} |
| 208 |
} |
| 209 |
|
| 210 |
// WithAggregationSelector sets the AggregationSelector the client will use to |
| 211 |
// determine the aggregation to use for an instrument based on its kind. If |
| 212 |
// this option is not used, the reader will use the DefaultAggregationSelector |
| 213 |
// from the go.opentelemetry.io/otel/sdk/metric package, or the aggregation |
| 214 |
// explicitly passed for a view matching an instrument. |
| 215 |
func WithAggregationSelector(selector metric.AggregationSelector) Option { |
| 216 |
return wrappedOption{oconf.WithAggregationSelector(selector)} |
| 217 |
} |
| 218 |
|
| 219 |
// WithProxy sets the Proxy function the client will use to determine the |
| 220 |
// proxy to use for an HTTP request. If this option is not used, the client |
| 221 |
// will use [http.ProxyFromEnvironment]. |
| 222 |
func WithProxy(pf HTTPTransportProxyFunc) Option { |
| 223 |
return wrappedOption{oconf.WithProxy(oconf.HTTPTransportProxyFunc(pf))} |
| 224 |
} |
| 225 |
|
| 226 |
// WithHTTPClient sets the HTTP client to used by the exporter. |
| 227 |
// |
| 228 |
// This option will take precedence over [WithProxy], [WithTimeout], |
| 229 |
// [WithTLSClientConfig] options as well as OTEL_EXPORTER_OTLP_CERTIFICATE, |
| 230 |
// OTEL_EXPORTER_OTLP_METRICS_CERTIFICATE, OTEL_EXPORTER_OTLP_TIMEOUT, |
| 231 |
// OTEL_EXPORTER_OTLP_METRICS_TIMEOUT environment variables. |
| 232 |
// |
| 233 |
// Timeout and all other fields of the passed [http.Client] are left intact. |
| 234 |
// |
| 235 |
// Be aware that passing an HTTP client with transport like |
| 236 |
// [go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp.NewTransport] can |
| 237 |
// cause the client to be instrumented twice and cause infinite recursion. |
| 238 |
func WithHTTPClient(c *http.Client) Option { |
| 239 |
return wrappedOption{oconf.WithHTTPClient(c)} |
| 240 |
} |