@@ -7,10 +7,13 @@ import (
7
7
"io"
8
8
"net/http"
9
9
"net/http/httptest"
10
+ "strings"
10
11
"testing"
11
12
"time"
12
13
13
14
"github.com/go-kit/log"
15
+ "github.com/prometheus/client_golang/prometheus"
16
+ "github.com/prometheus/client_golang/prometheus/testutil"
14
17
"github.com/prometheus/prometheus/prompb"
15
18
"github.com/stretchr/testify/assert"
16
19
"github.com/stretchr/testify/require"
@@ -305,7 +308,7 @@ func BenchmarkOTLPWriteHandler(b *testing.B) {
305
308
mockPushFunc := func (context.Context , * cortexpb.WriteRequest ) (* cortexpb.WriteResponse , error ) {
306
309
return & cortexpb.WriteResponse {}, nil
307
310
}
308
- handler := OTLPHandler (10000 , overrides , cfg , nil , mockPushFunc )
311
+ handler := OTLPHandler (10000 , overrides , cfg , nil , mockPushFunc , nil )
309
312
310
313
b .Run ("json with no compression" , func (b * testing.B ) {
311
314
req , err := getOTLPHttpRequest (& exportRequest , jsonContentType , "" )
@@ -384,32 +387,61 @@ func TestOTLPWriteHandler(t *testing.T) {
384
387
expectedStatusCode int
385
388
expectedErrMsg string
386
389
encodingType string
390
+ expectedMetrics string
387
391
}{
388
392
{
389
393
description : "Test proto format write with no compression" ,
390
394
maxRecvMsgSize : 10000 ,
391
395
contentType : pbContentType ,
392
396
expectedStatusCode : http .StatusOK ,
397
+ expectedMetrics : `
398
+ # HELP cortex_distributor_push_requests_uncompressed_size_bytes Histogram of push request's uncompressed size in bytes
399
+ # TYPE cortex_distributor_push_requests_uncompressed_size_bytes histogram
400
+ cortex_distributor_push_requests_uncompressed_size_bytes_bucket{format="otlp",user="user-1",le="+Inf"} 1
401
+ cortex_distributor_push_requests_uncompressed_size_bytes_sum{format="otlp",user="user-1"} 665
402
+ cortex_distributor_push_requests_uncompressed_size_bytes_count{format="otlp",user="user-1"} 1
403
+ ` ,
393
404
},
394
405
{
395
406
description : "Test proto format write with gzip" ,
396
407
maxRecvMsgSize : 10000 ,
397
408
contentType : pbContentType ,
398
409
expectedStatusCode : http .StatusOK ,
399
410
encodingType : "gzip" ,
411
+ expectedMetrics : `
412
+ # HELP cortex_distributor_push_requests_uncompressed_size_bytes Histogram of push request's uncompressed size in bytes
413
+ # TYPE cortex_distributor_push_requests_uncompressed_size_bytes histogram
414
+ cortex_distributor_push_requests_uncompressed_size_bytes_bucket{format="otlp",user="user-1",le="+Inf"} 1
415
+ cortex_distributor_push_requests_uncompressed_size_bytes_sum{format="otlp",user="user-1"} 665
416
+ cortex_distributor_push_requests_uncompressed_size_bytes_count{format="otlp",user="user-1"} 1
417
+ ` ,
400
418
},
401
419
{
402
420
description : "Test json format write with no compression" ,
403
421
maxRecvMsgSize : 10000 ,
404
422
contentType : jsonContentType ,
405
423
expectedStatusCode : http .StatusOK ,
424
+ expectedMetrics : `
425
+ # HELP cortex_distributor_push_requests_uncompressed_size_bytes Histogram of push request's uncompressed size in bytes
426
+ # TYPE cortex_distributor_push_requests_uncompressed_size_bytes histogram
427
+ cortex_distributor_push_requests_uncompressed_size_bytes_bucket{format="otlp",user="user-1",le="+Inf"} 1
428
+ cortex_distributor_push_requests_uncompressed_size_bytes_sum{format="otlp",user="user-1"} 1568
429
+ cortex_distributor_push_requests_uncompressed_size_bytes_count{format="otlp",user="user-1"} 1
430
+ ` ,
406
431
},
407
432
{
408
433
description : "Test json format write with gzip" ,
409
434
maxRecvMsgSize : 10000 ,
410
435
contentType : jsonContentType ,
411
436
expectedStatusCode : http .StatusOK ,
412
437
encodingType : "gzip" ,
438
+ expectedMetrics : `
439
+ # HELP cortex_distributor_push_requests_uncompressed_size_bytes Histogram of push request's uncompressed size in bytes
440
+ # TYPE cortex_distributor_push_requests_uncompressed_size_bytes histogram
441
+ cortex_distributor_push_requests_uncompressed_size_bytes_bucket{format="otlp",user="user-1",le="+Inf"} 1
442
+ cortex_distributor_push_requests_uncompressed_size_bytes_sum{format="otlp",user="user-1"} 1568
443
+ cortex_distributor_push_requests_uncompressed_size_bytes_count{format="otlp",user="user-1"} 1
444
+ ` ,
413
445
},
414
446
{
415
447
description : "request too big than maxRecvMsgSize (proto) with no compression" ,
@@ -458,14 +490,19 @@ func TestOTLPWriteHandler(t *testing.T) {
458
490
push := verifyOTLPWriteRequestHandler (t , cortexpb .API )
459
491
overrides , err := validation .NewOverrides (querier .DefaultLimitsConfig (), nil )
460
492
require .NoError (t , err )
461
- handler := OTLPHandler (test .maxRecvMsgSize , overrides , cfg , nil , push )
493
+ reg := prometheus .NewRegistry ()
494
+ handler := OTLPHandler (test .maxRecvMsgSize , overrides , cfg , nil , push , distributor .NewPushHandlerMetrics (reg ))
462
495
463
496
recorder := httptest .NewRecorder ()
464
497
handler .ServeHTTP (recorder , req )
465
498
466
499
resp := recorder .Result ()
467
500
require .Equal (t , test .expectedStatusCode , resp .StatusCode )
468
501
502
+ if test .expectedMetrics != "" {
503
+ require .NoError (t , testutil .GatherAndCompare (reg , strings .NewReader (test .expectedMetrics ), "cortex_distributor_push_requests" ))
504
+ }
505
+
469
506
if test .expectedErrMsg != "" {
470
507
b , err := io .ReadAll (resp .Body )
471
508
require .NoError (t , err )
0 commit comments