Debian Codegraph @

exporters/otlp/otlpmetric/otlpmetricgrpc/config.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package otlpmetricgrpc // import "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
5
6 import (
7 "fmt"
8 "time"
9
10 "google.golang.org/grpc"
11 "google.golang.org/grpc/credentials"
12
13 "go.opentelemetry.io/otel"
14 "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/oconf"
15 "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/retry"
16 "go.opentelemetry.io/otel/sdk/metric"
17 )
18
19 // Option applies a configuration option to the Exporter.
20 type Option interface {
21 applyGRPCOption(oconf.Config) oconf.Config
22 }
23
24 func asGRPCOptions(opts []Option) []oconf.GRPCOption {
25 converted := make([]oconf.GRPCOption, len(opts))
26 for i, o := range opts {
27 converted[i] = oconf.NewGRPCOption(o.applyGRPCOption)
28 }
29 return converted
30 }
31
32 // RetryConfig defines configuration for retrying the export of metric data
33 // that failed.
34 //
35 // This configuration does not define any network retry strategy. That is
36 // entirely handled by the gRPC ClientConn.
37 type RetryConfig retry.Config
38
39 type wrappedOption struct {
40 oconf.GRPCOption
41 }
42
43 func (w wrappedOption) applyGRPCOption(cfg oconf.Config) oconf.Config {
44 return w.ApplyGRPCOption(cfg)
45 }
46
47 // WithInsecure disables client transport security for the Exporter's gRPC
48 // connection, just like grpc.WithInsecure()
49 // (https://pkg.go.dev/google.golang.org/grpc#WithInsecure) does.
50 //
51 // If the OTEL_EXPORTER_OTLP_ENDPOINT or OTEL_EXPORTER_OTLP_METRICS_ENDPOINT
52 // environment variable is set, and this option is not passed, that variable
53 // value will be used to determine client security. If the endpoint has a
54 // scheme of "http" or "unix" client security will be disabled. If both are
55 // set, OTEL_EXPORTER_OTLP_METRICS_ENDPOINT will take precedence.
56 //
57 // By default, if an environment variable is not set, and this option is not
58 // passed, client security will be used.
59 //
60 // This option has no effect if WithGRPCConn is used.
61 func WithInsecure() Option {
62 return wrappedOption{oconf.WithInsecure()}
63 }
64
65 // WithEndpoint sets the target endpoint the Exporter will connect to.
66 //
67 // If the OTEL_EXPORTER_OTLP_ENDPOINT or OTEL_EXPORTER_OTLP_METRICS_ENDPOINT
68 // environment variable is set, and this option is not passed, that variable
69 // value will be used. If both environment variables are set,
70 // OTEL_EXPORTER_OTLP_METRICS_ENDPOINT will take precedence. If an environment
71 // variable is set, and this option is passed, this option will take precedence.
72 //
73 // If both this option and WithEndpointURL are used, the last used option will
74 // take precedence.
75 //
76 // By default, if an environment variable is not set, and this option is not
77 // passed, "localhost:4317" will be used.
78 //
79 // This option has no effect if WithGRPCConn is used.
80 func WithEndpoint(endpoint string) Option {
81 return wrappedOption{oconf.WithEndpoint(endpoint)}
82 }
83
84 // WithEndpointURL sets the target endpoint URL the Exporter will connect to.
85 //
86 // If the OTEL_EXPORTER_OTLP_ENDPOINT or OTEL_EXPORTER_OTLP_METRICS_ENDPOINT
87 // environment variable is set, and this option is not passed, that variable
88 // value will be used. If both environment variables are set,
89 // OTEL_EXPORTER_OTLP_METRICS_ENDPOINT will take precedence. If an environment
90 // variable is set, and this option is passed, this option will take precedence.
91 //
92 // If both this option and WithEndpoint are used, the last used option will
93 // take precedence.
94 //
95 // If an invalid URL is provided, the default value will be kept.
96 //
97 // By default, if an environment variable is not set, and this option is not
98 // passed, "localhost:4317" will be used.
99 //
100 // This option has no effect if WithGRPCConn is used.
101 func WithEndpointURL(u string) Option {
102 return wrappedOption{oconf.WithEndpointURL(u)}
103 }
104
105 // WithReconnectionPeriod set the minimum amount of time between connection
106 // attempts to the target endpoint.
107 //
108 // This option has no effect if WithGRPCConn is used.
109 func WithReconnectionPeriod(rp time.Duration) Option {
110 return wrappedOption{oconf.NewGRPCOption(func(cfg oconf.Config) oconf.Config {
111 cfg.ReconnectionPeriod = rp
112 return cfg
113 })}
114 }
115
116 func compressorToCompression(compressor string) oconf.Compression {
117 if compressor == "gzip" {
118 return oconf.GzipCompression
119 }
120
121 otel.Handle(fmt.Errorf("invalid compression type: '%s', using no compression as default", compressor))
122 return oconf.NoCompression
123 }
124
125 // WithCompressor sets the compressor the gRPC client uses.
126 // Supported compressor values: "gzip".
127 //
128 // If the OTEL_EXPORTER_OTLP_COMPRESSION or
129 // OTEL_EXPORTER_OTLP_METRICS_COMPRESSION environment variable is set, and
130 // this option is not passed, that variable value will be used. That value can
131 // be either "none" or "gzip". If both are set,
132 // OTEL_EXPORTER_OTLP_METRICS_COMPRESSION will take precedence.
133 //
134 // By default, if an environment variable is not set, and this option is not
135 // passed, no compressor will be used.
136 //
137 // This option has no effect if WithGRPCConn is used.
138 func WithCompressor(compressor string) Option {
139 return wrappedOption{oconf.WithCompression(compressorToCompression(compressor))}
140 }
141
142 // WithHeaders will send the provided headers with each gRPC requests.
143 //
144 // If the OTEL_EXPORTER_OTLP_HEADERS or OTEL_EXPORTER_OTLP_METRICS_HEADERS
145 // environment variable is set, and this option is not passed, that variable
146 // value will be used. The value will be parsed as a list of key value pairs.
147 // These pairs are expected to be in the W3C Correlation-Context format
148 // without additional semi-colon delimited metadata (i.e. "k1=v1,k2=v2"). If
149 // both are set, OTEL_EXPORTER_OTLP_METRICS_HEADERS will take precedence.
150 //
151 // By default, if an environment variable is not set, and this option is not
152 // passed, no user headers will be set.
153 func WithHeaders(headers map[string]string) Option {
154 return wrappedOption{oconf.WithHeaders(headers)}
155 }
156
157 // WithTLSCredentials sets the gRPC connection to use creds.
158 //
159 // If the OTEL_EXPORTER_OTLP_CERTIFICATE or
160 // OTEL_EXPORTER_OTLP_METRICS_CERTIFICATE environment variable is set, and
161 // this option is not passed, that variable value will be used. The value will
162 // be parsed the filepath of the TLS certificate chain to use. If both are
163 // set, OTEL_EXPORTER_OTLP_METRICS_CERTIFICATE will take precedence.
164 //
165 // By default, if an environment variable is not set, and this option is not
166 // passed, no TLS credentials will be used.
167 //
168 // This option has no effect if WithGRPCConn is used.
169 func WithTLSCredentials(creds credentials.TransportCredentials) Option {
170 return wrappedOption{oconf.NewGRPCOption(func(cfg oconf.Config) oconf.Config {
171 cfg.Metrics.GRPCCredentials = creds
172 return cfg
173 })}
174 }
175
176 // WithServiceConfig defines the default gRPC service config used.
177 //
178 // This option has no effect if WithGRPCConn is used.
179 func WithServiceConfig(serviceConfig string) Option {
180 return wrappedOption{oconf.NewGRPCOption(func(cfg oconf.Config) oconf.Config {
181 cfg.ServiceConfig = serviceConfig
182 return cfg
183 })}
184 }
185
186 // WithDialOption sets explicit grpc.DialOptions to use when establishing a
187 // gRPC connection. The options here are appended to the internal grpc.DialOptions
188 // used so they will take precedence over any other internal grpc.DialOptions
189 // they might conflict with.
190 // The [grpc.WithBlock], [grpc.WithTimeout], and [grpc.WithReturnConnectionError]
191 // grpc.DialOptions are ignored.
192 //
193 // This option has no effect if WithGRPCConn is used.
194 func WithDialOption(opts ...grpc.DialOption) Option {
195 return wrappedOption{oconf.NewGRPCOption(func(cfg oconf.Config) oconf.Config {
196 cfg.DialOptions = opts
197 return cfg
198 })}
199 }
200
201 // WithGRPCConn sets conn as the gRPC ClientConn used for all communication.
202 //
203 // This option takes precedence over any other option that relates to
204 // establishing or persisting a gRPC connection to a target endpoint. Any
205 // other option of those types passed will be ignored.
206 //
207 // It is the callers responsibility to close the passed conn. The Exporter
208 // Shutdown method will not close this connection.
209 func WithGRPCConn(conn *grpc.ClientConn) Option {
210 return wrappedOption{oconf.NewGRPCOption(func(cfg oconf.Config) oconf.Config {
211 cfg.GRPCConn = conn
212 return cfg
213 })}
214 }
215
216 // WithTimeout sets the max amount of time an Exporter will attempt an export.
217 //
218 // This takes precedence over any retry settings defined by WithRetry. Once
219 // this time limit has been reached the export is abandoned and the metric
220 // data is dropped.
221 //
222 // If the OTEL_EXPORTER_OTLP_TIMEOUT or OTEL_EXPORTER_OTLP_METRICS_TIMEOUT
223 // environment variable is set, and this option is not passed, that variable
224 // value will be used. The value will be parsed as an integer representing the
225 // timeout in milliseconds. If both are set,
226 // OTEL_EXPORTER_OTLP_METRICS_TIMEOUT will take precedence.
227 //
228 // By default, if an environment variable is not set, and this option is not
229 // passed, a timeout of 10 seconds will be used.
230 func WithTimeout(duration time.Duration) Option {
231 return wrappedOption{oconf.WithTimeout(duration)}
232 }
233
234 // WithRetry sets the retry policy for transient retryable errors that are
235 // returned by the target endpoint.
236 //
237 // If the target endpoint responds with not only a retryable error, but
238 // explicitly returns a backoff time in the response, that time will take
239 // precedence over these settings.
240 //
241 // These settings define the retry strategy implemented by the exporter.
242 // These settings do not define any network retry strategy.
243 // That is handled by the gRPC ClientConn.
244 //
245 // If unset, the default retry policy will be used. It will retry the export
246 // 5 seconds after receiving a retryable error and increase exponentially
247 // after each error for no more than a total time of 1 minute.
248 func WithRetry(settings RetryConfig) Option {
249 return wrappedOption{oconf.WithRetry(retry.Config(settings))}
250 }
251
252 // WithTemporalitySelector sets the TemporalitySelector the client will use to
253 // determine the Temporality of an instrument based on its kind. If this option
254 // is not used, the client will use the DefaultTemporalitySelector from the
255 // go.opentelemetry.io/otel/sdk/metric package.
256 func WithTemporalitySelector(selector metric.TemporalitySelector) Option {
257 return wrappedOption{oconf.WithTemporalitySelector(selector)}
258 }
259
260 // WithAggregationSelector sets the AggregationSelector the client will use to
261 // determine the aggregation to use for an instrument based on its kind. If
262 // this option is not used, the reader will use the DefaultAggregationSelector
263 // from the go.opentelemetry.io/otel/sdk/metric package, or the aggregation
264 // explicitly passed for a view matching an instrument.
265 func WithAggregationSelector(selector metric.AggregationSelector) Option {
266 return wrappedOption{oconf.WithAggregationSelector(selector)}
267 }