Debian Codegraph @

exporters/otlp/otlpmetric/otlpmetrichttp/client_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package otlpmetrichttp
5
6 import (
7 "context"
8 "crypto/tls"
9 "errors"
10 "fmt"
11 "io"
12 "net/http"
13 "net/http/httptest"
14 "net/url"
15 "strings"
16 "sync"
17 "testing"
18 "time"
19
20 "github.com/stretchr/testify/assert"
21 "github.com/stretchr/testify/require"
22 mpb "go.opentelemetry.io/proto/otlp/metrics/v1"
23
24 "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf"
25 "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp/internal/otest"
26 "go.opentelemetry.io/otel/sdk/metric"
27 "go.opentelemetry.io/otel/sdk/metric/metricdata"
28 )
29
30 type clientShim struct {
31 *client
32 }
33
34 func (clientShim) Temporality(metric.InstrumentKind) metricdata.Temporality {
35 return metricdata.CumulativeTemporality
36 }
37
38 func (clientShim) Aggregation(metric.InstrumentKind) metric.Aggregation {
39 return nil
40 }
41
42 func (clientShim) ForceFlush(ctx context.Context) error {
43 return ctx.Err()
44 }
45
46 func TestClient(t *testing.T) {
47 factory := func(rCh <-chan otest.ExportResult) (otest.Client, otest.Collector) {
48 coll, err := otest.NewHTTPCollector("", rCh)
49 require.NoError(t, err)
50
51 addr := coll.Addr().String()
52 opts := []Option{WithEndpoint(addr), WithInsecure()}
53 cfg := oconf.NewHTTPConfig(asHTTPOptions(opts)...)
54 client, err := newClient(cfg)
55 require.NoError(t, err)
56 return clientShim{client}, coll
57 }
58
59 t.Run("Integration", otest.RunClientTests(factory))
60 }
61
62 func TestClientWithHTTPCollectorRespondingPlainText(t *testing.T) {
63 ctx := t.Context()
64 coll, err := otest.NewHTTPCollector("", nil, otest.WithHTTPCollectorRespondingPlainText())
65 require.NoError(t, err)
66
67 addr := coll.Addr().String()
68 opts := []Option{WithEndpoint(addr), WithInsecure()}
69 cfg := oconf.NewHTTPConfig(asHTTPOptions(opts)...)
70 client, err := newClient(cfg)
71 require.NoError(t, err)
72
73 require.NoError(t, client.UploadMetrics(ctx, &mpb.ResourceMetrics{}))
74 require.NoError(t, client.Shutdown(ctx))
75 got := coll.Collect().Dump()
76 require.Len(t, got, 1, "upload of one ResourceMetrics")
77 }
78
79 func TestNewWithInvalidEndpoint(t *testing.T) {
80 ctx := t.Context()
81 exp, err := New(ctx, WithEndpoint("host:invalid-port"))
82 assert.Error(t, err)
83 assert.Nil(t, exp)
84 }
85
86 func TestConfig(t *testing.T) {
87 factoryFunc := func(ePt string, rCh <-chan otest.ExportResult, o ...Option) (metric.Exporter, *otest.HTTPCollector) {
88 coll, err := otest.NewHTTPCollector(ePt, rCh)
89 require.NoError(t, err)
90
91 opts := []Option{WithEndpoint(coll.Addr().String())}
92 if !strings.HasPrefix(strings.ToLower(ePt), "https") {
93 opts = append(opts, WithInsecure())
94 }
95 opts = append(opts, o...)
96
97 ctx := t.Context()
98 exp, err := New(ctx, opts...)
99 require.NoError(t, err)
100 return exp, coll
101 }
102
103 t.Run("WithEndpointURL", func(t *testing.T) {
104 coll, err := otest.NewHTTPCollector("", nil)
105 require.NoError(t, err)
106 ctx := context.Background() //nolint:usetesting // required to avoid getting a canceled context at cleanup.
107
108 exp, err := New(ctx, WithEndpointURL("http://"+coll.Addr().String()))
109 require.NoError(t, err)
110 t.Cleanup(func() { require.NoError(t, coll.Shutdown(ctx)) })
111 t.Cleanup(func() { require.NoError(t, exp.Shutdown(ctx)) })
112
113 assert.NoError(t, exp.Export(ctx, &metricdata.ResourceMetrics{}))
114 assert.Len(t, coll.Collect().Dump(), 1)
115 })
116
117 t.Run("WithHeaders", func(t *testing.T) {
118 key := http.CanonicalHeaderKey("my-custom-header")
119 headers := map[string]string{key: "custom-value"}
120 exp, coll := factoryFunc("", nil, WithHeaders(headers))
121 ctx := context.Background() //nolint:usetesting // required to avoid getting a canceled context at cleanup.
122 t.Cleanup(func() { require.NoError(t, coll.Shutdown(ctx)) })
123 require.NoError(t, exp.Export(ctx, &metricdata.ResourceMetrics{}))
124 // Ensure everything is flushed.
125 require.NoError(t, exp.Shutdown(ctx))
126
127 got := coll.Headers()
128 require.Regexp(t, "OTel Go OTLP over HTTP/protobuf metrics exporter/[01]\\..*", got)
129 require.Contains(t, got, key)
130 assert.Equal(t, []string{headers[key]}, got[key])
131 })
132
133 t.Run("WithTimeout", func(t *testing.T) {
134 // Do not send on rCh so the Collector never responds to the client.
135 rCh := make(chan otest.ExportResult)
136 exp, coll := factoryFunc(
137 "",
138 rCh,
139 WithTimeout(time.Millisecond),
140 WithRetry(RetryConfig{Enabled: false}),
141 )
142 ctx := context.Background() //nolint:usetesting // required to avoid getting a canceled context at cleanup.
143 t.Cleanup(func() { require.NoError(t, coll.Shutdown(ctx)) })
144 // Push this after Shutdown so the HTTP server doesn't hang.
145 t.Cleanup(func() { close(rCh) })
146 t.Cleanup(func() { require.NoError(t, exp.Shutdown(ctx)) })
147 err := exp.Export(ctx, &metricdata.ResourceMetrics{})
148 assert.ErrorAs(t, err, new(retryableError))
149 })
150
151 t.Run("WithCompressionGZip", func(t *testing.T) {
152 exp, coll := factoryFunc("", nil, WithCompression(GzipCompression))
153 ctx := context.Background() //nolint:usetesting // required to avoid getting a canceled context at cleanup.
154 t.Cleanup(func() { require.NoError(t, coll.Shutdown(ctx)) })
155 t.Cleanup(func() { require.NoError(t, exp.Shutdown(ctx)) })
156 assert.NoError(t, exp.Export(ctx, &metricdata.ResourceMetrics{}))
157 assert.Len(t, coll.Collect().Dump(), 1)
158 })
159
160 t.Run("WithRetry", func(t *testing.T) {
161 emptyErr := errors.New("")
162 rCh := make(chan otest.ExportResult, 5)
163 header := http.Header{http.CanonicalHeaderKey("Retry-After"): {"10"}}
164 // All retryable errors.
165 rCh <- otest.ExportResult{Err: &otest.HTTPResponseError{
166 Status: http.StatusServiceUnavailable,
167 Err: emptyErr,
168 Header: header,
169 }}
170 rCh <- otest.ExportResult{Err: &otest.HTTPResponseError{
171 Status: http.StatusTooManyRequests,
172 Err: emptyErr,
173 }}
174 rCh <- otest.ExportResult{Err: &otest.HTTPResponseError{
175 Status: http.StatusGatewayTimeout,
176 Err: emptyErr,
177 }}
178 rCh <- otest.ExportResult{Err: &otest.HTTPResponseError{
179 Status: http.StatusBadGateway,
180 Err: emptyErr,
181 }}
182 rCh <- otest.ExportResult{}
183 exp, coll := factoryFunc("", rCh, WithRetry(RetryConfig{
184 Enabled: true,
185 InitialInterval: time.Nanosecond,
186 MaxInterval: time.Millisecond,
187 MaxElapsedTime: time.Minute,
188 }))
189 ctx := context.Background() //nolint:usetesting // required to avoid getting a canceled context at cleanup.
190 t.Cleanup(func() { require.NoError(t, coll.Shutdown(ctx)) })
191 // Push this after Shutdown so the HTTP server doesn't hang.
192 t.Cleanup(func() { close(rCh) })
193 t.Cleanup(func() { require.NoError(t, exp.Shutdown(ctx)) })
194 assert.NoError(t, exp.Export(ctx, &metricdata.ResourceMetrics{}), "failed retry")
195 assert.Empty(t, rCh, "failed HTTP responses did not occur")
196 })
197
198 t.Run("WithRetryAndExporterErr", func(t *testing.T) {
199 exporterErr := errors.New("rpc error: code = Unavailable desc = service.name not found in resource attributes")
200 rCh := make(chan otest.ExportResult, 1)
201 rCh <- otest.ExportResult{Err: &otest.HTTPResponseError{
202 Status: http.StatusTooManyRequests,
203 Err: exporterErr,
204 }}
205 exp, coll := factoryFunc("", rCh, WithRetry(RetryConfig{
206 Enabled: false,
207 }))
208 ctx := context.Background() //nolint:usetesting // required to avoid getting a canceled context at cleanup.
209 t.Cleanup(func() { require.NoError(t, coll.Shutdown(ctx)) })
210 // Push this after Shutdown so the HTTP server doesn't hang.
211 t.Cleanup(func() { close(rCh) })
212 t.Cleanup(func() { require.NoError(t, exp.Shutdown(ctx)) })
213 err := exp.Export(ctx, &metricdata.ResourceMetrics{})
214 assert.ErrorContains(t, err, exporterErr.Error())
215
216 // To test the `Unwrap` and `As` function of retryable error
217 var retryErr *retryableError
218 assert.ErrorAs(t, err, &retryErr)
219 assert.ErrorIs(t, err, *retryErr)
220 })
221
222 t.Run("WithURLPath", func(t *testing.T) {
223 path := "/prefix/v2/metrics"
224 ePt := fmt.Sprintf("http://localhost:0%s", path)
225 exp, coll := factoryFunc(ePt, nil, WithURLPath(path))
226 ctx := context.Background() //nolint:usetesting // required to avoid getting a canceled context at cleanup.
227 t.Cleanup(func() { require.NoError(t, coll.Shutdown(ctx)) })
228 t.Cleanup(func() { require.NoError(t, exp.Shutdown(ctx)) })
229 assert.NoError(t, exp.Export(ctx, &metricdata.ResourceMetrics{}))
230 assert.Len(t, coll.Collect().Dump(), 1)
231 })
232
233 t.Run("WithTLSClientConfig", func(t *testing.T) {
234 ePt := "https://localhost:0"
235 tlsCfg := &tls.Config{InsecureSkipVerify: true}
236 exp, coll := factoryFunc(ePt, nil, WithTLSClientConfig(tlsCfg))
237 ctx := context.Background() //nolint:usetesting // required to avoid getting a canceled context at cleanup.
238 t.Cleanup(func() { require.NoError(t, coll.Shutdown(ctx)) })
239 t.Cleanup(func() { require.NoError(t, exp.Shutdown(ctx)) })
240 assert.NoError(t, exp.Export(ctx, &metricdata.ResourceMetrics{}))
241 assert.Len(t, coll.Collect().Dump(), 1)
242 })
243
244 t.Run("WithInsecureAndTLSClientConfig", func(t *testing.T) {
245 exp, err := New(t.Context(),
246 WithEndpoint("localhost:4318"),
247 WithInsecure(),
248 WithTLSClientConfig(&tls.Config{}),
249 )
250 require.ErrorIs(t, err, errInsecureEndpointWithTLS)
251 assert.Nil(t, exp)
252 })
253
254 t.Run("WithCustomUserAgent", func(t *testing.T) {
255 key := http.CanonicalHeaderKey("user-agent")
256 headers := map[string]string{key: "custom-user-agent"}
257 exp, coll := factoryFunc("", nil, WithHeaders(headers))
258 ctx := context.Background() //nolint:usetesting // required to avoid getting a canceled context at cleanup.
259 t.Cleanup(func() { require.NoError(t, coll.Shutdown(ctx)) })
260 require.NoError(t, exp.Export(ctx, &metricdata.ResourceMetrics{}))
261 // Ensure everything is flushed.
262 require.NoError(t, exp.Shutdown(ctx))
263
264 got := coll.Headers()
265 require.Contains(t, got, key)
266 assert.Equal(t, []string{headers[key]}, got[key])
267 })
268
269 t.Run("WithProxy", func(t *testing.T) {
270 headerKeySetInProxy := http.CanonicalHeaderKey("X-Using-Proxy")
271 headerValueSetInProxy := "true"
272 exp, coll := factoryFunc("", nil, WithHTTPClient(&http.Client{
273 Transport: &http.Transport{
274 Proxy: func(r *http.Request) (*url.URL, error) {
275 r.Header.Set(headerKeySetInProxy, headerValueSetInProxy)
276 return r.URL, nil
277 },
278 },
279 }))
280 ctx := context.Background() //nolint:usetesting // required to avoid getting a canceled context at cleanup.
281 t.Cleanup(func() { require.NoError(t, coll.Shutdown(ctx)) })
282 require.NoError(t, exp.Export(ctx, &metricdata.ResourceMetrics{}))
283 // Ensure everything is flushed.
284 require.NoError(t, exp.Shutdown(ctx))
285
286 got := coll.Headers()
287 require.Contains(t, got, headerKeySetInProxy)
288 assert.Equal(t, []string{headerValueSetInProxy}, got[headerKeySetInProxy])
289 })
290
291 t.Run("WithHTTPClient", func(t *testing.T) {
292 headerKeySetInProxy := http.CanonicalHeaderKey("X-Using-Proxy")
293 headerValueSetInProxy := "true"
294 exp, coll := factoryFunc("", nil, WithProxy(func(r *http.Request) (*url.URL, error) {
295 r.Header.Set(headerKeySetInProxy, headerValueSetInProxy)
296 return r.URL, nil
297 }))
298 ctx := context.Background() //nolint:usetesting // required to avoid getting a canceled context at cleanup.
299 t.Cleanup(func() { require.NoError(t, coll.Shutdown(ctx)) })
300 require.NoError(t, exp.Export(ctx, &metricdata.ResourceMetrics{}))
301 // Ensure everything is flushed.
302 require.NoError(t, exp.Shutdown(ctx))
303
304 got := coll.Headers()
305 require.Contains(t, got, headerKeySetInProxy)
306 assert.Equal(t, []string{headerValueSetInProxy}, got[headerKeySetInProxy])
307 })
308
309 t.Run("non-retryable errors are propagated", func(t *testing.T) {
310 exporterErr := errors.New("missing required attribute aaa")
311 rCh := make(chan otest.ExportResult, 1)
312 rCh <- otest.ExportResult{Err: &otest.HTTPResponseError{
313 Status: http.StatusBadRequest,
314 Err: exporterErr,
315 }}
316 exp, coll := factoryFunc("", rCh)
317 ctx := context.Background() //nolint:usetesting // required to avoid getting a canceled context at cleanup.
318 t.Cleanup(func() { require.NoError(t, coll.Shutdown(ctx)) })
319 // Push this after Shutdown so the HTTP server doesn't hang.
320 t.Cleanup(func() { close(rCh) })
321 t.Cleanup(func() { require.NoError(t, exp.Shutdown(ctx)) })
322 exCtx, cancel := context.WithTimeout(ctx, time.Second)
323 defer cancel()
324 err := exp.Export(exCtx, &metricdata.ResourceMetrics{})
325 assert.ErrorContains(t, err, exporterErr.Error())
326
327 assert.NoError(t, exCtx.Err())
328 })
329 }
330
331 func TestGetBodyCalledOnRedirect(t *testing.T) {
332 // Test that req.GetBody is set correctly, allowing the HTTP transport
333 // to re-send the body on 307 redirects.
334 var mu sync.Mutex
335 var requestBodies [][]byte
336
337 handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
338 body, err := io.ReadAll(r.Body)
339 if err != nil {
340 w.WriteHeader(http.StatusInternalServerError)
341 return
342 }
343
344 mu.Lock()
345 requestBodies = append(requestBodies, body)
346 isFirstRequest := len(requestBodies) == 1
347 mu.Unlock()
348
349 if isFirstRequest {
350 w.Header().Set("Location", "/v1/metrics/final")
351 w.WriteHeader(http.StatusTemporaryRedirect)
352 return
353 }
354
355 w.Header().Set("Content-Type", "application/x-protobuf")
356 w.WriteHeader(http.StatusOK)
357 })
358
359 server := httptest.NewServer(handler)
360 defer server.Close()
361
362 opts := []Option{WithEndpoint(server.Listener.Addr().String()), WithInsecure()}
363 cfg := oconf.NewHTTPConfig(asHTTPOptions(opts)...)
364 client, err := newClient(cfg)
365 require.NoError(t, err)
366
367 exporter, err := newExporter(client, cfg)
368 require.NoError(t, err)
369 ctx := t.Context()
370 defer func() { _ = exporter.Shutdown(ctx) }()
371
372 err = exporter.Export(ctx, &metricdata.ResourceMetrics{})
373 require.NoError(t, err)
374
375 mu.Lock()
376 defer mu.Unlock()
377
378 require.Len(t, requestBodies, 2, "expected 2 requests (original + redirect)")
379 assert.NotEmpty(t, requestBodies[0], "original request body should not be empty")
380 assert.Equal(t, requestBodies[0], requestBodies[1], "redirect body should match original")
381 }
382
383 func TestResponseBodySizeLimit(t *testing.T) {
384 // Override the limit to 1 byte so any non-empty response body exceeds it.
385 orig := maxResponseBodySize
386 maxResponseBodySize = 1
387 t.Cleanup(func() { maxResponseBodySize = orig })
388
389 // largeBody is larger than the 1-byte limit.
390 largeBody := []byte("xx")
391
392 tests := []struct {
393 name string
394 status int
395 contentType string
396 }{
397 {
398 name: "success response body too large",
399 status: http.StatusOK,
400 contentType: "application/x-protobuf",
401 },
402 {
403 name: "error response body too large",
404 status: http.StatusServiceUnavailable,
405 contentType: "text/plain",
406 },
407 }
408 for _, tc := range tests {
409 t.Run(tc.name, func(t *testing.T) {
410 var calls int
411 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
412 calls++
413 w.Header().Set("Content-Type", tc.contentType)
414 w.WriteHeader(tc.status)
415 _, _ = w.Write(largeBody)
416 }))
417 t.Cleanup(srv.Close)
418
419 opts := []Option{
420 WithEndpoint(srv.Listener.Addr().String()),
421 WithInsecure(),
422 WithRetry(RetryConfig{Enabled: false}),
423 }
424 cfg := oconf.NewHTTPConfig(asHTTPOptions(opts)...)
425 c, err := newClient(cfg)
426 require.NoError(t, err)
427 t.Cleanup(func() { _ = c.Shutdown(t.Context()) })
428
429 err = c.UploadMetrics(t.Context(), &mpb.ResourceMetrics{})
430 assert.ErrorContains(t, err, "response body too large")
431 assert.Equal(t, 1, calls, "request must not be retried after body-too-large error")
432 })
433 }
434 }