|
1 | 1 | package metrics |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "fmt" |
5 | 6 | "strings" |
| 7 | + "time" |
6 | 8 |
|
7 | 9 | "github.com/prometheus/client_golang/prometheus" |
| 10 | + "github.com/sirupsen/logrus" |
8 | 11 |
|
9 | 12 | "github.com/nikiforov-soft/yasp/config" |
10 | 13 | "github.com/nikiforov-soft/yasp/internal/syncx" |
11 | 14 | ) |
12 | 15 |
|
13 | 16 | type collector struct { |
14 | | - metricsMapping *config.MetricsMapping |
15 | | - metrics *syncx.Map[string, *metric] |
| 17 | + closeCtx context.Context |
| 18 | + closeCtxCancel context.CancelFunc |
| 19 | + desc *prometheus.Desc |
| 20 | + metricVec any |
| 21 | + metrics *syncx.Map[string, metricHistory] |
| 22 | + metricsMapping *config.MetricsMapping |
| 23 | + stalenessInterval time.Duration |
| 24 | +} |
| 25 | + |
| 26 | +func newCollector(metricsMapping *config.MetricsMapping, stalenessInterval time.Duration) (*collector, error) { |
| 27 | + metricVec, err := newMetricVec(metricsMapping) |
| 28 | + if err != nil { |
| 29 | + return nil, err |
| 30 | + } |
| 31 | + |
| 32 | + closeCtx, closeCtxCancel := context.WithCancel(context.Background()) |
| 33 | + c := &collector{ |
| 34 | + closeCtx: closeCtx, |
| 35 | + closeCtxCancel: closeCtxCancel, |
| 36 | + desc: newDesc(metricsMapping), |
| 37 | + metricVec: metricVec, |
| 38 | + metrics: &syncx.Map[string, metricHistory]{}, |
| 39 | + metricsMapping: metricsMapping, |
| 40 | + stalenessInterval: stalenessInterval, |
| 41 | + } |
| 42 | + |
| 43 | + if err := prometheus.Register(c); err != nil { |
| 44 | + return nil, fmt.Errorf("failed to register metrics collector: %w", err) |
| 45 | + } |
| 46 | + |
| 47 | + if stalenessInterval > 0 { |
| 48 | + go c.runMetricsCleanupTask() |
| 49 | + } |
| 50 | + |
| 51 | + return c, nil |
16 | 52 | } |
17 | 53 |
|
18 | 54 | func (c *collector) Describe(ch chan<- *prometheus.Desc) { |
19 | | - ch <- c.buildDesc() |
| 55 | + ch <- c.desc |
20 | 56 | } |
21 | 57 |
|
22 | 58 | func (c *collector) Collect(ch chan<- prometheus.Metric) { |
23 | | - c.metrics.Range(func(_ string, m *metric) bool { |
24 | | - metricValue, err := c.collectMetrics(m) |
| 59 | + c.metrics.Range(func(_ string, m metricHistory) bool { |
| 60 | + metricValue, err := c.getMetric(m.labels) |
25 | 61 | if err != nil { |
26 | | - ch <- prometheus.NewInvalidMetric(c.buildDesc(), err) |
| 62 | + ch <- prometheus.NewInvalidMetric(c.desc, err) |
27 | 63 | } else { |
28 | | - ch <- prometheus.NewMetricWithTimestamp(m.timestamp, metricValue) |
| 64 | + ch <- prometheus.NewMetricWithTimestamp(m.lastUpdatedAt, metricValue) |
29 | 65 | } |
30 | 66 | return true |
31 | 67 | }) |
32 | 68 | } |
33 | 69 |
|
34 | | -func (c *collector) buildDesc() *prometheus.Desc { |
35 | | - return prometheus.NewDesc( |
36 | | - prometheus.BuildFQName(c.metricsMapping.Namespace, c.metricsMapping.Subsystem, c.metricsMapping.Name), |
37 | | - c.metricsMapping.Description, |
38 | | - c.metricsMapping.Labels, |
39 | | - nil, |
40 | | - ) |
| 70 | +func (c *collector) Close() { |
| 71 | + c.closeCtxCancel() |
| 72 | + prometheus.Unregister(c) |
| 73 | +} |
| 74 | + |
| 75 | +func (c *collector) Observe(value float64, labels prometheus.Labels) error { |
| 76 | + switch m := c.metricVec.(type) { |
| 77 | + case *prometheus.CounterVec: |
| 78 | + m.With(labels).Add(value) |
| 79 | + case *prometheus.GaugeVec: |
| 80 | + m.With(labels).Set(value) |
| 81 | + case *prometheus.HistogramVec: |
| 82 | + m.With(labels).Observe(value) |
| 83 | + case *prometheus.SummaryVec: |
| 84 | + m.With(labels).Observe(value) |
| 85 | + default: |
| 86 | + return fmt.Errorf("unknown metricVec type: %T", c.metricVec) |
| 87 | + } |
| 88 | + |
| 89 | + c.metrics.Store(computeHash(c.metricsMapping, labels), metricHistory{ |
| 90 | + labels: labels, |
| 91 | + lastUpdatedAt: time.Now(), |
| 92 | + }) |
| 93 | + |
| 94 | + return nil |
| 95 | +} |
| 96 | + |
| 97 | +func (c *collector) getMetric(labels prometheus.Labels) (prometheus.Metric, error) { |
| 98 | + switch m := c.metricVec.(type) { |
| 99 | + case *prometheus.CounterVec: |
| 100 | + return m.GetMetricWith(labels) |
| 101 | + case *prometheus.GaugeVec: |
| 102 | + return m.GetMetricWith(labels) |
| 103 | + case *prometheus.HistogramVec: |
| 104 | + return m.MetricVec.GetMetricWith(labels) |
| 105 | + case *prometheus.SummaryVec: |
| 106 | + return m.MetricVec.GetMetricWith(labels) |
| 107 | + default: |
| 108 | + return nil, fmt.Errorf("unsupported metricVec type: %T", m) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func (c *collector) runMetricsCleanupTask() { |
| 113 | + ticker := time.NewTicker(time.Second) |
| 114 | + for { |
| 115 | + select { |
| 116 | + case <-c.closeCtx.Done(): |
| 117 | + return |
| 118 | + case <-ticker.C: |
| 119 | + c.pruneStaleMetrics() |
| 120 | + } |
| 121 | + } |
41 | 122 | } |
42 | 123 |
|
43 | | -func (c *collector) collectMetrics(m *metric) (prometheus.Metric, error) { |
44 | | - desc := c.buildDesc() |
45 | | - labels := flattenLabels(c.metricsMapping.Labels, m.labels) |
| 124 | +func (c *collector) pruneStaleMetrics() { |
| 125 | + var keysToDelete []string |
| 126 | + c.metrics.Range(func(key string, value metricHistory) bool { |
| 127 | + if time.Since(value.lastUpdatedAt) > c.stalenessInterval { |
| 128 | + keysToDelete = append(keysToDelete, key) |
| 129 | + } |
| 130 | + return true |
| 131 | + }) |
| 132 | + |
| 133 | + for _, key := range keysToDelete { |
| 134 | + mh, ok := c.metrics.LoadAndDelete(key) |
| 135 | + if !ok { |
| 136 | + continue |
| 137 | + } |
| 138 | + |
| 139 | + logrus. |
| 140 | + WithField("name", c.metricsMapping.Name). |
| 141 | + WithField("labels", mh.labels). |
| 142 | + WithField("updatedAt", mh.lastUpdatedAt). |
| 143 | + Debug("stale metric removed") |
46 | 144 |
|
47 | | - switch strings.ToLower(c.metricsMapping.Type) { |
| 145 | + switch m := c.metricVec.(type) { |
| 146 | + case *prometheus.CounterVec: |
| 147 | + m.Delete(mh.labels) |
| 148 | + case *prometheus.GaugeVec: |
| 149 | + m.Delete(mh.labels) |
| 150 | + case *prometheus.HistogramVec: |
| 151 | + m.Delete(mh.labels) |
| 152 | + case *prometheus.SummaryVec: |
| 153 | + m.Delete(mh.labels) |
| 154 | + } |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +func newMetricVec(mapping *config.MetricsMapping) (any, error) { |
| 159 | + switch strings.ToLower(mapping.Type) { |
48 | 160 | case "counter": |
49 | | - return prometheus.NewConstMetric( |
50 | | - desc, |
51 | | - prometheus.CounterValue, |
52 | | - m.value, |
53 | | - labels..., |
54 | | - ) |
| 161 | + return prometheus.NewCounterVec(prometheus.CounterOpts{ |
| 162 | + Namespace: mapping.Namespace, |
| 163 | + Subsystem: mapping.Subsystem, |
| 164 | + Name: mapping.Name, |
| 165 | + ConstLabels: mapping.ConstLabels, |
| 166 | + Help: mapping.Description, |
| 167 | + }, mapping.Labels), nil |
55 | 168 | case "gauge": |
56 | | - return prometheus.NewConstMetric( |
57 | | - desc, |
58 | | - prometheus.GaugeValue, |
59 | | - m.value, |
60 | | - labels..., |
61 | | - ) |
62 | | - case "histogram": |
63 | | - buckets := make(map[float64]uint64) |
64 | | - for bucket, count := range m.histogramBuckets { |
65 | | - buckets[bucket] = uint64(count) |
66 | | - } |
67 | | - return prometheus.NewConstHistogram( |
68 | | - desc, |
69 | | - m.histogramCount, |
70 | | - m.histogramSum, |
71 | | - buckets, |
72 | | - labels..., |
73 | | - ) |
| 169 | + return prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 170 | + Namespace: mapping.Namespace, |
| 171 | + Subsystem: mapping.Subsystem, |
| 172 | + Name: mapping.Name, |
| 173 | + ConstLabels: mapping.ConstLabels, |
| 174 | + Help: mapping.Description, |
| 175 | + }, mapping.Labels), nil |
74 | 176 | case "summary": |
75 | | - return prometheus.NewConstSummary( |
76 | | - desc, |
77 | | - m.summaryCount, |
78 | | - m.summarySum, |
79 | | - m.summaryQuantiles, |
80 | | - labels..., |
81 | | - ) |
| 177 | + return prometheus.NewSummaryVec(prometheus.SummaryOpts{ |
| 178 | + Namespace: mapping.Namespace, |
| 179 | + Subsystem: mapping.Subsystem, |
| 180 | + Name: mapping.Name, |
| 181 | + Help: mapping.Description, |
| 182 | + ConstLabels: mapping.ConstLabels, |
| 183 | + Objectives: mapping.Objectives, |
| 184 | + MaxAge: mapping.MaxAge, |
| 185 | + AgeBuckets: mapping.AgeBuckets, |
| 186 | + BufCap: mapping.BufCap, |
| 187 | + }, mapping.Labels), nil |
| 188 | + case "histogram": |
| 189 | + return prometheus.NewHistogramVec(prometheus.HistogramOpts{ |
| 190 | + Namespace: mapping.Namespace, |
| 191 | + Subsystem: mapping.Subsystem, |
| 192 | + Name: mapping.Name, |
| 193 | + Help: mapping.Description, |
| 194 | + ConstLabels: mapping.ConstLabels, |
| 195 | + Buckets: mapping.Buckets, |
| 196 | + NativeHistogramBucketFactor: mapping.NativeHistogramBucketFactor, |
| 197 | + NativeHistogramZeroThreshold: mapping.NativeHistogramZeroThreshold, |
| 198 | + NativeHistogramMaxBucketNumber: mapping.NativeHistogramMaxBucketNumber, |
| 199 | + NativeHistogramMinResetDuration: mapping.NativeHistogramMinResetDuration, |
| 200 | + NativeHistogramMaxZeroThreshold: mapping.NativeHistogramMaxZeroThreshold, |
| 201 | + NativeHistogramMaxExemplars: mapping.NativeHistogramMaxExemplars, |
| 202 | + NativeHistogramExemplarTTL: mapping.NativeHistogramExemplarTTL, |
| 203 | + }, mapping.Labels), nil |
82 | 204 | default: |
83 | | - return nil, fmt.Errorf("unsupported metric type: %s", c.metricsMapping.Type) |
| 205 | + return nil, fmt.Errorf("unsupported type: %s", mapping.Type) |
84 | 206 | } |
85 | 207 | } |
| 208 | + |
| 209 | +func newDesc(mapping *config.MetricsMapping) *prometheus.Desc { |
| 210 | + return prometheus.NewDesc( |
| 211 | + prometheus.BuildFQName(mapping.Namespace, mapping.Subsystem, mapping.Name), |
| 212 | + mapping.Description, |
| 213 | + mapping.Labels, |
| 214 | + mapping.ConstLabels, |
| 215 | + ) |
| 216 | +} |
0 commit comments