Add logs.

This commit is contained in:
Владислав Весельский 2025-01-28 17:04:59 +03:00
parent 6bda66b44d
commit bc73d56c8b
1 changed files with 29 additions and 15 deletions

44
fcm.go
View File

@ -410,7 +410,18 @@ func (c *Client) SendEach(ctx context.Context, messages []Message) (MessageMulti
ctx, span := tracer.Start(ctx, "Client.SendEach")
defer span.End()
return c.doSendEachInBatch(ctx, messages, false)
resp, err := c.doSendEachInBatch(ctx, messages, false)
if err != nil {
span.SetStatus(codes.Error, err.Error())
span.RecordError(err)
return MessageMultiSendResponse{}, err
}
if resp.Failed > 0 {
span.SetStatus(codes.Error, "Some notifications not sent.")
}
return resp, nil
}
func (c *Client) doSendEachInBatch(
@ -426,8 +437,12 @@ func (c *Client) doSendEachInBatch(
if messageCount == 0 {
return MessageMultiSendResponse{}, nil
}
if messageCount > maxMessages {
return MessageMultiSendResponse{}, errors.New(fmt.Sprintf("messages limit (%d) exceeded: %d", maxMessages, messageCount))
err := errors.New(fmt.Sprintf("messages limit (%d) exceeded: %d", maxMessages, messageCount))
span.SetStatus(codes.Error, err.Error())
span.RecordError(err)
return MessageMultiSendResponse{}, err
}
var responses = make([]MessageSendResponse, len(messages))
@ -453,6 +468,7 @@ func (c *Client) doSendEachInBatch(
MessageID: resp,
}
} else {
span.SetStatus(codes.Error, "Some notifications not sent.")
responses[idx] = MessageSendResponse{
Success: false,
Error: err,
@ -463,30 +479,28 @@ func (c *Client) doSendEachInBatch(
// Wait for all Validate/Send calls to finish
wg.Wait()
successAmount := 0
sentAmount := 0
for _, r := range responses {
if r.Success {
successAmount++
sentAmount++
}
}
failedAmount := len(responses) - successAmount
failedAmount := len(responses) - sentAmount
span.SetAttributes(
attribute.Int("res.total", len(responses)),
attribute.Int("res.sent", successAmount),
attribute.Int("res.failed", failedAmount),
attribute.Int("resp.total", len(responses)),
attribute.Int("resp.sent_amount", sentAmount),
attribute.Int("resp.failed_amount", failedAmount),
)
if failedAmount > 0 {
span.SetStatus(codes.Error, "Got errors from method Client.Send().")
resp := MessageMultiSendResponse{
Responses: responses,
Sent: sentAmount,
Failed: failedAmount,
}
return MessageMultiSendResponse{
Responses: responses,
Sent: successAmount,
Failed: failedAmount,
}, nil
return resp, nil
}
type MessageSendResponse struct {