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, )
| 122 | // It uses prometheus client_golang client code to request PromQL query against given Prometheus server |
| 123 | // to return answer. |
| 124 | func whatsUpHandler( |
| 125 | apiClient v1.API, |
| 126 | ) http.HandlerFunc { |
| 127 | return func(w http.ResponseWriter, r *http.Request) { |
| 128 | ctx := r.Context() |
| 129 | w.Header().Set("Content-Type", "application/json") |
| 130 | |
| 131 | var upResponse model.Value |
| 132 | if err := tracing.DoInSpan(ctx, "query Prometheus", func(ctx context.Context) error { |
| 133 | res, warn, err := apiClient.Query(ctx, "up", time.Now()) |
| 134 | if err != nil { |
| 135 | return err |
| 136 | } |
| 137 | |
| 138 | if len(warn) > 0 { |
| 139 | return errors.Newf("got warnings from Prometheus %v", warn) |
| 140 | } |
| 141 | upResponse = res |
| 142 | return nil |
| 143 | }); err != nil { |
| 144 | // We return OK status, so browser can render nice result. |
| 145 | w.WriteHeader(http.StatusOK) |
| 146 | // NOTE: Pass-through error might be not always safe, sanitize on production. |
| 147 | b, _ := json.Marshal(response{Error: err}) |
| 148 | _, _ = fmt.Fprintln(w, string(b)) |
| 149 | return |
| 150 | } |
| 151 | |
| 152 | resp := response{} |
| 153 | switch r := upResponse.(type) { |
| 154 | case model.Vector: |
| 155 | for _, s := range r { |
| 156 | resp.Instances = append(resp.Instances, string(s.Metric["instance"])) |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | w.WriteHeader(http.StatusOK) |
| 161 | b, _ := json.Marshal(resp) |
| 162 | _, _ = fmt.Fprintln(w, string(b)) |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | func getExemplarFn(ctx context.Context) prometheus.Labels { |
| 167 | if spanCtx := tracing.GetSpan(ctx); spanCtx.Context().IsSampled() { |
no test coverage detected