MCPcopy
hub / github.com/prometheus/prometheus / query

Method query

cmd/prometheus/query_log_test.go:99–145  ·  view source on GitHub ↗

query runs a query according to the test origin.

(t *testing.T)

Source from the content-addressed store, hash-verified

97
98// query runs a query according to the test origin.
99func (p *queryLogTest) query(t *testing.T) {
100 switch p.origin {
101 case apiOrigin:
102 r, err := http.Get(fmt.Sprintf(
103 "http://%s:%d%s/api/v1/query_range?step=5&start=0&end=3600&query=%s",
104 p.host,
105 p.port,
106 p.prefix,
107 url.QueryEscape("query_with_api"),
108 ))
109 require.NoError(t, err)
110 require.Equal(t, 200, r.StatusCode)
111 case consoleOrigin:
112 r, err := http.Get(fmt.Sprintf(
113 "http://%s:%d%s/consoles/test.html",
114 p.host,
115 p.port,
116 p.prefix,
117 ))
118 require.NoError(t, err)
119 require.Equal(t, 200, r.StatusCode)
120 case ruleOrigin:
121 // Poll the /api/v1/rules endpoint until a new rule evaluation is detected.
122 var lastEvalTime time.Time
123 for {
124 r, err := http.Get(fmt.Sprintf("http://%s:%d/api/v1/rules", p.host, p.port))
125 require.NoError(t, err)
126
127 rulesBody, err := io.ReadAll(r.Body)
128 require.NoError(t, err)
129 defer r.Body.Close()
130
131 // Parse the rules response to find the last evaluation time.
132 newEvalTime := parseLastEvaluation(rulesBody)
133 if newEvalTime.After(lastEvalTime) {
134 if !lastEvalTime.IsZero() {
135 break
136 }
137 lastEvalTime = newEvalTime
138 }
139
140 time.Sleep(100 * time.Millisecond)
141 }
142 default:
143 panic("can't query this origin")
144 }
145}
146
147// parseLastEvaluation extracts the last evaluation timestamp from the /api/v1/rules response.
148func parseLastEvaluation(rulesBody []byte) time.Time {

Callers 1

runMethod · 0.95

Calls 4

parseLastEvaluationFunction · 0.85
GetMethod · 0.65
CloseMethod · 0.65
IsZeroMethod · 0.45

Tested by

no test coverage detected