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"
|
||||||
"go.opentelemetry.io/otel/attribute"
|
"go.opentelemetry.io/otel/attribute"
|
||||||
|
"go.opentelemetry.io/otel/codes"
|
||||||
)
|
)
|
||||||
|
|
||||||
const tracerName = "git.bit5.ru/backend/fcm"
|
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()
|
accessToken, err := c.ts.Token()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
span.SetStatus(codes.Error, err.Error())
|
||||||
|
span.RecordError(err)
|
||||||
return SendResponse{}, err
|
return SendResponse{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := json.Marshal(req)
|
data, err := json.Marshal(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
span.SetStatus(codes.Error, err.Error())
|
||||||
|
span.RecordError(err)
|
||||||
return SendResponse{}, errors.WithStack(err)
|
return SendResponse{}, errors.WithStack(err)
|
||||||
}
|
}
|
||||||
if loggerEnabled {
|
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))
|
request, err := http.NewRequest(http.MethodPost, c.cfg.SendEndpoint, bytes.NewReader(data))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
span.SetStatus(codes.Error, err.Error())
|
||||||
|
span.RecordError(err)
|
||||||
return SendResponse{}, errors.WithStack(err)
|
return SendResponse{}, errors.WithStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -189,22 +196,33 @@ func (c *Client) doSendRequest(ctx context.Context, req SendRequest, loggerEnabl
|
||||||
}
|
}
|
||||||
defer response.Body.Close()
|
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(
|
span.SetAttributes(
|
||||||
attribute.Int("response.status_code", response.StatusCode),
|
attribute.Int("response.status_code", response.StatusCode),
|
||||||
|
attribute.String("response_body", bodyStr),
|
||||||
)
|
)
|
||||||
|
|
||||||
if response.StatusCode != http.StatusOK {
|
if response.StatusCode != http.StatusOK {
|
||||||
body, err := ioutil.ReadAll(response.Body)
|
err := errors.Errorf("Status is not OK. Status: %d. Body: %s", response.StatusCode, bodyStr)
|
||||||
if err != nil {
|
span.SetStatus(codes.Error, err.Error())
|
||||||
return SendResponse{}, errors.WithStack(err)
|
span.RecordError(err)
|
||||||
}
|
return SendResponse{}, err
|
||||||
|
|
||||||
return SendResponse{}, errors.New(string(body))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var resp SendResponse
|
var resp SendResponse
|
||||||
if err := json.NewDecoder(response.Body).Decode(&resp); err != nil {
|
if err := json.Unmarshal(body, &resp); err != nil {
|
||||||
return SendResponse{}, errors.WithStack(err)
|
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
|
return resp, nil
|
||||||
|
|
Loading…
Reference in New Issue