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