| 1 |
// Code generated by gotmpl. DO NOT MODIFY. |
| 2 |
// source: internal/shared/otlp/otlpmetric/oconf/options.go.tmpl |
| 3 |
|
| 4 |
// Copyright The OpenTelemetry Authors |
| 5 |
// SPDX-License-Identifier: Apache-2.0 |
| 6 |
|
| 7 |
// Package oconf provides configuration for the otlpmetric exporters. |
| 8 |
package oconf // import "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf" |
| 9 |
|
| 10 |
import ( |
| 11 |
"crypto/tls" |
| 12 |
"fmt" |
| 13 |
"net/http" |
| 14 |
"net/url" |
| 15 |
"path" |
| 16 |
"strings" |
| 17 |
"time" |
| 18 |
|
| 19 |
"google.golang.org/grpc" |
| 20 |
"google.golang.org/grpc/backoff" |
| 21 |
"google.golang.org/grpc/credentials" |
| 22 |
"google.golang.org/grpc/credentials/insecure" |
| 23 |
"google.golang.org/grpc/encoding/gzip" |
| 24 |
|
| 25 |
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp/internal/retry" |
| 26 |
"go.opentelemetry.io/otel/internal/global" |
| 27 |
"go.opentelemetry.io/otel/sdk/metric" |
| 28 |
) |
| 29 |
|
| 30 |
const ( |
| 31 |
// DefaultMaxAttempts describes how many times the driver |
| 32 |
// should retry the sending of the payload in case of a |
| 33 |
// retryable error. |
| 34 |
DefaultMaxAttempts int = 5 |
| 35 |
// DefaultMetricsPath is a default URL path for endpoint that |
| 36 |
// receives metrics. |
| 37 |
DefaultMetricsPath string = "/v1/metrics" |
| 38 |
// DefaultBackoff is a default base backoff time used in the |
| 39 |
// exponential backoff strategy. |
| 40 |
DefaultBackoff time.Duration = 300 * time.Millisecond |
| 41 |
// DefaultTimeout is a default max waiting time for the backend to process |
| 42 |
// each span or metrics batch. |
| 43 |
DefaultTimeout time.Duration = 10 * time.Second |
| 44 |
) |
| 45 |
|
| 46 |
type ( |
| 47 |
// HTTPTransportProxyFunc is a function that resolves which URL to use as proxy for a given request. |
| 48 |
// This type is compatible with `http.Transport.Proxy` and can be used to set a custom proxy function to the OTLP HTTP client. |
| 49 |
HTTPTransportProxyFunc func(*http.Request) (*url.URL, error) |
| 50 |
|
| 51 |
SignalConfig struct { |
| 52 |
Endpoint string |
| 53 |
Insecure bool |
| 54 |
TLSCfg *tls.Config |
| 55 |
Headers map[string]string |
| 56 |
Compression Compression |
| 57 |
Timeout time.Duration |
| 58 |
URLPath string |
| 59 |
|
| 60 |
TemporalitySelector metric.TemporalitySelector |
| 61 |
AggregationSelector metric.AggregationSelector |
| 62 |
|
| 63 |
// gRPC configurations |
| 64 |
GRPCCredentials credentials.TransportCredentials |
| 65 |
|
| 66 |
// HTTP configurations |
| 67 |
Proxy HTTPTransportProxyFunc |
| 68 |
HTTPClient *http.Client |
| 69 |
} |
| 70 |
|
| 71 |
Config struct { |
| 72 |
// Signal specific configurations |
| 73 |
Metrics SignalConfig |
| 74 |
|
| 75 |
RetryConfig retry.Config |
| 76 |
|
| 77 |
// gRPC configurations |
| 78 |
ReconnectionPeriod time.Duration |
| 79 |
ServiceConfig string |
| 80 |
DialOptions []grpc.DialOption |
| 81 |
GRPCConn *grpc.ClientConn |
| 82 |
} |
| 83 |
) |
| 84 |
|
| 85 |
// NewHTTPConfig returns a new Config with all settings applied from opts and |
| 86 |
// any unset setting using the default HTTP config values. |
| 87 |
func NewHTTPConfig(opts ...HTTPOption) Config { |
| 88 |
cfg := Config{ |
| 89 |
Metrics: SignalConfig{ |
| 90 |
Endpoint: fmt.Sprintf("%s:%d", DefaultCollectorHost, DefaultCollectorHTTPPort), |
| 91 |
URLPath: DefaultMetricsPath, |
| 92 |
Compression: NoCompression, |
| 93 |
Timeout: DefaultTimeout, |
| 94 |
|
| 95 |
TemporalitySelector: metric.DefaultTemporalitySelector, |
| 96 |
AggregationSelector: metric.DefaultAggregationSelector, |
| 97 |
}, |
| 98 |
RetryConfig: retry.DefaultConfig, |
| 99 |
} |
| 100 |
cfg = ApplyHTTPEnvConfigs(cfg) |
| 101 |
for _, opt := range opts { |
| 102 |
cfg = opt.ApplyHTTPOption(cfg) |
| 103 |
} |
| 104 |
cfg.Metrics.URLPath = cleanPath(cfg.Metrics.URLPath, DefaultMetricsPath) |
| 105 |
return cfg |
| 106 |
} |
| 107 |
|
| 108 |
// cleanPath returns a path with all spaces trimmed. If urlPath is empty, |
| 109 |
// defaultPath is returned instead. |
| 110 |
func cleanPath(urlPath string, defaultPath string) string { |
| 111 |
tmp := strings.TrimSpace(urlPath) |
| 112 |
if tmp == "" || tmp == "." { |
| 113 |
return defaultPath |
| 114 |
} |
| 115 |
if !path.IsAbs(tmp) { |
| 116 |
tmp = "/" + tmp |
| 117 |
} |
| 118 |
return tmp |
| 119 |
} |
| 120 |
|
| 121 |
// NewGRPCConfig returns a new Config with all settings applied from opts and |
| 122 |
// any unset setting using the default gRPC config values. |
| 123 |
func NewGRPCConfig(opts ...GRPCOption) Config { |
| 124 |
cfg := Config{ |
| 125 |
Metrics: SignalConfig{ |
| 126 |
Endpoint: fmt.Sprintf("%s:%d", DefaultCollectorHost, DefaultCollectorGRPCPort), |
| 127 |
URLPath: DefaultMetricsPath, |
| 128 |
Compression: NoCompression, |
| 129 |
Timeout: DefaultTimeout, |
| 130 |
|
| 131 |
TemporalitySelector: metric.DefaultTemporalitySelector, |
| 132 |
AggregationSelector: metric.DefaultAggregationSelector, |
| 133 |
}, |
| 134 |
RetryConfig: retry.DefaultConfig, |
| 135 |
} |
| 136 |
cfg = ApplyGRPCEnvConfigs(cfg) |
| 137 |
for _, opt := range opts { |
| 138 |
cfg = opt.ApplyGRPCOption(cfg) |
| 139 |
} |
| 140 |
|
| 141 |
if cfg.ServiceConfig != "" { |
| 142 |
cfg.DialOptions = append(cfg.DialOptions, grpc.WithDefaultServiceConfig(cfg.ServiceConfig)) |
| 143 |
} |
| 144 |
// Prioritize GRPCCredentials over Insecure (passing both is an error). |
| 145 |
if cfg.Metrics.GRPCCredentials != nil { |
| 146 |
cfg.DialOptions = append(cfg.DialOptions, grpc.WithTransportCredentials(cfg.Metrics.GRPCCredentials)) |
| 147 |
} else if cfg.Metrics.Insecure { |
| 148 |
cfg.DialOptions = append(cfg.DialOptions, grpc.WithTransportCredentials(insecure.NewCredentials())) |
| 149 |
} else { |
| 150 |
// Default to using the host's root CA. |
| 151 |
creds := credentials.NewTLS(nil) |
| 152 |
cfg.Metrics.GRPCCredentials = creds |
| 153 |
cfg.DialOptions = append(cfg.DialOptions, grpc.WithTransportCredentials(creds)) |
| 154 |
} |
| 155 |
if cfg.Metrics.Compression == GzipCompression { |
| 156 |
cfg.DialOptions = append(cfg.DialOptions, grpc.WithDefaultCallOptions(grpc.UseCompressor(gzip.Name))) |
| 157 |
} |
| 158 |
if cfg.ReconnectionPeriod != 0 { |
| 159 |
p := grpc.ConnectParams{ |
| 160 |
Backoff: backoff.DefaultConfig, |
| 161 |
MinConnectTimeout: cfg.ReconnectionPeriod, |
| 162 |
} |
| 163 |
cfg.DialOptions = append(cfg.DialOptions, grpc.WithConnectParams(p)) |
| 164 |
} |
| 165 |
|
| 166 |
return cfg |
| 167 |
} |
| 168 |
|
| 169 |
type ( |
| 170 |
// GenericOption applies an option to the HTTP or gRPC driver. |
| 171 |
GenericOption interface { |
| 172 |
ApplyHTTPOption(Config) Config |
| 173 |
ApplyGRPCOption(Config) Config |
| 174 |
|
| 175 |
// A private method to prevent users implementing the |
| 176 |
// interface and so future additions to it will not |
| 177 |
// violate compatibility. |
| 178 |
private() |
| 179 |
} |
| 180 |
|
| 181 |
// HTTPOption applies an option to the HTTP driver. |
| 182 |
HTTPOption interface { |
| 183 |
ApplyHTTPOption(Config) Config |
| 184 |
|
| 185 |
// A private method to prevent users implementing the |
| 186 |
// interface and so future additions to it will not |
| 187 |
// violate compatibility. |
| 188 |
private() |
| 189 |
} |
| 190 |
|
| 191 |
// GRPCOption applies an option to the gRPC driver. |
| 192 |
GRPCOption interface { |
| 193 |
ApplyGRPCOption(Config) Config |
| 194 |
|
| 195 |
// A private method to prevent users implementing the |
| 196 |
// interface and so future additions to it will not |
| 197 |
// violate compatibility. |
| 198 |
private() |
| 199 |
} |
| 200 |
) |
| 201 |
|
| 202 |
// genericOption is an option that applies the same logic |
| 203 |
// for both gRPC and HTTP. |
| 204 |
type genericOption struct { |
| 205 |
fn func(Config) Config |
| 206 |
} |
| 207 |
|
| 208 |
func (g *genericOption) ApplyGRPCOption(cfg Config) Config { |
| 209 |
return g.fn(cfg) |
| 210 |
} |
| 211 |
|
| 212 |
func (g *genericOption) ApplyHTTPOption(cfg Config) Config { |
| 213 |
return g.fn(cfg) |
| 214 |
} |
| 215 |
|
| 216 |
func (genericOption) private() {} |
| 217 |
|
| 218 |
func newGenericOption(fn func(cfg Config) Config) GenericOption { |
| 219 |
return &genericOption{fn: fn} |
| 220 |
} |
| 221 |
|
| 222 |
// splitOption is an option that applies different logics |
| 223 |
// for gRPC and HTTP. |
| 224 |
type splitOption struct { |
| 225 |
httpFn func(Config) Config |
| 226 |
grpcFn func(Config) Config |
| 227 |
} |
| 228 |
|
| 229 |
func (g *splitOption) ApplyGRPCOption(cfg Config) Config { |
| 230 |
return g.grpcFn(cfg) |
| 231 |
} |
| 232 |
|
| 233 |
func (g *splitOption) ApplyHTTPOption(cfg Config) Config { |
| 234 |
return g.httpFn(cfg) |
| 235 |
} |
| 236 |
|
| 237 |
func (splitOption) private() {} |
| 238 |
|
| 239 |
func newSplitOption(httpFn func(cfg Config) Config, grpcFn func(cfg Config) Config) GenericOption { |
| 240 |
return &splitOption{httpFn: httpFn, grpcFn: grpcFn} |
| 241 |
} |
| 242 |
|
| 243 |
// httpOption is an option that is only applied to the HTTP driver. |
| 244 |
type httpOption struct { |
| 245 |
fn func(Config) Config |
| 246 |
} |
| 247 |
|
| 248 |
func (h *httpOption) ApplyHTTPOption(cfg Config) Config { |
| 249 |
return h.fn(cfg) |
| 250 |
} |
| 251 |
|
| 252 |
func (httpOption) private() {} |
| 253 |
|
| 254 |
func NewHTTPOption(fn func(cfg Config) Config) HTTPOption { |
| 255 |
return &httpOption{fn: fn} |
| 256 |
} |
| 257 |
|
| 258 |
// grpcOption is an option that is only applied to the gRPC driver. |
| 259 |
type grpcOption struct { |
| 260 |
fn func(Config) Config |
| 261 |
} |
| 262 |
|
| 263 |
func (h *grpcOption) ApplyGRPCOption(cfg Config) Config { |
| 264 |
return h.fn(cfg) |
| 265 |
} |
| 266 |
|
| 267 |
func (grpcOption) private() {} |
| 268 |
|
| 269 |
func NewGRPCOption(fn func(cfg Config) Config) GRPCOption { |
| 270 |
return &grpcOption{fn: fn} |
| 271 |
} |
| 272 |
|
| 273 |
// Generic Options |
| 274 |
|
| 275 |
func WithEndpoint(endpoint string) GenericOption { |
| 276 |
return newGenericOption(func(cfg Config) Config { |
| 277 |
cfg.Metrics.Endpoint = endpoint |
| 278 |
return cfg |
| 279 |
}) |
| 280 |
} |
| 281 |
|
| 282 |
func WithEndpointURL(v string) GenericOption { |
| 283 |
return newGenericOption(func(cfg Config) Config { |
| 284 |
u, err := url.Parse(v) |
| 285 |
if err != nil { |
| 286 |
global.Error(err, "otlpmetric: parse endpoint url", "url", v) |
| 287 |
return cfg |
| 288 |
} |
| 289 |
|
| 290 |
cfg.Metrics.Endpoint = u.Host |
| 291 |
cfg.Metrics.URLPath = u.Path |
| 292 |
cfg.Metrics.Insecure = u.Scheme != "https" |
| 293 |
|
| 294 |
return cfg |
| 295 |
}) |
| 296 |
} |
| 297 |
|
| 298 |
func WithCompression(compression Compression) GenericOption { |
| 299 |
return newGenericOption(func(cfg Config) Config { |
| 300 |
cfg.Metrics.Compression = compression |
| 301 |
return cfg |
| 302 |
}) |
| 303 |
} |
| 304 |
|
| 305 |
func WithURLPath(urlPath string) GenericOption { |
| 306 |
return newGenericOption(func(cfg Config) Config { |
| 307 |
cfg.Metrics.URLPath = urlPath |
| 308 |
return cfg |
| 309 |
}) |
| 310 |
} |
| 311 |
|
| 312 |
func WithRetry(rc retry.Config) GenericOption { |
| 313 |
return newGenericOption(func(cfg Config) Config { |
| 314 |
cfg.RetryConfig = rc |
| 315 |
return cfg |
| 316 |
}) |
| 317 |
} |
| 318 |
|
| 319 |
func WithTLSClientConfig(tlsCfg *tls.Config) GenericOption { |
| 320 |
return newSplitOption(func(cfg Config) Config { |
| 321 |
cfg.Metrics.TLSCfg = tlsCfg.Clone() |
| 322 |
return cfg |
| 323 |
}, func(cfg Config) Config { |
| 324 |
cfg.Metrics.GRPCCredentials = credentials.NewTLS(tlsCfg) |
| 325 |
return cfg |
| 326 |
}) |
| 327 |
} |
| 328 |
|
| 329 |
func WithInsecure() GenericOption { |
| 330 |
return newGenericOption(func(cfg Config) Config { |
| 331 |
cfg.Metrics.Insecure = true |
| 332 |
return cfg |
| 333 |
}) |
| 334 |
} |
| 335 |
|
| 336 |
func WithSecure() GenericOption { |
| 337 |
return newGenericOption(func(cfg Config) Config { |
| 338 |
cfg.Metrics.Insecure = false |
| 339 |
return cfg |
| 340 |
}) |
| 341 |
} |
| 342 |
|
| 343 |
func WithHeaders(headers map[string]string) GenericOption { |
| 344 |
return newGenericOption(func(cfg Config) Config { |
| 345 |
cfg.Metrics.Headers = headers |
| 346 |
return cfg |
| 347 |
}) |
| 348 |
} |
| 349 |
|
| 350 |
func WithTimeout(duration time.Duration) GenericOption { |
| 351 |
return newGenericOption(func(cfg Config) Config { |
| 352 |
cfg.Metrics.Timeout = duration |
| 353 |
return cfg |
| 354 |
}) |
| 355 |
} |
| 356 |
|
| 357 |
func WithTemporalitySelector(selector metric.TemporalitySelector) GenericOption { |
| 358 |
return newGenericOption(func(cfg Config) Config { |
| 359 |
cfg.Metrics.TemporalitySelector = selector |
| 360 |
return cfg |
| 361 |
}) |
| 362 |
} |
| 363 |
|
| 364 |
func WithAggregationSelector(selector metric.AggregationSelector) GenericOption { |
| 365 |
return newGenericOption(func(cfg Config) Config { |
| 366 |
cfg.Metrics.AggregationSelector = selector |
| 367 |
return cfg |
| 368 |
}) |
| 369 |
} |
| 370 |
|
| 371 |
func WithProxy(pf HTTPTransportProxyFunc) GenericOption { |
| 372 |
return newGenericOption(func(cfg Config) Config { |
| 373 |
cfg.Metrics.Proxy = pf |
| 374 |
return cfg |
| 375 |
}) |
| 376 |
} |
| 377 |
|
| 378 |
func WithHTTPClient(c *http.Client) GenericOption { |
| 379 |
return newGenericOption(func(cfg Config) Config { |
| 380 |
cfg.Metrics.HTTPClient = c |
| 381 |
return cfg |
| 382 |
}) |
| 383 |
} |