whatsUpHandler returns all services that currently monitored by Prometheus. It uses prometheus client_golang client code to request PromQL query against given Prometheus server to return answer.
( apiClient v1.API, handled prometheus.Counter, lastNumElems prometheus.Gauge, handledDuration prometheus.Histogram, )
| 167 | // It uses prometheus client_golang client code to request PromQL query against given Prometheus server |
| 168 | // to return answer. |
| 169 | func whatsUpHandler( |
| 170 | apiClient v1.API, |
| 171 | handled prometheus.Counter, |
| 172 | lastNumElems prometheus.Gauge, |
| 173 | handledDuration prometheus.Histogram, |
| 174 | ) http.HandlerFunc { |
| 175 | return func(w http.ResponseWriter, r *http.Request) { |
| 176 | start := time.Now() |
| 177 | ctx := r.Context() |
| 178 | w.Header().Set("Content-Type", "application/json") |
| 179 | |
| 180 | var upResponse model.Value |
| 181 | if err := tracing.DoInSpan(ctx, "query Prometheus", func(ctx context.Context) error { |
| 182 | res, warn, err := apiClient.Query(ctx, "up", time.Now()) |
| 183 | if err != nil { |
| 184 | return err |
| 185 | } |
| 186 | |
| 187 | if len(warn) > 0 { |
| 188 | return errors.Newf("got warnings from Prometheus %v", warn) |
| 189 | } |
| 190 | upResponse = res |
| 191 | return nil |
| 192 | }); err != nil { |
| 193 | // We return OK status, so browser can render nice result. |
| 194 | w.WriteHeader(http.StatusOK) |
| 195 | // NOTE: Pass-through error might be not always safe, sanitize on production. |
| 196 | b, _ := json.Marshal(response{Error: err}) |
| 197 | _, _ = fmt.Fprintln(w, string(b)) |
| 198 | return |
| 199 | } |
| 200 | |
| 201 | resp := response{} |
| 202 | switch r := upResponse.(type) { |
| 203 | case model.Vector: |
| 204 | for _, s := range r { |
| 205 | resp.Instances = append(resp.Instances, string(s.Metric["instance"])) |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | w.WriteHeader(http.StatusOK) |
| 210 | b, _ := json.Marshal(resp) |
| 211 | _, _ = fmt.Fprintln(w, string(b)) |
| 212 | |
| 213 | lastNumElems.Set(float64(len(resp.Instances))) |
| 214 | handled.(prometheus.ExemplarAdder). |
| 215 | AddWithExemplar(1, getExemplarFn(ctx)) |
| 216 | handledDuration.(prometheus.ExemplarObserver). |
| 217 | ObserveWithExemplar(time.Since(start).Seconds(), getExemplarFn(ctx)) |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | func getExemplarFn(ctx context.Context) prometheus.Labels { |
| 222 | if spanCtx := tracing.GetSpan(ctx); spanCtx.Context().IsSampled() { |
no test coverage detected