Debian Codegraph @

exporters/otlp/otlplog/otlploghttp/client_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package otlploghttp
5
6 import (
7 "bytes"
8 "compress/gzip"
9 "context"
10 "crypto/ecdsa"
11 "crypto/elliptic"
12 "crypto/rand"
13 "crypto/tls"
14 "crypto/x509"
15 "crypto/x509/pkix"
16 "encoding/pem"
17 "errors"
18 "fmt"
19 "io"
20 "math/big"
21 "net"
22 "net/http"
23 "net/http/httptest"
24 "net/url"
25 "strings"
26 "sync"
27 "testing"
28 "time"
29
30 "github.com/google/go-cmp/cmp"
31 "github.com/stretchr/testify/assert"
32 "github.com/stretchr/testify/require"
33 collogpb "go.opentelemetry.io/proto/otlp/collector/logs/v1"
34 cpb "go.opentelemetry.io/proto/otlp/common/v1"
35 lpb "go.opentelemetry.io/proto/otlp/logs/v1"
36 rpb "go.opentelemetry.io/proto/otlp/resource/v1"
37 "google.golang.org/protobuf/proto"
38
39 "go.opentelemetry.io/otel"
40
41 "go.opentelemetry.io/otel/attribute"
42 "go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp/internal"
43 "go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp/internal/observ"
44
45 "go.opentelemetry.io/otel/sdk/instrumentation"
46 "go.opentelemetry.io/otel/sdk/log"
47 "go.opentelemetry.io/otel/sdk/metric"
48 "go.opentelemetry.io/otel/sdk/metric/metricdata"
49 "go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest"
50 semconv "go.opentelemetry.io/otel/semconv/v1.40.0"
51 "go.opentelemetry.io/otel/semconv/v1.40.0/otelconv"
52 )
53
54 var (
55 // Sat Jan 01 2000 00:00:00 GMT+0000.
56 ts = time.Date(2000, time.January, 0o1, 0, 0, 0, 0, time.FixedZone("GMT", 0))
57 obs = ts.Add(30 * time.Second)
58
59 kvAlice = &cpb.KeyValue{Key: "user", Value: &cpb.AnyValue{
60 Value: &cpb.AnyValue_StringValue{StringValue: "alice"},
61 }}
62 kvBob = &cpb.KeyValue{Key: "user", Value: &cpb.AnyValue{
63 Value: &cpb.AnyValue_StringValue{StringValue: "bob"},
64 }}
65 kvSrvName = &cpb.KeyValue{Key: "service.name", Value: &cpb.AnyValue{
66 Value: &cpb.AnyValue_StringValue{StringValue: "test server"},
67 }}
68 kvSrvVer = &cpb.KeyValue{Key: "service.version", Value: &cpb.AnyValue{
69 Value: &cpb.AnyValue_StringValue{StringValue: "v0.1.0"},
70 }}
71
72 pbSevA = lpb.SeverityNumber_SEVERITY_NUMBER_INFO
73 pbSevB = lpb.SeverityNumber_SEVERITY_NUMBER_ERROR
74
75 pbBodyA = &cpb.AnyValue{
76 Value: &cpb.AnyValue_StringValue{
77 StringValue: "a",
78 },
79 }
80 pbBodyB = &cpb.AnyValue{
81 Value: &cpb.AnyValue_StringValue{
82 StringValue: "b",
83 },
84 }
85
86 spanIDA = []byte{0, 0, 0, 0, 0, 0, 0, 1}
87 spanIDB = []byte{0, 0, 0, 0, 0, 0, 0, 2}
88 traceIDA = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
89 traceIDB = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}
90 flagsA = byte(1)
91 flagsB = byte(0)
92
93 logRecords = []*lpb.LogRecord{
94 {
95 TimeUnixNano: uint64(ts.UnixNano()),
96 ObservedTimeUnixNano: uint64(obs.UnixNano()),
97 SeverityNumber: pbSevA,
98 SeverityText: "A",
99 Body: pbBodyA,
100 Attributes: []*cpb.KeyValue{kvAlice},
101 Flags: uint32(flagsA),
102 TraceId: traceIDA,
103 SpanId: spanIDA,
104 },
105 {
106 TimeUnixNano: uint64(ts.UnixNano()),
107 ObservedTimeUnixNano: uint64(obs.UnixNano()),
108 SeverityNumber: pbSevA,
109 SeverityText: "A",
110 Body: pbBodyA,
111 Attributes: []*cpb.KeyValue{kvBob},
112 Flags: uint32(flagsA),
113 TraceId: traceIDA,
114 SpanId: spanIDA,
115 },
116 {
117 TimeUnixNano: uint64(ts.UnixNano()),
118 ObservedTimeUnixNano: uint64(obs.UnixNano()),
119 SeverityNumber: pbSevB,
120 SeverityText: "B",
121 Body: pbBodyB,
122 Attributes: []*cpb.KeyValue{kvAlice},
123 Flags: uint32(flagsB),
124 TraceId: traceIDB,
125 SpanId: spanIDB,
126 },
127 {
128 TimeUnixNano: uint64(ts.UnixNano()),
129 ObservedTimeUnixNano: uint64(obs.UnixNano()),
130 SeverityNumber: pbSevB,
131 SeverityText: "B",
132 Body: pbBodyB,
133 Attributes: []*cpb.KeyValue{kvBob},
134 Flags: uint32(flagsB),
135 TraceId: traceIDB,
136 SpanId: spanIDB,
137 },
138 }
139
140 scope = &cpb.InstrumentationScope{
141 Name: "test/code/path",
142 Version: "v0.1.0",
143 }
144 scopeLogs = []*lpb.ScopeLogs{
145 {
146 Scope: scope,
147 LogRecords: logRecords,
148 SchemaUrl: semconv.SchemaURL,
149 },
150 }
151
152 res = &rpb.Resource{
153 Attributes: []*cpb.KeyValue{kvSrvName, kvSrvVer},
154 }
155 resourceLogs = []*lpb.ResourceLogs{{
156 Resource: res,
157 ScopeLogs: scopeLogs,
158 SchemaUrl: semconv.SchemaURL,
159 }}
160 )
161
162 type exportResult struct {
163 Response *collogpb.ExportLogsServiceResponse
164 Err error
165 }
166
167 // storage stores uploaded OTLP log data in their proto form.
168 type storage struct {
169 dataMu sync.Mutex
170 data []*lpb.ResourceLogs
171 }
172
173 // newStorage returns a configure storage ready to store received requests.
174 func newStorage() *storage {
175 return &storage{}
176 }
177
178 // Add adds the request to the Storage.
179 func (s *storage) Add(request *collogpb.ExportLogsServiceRequest) {
180 s.dataMu.Lock()
181 defer s.dataMu.Unlock()
182 s.data = append(s.data, request.ResourceLogs...)
183 }
184
185 // Dump returns all added ResourceLogs and clears the storage.
186 func (s *storage) Dump() []*lpb.ResourceLogs {
187 s.dataMu.Lock()
188 defer s.dataMu.Unlock()
189
190 var data []*lpb.ResourceLogs
191 data, s.data = s.data, []*lpb.ResourceLogs{}
192 return data
193 }
194
195 var emptyExportLogsServiceResponse = func() []byte {
196 body := collogpb.ExportLogsServiceResponse{}
197 r, err := proto.Marshal(&body)
198 if err != nil {
199 panic(err)
200 }
201 return r
202 }()
203
204 type httpResponseError struct {
205 Err error
206 Status int
207 Header http.Header
208 }
209
210 func (e *httpResponseError) Error() string {
211 return fmt.Sprintf("%d: %s", e.Status, e.Err)
212 }
213
214 func (e *httpResponseError) Unwrap() error { return e.Err }
215
216 // httpCollector is an OTLP HTTP server that collects all requests it receives.
217 type httpCollector struct {
218 plainTextResponse bool
219
220 headersMu sync.Mutex
221 headers http.Header
222 storage *storage
223
224 resultCh <-chan exportResult
225 listener net.Listener
226 srv *http.Server
227 }
228
229 // newHTTPCollector returns a *HTTPCollector that is listening at the provided
230 // endpoint.
231 //
232 // If endpoint is an empty string, the returned collector will be listening on
233 // the localhost interface at an OS chosen port, not use TLS, and listen at the
234 // default OTLP log endpoint path ("/v1/logs"). If the endpoint contains a
235 // prefix of "https" the server will generate weak self-signed TLS certificates
236 // and use them to server data. If the endpoint contains a path, that path will
237 // be used instead of the default OTLP metric endpoint path.
238 //
239 // If errCh is not nil, the collector will respond to HTTP requests with errors
240 // sent on that channel. This means that if errCh is not nil Export calls will
241 // block until an error is received.
242 func newHTTPCollector(
243 endpoint string,
244 resultCh <-chan exportResult,
245 opts ...func(*httpCollector),
246 ) (*httpCollector, error) {
247 u, err := url.Parse(endpoint)
248 if err != nil {
249 return nil, err
250 }
251 if u.Host == "" {
252 u.Host = "localhost:0"
253 }
254 if u.Path == "" {
255 u.Path = defaultPath
256 }
257
258 c := &httpCollector{
259 headers: http.Header{},
260 storage: newStorage(),
261 resultCh: resultCh,
262 }
263 for _, opt := range opts {
264 opt(c)
265 }
266
267 c.listener, err = (&net.ListenConfig{}).Listen(context.Background(), "tcp", u.Host)
268 if err != nil {
269 return nil, err
270 }
271
272 mux := http.NewServeMux()
273 mux.Handle(u.Path, http.HandlerFunc(c.handler))
274 c.srv = &http.Server{
275 Handler: mux,
276 ReadTimeout: 10 * time.Second,
277 WriteTimeout: 10 * time.Second,
278 }
279 if u.Scheme == "https" {
280 cert, err := newWeakCertificate()
281 if err != nil {
282 return nil, err
283 }
284 c.srv.TLSConfig = &tls.Config{
285 Certificates: []tls.Certificate{cert},
286 }
287 go func() { _ = c.srv.ServeTLS(c.listener, "", "") }()
288 } else {
289 go func() { _ = c.srv.Serve(c.listener) }()
290 }
291 return c, nil
292 }
293
294 // withHTTPCollectorRespondingPlainText makes the HTTPCollector return
295 // a plaintext, instead of protobuf, response.
296 func withHTTPCollectorRespondingPlainText() func(*httpCollector) {
297 return func(s *httpCollector) {
298 s.plainTextResponse = true
299 }
300 }
301
302 // Shutdown shuts down the HTTP server closing all open connections and
303 // listeners.
304 func (c *httpCollector) Shutdown(ctx context.Context) error {
305 return c.srv.Shutdown(ctx)
306 }
307
308 // Addr returns the net.Addr c is listening at.
309 func (c *httpCollector) Addr() net.Addr {
310 return c.listener.Addr()
311 }
312
313 // Collect returns the Storage holding all collected requests.
314 func (c *httpCollector) Collect() *storage {
315 return c.storage
316 }
317
318 // Headers returns the headers received for all requests.
319 func (c *httpCollector) Headers() map[string][]string {
320 // Makes a copy.
321 c.headersMu.Lock()
322 defer c.headersMu.Unlock()
323 return c.headers.Clone()
324 }
325
326 func (c *httpCollector) handler(w http.ResponseWriter, r *http.Request) {
327 c.respond(w, c.record(r))
328 }
329
330 func (c *httpCollector) record(r *http.Request) exportResult {
331 // Currently only supports protobuf.
332 if v := r.Header.Get("Content-Type"); v != "application/x-protobuf" {
333 err := fmt.Errorf("content-type not supported: %s", v)
334 return exportResult{Err: err}
335 }
336
337 body, err := c.readBody(r)
338 if err != nil {
339 return exportResult{Err: err}
340 }
341 pbRequest := &collogpb.ExportLogsServiceRequest{}
342 err = proto.Unmarshal(body, pbRequest)
343 if err != nil {
344 return exportResult{
345 Err: &httpResponseError{
346 Err: err,
347 Status: http.StatusInternalServerError,
348 },
349 }
350 }
351 c.storage.Add(pbRequest)
352
353 c.headersMu.Lock()
354 for k, vals := range r.Header {
355 for _, v := range vals {
356 c.headers.Add(k, v)
357 }
358 }
359 c.headersMu.Unlock()
360
361 if c.resultCh != nil {
362 return <-c.resultCh
363 }
364 return exportResult{Err: err}
365 }
366
367 func (*httpCollector) readBody(r *http.Request) (body []byte, err error) {
368 var reader io.ReadCloser
369 switch r.Header.Get("Content-Encoding") {
370 case "gzip":
371 reader, err = gzip.NewReader(r.Body)
372 if err != nil {
373 _ = reader.Close()
374 return nil, &httpResponseError{
375 Err: err,
376 Status: http.StatusInternalServerError,
377 }
378 }
379 default:
380 reader = r.Body
381 }
382
383 defer func() {
384 cErr := reader.Close()
385 if err == nil && cErr != nil {
386 err = &httpResponseError{
387 Err: cErr,
388 Status: http.StatusInternalServerError,
389 }
390 }
391 }()
392 body, err = io.ReadAll(reader)
393 if err != nil {
394 err = &httpResponseError{
395 Err: err,
396 Status: http.StatusInternalServerError,
397 }
398 }
399 return body, err
400 }
401
402 func (c *httpCollector) respond(w http.ResponseWriter, resp exportResult) {
403 if resp.Err != nil {
404 w.Header().Set("Content-Type", "text/plain; charset=utf-8")
405 w.Header().Set("X-Content-Type-Options", "nosniff")
406 var e *httpResponseError
407 if errors.As(resp.Err, &e) {
408 for k, vals := range e.Header {
409 for _, v := range vals {
410 w.Header().Add(k, v)
411 }
412 }
413 w.WriteHeader(e.Status)
414 fmt.Fprintln(w, e.Error())
415 } else {
416 w.WriteHeader(http.StatusBadRequest)
417 fmt.Fprintln(w, resp.Err.Error())
418 }
419 return
420 }
421
422 if c.plainTextResponse {
423 w.Header().Set("Content-Type", "text/plain; charset=utf-8")
424 w.WriteHeader(http.StatusOK)
425 _, _ = w.Write([]byte("OK"))
426 return
427 }
428
429 w.Header().Set("Content-Type", "application/x-protobuf")
430 w.WriteHeader(http.StatusOK)
431 if resp.Response == nil {
432 _, _ = w.Write(emptyExportLogsServiceResponse)
433 } else {
434 r, err := proto.Marshal(resp.Response)
435 if err != nil {
436 panic(err)
437 }
438 _, _ = w.Write(r)
439 }
440 }
441
442 // Based on https://golang.org/src/crypto/tls/generate_cert.go,
443 // simplified and weakened.
444 func newWeakCertificate() (tls.Certificate, error) {
445 priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
446 if err != nil {
447 return tls.Certificate{}, err
448 }
449 notBefore := time.Now()
450 notAfter := notBefore.Add(time.Hour)
451 m := new(big.Int).Lsh(big.NewInt(1), 128)
452 sn, err := rand.Int(rand.Reader, m)
453 if err != nil {
454 return tls.Certificate{}, err
455 }
456 tmpl := x509.Certificate{
457 SerialNumber: sn,
458 Subject: pkix.Name{Organization: []string{"otel-go"}},
459 NotBefore: notBefore,
460 NotAfter: notAfter,
461 KeyUsage: x509.KeyUsageDigitalSignature,
462 ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
463 BasicConstraintsValid: true,
464 DNSNames: []string{"localhost"},
465 IPAddresses: []net.IP{net.IPv6loopback, net.IPv4(127, 0, 0, 1)},
466 }
467 derBytes, err := x509.CreateCertificate(rand.Reader, &tmpl, &tmpl, &priv.PublicKey, priv)
468 if err != nil {
469 return tls.Certificate{}, err
470 }
471 var certBuf bytes.Buffer
472 err = pem.Encode(&certBuf, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
473 if err != nil {
474 return tls.Certificate{}, err
475 }
476 privBytes, err := x509.MarshalPKCS8PrivateKey(priv)
477 if err != nil {
478 return tls.Certificate{}, err
479 }
480 var privBuf bytes.Buffer
481 err = pem.Encode(&privBuf, &pem.Block{Type: "PRIVATE KEY", Bytes: privBytes})
482 if err != nil {
483 return tls.Certificate{}, err
484 }
485 return tls.X509KeyPair(certBuf.Bytes(), privBuf.Bytes())
486 }
487
488 func TestClient(t *testing.T) {
489 factory := func(rCh <-chan exportResult) (*client, *httpCollector) {
490 coll, err := newHTTPCollector("", rCh)
491 require.NoError(t, err)
492
493 addr := coll.Addr().String()
494 opts := []Option{WithEndpoint(addr), WithInsecure()}
495 cfg := newConfig(opts)
496 client, err := newHTTPClient(t.Context(), cfg)
497 require.NoError(t, err)
498 return client, coll
499 }
500
501 t.Run("ClientHonorsContextErrors", func(t *testing.T) {
502 ctx, cancel := context.WithCancel(t.Context())
503 t.Cleanup(cancel)
504
505 t.Run("DeadlineExceeded", func(t *testing.T) {
506 innerCtx, innerCancel := context.WithTimeout(ctx, time.Nanosecond)
507 t.Cleanup(innerCancel)
508 <-innerCtx.Done()
509
510 c, _ := factory(nil)
511 assert.ErrorIs(t, c.uploadLogs(innerCtx, nil), context.DeadlineExceeded)
512 })
513
514 t.Run("Canceled", func(t *testing.T) {
515 innerCtx, innerCancel := context.WithCancel(ctx)
516 innerCancel()
517
518 c, _ := factory(nil)
519 assert.ErrorIs(t, c.uploadLogs(innerCtx, nil), context.Canceled)
520 })
521 })
522
523 t.Run("uploadLogs", func(t *testing.T) {
524 ctx := t.Context()
525 client, coll := factory(nil)
526
527 require.NoError(t, client.uploadLogs(ctx, resourceLogs))
528 got := coll.Collect().Dump()
529 require.Len(t, got, 1, "upload of one ResourceLogs")
530 diff := cmp.Diff(got[0], resourceLogs[0], cmp.Comparer(proto.Equal))
531 if diff != "" {
532 t.Fatalf("unexpected ResourceLogs:\n%s", diff)
533 }
534 })
535
536 t.Run("PartialSuccess", func(t *testing.T) {
537 const n, msg = 2, "bad data"
538 rCh := make(chan exportResult, 3)
539 rCh <- exportResult{
540 Response: &collogpb.ExportLogsServiceResponse{
541 PartialSuccess: &collogpb.ExportLogsPartialSuccess{
542 RejectedLogRecords: n,
543 ErrorMessage: msg,
544 },
545 },
546 }
547 rCh <- exportResult{
548 Response: &collogpb.ExportLogsServiceResponse{
549 PartialSuccess: &collogpb.ExportLogsPartialSuccess{
550 // Should not be logged.
551 RejectedLogRecords: 0,
552 ErrorMessage: "",
553 },
554 },
555 }
556 rCh <- exportResult{
557 Response: &collogpb.ExportLogsServiceResponse{},
558 }
559
560 ctx := t.Context()
561 client, _ := factory(rCh)
562
563 assert.ErrorIs(t, client.UploadLogs(ctx, resourceLogs), internal.PartialSuccess{})
564 assert.NoError(t, client.UploadLogs(ctx, resourceLogs))
565 assert.NoError(t, client.UploadLogs(ctx, resourceLogs))
566 })
567 }
568
569 func TestClientWithHTTPCollectorRespondingPlainText(t *testing.T) {
570 ctx := t.Context()
571 coll, err := newHTTPCollector("", nil, withHTTPCollectorRespondingPlainText())
572 require.NoError(t, err)
573
574 addr := coll.Addr().String()
575 opts := []Option{WithEndpoint(addr), WithInsecure()}
576 cfg := newConfig(opts)
577 client, err := newHTTPClient(t.Context(), cfg)
578 require.NoError(t, err)
579
580 require.NoError(t, client.uploadLogs(ctx, make([]*lpb.ResourceLogs, 1)))
581 got := coll.Collect().Dump()
582 require.Len(t, got, 1, "upload of one ResourceLogs")
583 }
584
585 func TestNewWithInvalidEndpoint(t *testing.T) {
586 ctx := t.Context()
587 exp, err := New(ctx, WithEndpoint("host:invalid-port"))
588 assert.Error(t, err)
589 assert.Nil(t, exp)
590 }
591
592 func TestConfig(t *testing.T) {
593 factoryFunc := func(ePt string, rCh <-chan exportResult, o ...Option) (log.Exporter, *httpCollector) {
594 coll, err := newHTTPCollector(ePt, rCh)
595 require.NoError(t, err)
596
597 opts := []Option{WithEndpoint(coll.Addr().String())}
598 if !strings.HasPrefix(strings.ToLower(ePt), "https") {
599 opts = append(opts, WithInsecure())
600 }
601 opts = append(opts, o...)
602
603 ctx := t.Context()
604 exp, err := New(ctx, opts...)
605 require.NoError(t, err)
606 return exp, coll
607 }
608
609 t.Run("WithEndpointURL", func(t *testing.T) {
610 coll, err := newHTTPCollector("", nil)
611 require.NoError(t, err)
612 ctx := context.Background() //nolint:usetesting // required to avoid getting a canceled context at cleanup.
613
614 target := "http://" + coll.Addr().String() + defaultPath
615 exp, err := New(ctx, WithEndpointURL(target))
616 require.NoError(t, err)
617 t.Cleanup(func() { require.NoError(t, coll.Shutdown(ctx)) })
618 t.Cleanup(func() { require.NoError(t, exp.Shutdown(ctx)) })
619
620 assert.NoError(t, exp.Export(ctx, make([]log.Record, 1)))
621 assert.Len(t, coll.Collect().Dump(), 1)
622 })
623
624 t.Run("WithHeaders", func(t *testing.T) {
625 key := http.CanonicalHeaderKey("my-custom-header")
626 headers := map[string]string{key: "custom-value"}
627 exp, coll := factoryFunc("", nil, WithHeaders(headers))
628 ctx := context.Background() //nolint:usetesting // required to avoid getting a canceled context at cleanup.
629 t.Cleanup(func() { require.NoError(t, coll.Shutdown(ctx)) })
630 require.NoError(t, exp.Export(ctx, make([]log.Record, 1)))
631 // Ensure everything is flushed.
632 require.NoError(t, exp.Shutdown(ctx))
633
634 got := coll.Headers()
635 require.Regexp(t, "OTel Go OTLP over HTTP/protobuf logs exporter/[01]\\..*", got)
636 require.Contains(t, got, key)
637 assert.Equal(t, []string{headers[key]}, got[key])
638 })
639
640 t.Run("WithTimeout", func(t *testing.T) {
641 // Do not send on rCh so the Collector never responds to the client.
642 rCh := make(chan exportResult)
643 exp, coll := factoryFunc(
644 "",
645 rCh,
646 WithTimeout(time.Millisecond),
647 WithRetry(RetryConfig{Enabled: false}),
648 )
649 ctx := context.Background() //nolint:usetesting // required to avoid getting a canceled context at cleanup.
650 t.Cleanup(func() { require.NoError(t, coll.Shutdown(ctx)) })
651 // Push this after Shutdown so the HTTP server doesn't hang.
652 t.Cleanup(func() { close(rCh) })
653 t.Cleanup(func() { require.NoError(t, exp.Shutdown(ctx)) })
654 err := exp.Export(ctx, make([]log.Record, 1))
655 assert.ErrorAs(t, err, new(retryableError))
656 })
657
658 t.Run("WithCompressionGZip", func(t *testing.T) {
659 exp, coll := factoryFunc("", nil, WithCompression(GzipCompression))
660 ctx := context.Background() //nolint:usetesting // required to avoid getting a canceled context at cleanup.
661 t.Cleanup(func() { require.NoError(t, coll.Shutdown(ctx)) })
662 t.Cleanup(func() { require.NoError(t, exp.Shutdown(ctx)) })
663 assert.NoError(t, exp.Export(ctx, make([]log.Record, 1)))
664 assert.Len(t, coll.Collect().Dump(), 1)
665 })
666
667 t.Run("WithRetry", func(t *testing.T) {
668 emptyErr := errors.New("")
669 rCh := make(chan exportResult, 5)
670 header := http.Header{http.CanonicalHeaderKey("Retry-After"): {"10"}}
671 // All retryable errors.
672 rCh <- exportResult{Err: &httpResponseError{
673 Status: http.StatusServiceUnavailable,
674 Err: emptyErr,
675 Header: header,
676 }}
677 rCh <- exportResult{Err: &httpResponseError{
678 Status: http.StatusTooManyRequests,
679 Err: emptyErr,
680 }}
681 rCh <- exportResult{Err: &httpResponseError{
682 Status: http.StatusGatewayTimeout,
683 Err: emptyErr,
684 }}
685 rCh <- exportResult{Err: &httpResponseError{
686 Status: http.StatusBadGateway,
687 Err: emptyErr,
688 }}
689 rCh <- exportResult{}
690 exp, coll := factoryFunc("", rCh, WithRetry(RetryConfig{
691 Enabled: true,
692 InitialInterval: time.Nanosecond,
693 MaxInterval: time.Millisecond,
694 MaxElapsedTime: time.Minute,
695 }))
696 ctx := context.Background() //nolint:usetesting // required to avoid getting a canceled context at cleanup.
697 t.Cleanup(func() { require.NoError(t, coll.Shutdown(ctx)) })
698 // Push this after Shutdown so the HTTP server doesn't hang.
699 t.Cleanup(func() { close(rCh) })
700 t.Cleanup(func() { require.NoError(t, exp.Shutdown(ctx)) })
701 assert.NoError(t, exp.Export(ctx, make([]log.Record, 1)), "failed retry")
702 assert.Empty(t, rCh, "failed HTTP responses did not occur")
703 })
704
705 t.Run("WithRetryAndExporterErr", func(t *testing.T) {
706 exporterErr := errors.New("rpc error: code = Unavailable desc = service.name not found in resource attributes")
707 rCh := make(chan exportResult, 1)
708 rCh <- exportResult{Err: &httpResponseError{
709 Status: http.StatusTooManyRequests,
710 Err: exporterErr,
711 }}
712 exp, coll := factoryFunc("", rCh, WithRetry(RetryConfig{
713 Enabled: false,
714 }))
715 ctx := context.Background() //nolint:usetesting // required to avoid getting a canceled context at cleanup.
716 t.Cleanup(func() { require.NoError(t, coll.Shutdown(ctx)) })
717 // Push this after Shutdown so the HTTP server doesn't hang.
718 t.Cleanup(func() { close(rCh) })
719 t.Cleanup(func() { require.NoError(t, exp.Shutdown(ctx)) })
720 err := exp.Export(ctx, make([]log.Record, 1))
721 assert.ErrorContains(t, err, exporterErr.Error())
722
723 // To test the `Unwrap` and `As` function of retryable error
724 var retryErr *retryableError
725 assert.ErrorAs(t, err, &retryErr)
726 assert.ErrorIs(t, err, *retryErr)
727 })
728
729 t.Run("WithURLPath", func(t *testing.T) {
730 path := "/prefix/v2/logs"
731 ePt := fmt.Sprintf("http://localhost:0%s", path)
732 exp, coll := factoryFunc(ePt, nil, WithURLPath(path))
733 ctx := context.Background() //nolint:usetesting // required to avoid getting a canceled context at cleanup.
734 t.Cleanup(func() { require.NoError(t, coll.Shutdown(ctx)) })
735 t.Cleanup(func() { require.NoError(t, exp.Shutdown(ctx)) })
736 assert.NoError(t, exp.Export(ctx, make([]log.Record, 1)))
737 assert.Len(t, coll.Collect().Dump(), 1)
738 })
739
740 t.Run("WithTLSClientConfig", func(t *testing.T) {
741 ePt := "https://localhost:0"
742 tlsCfg := &tls.Config{InsecureSkipVerify: true}
743 exp, coll := factoryFunc(ePt, nil, WithTLSClientConfig(tlsCfg))
744 ctx := context.Background() //nolint:usetesting // required to avoid getting a canceled context at cleanup.
745 t.Cleanup(func() { require.NoError(t, coll.Shutdown(ctx)) })
746 t.Cleanup(func() { require.NoError(t, exp.Shutdown(ctx)) })
747 assert.NoError(t, exp.Export(ctx, make([]log.Record, 1)))
748 assert.Len(t, coll.Collect().Dump(), 1)
749 })
750
751 t.Run("WithInsecureAndTLSClientConfig", func(t *testing.T) {
752 exp, err := New(t.Context(),
753 WithEndpoint("localhost:4318"),
754 WithInsecure(),
755 WithTLSClientConfig(&tls.Config{}),
756 )
757 require.ErrorIs(t, err, errInsecureEndpointWithTLS)
758 assert.Nil(t, exp)
759 })
760
761 t.Run("WithCustomUserAgent", func(t *testing.T) {
762 key := http.CanonicalHeaderKey("user-agent")
763 headers := map[string]string{key: "custom-user-agent"}
764 exp, coll := factoryFunc("", nil, WithHeaders(headers))
765 ctx := context.Background() //nolint:usetesting // required to avoid getting a canceled context at cleanup.
766 t.Cleanup(func() { require.NoError(t, coll.Shutdown(ctx)) })
767 require.NoError(t, exp.Export(ctx, make([]log.Record, 1)))
768 // Ensure everything is flushed.
769 require.NoError(t, exp.Shutdown(ctx))
770
771 got := coll.Headers()
772 require.Contains(t, got, key)
773 assert.Equal(t, []string{headers[key]}, got[key])
774 })
775
776 t.Run("WithProxy", func(t *testing.T) {
777 headerKeySetInProxy := http.CanonicalHeaderKey("X-Using-Proxy")
778 headerValueSetInProxy := "true"
779 exp, coll := factoryFunc("", nil, WithProxy(func(r *http.Request) (*url.URL, error) {
780 r.Header.Set(headerKeySetInProxy, headerValueSetInProxy)
781 return r.URL, nil
782 }))
783 ctx := context.Background() //nolint:usetesting // required to avoid getting a canceled context at cleanup.
784 t.Cleanup(func() { require.NoError(t, coll.Shutdown(ctx)) })
785 require.NoError(t, exp.Export(ctx, make([]log.Record, 1)))
786 // Ensure everything is flushed.
787 require.NoError(t, exp.Shutdown(ctx))
788
789 got := coll.Headers()
790 require.Contains(t, got, headerKeySetInProxy)
791 assert.Equal(t, []string{headerValueSetInProxy}, got[headerKeySetInProxy])
792 })
793
794 t.Run("WithHTTPClient", func(t *testing.T) {
795 headerKeySetInProxy := http.CanonicalHeaderKey("X-Using-Proxy")
796 headerValueSetInProxy := "true"
797 exp, coll := factoryFunc("", nil, WithHTTPClient(&http.Client{
798 Transport: &http.Transport{
799 Proxy: func(r *http.Request) (*url.URL, error) {
800 r.Header.Set(headerKeySetInProxy, headerValueSetInProxy)
801 return r.URL, nil
802 },
803 },
804 }))
805 ctx := context.Background() //nolint:usetesting // required to avoid getting a canceled context at cleanup.
806 t.Cleanup(func() { require.NoError(t, coll.Shutdown(ctx)) })
807 require.NoError(t, exp.Export(ctx, make([]log.Record, 1)))
808 // Ensure everything is flushed.
809 require.NoError(t, exp.Shutdown(ctx))
810
811 got := coll.Headers()
812 require.Contains(t, got, headerKeySetInProxy)
813 assert.Equal(t, []string{headerValueSetInProxy}, got[headerKeySetInProxy])
814 })
815
816 t.Run("non-retryable errors are propagated", func(t *testing.T) {
817 exporterErr := errors.New("missing required attribute aaaa")
818 rCh := make(chan exportResult, 1)
819 rCh <- exportResult{Err: &httpResponseError{
820 Status: http.StatusBadRequest,
821 Err: exporterErr,
822 }}
823
824 exp, coll := factoryFunc("", rCh, WithRetry(RetryConfig{
825 Enabled: false,
826 }))
827 ctx := context.Background() //nolint:usetesting // required to avoid getting a canceled context at cleanup.
828 t.Cleanup(func() { require.NoError(t, coll.Shutdown(ctx)) })
829 // Push this after Shutdown so the HTTP server doesn't hang.
830 t.Cleanup(func() { close(rCh) })
831 t.Cleanup(func() { require.NoError(t, exp.Shutdown(ctx)) })
832 err := exp.Export(ctx, make([]log.Record, 1))
833 assert.ErrorContains(t, err, exporterErr.Error())
834 })
835 }
836
837 func TestGetBodyCalledOnRedirect(t *testing.T) {
838 // Test that req.GetBody is set correctly, allowing the HTTP transport
839 // to re-send the body on 307 redirects.
840 var mu sync.Mutex
841 var requestBodies [][]byte
842
843 handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
844 body, err := io.ReadAll(r.Body)
845 if err != nil {
846 w.WriteHeader(http.StatusInternalServerError)
847 return
848 }
849
850 mu.Lock()
851 requestBodies = append(requestBodies, body)
852 isFirstRequest := len(requestBodies) == 1
853 mu.Unlock()
854
855 if isFirstRequest {
856 w.Header().Set("Location", "/v1/logs/final")
857 w.WriteHeader(http.StatusTemporaryRedirect)
858 return
859 }
860
861 w.Header().Set("Content-Type", "application/x-protobuf")
862 w.WriteHeader(http.StatusOK)
863 })
864
865 server := httptest.NewServer(handler)
866 defer server.Close()
867
868 opts := []Option{WithEndpoint(server.Listener.Addr().String()), WithInsecure()}
869 cfg := newConfig(opts)
870 client, err := newHTTPClient(t.Context(), cfg)
871 require.NoError(t, err)
872
873 exporter, err := newExporter(client, cfg)
874 require.NoError(t, err)
875 ctx := t.Context()
876 defer func() { _ = exporter.Shutdown(ctx) }()
877
878 err = exporter.Export(ctx, make([]log.Record, 1))
879 require.NoError(t, err)
880
881 mu.Lock()
882 defer mu.Unlock()
883
884 require.Len(t, requestBodies, 2, "expected 2 requests (original + redirect)")
885 assert.NotEmpty(t, requestBodies[0], "original request body should not be empty")
886 assert.Equal(t, requestBodies[0], requestBodies[1], "redirect body should match original")
887 }
888
889 // SetExporterID sets the exporter ID counter to v and returns the previous
890 // value.
891 //
892 // This function is useful for testing purposes, allowing you to reset the
893 // counter. It should not be used in production code.
894 func SetExporterID(v int64) int64 {
895 return exporterN.Swap(v)
896 }
897
898 func newFactory(t testing.TB) func(rCh <-chan exportResult) (*client, *httpCollector, string) {
899 return func(rCh <-chan exportResult) (*client, *httpCollector, string) {
900 coll, err := newHTTPCollector("", rCh)
901 require.NoError(t, err)
902
903 addr := coll.Addr().String()
904 opts := []Option{WithEndpoint(addr), WithInsecure()}
905 cfg := newConfig(opts)
906 client, err := newHTTPClient(t.Context(), cfg)
907 require.NoError(t, err)
908 return client, coll, addr
909 }
910 }
911
912 func TestClientInstrumentation(t *testing.T) {
913 // Enable instrumentation for this test.
914 t.Setenv("OTEL_GO_X_OBSERVABILITY", "true")
915
916 // Reset client ID to be a deterministic.
917 const id = 0
918 SetExporterID(id)
919
920 // Save original meter provider and restore at end of test.
921 orig := otel.GetMeterProvider()
922 t.Cleanup(func() {
923 otel.SetMeterProvider(orig)
924 })
925
926 // Create a new meter provider to captrue metrics.
927 reader := metric.NewManualReader()
928 mp := metric.NewMeterProvider(metric.WithReader(reader))
929 otel.SetMeterProvider(mp)
930
931 factory := newFactory(t)
932
933 rCh := make(chan exportResult, 1)
934 rCh <- exportResult{
935 Response: &collogpb.ExportLogsServiceResponse{
936 PartialSuccess: &collogpb.ExportLogsPartialSuccess{
937 RejectedLogRecords: 2,
938 ErrorMessage: "partially successful",
939 },
940 },
941 }
942
943 client, coll, addr := factory(rCh)
944 t.Cleanup(func() {
945 ctx := context.Background() //nolint:usetesting // required to avoid getting a canceled context at cleanup.
946 assert.NoError(t, coll.Shutdown(ctx))
947 })
948 assert.ErrorIs(t, client.UploadLogs(t.Context(), resourceLogs), internal.PartialSuccess{})
949
950 var got metricdata.ResourceMetrics
951 require.NoError(t, reader.Collect(t.Context(), &got))
952
953 baseAttrs := []attribute.KeyValue{
954 semconv.OTelComponentName(observ.GetComponentName(id)),
955 semconv.OTelComponentTypeOtlpHTTPLogExporter,
956 }
957 baseAttrs = append(baseAttrs, observ.ServerAddrAttrs(addr)...)
958
959 want := metricdata.ScopeMetrics{
960 Scope: instrumentation.Scope{
961 Name: observ.ScopeName,
962 Version: observ.Version,
963 SchemaURL: semconv.SchemaURL,
964 },
965 Metrics: []metricdata.Metrics{
966 {
967 Name: otelconv.SDKExporterLogInflight{}.Name(),
968 Description: otelconv.SDKExporterLogInflight{}.Description(),
969 Unit: otelconv.SDKExporterLogInflight{}.Unit(),
970 Data: metricdata.Sum[int64]{
971 DataPoints: []metricdata.DataPoint[int64]{
972 {Attributes: attribute.NewSet(baseAttrs...)},
973 },
974 Temporality: metricdata.CumulativeTemporality,
975 },
976 },
977 {
978 Name: otelconv.SDKExporterLogExported{}.Name(),
979 Description: otelconv.SDKExporterLogExported{}.Description(),
980 Unit: otelconv.SDKExporterLogExported{}.Unit(),
981 Data: metricdata.Sum[int64]{
982 DataPoints: []metricdata.DataPoint[int64]{
983 {Attributes: attribute.NewSet(baseAttrs...)},
984 {Attributes: attribute.NewSet(append(
985 baseAttrs,
986 otelconv.SDKExporterLogExported{}.AttrErrorType("*errors.joinError"),
987 )...)},
988 },
989 Temporality: 0x1,
990 IsMonotonic: true,
991 },
992 },
993 {
994 Name: otelconv.SDKExporterOperationDuration{}.Name(),
995 Description: otelconv.SDKExporterOperationDuration{}.Description(),
996 Unit: otelconv.SDKExporterOperationDuration{}.Unit(),
997 Data: metricdata.Histogram[float64]{
998 DataPoints: []metricdata.HistogramDataPoint[float64]{
999 {Attributes: attribute.NewSet(append(
1000 baseAttrs,
1001 otelconv.SDKExporterOperationDuration{}.AttrErrorType("*errors.joinError"),
1002 otelconv.SDKExporterOperationDuration{}.AttrHTTPResponseStatusCode(200),
1003 )...)},
1004 },
1005 Temporality: 0x1,
1006 },
1007 },
1008 },
1009 }
1010
1011 require.Len(t, got.ScopeMetrics, 1)
1012 opt := []metricdatatest.Option{
1013 metricdatatest.IgnoreTimestamp(),
1014 metricdatatest.IgnoreExemplars(),
1015 metricdatatest.IgnoreValue(),
1016 }
1017 metricdatatest.AssertEqual(t, want, got.ScopeMetrics[0], opt...)
1018 }
1019
1020 func TestResponseBodySizeLimit(t *testing.T) {
1021 // Override the limit to 1 byte so any non-empty response body exceeds it.
1022 orig := maxResponseBodySize
1023 maxResponseBodySize = 1
1024 t.Cleanup(func() { maxResponseBodySize = orig })
1025
1026 // largeBody is larger than the 1-byte limit.
1027 largeBody := []byte("xx")
1028
1029 tests := []struct {
1030 name string
1031 status int
1032 contentType string
1033 }{
1034 {
1035 name: "success response body too large",
1036 status: http.StatusOK,
1037 contentType: "application/x-protobuf",
1038 },
1039 {
1040 name: "error response body too large",
1041 status: http.StatusServiceUnavailable,
1042 contentType: "text/plain",
1043 },
1044 }
1045 for _, tc := range tests {
1046 t.Run(tc.name, func(t *testing.T) {
1047 var calls int
1048 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
1049 calls++
1050 w.Header().Set("Content-Type", tc.contentType)
1051 w.WriteHeader(tc.status)
1052 _, _ = w.Write(largeBody)
1053 }))
1054 t.Cleanup(srv.Close)
1055
1056 opts := []Option{
1057 WithEndpoint(srv.Listener.Addr().String()),
1058 WithInsecure(),
1059 WithRetry(RetryConfig{Enabled: false}),
1060 }
1061 cfg := newConfig(opts)
1062 c, err := newHTTPClient(t.Context(), cfg)
1063 require.NoError(t, err)
1064
1065 err = c.UploadLogs(t.Context(), make([]*lpb.ResourceLogs, 1))
1066 assert.ErrorContains(t, err, "response body too large")
1067 assert.Equal(t, 1, calls, "request must not be retried after body-too-large error")
1068 })
1069 }
1070 }
1071
1072 func BenchmarkExporterExportLogs(b *testing.B) {
1073 const n = 10
1074
1075 run := func(b *testing.B) {
1076 factory := newFactory(b)
1077 client, coll, _ := factory(nil)
1078 b.Cleanup(func() {
1079 //nolint:usetesting // required to avoid getting a canceled context at cleanup.
1080 assert.NoError(b, coll.srv.Shutdown(context.Background()))
1081 })
1082
1083 stubs := make([]*lpb.ResourceLogs, n)
1084 for i := range stubs {
1085 stubs[i] = &lpb.ResourceLogs{
1086 Resource: &rpb.Resource{
1087 Attributes: []*cpb.KeyValue{kvSrvName, kvSrvVer},
1088 },
1089 ScopeLogs: []*lpb.ScopeLogs{
1090 {
1091 Scope: scope,
1092 LogRecords: logRecords[:(i+1)%(len(logRecords)+1)],
1093 SchemaUrl: semconv.SchemaURL,
1094 },
1095 },
1096 SchemaUrl: semconv.SchemaURL,
1097 }
1098 }
1099
1100 b.ReportAllocs()
1101 b.ResetTimer()
1102
1103 var err error
1104 for b.Loop() {
1105 err = client.UploadLogs(b.Context(), stubs)
1106 }
1107 _ = err
1108 }
1109
1110 b.Run("Observability", func(b *testing.B) {
1111 b.Setenv("OTEL_GO_X_OBSERVABILITY", "true")
1112 run(b)
1113 })
1114
1115 b.Run("NoObservability", run)
1116 }