Skip to content

Commit

Permalink
Cleanup.
Browse files Browse the repository at this point in the history
Signed-off-by: Cody Littley <[email protected]>
  • Loading branch information
cody-littley committed Nov 25, 2024
1 parent b28e075 commit f9df03e
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 94 deletions.
28 changes: 22 additions & 6 deletions common/metrics/count_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package metrics
import (
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)

var _ CountMetric = &countMetric{}
Expand All @@ -25,13 +26,32 @@ type countMetric struct {
}

// newCountMetric creates a new CountMetric instance.
func newCountMetric(name string, description string, vec *prometheus.CounterVec, labeler *labelMaker) CountMetric {
func newCountMetric(
registry *prometheus.Registry,
namespace string,
name string,
description string,
labelTemplate any) (CountMetric, error) {

labeler, err := newLabelMaker(labelTemplate)
if err != nil {
return nil, err
}

vec := promauto.With(registry).NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Name: fmt.Sprintf("%s_count", name),
},
labeler.getKeys(),
)

return &countMetric{
name: name,
description: description,
vec: vec,
labeler: labeler,
}
}, nil
}

func (m *countMetric) Name() string {
Expand Down Expand Up @@ -59,10 +79,6 @@ func (m *countMetric) Increment(label ...any) error {
}

func (m *countMetric) Add(value float64, label ...any) error {
if m.vec == nil {
return nil
}

if len(label) > 1 {
return fmt.Errorf("too many labels provided, expected 1, got %d", len(label))
}
Expand Down
26 changes: 18 additions & 8 deletions common/metrics/gauge_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package metrics
import (
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)

var _ GaugeMetric = &gaugeMetric{}
Expand All @@ -29,19 +30,33 @@ type gaugeMetric struct {

// newGaugeMetric creates a new GaugeMetric instance.
func newGaugeMetric(
registry *prometheus.Registry,
namespace string,
name string,
unit string,
description string,
vec *prometheus.GaugeVec,
labeler *labelMaker) GaugeMetric {
labelTemplate any) (GaugeMetric, error) {

labeler, err := newLabelMaker(labelTemplate)
if err != nil {
return nil, err
}

vec := promauto.With(registry).NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: fmt.Sprintf("%s_%s", name, unit),
},
labeler.getKeys(),
)

return &gaugeMetric{
name: name,
unit: unit,
description: description,
vec: vec,
labeler: labeler,
}
}, nil
}

func (m *gaugeMetric) Name() string {
Expand All @@ -65,11 +80,6 @@ func (m *gaugeMetric) LabelFields() []string {
}

func (m *gaugeMetric) Set(value float64, label ...any) error {
if m.vec == nil {
// metric is not enabled
return nil
}

if len(label) > 1 {
return fmt.Errorf("too many labels provided, expected 1, got %d", len(label))
}
Expand Down
28 changes: 20 additions & 8 deletions common/metrics/latency_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package metrics
import (
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"time"
)

Expand All @@ -27,17 +28,33 @@ type latencyMetric struct {

// newLatencyMetric creates a new LatencyMetric instance.
func newLatencyMetric(
registry *prometheus.Registry,
namespace string,
name string,
description string,
vec *prometheus.SummaryVec,
labeler *labelMaker) LatencyMetric {
objectives map[float64]float64,
labelTemplate any) (LatencyMetric, error) {

labeler, err := newLabelMaker(labelTemplate)
if err != nil {
return nil, err
}

vec := promauto.With(registry).NewSummaryVec(
prometheus.SummaryOpts{
Namespace: namespace,
Name: fmt.Sprintf("%s_ms", name),
Objectives: objectives,
},
labeler.getKeys(),
)

return &latencyMetric{
name: name,
description: description,
vec: vec,
labeler: labeler,
}
}, nil
}

func (m *latencyMetric) Name() string {
Expand All @@ -61,11 +78,6 @@ func (m *latencyMetric) LabelFields() []string {
}

func (m *latencyMetric) ReportLatency(latency time.Duration, label ...any) error {
if m.vec == nil {
// metric is not enabled
return nil
}

if len(label) > 1 {
return fmt.Errorf("too many labels provided, expected 1, got %d", len(label))
}
Expand Down
98 changes: 29 additions & 69 deletions common/metrics/metrics_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/Layr-Labs/eigensdk-go/logging"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"net/http"
"os"
Expand All @@ -31,20 +30,6 @@ type metrics struct {
// registry is the prometheus registry used to report metrics.
registry *prometheus.Registry

// TODO these maps are not needed any more!

// counterVecMap is a map of metric names to prometheus counter vectors.
// These are used to create new counter metrics.
counterVecMap map[string]*prometheus.CounterVec

// summaryVecMap is a map of metric names to prometheus summary vectors.
// These are used to create new latency metrics.
summaryVecMap map[string]*prometheus.SummaryVec

// gaugeVecMap is a map of metric names to prometheus gauge vectors.
// These are used to create new gauge metrics.
gaugeVecMap map[string]*prometheus.GaugeVec

// A map from metricID to Metric instance. If a metric is requested but that metric
// already exists, the existing metric will be returned instead of a new one being created.
metricMap map[metricID]Metric
Expand Down Expand Up @@ -88,16 +73,13 @@ func NewMetrics(logger logging.Logger, config *Config) Metrics {
}

m := &metrics{
logger: logger,
config: config,
registry: reg,
counterVecMap: make(map[string]*prometheus.CounterVec),
summaryVecMap: make(map[string]*prometheus.SummaryVec),
gaugeVecMap: make(map[string]*prometheus.GaugeVec),
metricMap: make(map[metricID]Metric),
isAlive: atomic.Bool{},
server: server,
quantilesMap: make(map[metricID]string),
logger: logger,
config: config,
registry: reg,
metricMap: make(map[metricID]Metric),
isAlive: atomic.Bool{},
server: server,
quantilesMap: make(map[metricID]string),
}
m.isAlive.Store(true)
return m
Expand Down Expand Up @@ -213,25 +195,18 @@ func (m *metrics) NewLatencyMetric(
}
m.quantilesMap[id] = quantilesString

labeler, err := newLabelMaker(labelTemplate)
metric, err := newLatencyMetric(
m.registry,
m.config.Namespace,
name,
description,
objectives,
labelTemplate)

if err != nil {
return nil, err
}

vec, ok := m.summaryVecMap[name]
if !ok {
vec = promauto.With(m.registry).NewSummaryVec(
prometheus.SummaryOpts{
Namespace: m.config.Namespace,
Name: id.NameWithUnit(),
Objectives: objectives,
},
labeler.getKeys(),
)
m.summaryVecMap[name] = vec
}

metric := newLatencyMetric(name, description, vec, labeler)
m.metricMap[id] = metric
return metric, nil
}
Expand Down Expand Up @@ -259,24 +234,16 @@ func (m *metrics) NewCountMetric(
return preExistingMetric.(CountMetric), nil
}

labeler, err := newLabelMaker(labelTemplate)
metric, err := newCountMetric(
m.registry,
m.config.Namespace,
name, description,
labelTemplate)

if err != nil {
return nil, err
}

vec, ok := m.counterVecMap[name]
if !ok {
vec = promauto.With(m.registry).NewCounterVec(
prometheus.CounterOpts{
Namespace: m.config.Namespace,
Name: id.NameWithUnit(),
},
labeler.getKeys(),
)
m.counterVecMap[name] = vec
}

metric := newCountMetric(name, description, vec, labeler)
m.metricMap[id] = metric

return metric, nil
Expand Down Expand Up @@ -315,26 +282,19 @@ func (m *metrics) newGaugeMetricUnsafe(
return preExistingMetric.(GaugeMetric), nil
}

labeler, err := newLabelMaker(labelTemplate)
metric, err := newGaugeMetric(
m.registry,
m.config.Namespace,
name,
unit,
description,
labelTemplate)

if err != nil {
return nil, err
}

vec, ok := m.gaugeVecMap[name]
if !ok {
vec = promauto.With(m.registry).NewGaugeVec(
prometheus.GaugeOpts{
Namespace: m.config.Namespace,
Name: id.NameWithUnit(),
},
labeler.getKeys(),
)
m.gaugeVecMap[name] = vec
}

metric := newGaugeMetric(name, unit, description, vec, labeler)
m.metricMap[id] = metric

return metric, nil
}

Expand Down
Loading

0 comments on commit f9df03e

Please sign in to comment.