Debian Codegraph @

exporters/otlp/otlptrace/otlptracehttp/client.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 "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 coltracepb "go.opentelemetry.io/proto/otlp/collector/trace/v1"
22 tracepb "go.opentelemetry.io/proto/otlp/trace/v1"
23 "google.golang.org/protobuf/proto"
24
25 "go.opentelemetry.io/otel/exporters/otlp/otlptrace"
26 "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp/internal"
27 "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp/internal/counter"
28 "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp/internal/observ"
29 "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig"
30 "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp/internal/retry"
31 )
32
33 const contentTypeProto = "application/x-protobuf"
34
35 // maxResponseBodySize is the maximum number of bytes to read from a response
36 // body. It is set to 4 MiB per the OTLP specification recommendation to
37 // mitigate excessive memory usage caused by a misconfigured or malicious
38 // server. If exceeded, the response is treated as a not-retryable error.
39 // This is a variable to allow tests to override it.
40 var maxResponseBodySize int64 = 4 * 1024 * 1024
41
42 var gzPool = sync.Pool{
43 New: func() any {
44 w := gzip.NewWriter(io.Discard)
45 return w
46 },
47 }
48
49 // Keep it in sync with golang's DefaultTransport from net/http! We
50 // have our own copy to avoid handling a situation where the
51 // DefaultTransport is overwritten with some different implementation
52 // of http.RoundTripper or it's modified by other package.
53 var ourTransport = &http.Transport{
54 Proxy: http.ProxyFromEnvironment,
55 DialContext: (&net.Dialer{
56 Timeout: 30 * time.Second,
57 KeepAlive: 30 * time.Second,
58 }).DialContext,
59 ForceAttemptHTTP2: true,
60 MaxIdleConns: 100,
61 IdleConnTimeout: 90 * time.Second,
62 TLSHandshakeTimeout: 10 * time.Second,
63 ExpectContinueTimeout: 1 * time.Second,
64 }
65
66 var errInsecureEndpointWithTLS = errors.New("insecure HTTP endpoint cannot use TLS client configuration")
67
68 type client struct {
69 name string
70 cfg otlpconfig.SignalConfig
71 generalCfg otlpconfig.Config
72 requestFunc retry.RequestFunc
73 client *http.Client
74 stopCh chan struct{}
75 stopOnce sync.Once
76
77 instID int64
78 inst *observ.Instrumentation
79 }
80
81 var _ otlptrace.Client = (*client)(nil)
82
83 // NewClient creates a new HTTP trace client.
84 func NewClient(opts ...Option) otlptrace.Client {
85 cfg := otlpconfig.NewHTTPConfig(asHTTPOptions(opts)...)
86
87 httpClient := cfg.Traces.HTTPClient
88
89 if httpClient == nil {
90 httpClient = &http.Client{
91 Transport: ourTransport,
92 Timeout: cfg.Traces.Timeout,
93 }
94
95 if cfg.Traces.TLSCfg != nil || cfg.Traces.Proxy != nil {
96 clonedTransport := ourTransport.Clone()
97 httpClient.Transport = clonedTransport
98
99 if cfg.Traces.TLSCfg != nil {
100 clonedTransport.TLSClientConfig = cfg.Traces.TLSCfg
101 }
102 if cfg.Traces.Proxy != nil {
103 clonedTransport.Proxy = cfg.Traces.Proxy
104 }
105 }
106 }
107
108 stopCh := make(chan struct{})
109 return &client{
110 name: "traces",
111 cfg: cfg.Traces,
112 generalCfg: cfg,
113 requestFunc: cfg.RetryConfig.RequestFunc(evaluate),
114 stopCh: stopCh,
115 client: httpClient,
116 instID: counter.NextExporterID(),
117 }
118 }
119
120 // Start does nothing in a HTTP client.
121 func (c *client) Start(ctx context.Context) error {
122 if c.cfg.Insecure && c.cfg.TLSCfg != nil {
123 return errInsecureEndpointWithTLS
124 }
125
126 // Initialize the instrumentation if not already done.
127 //
128 // Initialize here instead of NewClient to allow any errors to be passed
129 // back to the caller and so that any setup of the environment variables to
130 // enable instrumentation can be set via code.
131 var err error
132 if c.inst == nil {
133 c.inst, err = observ.NewInstrumentation(c.instID, c.cfg.Endpoint)
134 }
135
136 // nothing to do
137 select {
138 case <-ctx.Done():
139 err = errors.Join(err, ctx.Err())
140 default:
141 }
142 return err
143 }
144
145 // Stop shuts down the client and interrupt any in-flight request.
146 func (d *client) Stop(ctx context.Context) error {
147 d.stopOnce.Do(func() {
148 close(d.stopCh)
149 })
150 select {
151 case <-ctx.Done():
152 return ctx.Err()
153 default:
154 }
155 return nil
156 }
157
158 // UploadTraces sends a batch of spans to the collector.
159 func (d *client) UploadTraces(ctx context.Context, protoSpans []*tracepb.ResourceSpans) (uploadErr error) {
160 pbRequest := &coltracepb.ExportTraceServiceRequest{
161 ResourceSpans: protoSpans,
162 }
163 rawRequest, err := proto.Marshal(pbRequest)
164 if err != nil {
165 return err
166 }
167
168 ctx, cancel := d.contextWithStop(ctx)
169 defer cancel()
170
171 request, err := d.newRequest(rawRequest)
172 if err != nil {
173 return err
174 }
175
176 var statusCode int
177 if d.inst != nil {
178 op := d.inst.ExportSpans(ctx, len(protoSpans))
179 defer func() { op.End(uploadErr, statusCode) }()
180 }
181
182 return errors.Join(uploadErr, d.requestFunc(ctx, func(ctx context.Context) error {
183 select {
184 case <-ctx.Done():
185 return ctx.Err()
186 default:
187 }
188
189 request.reset(ctx)
190 // nolint:gosec // URL is constructed from validated OTLP endpoint configuration
191 resp, err := d.client.Do(request.Request)
192 var urlErr *url.Error
193 if errors.As(err, &urlErr) && urlErr.Temporary() {
194 return newResponseError(http.Header{}, err)
195 }
196 if err != nil {
197 return err
198 }
199
200 if resp != nil && resp.Body != nil {
201 defer func() {
202 if err := resp.Body.Close(); err != nil {
203 uploadErr = errors.Join(uploadErr, err)
204 }
205 }()
206 }
207
208 statusCode = resp.StatusCode
209 if statusCode >= 200 && statusCode <= 299 {
210 // Success, do not retry.
211 // Read the partial success message, if any.
212 var respData bytes.Buffer
213 if _, err := io.Copy(&respData, http.MaxBytesReader(nil, resp.Body, maxResponseBodySize)); err != nil {
214 var maxBytesErr *http.MaxBytesError
215 if errors.As(err, &maxBytesErr) {
216 return fmt.Errorf("response body too large: exceeded %d bytes", maxBytesErr.Limit)
217 }
218 return err
219 }
220 if respData.Len() == 0 {
221 return nil
222 }
223
224 if resp.Header.Get("Content-Type") == "application/x-protobuf" {
225 var respProto coltracepb.ExportTraceServiceResponse
226 if err := proto.Unmarshal(respData.Bytes(), &respProto); err != nil {
227 return err
228 }
229
230 if respProto.PartialSuccess != nil {
231 msg := respProto.PartialSuccess.GetErrorMessage()
232 n := respProto.PartialSuccess.GetRejectedSpans()
233 if n != 0 || msg != "" {
234 err := internal.TracePartialSuccessError(n, msg)
235 uploadErr = errors.Join(uploadErr, err)
236 }
237 }
238 }
239 return nil
240 }
241 // Error cases.
242
243 // server may return a message with the response
244 // body, so we read it to include in the error
245 // message to be returned. It will help in
246 // debugging the actual issue.
247 var respData bytes.Buffer
248 if _, err := io.Copy(&respData, http.MaxBytesReader(nil, resp.Body, maxResponseBodySize)); err != nil {
249 var maxBytesErr *http.MaxBytesError
250 if errors.As(err, &maxBytesErr) {
251 return fmt.Errorf("response body too large: exceeded %d bytes", maxBytesErr.Limit)
252 }
253 return err
254 }
255 respStr := strings.TrimSpace(respData.String())
256 if respStr == "" {
257 respStr = "(empty)"
258 }
259 bodyErr := fmt.Errorf("body: %s", respStr)
260
261 switch statusCode {
262 case http.StatusTooManyRequests,
263 http.StatusBadGateway,
264 http.StatusServiceUnavailable,
265 http.StatusGatewayTimeout:
266 // Retryable failure.
267 return newResponseError(resp.Header, bodyErr)
268 default:
269 // Non-retryable failure.
270 return fmt.Errorf("failed to send to %s: %s (%w)", request.URL, resp.Status, bodyErr)
271 }
272 }))
273 }
274
275 func (d *client) newRequest(body []byte) (request, error) {
276 u := url.URL{Scheme: d.getScheme(), Host: d.cfg.Endpoint, Path: d.cfg.URLPath}
277 r, err := http.NewRequestWithContext(context.Background(), http.MethodPost, u.String(), http.NoBody)
278 if err != nil {
279 return request{Request: r}, err
280 }
281
282 userAgent := "OTel OTLP Exporter Go/" + otlptrace.Version()
283 r.Header.Set("User-Agent", userAgent)
284
285 for k, v := range d.cfg.Headers {
286 r.Header.Set(k, v)
287 }
288 r.Header.Set("Content-Type", contentTypeProto)
289
290 req := request{Request: r}
291 switch Compression(d.cfg.Compression) {
292 case NoCompression:
293 r.ContentLength = int64(len(body))
294 req.bodyReader = bodyReader(body)
295 req.GetBody = bodyReaderErr(body)
296 case GzipCompression:
297 // Ensure the content length is not used.
298 r.ContentLength = -1
299 r.Header.Set("Content-Encoding", "gzip")
300
301 gz := gzPool.Get().(*gzip.Writer)
302 defer gzPool.Put(gz)
303
304 var b bytes.Buffer
305 gz.Reset(&b)
306
307 if _, err := gz.Write(body); err != nil {
308 return req, err
309 }
310 // Close needs to be called to ensure body is fully written.
311 if err := gz.Close(); err != nil {
312 return req, err
313 }
314
315 req.bodyReader = bodyReader(b.Bytes())
316 req.GetBody = bodyReaderErr(b.Bytes())
317 }
318
319 return req, nil
320 }
321
322 // MarshalLog is the marshaling function used by the logging system to represent this Client.
323 func (d *client) MarshalLog() any {
324 return struct {
325 Type string
326 Endpoint string
327 Insecure bool
328 }{
329 Type: "otlptracehttp",
330 Endpoint: d.cfg.Endpoint,
331 Insecure: d.cfg.Insecure,
332 }
333 }
334
335 // bodyReader returns a closure returning a new reader for buf.
336 func bodyReader(buf []byte) func() io.ReadCloser {
337 return func() io.ReadCloser {
338 return io.NopCloser(bytes.NewReader(buf))
339 }
340 }
341
342 // bodyReaderErr returns a closure returning a new reader for buf.
343 func bodyReaderErr(buf []byte) func() (io.ReadCloser, error) {
344 return func() (io.ReadCloser, error) {
345 return io.NopCloser(bytes.NewReader(buf)), nil
346 }
347 }
348
349 // request wraps an http.Request with a resettable body reader.
350 type request struct {
351 *http.Request
352
353 // bodyReader allows the same body to be used for multiple requests.
354 bodyReader func() io.ReadCloser
355 }
356
357 // reset reinitializes the request Body and uses ctx for the request.
358 func (r *request) reset(ctx context.Context) {
359 r.Body = r.bodyReader()
360 r.Request = r.WithContext(ctx)
361 }
362
363 // retryableError represents a request failure that can be retried.
364 type retryableError struct {
365 throttle int64
366 err error
367 }
368
369 // newResponseError returns a retryableError and will extract any explicit
370 // throttle delay contained in headers. The returned error wraps wrapped
371 // if it is not nil.
372 func newResponseError(header http.Header, wrapped error) error {
373 var rErr retryableError
374 if s, ok := header["Retry-After"]; ok {
375 if t, err := strconv.ParseInt(s[0], 10, 64); err == nil {
376 rErr.throttle = t
377 }
378 }
379
380 rErr.err = wrapped
381 return rErr
382 }
383
384 func (e retryableError) Error() string {
385 if e.err != nil {
386 return "retry-able request failure: " + e.err.Error()
387 }
388
389 return "retry-able request failure"
390 }
391
392 func (e retryableError) Unwrap() error {
393 return e.err
394 }
395
396 func (e retryableError) As(target any) bool {
397 if e.err == nil {
398 return false
399 }
400
401 switch v := target.(type) {
402 case **retryableError:
403 *v = &e
404 return true
405 default:
406 return false
407 }
408 }
409
410 // evaluate returns if err is retry-able. If it is and it includes an explicit
411 // throttling delay, that delay is also returned.
412 func evaluate(err error) (bool, time.Duration) {
413 if err == nil {
414 return false, 0
415 }
416
417 // Do not use errors.As here, this should only be flattened one layer. If
418 // there are several chained errors, all the errors above it will be
419 // discarded if errors.As is used instead.
420 rErr, ok := err.(retryableError) //nolint:errorlint
421 if !ok {
422 return false, 0
423 }
424
425 return true, time.Duration(rErr.throttle)
426 }
427
428 func (d *client) getScheme() string {
429 if d.cfg.Insecure {
430 return "http"
431 }
432 return "https"
433 }
434
435 func (d *client) contextWithStop(ctx context.Context) (context.Context, context.CancelFunc) {
436 // Unify the parent context Done signal with the client's stop
437 // channel.
438 ctx, cancel := context.WithCancel(ctx)
439 go func(ctx context.Context, cancel context.CancelFunc) {
440 select {
441 case <-ctx.Done():
442 // Nothing to do, either cancelled or deadline
443 // happened.
444 case <-d.stopCh:
445 cancel()
446 }
447 }(ctx, cancel)
448 return ctx, cancel
449 }