| 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 |
"bytes" |
| 8 |
"compress/gzip" |
| 9 |
"context" |
| 10 |
"errors" |
| 11 |
"fmt" |
| 12 |
"io" |
| 13 |
"net" |
| 14 |
"net/http" |
| 15 |
"net/url" |
| 16 |
"strconv" |
| 17 |
"strings" |
| 18 |
"sync" |
| 19 |
"time" |
| 20 |
|
| 21 |
colmetricpb "go.opentelemetry.io/proto/otlp/collector/metrics/v1" |
| 22 |
metricpb "go.opentelemetry.io/proto/otlp/metrics/v1" |
| 23 |
"google.golang.org/protobuf/proto" |
| 24 |
|
| 25 |
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp/internal" |
| 26 |
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf" |
| 27 |
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp/internal/retry" |
| 28 |
) |
| 29 |
|
| 30 |
type client struct { |
| 31 |
// req is cloned for every upload the client makes. |
| 32 |
req *http.Request |
| 33 |
compression Compression |
| 34 |
requestFunc retry.RequestFunc |
| 35 |
httpClient *http.Client |
| 36 |
} |
| 37 |
|
| 38 |
// Keep it in sync with golang's DefaultTransport from net/http! We |
| 39 |
// have our own copy to avoid handling a situation where the |
| 40 |
// DefaultTransport is overwritten with some different implementation |
| 41 |
// of http.RoundTripper or it's modified by another package. |
| 42 |
var ourTransport = &http.Transport{ |
| 43 |
Proxy: http.ProxyFromEnvironment, |
| 44 |
DialContext: (&net.Dialer{ |
| 45 |
Timeout: 30 * time.Second, |
| 46 |
KeepAlive: 30 * time.Second, |
| 47 |
}).DialContext, |
| 48 |
ForceAttemptHTTP2: true, |
| 49 |
MaxIdleConns: 100, |
| 50 |
IdleConnTimeout: 90 * time.Second, |
| 51 |
TLSHandshakeTimeout: 10 * time.Second, |
| 52 |
ExpectContinueTimeout: 1 * time.Second, |
| 53 |
} |
| 54 |
|
| 55 |
var errInsecureEndpointWithTLS = errors.New("insecure HTTP endpoint cannot use TLS client configuration") |
| 56 |
|
| 57 |
// maxResponseBodySize is the maximum number of bytes to read from a response |
| 58 |
// body. It is set to 4 MiB per the OTLP specification recommendation to |
| 59 |
// mitigate excessive memory usage caused by a misconfigured or malicious |
| 60 |
// server. If exceeded, the response is treated as a not-retryable error. |
| 61 |
// This is a variable to allow tests to override it. |
| 62 |
var maxResponseBodySize int64 = 4 * 1024 * 1024 |
| 63 |
|
| 64 |
// newClient creates a new HTTP metric client. |
| 65 |
func newClient(cfg oconf.Config) (*client, error) { |
| 66 |
if cfg.Metrics.Insecure && cfg.Metrics.TLSCfg != nil { |
| 67 |
return nil, errInsecureEndpointWithTLS |
| 68 |
} |
| 69 |
|
| 70 |
httpClient := cfg.Metrics.HTTPClient |
| 71 |
if httpClient == nil { |
| 72 |
httpClient = &http.Client{ |
| 73 |
Transport: ourTransport, |
| 74 |
Timeout: cfg.Metrics.Timeout, |
| 75 |
} |
| 76 |
|
| 77 |
if cfg.Metrics.TLSCfg != nil || cfg.Metrics.Proxy != nil { |
| 78 |
clonedTransport := ourTransport.Clone() |
| 79 |
httpClient.Transport = clonedTransport |
| 80 |
|
| 81 |
if cfg.Metrics.TLSCfg != nil { |
| 82 |
clonedTransport.TLSClientConfig = cfg.Metrics.TLSCfg |
| 83 |
} |
| 84 |
if cfg.Metrics.Proxy != nil { |
| 85 |
clonedTransport.Proxy = cfg.Metrics.Proxy |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
u := &url.URL{ |
| 91 |
Scheme: "https", |
| 92 |
Host: cfg.Metrics.Endpoint, |
| 93 |
Path: cfg.Metrics.URLPath, |
| 94 |
} |
| 95 |
if cfg.Metrics.Insecure { |
| 96 |
u.Scheme = "http" |
| 97 |
} |
| 98 |
// Body is set when this is cloned during upload. |
| 99 |
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, u.String(), http.NoBody) |
| 100 |
if err != nil { |
| 101 |
return nil, err |
| 102 |
} |
| 103 |
|
| 104 |
userAgent := "OTel Go OTLP over HTTP/protobuf metrics exporter/" + Version() |
| 105 |
req.Header.Set("User-Agent", userAgent) |
| 106 |
|
| 107 |
if n := len(cfg.Metrics.Headers); n > 0 { |
| 108 |
for k, v := range cfg.Metrics.Headers { |
| 109 |
req.Header.Set(k, v) |
| 110 |
} |
| 111 |
} |
| 112 |
req.Header.Set("Content-Type", "application/x-protobuf") |
| 113 |
|
| 114 |
return &client{ |
| 115 |
compression: Compression(cfg.Metrics.Compression), |
| 116 |
req: req, |
| 117 |
requestFunc: cfg.RetryConfig.RequestFunc(evaluate), |
| 118 |
httpClient: httpClient, |
| 119 |
}, nil |
| 120 |
} |
| 121 |
|
| 122 |
// Shutdown shuts down the client, freeing all resources. |
| 123 |
func (c *client) Shutdown(ctx context.Context) error { |
| 124 |
// The otlpmetric.Exporter synchronizes access to client methods and |
| 125 |
// ensures this is called only once. The only thing that needs to be done |
| 126 |
// here is to release any computational resources the client holds. |
| 127 |
|
| 128 |
c.requestFunc = nil |
| 129 |
c.httpClient = nil |
| 130 |
return ctx.Err() |
| 131 |
} |
| 132 |
|
| 133 |
// UploadMetrics sends protoMetrics to the connected endpoint. |
| 134 |
// |
| 135 |
// Retryable errors from the server will be handled according to any |
| 136 |
// RetryConfig the client was created with. |
| 137 |
func (c *client) UploadMetrics(ctx context.Context, protoMetrics *metricpb.ResourceMetrics) (uploadErr error) { |
| 138 |
// The otlpmetric.Exporter synchronizes access to client methods, and |
| 139 |
// ensures this is not called after the Exporter is shutdown. Only thing |
| 140 |
// to do here is send data. |
| 141 |
|
| 142 |
pbRequest := &colmetricpb.ExportMetricsServiceRequest{ |
| 143 |
ResourceMetrics: []*metricpb.ResourceMetrics{protoMetrics}, |
| 144 |
} |
| 145 |
body, err := proto.Marshal(pbRequest) |
| 146 |
if err != nil { |
| 147 |
return err |
| 148 |
} |
| 149 |
request, err := c.newRequest(ctx, body) |
| 150 |
if err != nil { |
| 151 |
return err |
| 152 |
} |
| 153 |
|
| 154 |
return errors.Join(uploadErr, c.requestFunc(ctx, func(iCtx context.Context) error { |
| 155 |
select { |
| 156 |
case <-iCtx.Done(): |
| 157 |
return iCtx.Err() |
| 158 |
default: |
| 159 |
} |
| 160 |
|
| 161 |
request.reset(iCtx) |
| 162 |
// nolint:gosec // URL is constructed from validated OTLP endpoint configuration |
| 163 |
resp, err := c.httpClient.Do(request.Request) |
| 164 |
var urlErr *url.Error |
| 165 |
if errors.As(err, &urlErr) && urlErr.Temporary() { |
| 166 |
return newResponseError(http.Header{}, err) |
| 167 |
} |
| 168 |
if err != nil { |
| 169 |
return err |
| 170 |
} |
| 171 |
if resp != nil && resp.Body != nil { |
| 172 |
defer func() { |
| 173 |
if err := resp.Body.Close(); err != nil { |
| 174 |
uploadErr = errors.Join(uploadErr, err) |
| 175 |
} |
| 176 |
}() |
| 177 |
} |
| 178 |
|
| 179 |
if sc := resp.StatusCode; sc >= 200 && sc <= 299 { |
| 180 |
// Success, do not retry. |
| 181 |
|
| 182 |
// Read the partial success message, if any. |
| 183 |
var respData bytes.Buffer |
| 184 |
if _, err := io.Copy(&respData, http.MaxBytesReader(nil, resp.Body, maxResponseBodySize)); err != nil { |
| 185 |
var maxBytesErr *http.MaxBytesError |
| 186 |
if errors.As(err, &maxBytesErr) { |
| 187 |
return fmt.Errorf("response body too large: exceeded %d bytes", maxBytesErr.Limit) |
| 188 |
} |
| 189 |
return err |
| 190 |
} |
| 191 |
if respData.Len() == 0 { |
| 192 |
return nil |
| 193 |
} |
| 194 |
|
| 195 |
if resp.Header.Get("Content-Type") == "application/x-protobuf" { |
| 196 |
var respProto colmetricpb.ExportMetricsServiceResponse |
| 197 |
if err := proto.Unmarshal(respData.Bytes(), &respProto); err != nil { |
| 198 |
return err |
| 199 |
} |
| 200 |
|
| 201 |
if respProto.PartialSuccess != nil { |
| 202 |
msg := respProto.PartialSuccess.GetErrorMessage() |
| 203 |
n := respProto.PartialSuccess.GetRejectedDataPoints() |
| 204 |
if n != 0 || msg != "" { |
| 205 |
err := internal.MetricPartialSuccessError(n, msg) |
| 206 |
uploadErr = errors.Join(uploadErr, err) |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
return nil |
| 211 |
} |
| 212 |
// Error cases. |
| 213 |
|
| 214 |
// server may return a message with the response |
| 215 |
// body, so we read it to include in the error |
| 216 |
// message to be returned. It will help in |
| 217 |
// debugging the actual issue. |
| 218 |
var respData bytes.Buffer |
| 219 |
if _, err := io.Copy(&respData, http.MaxBytesReader(nil, resp.Body, maxResponseBodySize)); err != nil { |
| 220 |
var maxBytesErr *http.MaxBytesError |
| 221 |
if errors.As(err, &maxBytesErr) { |
| 222 |
return fmt.Errorf("response body too large: exceeded %d bytes", maxBytesErr.Limit) |
| 223 |
} |
| 224 |
return err |
| 225 |
} |
| 226 |
respStr := strings.TrimSpace(respData.String()) |
| 227 |
if respStr == "" { |
| 228 |
respStr = "(empty)" |
| 229 |
} |
| 230 |
bodyErr := fmt.Errorf("body: %s", respStr) |
| 231 |
|
| 232 |
switch resp.StatusCode { |
| 233 |
case http.StatusTooManyRequests, |
| 234 |
http.StatusBadGateway, |
| 235 |
http.StatusServiceUnavailable, |
| 236 |
http.StatusGatewayTimeout: |
| 237 |
// Retryable failure. |
| 238 |
return newResponseError(resp.Header, bodyErr) |
| 239 |
default: |
| 240 |
// Non-retryable failure. |
| 241 |
return fmt.Errorf("failed to send metrics to %s: %s (%w)", request.URL, resp.Status, bodyErr) |
| 242 |
} |
| 243 |
})) |
| 244 |
} |
| 245 |
|
| 246 |
var gzPool = sync.Pool{ |
| 247 |
New: func() any { |
| 248 |
w := gzip.NewWriter(io.Discard) |
| 249 |
return w |
| 250 |
}, |
| 251 |
} |
| 252 |
|
| 253 |
func (c *client) newRequest(ctx context.Context, body []byte) (request, error) { |
| 254 |
r := c.req.Clone(ctx) |
| 255 |
req := request{Request: r} |
| 256 |
|
| 257 |
switch c.compression { |
| 258 |
case NoCompression: |
| 259 |
r.ContentLength = int64(len(body)) |
| 260 |
req.bodyReader = bodyReader(body) |
| 261 |
req.GetBody = bodyReaderErr(body) |
| 262 |
case GzipCompression: |
| 263 |
// Ensure the content length is not used. |
| 264 |
r.ContentLength = -1 |
| 265 |
r.Header.Set("Content-Encoding", "gzip") |
| 266 |
|
| 267 |
gz := gzPool.Get().(*gzip.Writer) |
| 268 |
defer gzPool.Put(gz) |
| 269 |
|
| 270 |
var b bytes.Buffer |
| 271 |
gz.Reset(&b) |
| 272 |
|
| 273 |
if _, err := gz.Write(body); err != nil { |
| 274 |
return req, err |
| 275 |
} |
| 276 |
// Close needs to be called to ensure body is fully written. |
| 277 |
if err := gz.Close(); err != nil { |
| 278 |
return req, err |
| 279 |
} |
| 280 |
|
| 281 |
req.bodyReader = bodyReader(b.Bytes()) |
| 282 |
req.GetBody = bodyReaderErr(body) |
| 283 |
} |
| 284 |
|
| 285 |
return req, nil |
| 286 |
} |
| 287 |
|
| 288 |
// bodyReader returns a closure returning a new reader for buf. |
| 289 |
func bodyReader(buf []byte) func() io.ReadCloser { |
| 290 |
return func() io.ReadCloser { |
| 291 |
return io.NopCloser(bytes.NewReader(buf)) |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
// bodyReaderErr returns a closure returning a new reader for buf. |
| 296 |
func bodyReaderErr(buf []byte) func() (io.ReadCloser, error) { |
| 297 |
return func() (io.ReadCloser, error) { |
| 298 |
return io.NopCloser(bytes.NewReader(buf)), nil |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
// request wraps an http.Request with a resettable body reader. |
| 303 |
type request struct { |
| 304 |
*http.Request |
| 305 |
|
| 306 |
// bodyReader allows the same body to be used for multiple requests. |
| 307 |
bodyReader func() io.ReadCloser |
| 308 |
} |
| 309 |
|
| 310 |
// reset reinitializes the request Body and uses ctx for the request. |
| 311 |
func (r *request) reset(ctx context.Context) { |
| 312 |
r.Body = r.bodyReader() |
| 313 |
r.Request = r.WithContext(ctx) |
| 314 |
} |
| 315 |
|
| 316 |
// retryableError represents a request failure that can be retried. |
| 317 |
type retryableError struct { |
| 318 |
throttle int64 |
| 319 |
err error |
| 320 |
} |
| 321 |
|
| 322 |
// newResponseError returns a retryableError and will extract any explicit |
| 323 |
// throttle delay contained in headers. The returned error wraps wrapped |
| 324 |
// if it is not nil. |
| 325 |
func newResponseError(header http.Header, wrapped error) error { |
| 326 |
var rErr retryableError |
| 327 |
if v := header.Get("Retry-After"); v != "" { |
| 328 |
if t, err := strconv.ParseInt(v, 10, 64); err == nil { |
| 329 |
rErr.throttle = t |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
rErr.err = wrapped |
| 334 |
return rErr |
| 335 |
} |
| 336 |
|
| 337 |
func (e retryableError) Error() string { |
| 338 |
if e.err != nil { |
| 339 |
return "retry-able request failure: " + e.err.Error() |
| 340 |
} |
| 341 |
|
| 342 |
return "retry-able request failure" |
| 343 |
} |
| 344 |
|
| 345 |
func (e retryableError) Unwrap() error { |
| 346 |
return e.err |
| 347 |
} |
| 348 |
|
| 349 |
func (e retryableError) As(target any) bool { |
| 350 |
if e.err == nil { |
| 351 |
return false |
| 352 |
} |
| 353 |
|
| 354 |
switch v := target.(type) { |
| 355 |
case **retryableError: |
| 356 |
*v = &e |
| 357 |
return true |
| 358 |
default: |
| 359 |
return false |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
// evaluate returns if err is retry-able. If it is and it includes an explicit |
| 364 |
// throttling delay, that delay is also returned. |
| 365 |
func evaluate(err error) (bool, time.Duration) { |
| 366 |
if err == nil { |
| 367 |
return false, 0 |
| 368 |
} |
| 369 |
|
| 370 |
// Do not use errors.As here, this should only be flattened one layer. If |
| 371 |
// there are several chained errors, all the errors above it will be |
| 372 |
// discarded if errors.As is used instead. |
| 373 |
rErr, ok := err.(retryableError) //nolint:errorlint |
| 374 |
if !ok { |
| 375 |
return false, 0 |
| 376 |
} |
| 377 |
|
| 378 |
return true, time.Duration(rErr.throttle) |
| 379 |
} |