|
| 1 | +package watchers |
| 2 | + |
| 3 | +import ( |
| 4 | + "sync" |
| 5 | + "time" |
| 6 | +) |
| 7 | + |
| 8 | +type MetricsProvider interface { |
| 9 | + NewAddedMetric(kind string) CounterMetric |
| 10 | + NewDepthMetric(kind string) GaugeMetric |
| 11 | + NewDelayedMetric(kind string) ObservableMetric |
| 12 | + NewWorkDurationMetric(kind string) ObservableMetric |
| 13 | + NewRetriesMetric(kind string) CounterMetric |
| 14 | +} |
| 15 | + |
| 16 | +type CounterMetric interface { |
| 17 | + Inc() |
| 18 | +} |
| 19 | +type GaugeMetric interface { |
| 20 | + CounterMetric |
| 21 | + Dec() |
| 22 | +} |
| 23 | +type ObservableMetric interface { |
| 24 | + Observe(float64) |
| 25 | +} |
| 26 | + |
| 27 | +type noopMetric struct{} |
| 28 | + |
| 29 | +func (noopMetric) Inc() {} |
| 30 | +func (noopMetric) Dec() {} |
| 31 | +func (noopMetric) Observe(float64) {} |
| 32 | + |
| 33 | +type noopMetricsProvider struct{} |
| 34 | + |
| 35 | +func (noopMetricsProvider) NewAddedMetric(kind string) CounterMetric { return noopMetric{} } |
| 36 | +func (noopMetricsProvider) NewDepthMetric(kind string) GaugeMetric { return noopMetric{} } |
| 37 | +func (noopMetricsProvider) NewDelayedMetric(kind string) ObservableMetric { return noopMetric{} } |
| 38 | +func (noopMetricsProvider) NewWorkDurationMetric(kind string) ObservableMetric { return noopMetric{} } |
| 39 | +func (noopMetricsProvider) NewRetriesMetric(kind string) CounterMetric { return noopMetric{} } |
| 40 | + |
| 41 | +type metricsSet struct { |
| 42 | + Added CounterMetric |
| 43 | + Depth GaugeMetric |
| 44 | + Delayed ObservableMetric |
| 45 | + WorkDuration ObservableMetric |
| 46 | + Retries CounterMetric |
| 47 | +} |
| 48 | + |
| 49 | +type metricsQueue struct { |
| 50 | + provider MetricsProvider |
| 51 | + |
| 52 | + metricsMu sync.Mutex |
| 53 | + metrics map[string]metricsSet |
| 54 | + pendingMu sync.Mutex |
| 55 | + pending map[ResourceChange]time.Time |
| 56 | +} |
| 57 | + |
| 58 | +func (q *metricsQueue) add(evt ResourceChange) { |
| 59 | + q.pendingMu.Lock() |
| 60 | + defer q.pendingMu.Unlock() |
| 61 | + if _, ok := q.pending[evt]; ok { |
| 62 | + return |
| 63 | + } |
| 64 | + q.pending[evt] = time.Now() |
| 65 | + ms := q.metricsFor(evt.Handler.Kind()) |
| 66 | + ms.Added.Inc() |
| 67 | + ms.Depth.Inc() |
| 68 | +} |
| 69 | + |
| 70 | +type metricsClose func(evt ResourceChange, retry bool) |
| 71 | + |
| 72 | +func (q *metricsQueue) get(evt ResourceChange) metricsClose { |
| 73 | + q.pendingMu.Lock() |
| 74 | + defer q.pendingMu.Unlock() |
| 75 | + queuedAt, ok := q.pending[evt] |
| 76 | + if !ok { |
| 77 | + return q.done(time.Now()) |
| 78 | + } |
| 79 | + q.metricsFor(evt.Handler.Kind()).Delayed.Observe(time.Since(queuedAt).Seconds()) |
| 80 | + delete(q.pending, evt) |
| 81 | + return q.done(time.Now()) |
| 82 | +} |
| 83 | + |
| 84 | +func (q *metricsQueue) done(startTime time.Time) metricsClose { |
| 85 | + return func(evt ResourceChange, retry bool) { |
| 86 | + ms := q.metricsFor(evt.Handler.Kind()) |
| 87 | + ms.WorkDuration.Observe(time.Since(startTime).Seconds()) |
| 88 | + ms.Depth.Dec() |
| 89 | + if retry { |
| 90 | + ms.Retries.Inc() |
| 91 | + q.add(evt) |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func (q *metricsQueue) metricsFor(kind string) metricsSet { |
| 97 | + q.metricsMu.Lock() |
| 98 | + defer q.metricsMu.Unlock() |
| 99 | + if q.metrics == nil { |
| 100 | + q.metrics = map[string]metricsSet{} |
| 101 | + } |
| 102 | + if m, ok := q.metrics[kind]; ok { |
| 103 | + return m |
| 104 | + } |
| 105 | + m := metricsSet{ |
| 106 | + Added: q.provider.NewAddedMetric(kind), |
| 107 | + Depth: q.provider.NewDepthMetric(kind), |
| 108 | + Delayed: q.provider.NewDelayedMetric(kind), |
| 109 | + WorkDuration: q.provider.NewWorkDurationMetric(kind), |
| 110 | + Retries: q.provider.NewRetriesMetric(kind), |
| 111 | + } |
| 112 | + q.metrics[kind] = m |
| 113 | + return m |
| 114 | +} |
0 commit comments