MCPcopy
hub / github.com/prometheus/prometheus / createSampledResponseHandler

Function createSampledResponseHandler

storage/remote/client_test.go:1011–1057  ·  view source on GitHub ↗

createSampledResponseHandler creates a mock handler for sampled responses.

(t *testing.T, queries []*prompb.Query)

Source from the content-addressed store, hash-verified

1009
1010// createSampledResponseHandler creates a mock handler for sampled responses.
1011func createSampledResponseHandler(t *testing.T, queries []*prompb.Query) http.HandlerFunc {
1012 return http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
1013 w.Header().Set("Content-Type", "application/x-protobuf")
1014
1015 var results []*prompb.QueryResult
1016 for queryIndex, query := range queries {
1017 var timeseries []*prompb.TimeSeries
1018
1019 // Create 2 series per query
1020 for seriesIndex := range 2 {
1021 var labels []prompb.Label
1022 if queryIndex == 0 {
1023 labels = []prompb.Label{
1024 {Name: "job", Value: "prometheus"},
1025 {Name: "instance", Value: fmt.Sprintf("localhost:%d", 9090+seriesIndex)},
1026 }
1027 } else {
1028 labels = []prompb.Label{
1029 {Name: "job", Value: "node_exporter"},
1030 {Name: "instance", Value: fmt.Sprintf("localhost:%d", 9100+seriesIndex)},
1031 }
1032 }
1033
1034 // Create 2 samples per series within query time range
1035 samples := []prompb.Sample{
1036 {Timestamp: query.StartTimestampMs, Value: float64(queryIndex*10 + seriesIndex)},
1037 {Timestamp: query.EndTimestampMs, Value: float64(queryIndex*10 + seriesIndex + 1)},
1038 }
1039
1040 timeseries = append(timeseries, &prompb.TimeSeries{
1041 Labels: labels,
1042 Samples: samples,
1043 })
1044 }
1045
1046 results = append(results, &prompb.QueryResult{Timeseries: timeseries})
1047 }
1048
1049 resp := &prompb.ReadResponse{Results: results}
1050 data, err := proto.Marshal(resp)
1051 require.NoError(t, err)
1052
1053 compressed := snappy.Encode(nil, data)
1054 _, err = w.Write(compressed)
1055 require.NoError(t, err)
1056 })
1057}
1058
1059// createOverlappingSeriesHandler creates responses with same series from multiple queries.
1060func createOverlappingSeriesHandler(t *testing.T, queries []*prompb.Query) http.HandlerFunc {

Callers

nothing calls this directly

Calls 5

HeaderMethod · 0.80
SetMethod · 0.65
EncodeMethod · 0.65
WriteMethod · 0.65
MarshalMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…