Save resopnse to span.
This commit is contained in:
parent
2ccf846b91
commit
ec843af272
34
fcm.go
34
fcm.go
|
@ -29,6 +29,7 @@ import (
|
|||
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
)
|
||||
|
||||
const tracerName = "git.bit5.ru/backend/fcm"
|
||||
|
@ -164,11 +165,15 @@ func (c *Client) doSendRequest(ctx context.Context, req SendRequest, loggerEnabl
|
|||
|
||||
accessToken, err := c.ts.Token()
|
||||
if err != nil {
|
||||
span.SetStatus(codes.Error, err.Error())
|
||||
span.RecordError(err)
|
||||
return SendResponse{}, err
|
||||
}
|
||||
|
||||
data, err := json.Marshal(req)
|
||||
if err != nil {
|
||||
span.SetStatus(codes.Error, err.Error())
|
||||
span.RecordError(err)
|
||||
return SendResponse{}, errors.WithStack(err)
|
||||
}
|
||||
if loggerEnabled {
|
||||
|
@ -177,6 +182,8 @@ func (c *Client) doSendRequest(ctx context.Context, req SendRequest, loggerEnabl
|
|||
|
||||
request, err := http.NewRequest(http.MethodPost, c.cfg.SendEndpoint, bytes.NewReader(data))
|
||||
if err != nil {
|
||||
span.SetStatus(codes.Error, err.Error())
|
||||
span.RecordError(err)
|
||||
return SendResponse{}, errors.WithStack(err)
|
||||
}
|
||||
|
||||
|
@ -189,22 +196,33 @@ func (c *Client) doSendRequest(ctx context.Context, req SendRequest, loggerEnabl
|
|||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(response.Body)
|
||||
if err != nil {
|
||||
span.SetStatus(codes.Error, err.Error())
|
||||
span.RecordError(err)
|
||||
return SendResponse{}, errors.WithStack(err)
|
||||
}
|
||||
|
||||
bodyStr := string(body)
|
||||
|
||||
span.SetAttributes(
|
||||
attribute.Int("response.status_code", response.StatusCode),
|
||||
attribute.String("response_body", bodyStr),
|
||||
)
|
||||
|
||||
if response.StatusCode != http.StatusOK {
|
||||
body, err := ioutil.ReadAll(response.Body)
|
||||
if err != nil {
|
||||
return SendResponse{}, errors.WithStack(err)
|
||||
}
|
||||
|
||||
return SendResponse{}, errors.New(string(body))
|
||||
err := errors.Errorf("Status is not OK. Status: %d. Body: %s", response.StatusCode, bodyStr)
|
||||
span.SetStatus(codes.Error, err.Error())
|
||||
span.RecordError(err)
|
||||
return SendResponse{}, err
|
||||
}
|
||||
|
||||
var resp SendResponse
|
||||
if err := json.NewDecoder(response.Body).Decode(&resp); err != nil {
|
||||
return SendResponse{}, errors.WithStack(err)
|
||||
if err := json.Unmarshal(body, &resp); err != nil {
|
||||
newErr := errors.Errorf("Can not parse send response as JSON. Response: %q. Error: %v", string(body), err)
|
||||
span.SetStatus(codes.Error, err.Error())
|
||||
span.RecordError(err)
|
||||
return SendResponse{}, newErr
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
|
|
Loading…
Reference in New Issue