|
| 1 | +// (c) 2025, Ava Labs, Inc. All rights reserved. |
| 2 | +// See the file LICENSE for licensing terms. |
| 3 | + |
| 4 | +package prometheus |
| 5 | + |
| 6 | +import ( |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "sort" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/prometheus/client_golang/prometheus" |
| 13 | + |
| 14 | + "github.com/ava-labs/libevm/metrics" |
| 15 | + |
| 16 | + dto "github.com/prometheus/client_model/go" |
| 17 | +) |
| 18 | + |
| 19 | +type Gatherer struct { |
| 20 | + registry Registry |
| 21 | +} |
| 22 | + |
| 23 | +var _ prometheus.Gatherer = (*Gatherer)(nil) |
| 24 | + |
| 25 | +// NewGatherer returns a gatherer using the given registry. |
| 26 | +// Note this gatherer implements the [prometheus.Gatherer] interface. |
| 27 | +func NewGatherer(registry Registry) *Gatherer { |
| 28 | + return &Gatherer{ |
| 29 | + registry: registry, |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func (g *Gatherer) Gather() (mfs []*dto.MetricFamily, err error) { |
| 34 | + // Gather and pre-sort the metrics to avoid random listings |
| 35 | + var names []string |
| 36 | + g.registry.Each(func(name string, i interface{}) { |
| 37 | + names = append(names, name) |
| 38 | + }) |
| 39 | + sort.Strings(names) |
| 40 | + |
| 41 | + mfs = make([]*dto.MetricFamily, 0, len(names)) |
| 42 | + for _, name := range names { |
| 43 | + mf, err := metricFamily(g.registry, name) |
| 44 | + if errors.Is(err, errMetricSkip) { |
| 45 | + continue |
| 46 | + } |
| 47 | + mfs = append(mfs, mf) |
| 48 | + } |
| 49 | + |
| 50 | + return mfs, nil |
| 51 | +} |
| 52 | + |
| 53 | +var ( |
| 54 | + errMetricSkip = errors.New("metric skipped") |
| 55 | +) |
| 56 | + |
| 57 | +func ptrTo[T any](x T) *T { return &x } |
| 58 | + |
| 59 | +func metricFamily(registry Registry, name string) (mf *dto.MetricFamily, err error) { |
| 60 | + metric := registry.Get(name) |
| 61 | + name = strings.ReplaceAll(name, "/", "_") |
| 62 | + |
| 63 | + switch m := metric.(type) { |
| 64 | + case metrics.Counter: |
| 65 | + return &dto.MetricFamily{ |
| 66 | + Name: &name, |
| 67 | + Type: dto.MetricType_COUNTER.Enum(), |
| 68 | + Metric: []*dto.Metric{{ |
| 69 | + Counter: &dto.Counter{ |
| 70 | + Value: ptrTo(float64(m.Snapshot().Count())), |
| 71 | + }, |
| 72 | + }}, |
| 73 | + }, nil |
| 74 | + case metrics.CounterFloat64: |
| 75 | + return &dto.MetricFamily{ |
| 76 | + Name: &name, |
| 77 | + Type: dto.MetricType_COUNTER.Enum(), |
| 78 | + Metric: []*dto.Metric{{ |
| 79 | + Counter: &dto.Counter{ |
| 80 | + Value: ptrTo(m.Snapshot().Count()), |
| 81 | + }, |
| 82 | + }}, |
| 83 | + }, nil |
| 84 | + case metrics.Gauge: |
| 85 | + return &dto.MetricFamily{ |
| 86 | + Name: &name, |
| 87 | + Type: dto.MetricType_GAUGE.Enum(), |
| 88 | + Metric: []*dto.Metric{{ |
| 89 | + Gauge: &dto.Gauge{ |
| 90 | + Value: ptrTo(float64(m.Snapshot().Value())), |
| 91 | + }, |
| 92 | + }}, |
| 93 | + }, nil |
| 94 | + case metrics.GaugeFloat64: |
| 95 | + return &dto.MetricFamily{ |
| 96 | + Name: &name, |
| 97 | + Type: dto.MetricType_GAUGE.Enum(), |
| 98 | + Metric: []*dto.Metric{{ |
| 99 | + Gauge: &dto.Gauge{ |
| 100 | + Value: ptrTo(m.Snapshot().Value()), |
| 101 | + }, |
| 102 | + }}, |
| 103 | + }, nil |
| 104 | + case metrics.Histogram: |
| 105 | + snapshot := m.Snapshot() |
| 106 | + |
| 107 | + quantiles := []float64{.5, .75, .95, .99, .999, .9999} |
| 108 | + thresholds := snapshot.Percentiles(quantiles) |
| 109 | + dtoQuantiles := make([]*dto.Quantile, len(quantiles)) |
| 110 | + for i := range thresholds { |
| 111 | + dtoQuantiles[i] = &dto.Quantile{ |
| 112 | + Quantile: ptrTo(quantiles[i]), |
| 113 | + Value: ptrTo(thresholds[i]), |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return &dto.MetricFamily{ |
| 118 | + Name: &name, |
| 119 | + Type: dto.MetricType_SUMMARY.Enum(), |
| 120 | + Metric: []*dto.Metric{{ |
| 121 | + Summary: &dto.Summary{ |
| 122 | + SampleCount: ptrTo(uint64(snapshot.Count())), //nolint:gosec |
| 123 | + SampleSum: ptrTo(float64(snapshot.Sum())), |
| 124 | + Quantile: dtoQuantiles, |
| 125 | + }, |
| 126 | + }}, |
| 127 | + }, nil |
| 128 | + case metrics.Meter: |
| 129 | + return &dto.MetricFamily{ |
| 130 | + Name: &name, |
| 131 | + Type: dto.MetricType_GAUGE.Enum(), |
| 132 | + Metric: []*dto.Metric{{ |
| 133 | + Gauge: &dto.Gauge{ |
| 134 | + Value: ptrTo(float64(m.Snapshot().Count())), |
| 135 | + }, |
| 136 | + }}, |
| 137 | + }, nil |
| 138 | + case metrics.Timer: |
| 139 | + snapshot := m.Snapshot() |
| 140 | + |
| 141 | + quantiles := []float64{.5, .75, .95, .99, .999, .9999} |
| 142 | + thresholds := snapshot.Percentiles(quantiles) |
| 143 | + dtoQuantiles := make([]*dto.Quantile, len(quantiles)) |
| 144 | + for i := range thresholds { |
| 145 | + dtoQuantiles[i] = &dto.Quantile{ |
| 146 | + Quantile: ptrTo(quantiles[i]), |
| 147 | + Value: ptrTo(thresholds[i]), |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + return &dto.MetricFamily{ |
| 152 | + Name: &name, |
| 153 | + Type: dto.MetricType_SUMMARY.Enum(), |
| 154 | + Metric: []*dto.Metric{{ |
| 155 | + Summary: &dto.Summary{ |
| 156 | + SampleCount: ptrTo(uint64(snapshot.Count())), //nolint:gosec |
| 157 | + SampleSum: ptrTo(float64(snapshot.Sum())), |
| 158 | + Quantile: dtoQuantiles, |
| 159 | + }, |
| 160 | + }}, |
| 161 | + }, nil |
| 162 | + case metrics.ResettingTimer: |
| 163 | + snapshot := m.Snapshot() |
| 164 | + if snapshot.Count() == 0 { |
| 165 | + return nil, fmt.Errorf("%w: resetting timer metric count is zero", errMetricSkip) |
| 166 | + } |
| 167 | + |
| 168 | + pvShortPercent := []float64{50, 95, 99} |
| 169 | + thresholds := snapshot.Percentiles(pvShortPercent) |
| 170 | + dtoQuantiles := make([]*dto.Quantile, len(pvShortPercent)) |
| 171 | + for i := range pvShortPercent { |
| 172 | + dtoQuantiles[i] = &dto.Quantile{ |
| 173 | + Quantile: ptrTo(pvShortPercent[i]), |
| 174 | + Value: ptrTo(thresholds[i]), |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + return &dto.MetricFamily{ |
| 179 | + Name: &name, |
| 180 | + Type: dto.MetricType_SUMMARY.Enum(), |
| 181 | + Metric: []*dto.Metric{{ |
| 182 | + Summary: &dto.Summary{ |
| 183 | + SampleCount: ptrTo(uint64(snapshot.Count())), //nolint:gosec |
| 184 | + // TODO: do we need to specify SampleSum here? and if so |
| 185 | + // what should that be? |
| 186 | + Quantile: dtoQuantiles, |
| 187 | + }, |
| 188 | + }}, |
| 189 | + }, nil |
| 190 | + default: |
| 191 | + return nil, fmt.Errorf("metric type is not supported: %T", metric) |
| 192 | + } |
| 193 | +} |
0 commit comments