-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetrics.go
More file actions
141 lines (114 loc) · 4.14 KB
/
metrics.go
File metadata and controls
141 lines (114 loc) · 4.14 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package sentry_transport
import (
"sync/atomic"
"github.com/prometheus/client_golang/prometheus"
)
const (
namespace = "rr_sentry_transport"
)
// metricsCollector implements prometheus.Collector interface
type metricsCollector struct {
// Atomic counters for thread-safe metric updates
deadLetterEvents *uint64 // Total events moved to dead letter queue
droppedEvents *uint64 // Total dropped events (queue full, etc.)
failedEvents *uint64 // Total failed events (HTTP errors, etc.)
successfulEvents *uint64 // Total successfully sent events
// Prometheus metric descriptors
deadLetterEventsDesc *prometheus.Desc
droppedEventsDesc *prometheus.Desc
failedEventsDesc *prometheus.Desc
successfulEventsDesc *prometheus.Desc
// Vector metric for events by type
eventsByType *prometheus.CounterVec
}
// newMetricsCollector creates a new metrics collector
func newMetricsCollector() *metricsCollector {
return &metricsCollector{
// Initialize atomic counters
deadLetterEvents: ptrTo(uint64(0)),
droppedEvents: ptrTo(uint64(0)),
failedEvents: ptrTo(uint64(0)),
successfulEvents: ptrTo(uint64(0)),
// Create metric descriptors
deadLetterEventsDesc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "dead_letter_events_total"),
"Total number of events moved to dead letter queue",
nil, nil),
droppedEventsDesc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "dropped_events_total"),
"Total number of dropped Sentry events",
nil, nil),
failedEventsDesc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "failed_events_total"),
"Total number of failed Sentry events",
nil, nil),
successfulEventsDesc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "successful_events_total"),
"Total number of successfully sent events",
nil, nil),
// Vector metric with event type label
eventsByType: prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: prometheus.BuildFQName(namespace, "", "events_by_type_total"),
Help: "Total number of events by event type",
},
[]string{"event_type"}),
}
}
// Public methods for updating metrics (called from business logic)
// IncDeadLetterEvents increments dead letter events counter
func (mc *metricsCollector) IncDeadLetterEvents() {
atomic.AddUint64(mc.deadLetterEvents, 1)
}
// IncDroppedEvents increments dropped events counter
func (mc *metricsCollector) IncDroppedEvents() {
atomic.AddUint64(mc.droppedEvents, 1)
}
// IncFailedEvents increments failed events counter
func (mc *metricsCollector) IncFailedEvents() {
atomic.AddUint64(mc.failedEvents, 1)
}
// IncSuccessfulEvents increments successful events counter
func (mc *metricsCollector) IncSuccessfulEvents() {
atomic.AddUint64(mc.successfulEvents, 1)
}
// IncEventsByType increments events counter for specific event type
func (mc *metricsCollector) IncEventsByType(eventType string) {
mc.eventsByType.WithLabelValues(eventType).Inc()
}
// Implement prometheus.Collector interface
// Describe sends all metric descriptions to Prometheus
func (mc *metricsCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- mc.deadLetterEventsDesc
ch <- mc.droppedEventsDesc
ch <- mc.failedEventsDesc
ch <- mc.successfulEventsDesc
// Vector metric handles its own description
mc.eventsByType.Describe(ch)
}
// Collect sends current metric values to Prometheus
func (mc *metricsCollector) Collect(ch chan<- prometheus.Metric) {
// Send current values of atomic counters
ch <- prometheus.MustNewConstMetric(
mc.deadLetterEventsDesc,
prometheus.CounterValue,
float64(atomic.LoadUint64(mc.deadLetterEvents)))
ch <- prometheus.MustNewConstMetric(
mc.droppedEventsDesc,
prometheus.CounterValue,
float64(atomic.LoadUint64(mc.droppedEvents)))
ch <- prometheus.MustNewConstMetric(
mc.failedEventsDesc,
prometheus.CounterValue,
float64(atomic.LoadUint64(mc.failedEvents)))
ch <- prometheus.MustNewConstMetric(
mc.successfulEventsDesc,
prometheus.CounterValue,
float64(atomic.LoadUint64(mc.successfulEvents)))
// Vector metric collects itself
mc.eventsByType.Collect(ch)
}
// Helper function for pointer creation
func ptrTo[T any](v T) *T {
return &v
}