add http status code to APIResponse

This commit is contained in:
Vivek Kumar 2024-06-17 11:21:00 +05:30
parent 00c093074c
commit b01d7ac59d
No known key found for this signature in database
GPG key ID: 81605FDFBA4EF440
3 changed files with 26 additions and 23 deletions

View file

@ -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) req, err := http.NewRequestWithContext(ctx, verb, u.String(), reqBody)
if err != nil { 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 key, values := range c.headers {
for _, value := range values { for _, value := range values {
@ -81,7 +83,7 @@ func (c *httpAPIClient) Do(ctx context.Context, verb, endpoint string, query url
}() }()
if err != nil { if err != nil {
return APIResponse{}, err return APIResponse{HTTPStatusCode: 400}, err
} }
if klog.V(6).Enabled() { if klog.V(6).Enabled() {
@ -92,7 +94,9 @@ 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 // codes that aren't 2xx, 400, 422, or 503 won't return JSON objects
if code/100 != 2 && code != 400 && code != 422 && code != 503 { if code/100 != 2 && code != 400 && code != 422 && code != 503 {
return APIResponse{}, &Error{ return APIResponse{
HTTPStatusCode: code,
}, &Error{
Type: ErrBadResponse, Type: ErrBadResponse,
Msg: fmt.Sprintf("unknown response code %d", code), Msg: fmt.Sprintf("unknown response code %d", code),
} }
@ -102,7 +106,9 @@ func (c *httpAPIClient) Do(ctx context.Context, verb, endpoint string, query url
if klog.V(8).Enabled() { if klog.V(8).Enabled() {
data, err := io.ReadAll(body) data, err := io.ReadAll(body)
if err != nil { 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)) klog.Infof("Response Body: %s", string(data))
body = bytes.NewReader(data) body = bytes.NewReader(data)
@ -110,12 +116,16 @@ func (c *httpAPIClient) Do(ctx context.Context, verb, endpoint string, query url
var res APIResponse var res APIResponse
if err = json.NewDecoder(body).Decode(&res); err != nil { if err = json.NewDecoder(body).Decode(&res); err != nil {
return APIResponse{}, &Error{ return APIResponse{
HTTPStatusCode: code,
}, &Error{
Type: ErrBadResponse, Type: ErrBadResponse,
Msg: err.Error(), Msg: err.Error(),
} }
} }
res.HTTPStatusCode = code
if res.Status == ResponseError { if res.Status == ResponseError {
return res, &Error{ return res, &Error{
Type: res.ErrorType, Type: res.ErrorType,

View file

@ -92,18 +92,8 @@ func (c *instrumentedGenericClient) Do(ctx context.Context, verb, endpoint strin
var resp client.APIResponse var resp client.APIResponse
defer func() { defer func() {
endTime := time.Now() endTime := time.Now()
if err != nil {
if apiErr, wasAPIErr := err.(*client.Error); wasAPIErr { apiRequestsTotal.With(prometheus.Labels{"code": string(resp.HTTPStatusCode), "path": endpoint, "server": c.serverName}).Inc()
// 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()
}
queryLatency.With(prometheus.Labels{"path": endpoint, "server": c.serverName}).Observe(endTime.Sub(startTime).Seconds()) queryLatency.With(prometheus.Labels{"path": endpoint, "server": c.serverName}).Observe(endTime.Sub(startTime).Seconds())
}() }()

View file

@ -60,4 +60,7 @@ type APIResponse struct {
ErrorType ErrorType `json:"errorType"` ErrorType ErrorType `json:"errorType"`
// Error is the error message, if this is an error response. // Error is the error message, if this is an error response.
Error string `json:"error"` Error string `json:"error"`
// StatusCode is the HTTP status code of the response.
HTTPStatusCode int `json:"httpStatusCode"`
} }