|
| 1 | +package processor |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
| 6 | + |
| 7 | + "github.com/Azure/azure-sdk-for-go/sdk/messaging/azservicebus" |
| 8 | + prom "github.com/prometheus/client_golang/prometheus" |
| 9 | + dto "github.com/prometheus/client_model/go" |
| 10 | +) |
| 11 | + |
| 12 | +const ( |
| 13 | + subsystem = "goshuttle_handler" |
| 14 | + messageTypeLabel = "messageType" |
| 15 | + deliveryCountLabel = "deliveryCount" |
| 16 | + successLabel = "success" |
| 17 | +) |
| 18 | + |
| 19 | +var ( |
| 20 | + metricsRegistry = newRegistry() |
| 21 | + // Processor exposes a Recorder interface to manipulate the Processor metrics. |
| 22 | + Metric Recorder = metricsRegistry |
| 23 | +) |
| 24 | + |
| 25 | +func newRegistry() *Registry { |
| 26 | + return &Registry{ |
| 27 | + MessageReceivedCount: prom.NewCounterVec(prom.CounterOpts{ |
| 28 | + Name: "message_received_total", |
| 29 | + Help: "total number of messages received by the processor", |
| 30 | + Subsystem: subsystem, |
| 31 | + }, []string{}), |
| 32 | + MessageHandledCount: prom.NewCounterVec(prom.CounterOpts{ |
| 33 | + Name: "message_handled_total", |
| 34 | + Help: "total number of messages handled by this handler", |
| 35 | + Subsystem: subsystem, |
| 36 | + }, []string{messageTypeLabel, deliveryCountLabel}), |
| 37 | + MessageLockRenewedCount: prom.NewCounterVec(prom.CounterOpts{ |
| 38 | + Name: "message_lock_renewed_total", |
| 39 | + Help: "total number of message lock renewal", |
| 40 | + Subsystem: subsystem, |
| 41 | + }, []string{messageTypeLabel, successLabel}), |
| 42 | + MessageDeadlineReachedCount: prom.NewCounterVec(prom.CounterOpts{ |
| 43 | + Name: "message_deadline_reached_total", |
| 44 | + Help: "total number of message lock renewal", |
| 45 | + Subsystem: subsystem, |
| 46 | + }, []string{messageTypeLabel}), |
| 47 | + ConcurrentMessageCount: prom.NewGaugeVec(prom.GaugeOpts{ |
| 48 | + Name: "concurrent_message_count", |
| 49 | + Help: "number of messages being handled concurrently", |
| 50 | + Subsystem: subsystem, |
| 51 | + }, []string{messageTypeLabel}), |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func getMessageTypeLabel(msg *azservicebus.ReceivedMessage) prom.Labels { |
| 56 | + typeName := msg.ApplicationProperties["type"] |
| 57 | + return map[string]string{ |
| 58 | + messageTypeLabel: fmt.Sprintf("%s", typeName), |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func (m *Registry) Init(reg prom.Registerer) { |
| 63 | + reg.MustRegister( |
| 64 | + m.MessageReceivedCount, |
| 65 | + m.MessageHandledCount, |
| 66 | + m.MessageLockRenewedCount, |
| 67 | + m.MessageDeadlineReachedCount, |
| 68 | + m.ConcurrentMessageCount) |
| 69 | +} |
| 70 | + |
| 71 | +type Registry struct { |
| 72 | + MessageReceivedCount *prom.CounterVec |
| 73 | + MessageHandledCount *prom.CounterVec |
| 74 | + MessageLockRenewedCount *prom.CounterVec |
| 75 | + MessageDeadlineReachedCount *prom.CounterVec |
| 76 | + ConcurrentMessageCount *prom.GaugeVec |
| 77 | +} |
| 78 | + |
| 79 | +// Recorder allows to initialize the metric registry and increase/decrease the registered metrics at runtime. |
| 80 | +type Recorder interface { |
| 81 | + Init(registerer prom.Registerer) |
| 82 | + IncMessageDeadlineReachedCount(msg *azservicebus.ReceivedMessage) |
| 83 | + IncMessageLockRenewedFailure(msg *azservicebus.ReceivedMessage) |
| 84 | + IncMessageLockRenewedSuccess(msg *azservicebus.ReceivedMessage) |
| 85 | + DecConcurrentMessageCount(msg *azservicebus.ReceivedMessage) |
| 86 | + IncMessageHandled(msg *azservicebus.ReceivedMessage) |
| 87 | + IncMessageReceived(float64) |
| 88 | + IncConcurrentMessageCount(msg *azservicebus.ReceivedMessage) |
| 89 | +} |
| 90 | + |
| 91 | +// IncMessageLockRenewedSuccess increase the message lock renewal success counter |
| 92 | +func (m *Registry) IncMessageLockRenewedSuccess(msg *azservicebus.ReceivedMessage) { |
| 93 | + labels := getMessageTypeLabel(msg) |
| 94 | + labels[successLabel] = "true" |
| 95 | + m.MessageLockRenewedCount.With(labels).Inc() |
| 96 | +} |
| 97 | + |
| 98 | +// IncMessageLockRenewedFailure increase the message lock renewal failure counter |
| 99 | +func (m *Registry) IncMessageLockRenewedFailure(msg *azservicebus.ReceivedMessage) { |
| 100 | + labels := getMessageTypeLabel(msg) |
| 101 | + labels[successLabel] = "false" |
| 102 | + m.MessageLockRenewedCount.With(labels).Inc() |
| 103 | +} |
| 104 | + |
| 105 | +// IncMessageHandled increase the message Handled |
| 106 | +func (m *Registry) IncMessageHandled(msg *azservicebus.ReceivedMessage) { |
| 107 | + labels := getMessageTypeLabel(msg) |
| 108 | + labels[deliveryCountLabel] = strconv.FormatUint(uint64(msg.DeliveryCount), 10) |
| 109 | + m.MessageHandledCount.With(labels).Inc() |
| 110 | +} |
| 111 | + |
| 112 | +// IncConcurrentMessageCount increases the concurrent message counter |
| 113 | +func (m *Registry) IncConcurrentMessageCount(msg *azservicebus.ReceivedMessage) { |
| 114 | + m.ConcurrentMessageCount.With(getMessageTypeLabel(msg)).Inc() |
| 115 | +} |
| 116 | + |
| 117 | +// DecConcurrentMessageCount decreases the concurrent message counter |
| 118 | +func (m *Registry) DecConcurrentMessageCount(msg *azservicebus.ReceivedMessage) { |
| 119 | + m.ConcurrentMessageCount.With(getMessageTypeLabel(msg)).Dec() |
| 120 | +} |
| 121 | + |
| 122 | +// IncMessageDeadlineReachedCount increases the message deadline reached counter |
| 123 | +func (m *Registry) IncMessageDeadlineReachedCount(msg *azservicebus.ReceivedMessage) { |
| 124 | + labels := getMessageTypeLabel(msg) |
| 125 | + m.MessageDeadlineReachedCount.With(labels).Inc() |
| 126 | +} |
| 127 | + |
| 128 | +// IncMessageReceived increases the message received counter |
| 129 | +func (m *Registry) IncMessageReceived(count float64) { |
| 130 | + m.MessageReceivedCount.With(map[string]string{}).Add(count) |
| 131 | +} |
| 132 | + |
| 133 | +// Informer allows to inspect metrics value stored in the registry at runtime |
| 134 | +type Informer struct { |
| 135 | + registry *Registry |
| 136 | +} |
| 137 | + |
| 138 | +// NewInformer creates an Informer for the current registry |
| 139 | +func NewInformer() *Informer { |
| 140 | + return &Informer{registry: metricsRegistry} |
| 141 | +} |
| 142 | + |
| 143 | +// GetMessageLockRenewedFailureCount retrieves the current value of the MessageLockRenewedFailureCount metric |
| 144 | +func (i *Informer) GetMessageLockRenewedFailureCount() (float64, error) { |
| 145 | + var total float64 |
| 146 | + collect(i.registry.MessageLockRenewedCount, func(m dto.Metric) { |
| 147 | + if !hasLabel(m, successLabel, "false") { |
| 148 | + return |
| 149 | + } |
| 150 | + total += m.GetCounter().GetValue() |
| 151 | + }) |
| 152 | + return total, nil |
| 153 | +} |
| 154 | + |
| 155 | +func hasLabel(m dto.Metric, key string, value string) bool { |
| 156 | + for _, pair := range m.Label { |
| 157 | + if pair == nil { |
| 158 | + continue |
| 159 | + } |
| 160 | + if pair.GetName() == key && pair.GetValue() == value { |
| 161 | + return true |
| 162 | + } |
| 163 | + } |
| 164 | + return false |
| 165 | +} |
| 166 | + |
| 167 | +// collect calls the function for each metric associated with the Collector |
| 168 | +func collect(col prom.Collector, do func(dto.Metric)) { |
| 169 | + c := make(chan prom.Metric) |
| 170 | + go func(c chan prom.Metric) { |
| 171 | + col.Collect(c) |
| 172 | + close(c) |
| 173 | + }(c) |
| 174 | + for x := range c { // eg range across distinct label vector values |
| 175 | + m := dto.Metric{} |
| 176 | + _ = x.Write(&m) |
| 177 | + do(m) |
| 178 | + } |
| 179 | +} |
0 commit comments