1
1
package transport
2
2
3
3
import (
4
+ "bytes"
4
5
"context"
5
6
"io"
6
7
"net/http"
7
8
"net/http/httptest"
9
+ "net/url"
8
10
"strings"
9
11
"testing"
12
+ "time"
10
13
11
14
"github.com/go-kit/log"
12
15
"github.com/pkg/errors"
@@ -16,6 +19,8 @@ import (
16
19
"github.com/stretchr/testify/require"
17
20
"github.com/weaveworks/common/httpgrpc"
18
21
"github.com/weaveworks/common/user"
22
+
23
+ querier_stats "github.com/cortexproject/cortex/pkg/querier/stats"
19
24
)
20
25
21
26
type roundTripperFunc func (* http.Request ) (* http.Response , error )
@@ -274,8 +279,44 @@ func TestHandler_ServeHTTP(t *testing.T) {
274
279
assert .Equal (t , tt .expectedMetrics , count )
275
280
276
281
if tt .additionalMetricsCheckFunc != nil {
277
- tt .additionalMetricsCheckFunc (handler .( * Handler ) )
282
+ tt .additionalMetricsCheckFunc (handler )
278
283
}
279
284
})
280
285
}
281
286
}
287
+
288
+ func TestReportQueryStatsFormat (t * testing.T ) {
289
+ outputBuf := bytes .NewBuffer (nil )
290
+ logger := log .NewSyncLogger (log .NewLogfmtLogger (outputBuf ))
291
+ handler := NewHandler (HandlerConfig {QueryStatsEnabled : true }, http .DefaultTransport , logger , nil )
292
+
293
+ userID := "fake"
294
+ queryString := url .Values (map [string ][]string {"query" : {"up" }})
295
+ req , err := http .NewRequest (http .MethodGet , "http://localhost:8080/prometheus/api/v1/query" , nil )
296
+ require .NoError (t , err )
297
+ req .Header = http.Header {
298
+ "User-Agent" : []string {"Grafana" },
299
+ }
300
+ resp := & http.Response {
301
+ ContentLength : 1000 ,
302
+ }
303
+ stats := & querier_stats.QueryStats {
304
+ Stats : querier_stats.Stats {
305
+ WallTime : 3 * time .Second ,
306
+ FetchedSeriesCount : 100 ,
307
+ FetchedChunksCount : 200 ,
308
+ FetchedSamplesCount : 300 ,
309
+ FetchedChunkBytes : 1024 ,
310
+ FetchedDataBytes : 2048 ,
311
+ },
312
+ }
313
+ responseErr := errors .New ("foo_err" )
314
+ handler .reportQueryStats (req , userID , queryString , time .Second , stats , responseErr , http .StatusOK , resp )
315
+
316
+ data , err := io .ReadAll (outputBuf )
317
+ require .NoError (t , err )
318
+
319
+ expectedLog := `level=error msg="query stats" component=query-frontend method=GET path=/prometheus/api/v1/query response_time=1s query_wall_time_seconds=3 fetched_series_count=100 fetched_chunks_count=200 fetched_samples_count=300 fetched_chunks_bytes=1024 fetched_data_bytes=2048 status_code=200 response_size=1000 query_length=2 user_agent=Grafana error=foo_err param_query=up
320
+ `
321
+ require .Equal (t , expectedLog , string (data ))
322
+ }
0 commit comments