MCPcopy Create free account
hub / github.com/prometheus/prometheus / respond

Method respond

web/api/v1/api.go:2152–2181  ·  view source on GitHub ↗

Query string is needed to get the position information for the annotations, and it can be empty if the position information isn't needed.

(w http.ResponseWriter, req *http.Request, data any, warnings annotations.Annotations, query string)

Source from the content-addressed store, hash-verified

2150// Query string is needed to get the position information for the annotations, and it
2151// can be empty if the position information isn't needed.
2152func (api *API) respond(w http.ResponseWriter, req *http.Request, data any, warnings annotations.Annotations, query string) {
2153 statusMessage := statusSuccess
2154 warn, info := warnings.AsStrings(query, 10, 10)
2155
2156 resp := &Response{
2157 Status: statusMessage,
2158 Data: data,
2159 Warnings: warn,
2160 Infos: info,
2161 }
2162
2163 codec, err := api.negotiateCodec(req, resp)
2164 if err != nil {
2165 api.respondError(w, &apiError{errorNotAcceptable, err}, nil)
2166 return
2167 }
2168
2169 b, err := codec.Encode(resp)
2170 if err != nil {
2171 api.logger.Error("error marshaling response", "url", req.URL, "err", err)
2172 http.Error(w, err.Error(), http.StatusInternalServerError)
2173 return
2174 }
2175
2176 w.Header().Set("Content-Type", codec.ContentType().String())
2177 w.WriteHeader(http.StatusOK)
2178 if n, err := w.Write(b); err != nil {
2179 api.logger.Error("error writing response", "url", req.URL, "bytesWritten", n, "err", err)
2180 }
2181}
2182
2183func (api *API) negotiateCodec(req *http.Request, resp *Response) (Codec, error) {
2184 for _, clause := range goautoneg.ParseAccept(req.Header.Get("Accept")) {

Callers 6

RegisterMethod · 0.95
serveWALReplayStatusMethod · 0.95
notificationsMethod · 0.95
TestRespondSuccessFunction · 0.95
BenchmarkRespondFunction · 0.95

Calls 11

negotiateCodecMethod · 0.95
respondErrorMethod · 0.95
AsStringsMethod · 0.80
HeaderMethod · 0.80
WriteHeaderMethod · 0.80
EncodeMethod · 0.65
ErrorMethod · 0.65
SetMethod · 0.65
StringMethod · 0.65
ContentTypeMethod · 0.65
WriteMethod · 0.65

Tested by 3

TestRespondSuccessFunction · 0.76
BenchmarkRespondFunction · 0.76