forked from grafana/tempo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_range_test.go
109 lines (92 loc) · 2.98 KB
/
query_range_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package e2e
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"testing"
"time"
"github.com/grafana/e2e"
"github.com/grafana/tempo/integration/util"
"github.com/grafana/tempo/pkg/tempopb"
"github.com/stretchr/testify/require"
)
const configQueryRange = "config-query-range.yaml"
// Set debugMode to true to print the response body
var debugMode = false
func TestQueryRangeExemplars(t *testing.T) {
s, err := e2e.NewScenario("tempo_e2e")
require.NoError(t, err)
defer s.Close()
require.NoError(t, util.CopyFileToSharedDir(s, configQueryRange, "config.yaml"))
tempo := util.NewTempoAllInOne()
require.NoError(t, s.StartAndWaitReady(tempo))
jaegerClient, err := util.NewJaegerGRPCClient(tempo.Endpoint(14250))
require.NoError(t, err)
require.NotNil(t, jaegerClient)
ticker := time.NewTicker(500 * time.Millisecond)
defer ticker.Stop()
timer := time.NewTimer(5 * time.Second)
defer timer.Stop()
sendLoop:
for {
select {
case <-ticker.C:
require.NoError(t, jaegerClient.EmitBatch(context.Background(), util.MakeThriftBatch()))
case <-timer.C:
break sendLoop
}
}
// Wait for traces to be flushed to blocks
require.NoError(t, tempo.WaitSumMetricsWithOptions(e2e.GreaterOrEqual(1), []string{"tempo_metrics_generator_processor_local_blocks_spans_total"}, e2e.WaitMissingMetrics))
require.NoError(t, tempo.WaitSumMetricsWithOptions(e2e.GreaterOrEqual(1), []string{"tempo_metrics_generator_processor_local_blocks_cut_blocks"}, e2e.WaitMissingMetrics))
for _, query := range []string{
"{} | rate()",
"{} | compare({status=error})",
} {
t.Run(query, func(t *testing.T) {
callQueryRange(t, tempo.Endpoint(3200), query, debugMode)
})
}
res := doRequest(t, tempo.Endpoint(3200), "{. a}")
require.Equal(t, 400, res.StatusCode)
}
func callQueryRange(t *testing.T, endpoint, query string, printBody bool) {
res := doRequest(t, endpoint, query)
require.Equal(t, http.StatusOK, res.StatusCode)
// Read body and print it
body, err := io.ReadAll(res.Body)
require.NoError(t, err)
if printBody {
fmt.Println(string(body))
}
queryRangeRes := tempopb.QueryRangeResponse{}
require.NoError(t, json.Unmarshal(body, &queryRangeRes))
require.NotNil(t, queryRangeRes)
require.GreaterOrEqual(t, len(queryRangeRes.GetSeries()), 1)
exemplarCount := 0
for _, series := range queryRangeRes.GetSeries() {
exemplarCount += len(series.GetExemplars())
}
require.GreaterOrEqual(t, exemplarCount, 1)
}
func doRequest(t *testing.T, endpoint, query string) *http.Response {
url := buildURL(endpoint, fmt.Sprintf("%s with(exemplars=true)", query))
req, err := http.NewRequest(http.MethodGet, url, nil)
require.NoError(t, err)
res, err := http.DefaultClient.Do(req)
require.NoError(t, err)
return res
}
func buildURL(endpoint, query string) string {
return fmt.Sprintf(
"http://%s/api/metrics/query_range?query=%s&start=%d&end=%d&step=%s",
endpoint,
url.QueryEscape(query),
time.Now().Add(-5*time.Minute).UnixNano(),
time.Now().Add(time.Minute).UnixNano(),
"5s",
)
}