@@ -7,10 +7,13 @@ import (
77 "io"
88 "net/http"
99 "net/http/httptest"
10+ "strings"
1011 "testing"
1112 "time"
1213
1314 "github.com/go-kit/log"
15+ "github.com/prometheus/client_golang/prometheus"
16+ "github.com/prometheus/client_golang/prometheus/testutil"
1417 "github.com/prometheus/prometheus/prompb"
1518 "github.com/stretchr/testify/assert"
1619 "github.com/stretchr/testify/require"
@@ -305,7 +308,7 @@ func BenchmarkOTLPWriteHandler(b *testing.B) {
305308 mockPushFunc := func (context.Context , * cortexpb.WriteRequest ) (* cortexpb.WriteResponse , error ) {
306309 return & cortexpb.WriteResponse {}, nil
307310 }
308- handler := OTLPHandler (10000 , overrides , cfg , nil , mockPushFunc )
311+ handler := OTLPHandler (10000 , overrides , cfg , nil , mockPushFunc , nil )
309312
310313 b .Run ("json with no compression" , func (b * testing.B ) {
311314 req , err := getOTLPHttpRequest (& exportRequest , jsonContentType , "" )
@@ -384,32 +387,61 @@ func TestOTLPWriteHandler(t *testing.T) {
384387 expectedStatusCode int
385388 expectedErrMsg string
386389 encodingType string
390+ expectedMetrics string
387391 }{
388392 {
389393 description : "Test proto format write with no compression" ,
390394 maxRecvMsgSize : 10000 ,
391395 contentType : pbContentType ,
392396 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+ ` ,
393404 },
394405 {
395406 description : "Test proto format write with gzip" ,
396407 maxRecvMsgSize : 10000 ,
397408 contentType : pbContentType ,
398409 expectedStatusCode : http .StatusOK ,
399410 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+ ` ,
400418 },
401419 {
402420 description : "Test json format write with no compression" ,
403421 maxRecvMsgSize : 10000 ,
404422 contentType : jsonContentType ,
405423 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+ ` ,
406431 },
407432 {
408433 description : "Test json format write with gzip" ,
409434 maxRecvMsgSize : 10000 ,
410435 contentType : jsonContentType ,
411436 expectedStatusCode : http .StatusOK ,
412437 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+ ` ,
413445 },
414446 {
415447 description : "request too big than maxRecvMsgSize (proto) with no compression" ,
@@ -458,14 +490,19 @@ func TestOTLPWriteHandler(t *testing.T) {
458490 push := verifyOTLPWriteRequestHandler (t , cortexpb .API )
459491 overrides , err := validation .NewOverrides (querier .DefaultLimitsConfig (), nil )
460492 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 ))
462495
463496 recorder := httptest .NewRecorder ()
464497 handler .ServeHTTP (recorder , req )
465498
466499 resp := recorder .Result ()
467500 require .Equal (t , test .expectedStatusCode , resp .StatusCode )
468501
502+ if test .expectedMetrics != "" {
503+ require .NoError (t , testutil .GatherAndCompare (reg , strings .NewReader (test .expectedMetrics ), "cortex_distributor_push_requests" ))
504+ }
505+
469506 if test .expectedErrMsg != "" {
470507 b , err := io .ReadAll (resp .Body )
471508 require .NoError (t , err )
0 commit comments