MCPcopy Create free account
hub / github.com/cortexproject/cortex / TestHandler_MetricCollection

Function TestHandler_MetricCollection

pkg/util/push/push_test.go:1071–1120  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1069}
1070
1071func TestHandler_MetricCollection(t *testing.T) {
1072 var limits validation.Limits
1073 flagext.DefaultValues(&limits)
1074 overrides := validation.NewOverrides(limits, nil)
1075
1076 counter := prometheus.NewCounterVec(prometheus.CounterOpts{
1077 Name: "test_counter",
1078 Help: "test help",
1079 }, []string{"type"})
1080
1081 handler := Handler(true, false, 100000, overrides, nil, verifyWriteRequestHandler(t, cortexpb.API), counter)
1082
1083 t.Run("counts v1 requests", func(t *testing.T) {
1084 req := createRequest(t, createPrometheusRemoteWriteProtobuf(t), false)
1085 resp := httptest.NewRecorder()
1086 handler.ServeHTTP(resp, req)
1087 assert.Equal(t, http.StatusOK, resp.Code)
1088
1089 val := testutil.ToFloat64(counter.WithLabelValues("prw1"))
1090 assert.Equal(t, 1.0, val)
1091 })
1092
1093 t.Run("counts v2 requests", func(t *testing.T) {
1094 ctx := context.Background()
1095 ctx = user.InjectOrgID(ctx, "user-1")
1096
1097 req := createRequest(t, createPrometheusRemoteWriteV2Protobuf(t), true)
1098 req = req.WithContext(ctx)
1099 resp := httptest.NewRecorder()
1100 handler.ServeHTTP(resp, req)
1101 assert.Equal(t, http.StatusNoContent, resp.Code)
1102
1103 val := testutil.ToFloat64(counter.WithLabelValues("prw2"))
1104 assert.Equal(t, 1.0, val)
1105 })
1106
1107 t.Run("counts unknown or invalid content-type as unknown when acceptUnknownRemoteWriteContentType is true", func(t *testing.T) {
1108 handlerWithUnknown := Handler(true, true, 100000, overrides, nil, verifyWriteRequestHandler(t, cortexpb.API), counter)
1109 req := createRequestWithHeaders(t, map[string]string{
1110 "Content-Type": "yolo",
1111 "Content-Encoding": "snappy",
1112 }, createCortexWriteRequestProtobuf(t, false, cortexpb.API))
1113 resp := httptest.NewRecorder()
1114 handlerWithUnknown.ServeHTTP(resp, req)
1115 assert.Equal(t, http.StatusOK, resp.Code)
1116
1117 val := testutil.ToFloat64(counter.WithLabelValues("unknown"))
1118 assert.Equal(t, 1.0, val)
1119 })
1120}
1121
1122func verifyWriteRequestHandler(t *testing.T, expectSource cortexpb.SourceEnum) func(ctx context.Context, request *cortexpb.WriteRequest) (response *cortexpb.WriteResponse, err error) {
1123 t.Helper()

Callers

nothing calls this directly

Calls 12

DefaultValuesFunction · 0.92
NewOverridesFunction · 0.92
createRequestWithHeadersFunction · 0.85
HandlerFunction · 0.70
createRequestFunction · 0.70
RunMethod · 0.65
EqualMethod · 0.65
ServeHTTPMethod · 0.45

Tested by

no test coverage detected