mirror of
https://github.com/kubernetes-sigs/prometheus-adapter.git
synced 2026-04-05 17:27:51 +00:00
add http status code to APIResponse
This commit is contained in:
parent
00c093074c
commit
b01d7ac59d
3 changed files with 26 additions and 23 deletions
|
|
@ -62,7 +62,9 @@ func (c *httpAPIClient) Do(ctx context.Context, verb, endpoint string, query url
|
|||
|
||||
req, err := http.NewRequestWithContext(ctx, verb, u.String(), reqBody)
|
||||
if err != nil {
|
||||
return APIResponse{}, fmt.Errorf("error constructing HTTP request to Prometheus: %v", err)
|
||||
return APIResponse{
|
||||
HTTPStatusCode: 400,
|
||||
}, fmt.Errorf("error constructing HTTP request to Prometheus: %v", err)
|
||||
}
|
||||
for key, values := range c.headers {
|
||||
for _, value := range values {
|
||||
|
|
@ -81,7 +83,7 @@ func (c *httpAPIClient) Do(ctx context.Context, verb, endpoint string, query url
|
|||
}()
|
||||
|
||||
if err != nil {
|
||||
return APIResponse{}, err
|
||||
return APIResponse{HTTPStatusCode: 400}, err
|
||||
}
|
||||
|
||||
if klog.V(6).Enabled() {
|
||||
|
|
@ -92,17 +94,21 @@ func (c *httpAPIClient) Do(ctx context.Context, verb, endpoint string, query url
|
|||
|
||||
// codes that aren't 2xx, 400, 422, or 503 won't return JSON objects
|
||||
if code/100 != 2 && code != 400 && code != 422 && code != 503 {
|
||||
return APIResponse{}, &Error{
|
||||
Type: ErrBadResponse,
|
||||
Msg: fmt.Sprintf("unknown response code %d", code),
|
||||
}
|
||||
return APIResponse{
|
||||
HTTPStatusCode: code,
|
||||
}, &Error{
|
||||
Type: ErrBadResponse,
|
||||
Msg: fmt.Sprintf("unknown response code %d", code),
|
||||
}
|
||||
}
|
||||
|
||||
var body io.Reader = resp.Body
|
||||
if klog.V(8).Enabled() {
|
||||
data, err := io.ReadAll(body)
|
||||
if err != nil {
|
||||
return APIResponse{}, fmt.Errorf("unable to log response body: %v", err)
|
||||
return APIResponse{
|
||||
HTTPStatusCode: code,
|
||||
}, fmt.Errorf("unable to log response body: %v", err)
|
||||
}
|
||||
klog.Infof("Response Body: %s", string(data))
|
||||
body = bytes.NewReader(data)
|
||||
|
|
@ -110,12 +116,16 @@ func (c *httpAPIClient) Do(ctx context.Context, verb, endpoint string, query url
|
|||
|
||||
var res APIResponse
|
||||
if err = json.NewDecoder(body).Decode(&res); err != nil {
|
||||
return APIResponse{}, &Error{
|
||||
Type: ErrBadResponse,
|
||||
Msg: err.Error(),
|
||||
}
|
||||
return APIResponse{
|
||||
HTTPStatusCode: code,
|
||||
}, &Error{
|
||||
Type: ErrBadResponse,
|
||||
Msg: err.Error(),
|
||||
}
|
||||
}
|
||||
|
||||
res.HTTPStatusCode = code
|
||||
|
||||
if res.Status == ResponseError {
|
||||
return res, &Error{
|
||||
Type: res.ErrorType,
|
||||
|
|
|
|||
|
|
@ -92,18 +92,8 @@ func (c *instrumentedGenericClient) Do(ctx context.Context, verb, endpoint strin
|
|||
var resp client.APIResponse
|
||||
defer func() {
|
||||
endTime := time.Now()
|
||||
if err != nil {
|
||||
if apiErr, wasAPIErr := err.(*client.Error); wasAPIErr {
|
||||
// measure API errors
|
||||
apiRequestsTotal.With(prometheus.Labels{"code": string(apiErr.Type), "path": endpoint, "server": c.serverName}).Inc()
|
||||
} else {
|
||||
// increment a generic error code counter
|
||||
apiRequestsTotal.With(prometheus.Labels{"code": "generic", "path": endpoint, "server": c.serverName}).Inc()
|
||||
}
|
||||
return
|
||||
} else {
|
||||
apiRequestsTotal.With(prometheus.Labels{"code": string(resp.Status), "path": endpoint, "server": c.serverName}).Inc()
|
||||
}
|
||||
|
||||
apiRequestsTotal.With(prometheus.Labels{"code": string(resp.HTTPStatusCode), "path": endpoint, "server": c.serverName}).Inc()
|
||||
queryLatency.With(prometheus.Labels{"path": endpoint, "server": c.serverName}).Observe(endTime.Sub(startTime).Seconds())
|
||||
}()
|
||||
|
||||
|
|
|
|||
|
|
@ -60,4 +60,7 @@ type APIResponse struct {
|
|||
ErrorType ErrorType `json:"errorType"`
|
||||
// Error is the error message, if this is an error response.
|
||||
Error string `json:"error"`
|
||||
|
||||
// StatusCode is the HTTP status code of the response.
|
||||
HTTPStatusCode int `json:"httpStatusCode"`
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue