-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreceived_message.go
55 lines (43 loc) · 1.21 KB
/
received_message.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package pubsub_prometheus
import (
"context"
"time"
"github.com/hmoragrega/pubsub"
"github.com/prometheus/client_golang/prometheus"
)
var _ pubsub.ReceivedMessage = (*receivedMessage)(nil)
type receivedMessage struct {
pubsub.ReceivedMessage
consumerName string
ackCounter *prometheus.CounterVec
}
func wrap(msg pubsub.ReceivedMessage, consumerName string, ackCounter *prometheus.CounterVec) *receivedMessage {
return &receivedMessage{
ReceivedMessage: msg,
consumerName: consumerName,
ackCounter: ackCounter,
}
}
func (m *receivedMessage) Ack(ctx context.Context) (err error) {
defer func() {
m.count("ack", err)
}()
return m.ReceivedMessage.Ack(ctx)
}
func (m *receivedMessage) NAck(ctx context.Context) (err error) {
defer func() {
m.count("nack", err)
}()
return m.ReceivedMessage.NAck(ctx)
}
func (m *receivedMessage) ReSchedule(ctx context.Context, delay time.Duration) (err error) {
defer func() {
m.count("re-schedule", err)
}()
return m.ReceivedMessage.ReSchedule(ctx, delay)
}
func (m *receivedMessage) count(operation string, err error) {
m.ackCounter.
With(metricLabels(map[string]string{operationKey: operation}, m.consumerName, m.ReceivedMessage, err)).
Add(1)
}