Skip to content

Commit

Permalink
adding metric to sender (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
karenychen authored Aug 18, 2023
1 parent 2ea25ad commit cd2cf51
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions v2/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/Azure/azure-sdk-for-go/sdk/messaging/azservicebus"
"github.com/Azure/go-shuttle/v2/metrics/sender"
)

const (
Expand Down Expand Up @@ -77,8 +78,14 @@ func (d *Sender) SendMessage(ctx context.Context, mb MessageBody, options ...fun

select {
case <-ctx.Done():
sender.Metric.IncSendMessageFailureCount()
return fmt.Errorf("failed to send message: %w", ctx.Err())
case err := <-errChan:
if err == nil {
sender.Metric.IncSendMessageSuccessCount()
} else {
sender.Metric.IncSendMessageFailureCount()
}
return err
}

Expand Down Expand Up @@ -129,9 +136,6 @@ func (d *Sender) SendMessageBatch(ctx context.Context, messages []*azservicebus.
ctx, cancel = context.WithTimeout(ctx, d.options.SendTimeout)
defer cancel()
}
if err := d.sbSender.SendMessageBatch(ctx, batch, nil); err != nil {
return fmt.Errorf("failed to send message batch: %w", err)
}

errChan := make(chan error)

Expand All @@ -144,8 +148,14 @@ func (d *Sender) SendMessageBatch(ctx context.Context, messages []*azservicebus.

select {
case <-ctx.Done():
sender.Metric.IncSendMessageFailureCount()
return fmt.Errorf("failed to send message batch: %w", ctx.Err())
case err := <-errChan:
if err == nil {
sender.Metric.IncSendMessageSuccessCount()
} else {
sender.Metric.IncSendMessageFailureCount()
}
return err
}

Expand Down

0 comments on commit cd2cf51

Please sign in to comment.