Skip to content

Commit

Permalink
Made suggested changes.
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 26, 2024
1 parent 04df0d0 commit afe3120
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 67 deletions.
23 changes: 12 additions & 11 deletions common/metrics/count_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package metrics

import (
"fmt"
"github.com/Layr-Labs/eigensdk-go/logging"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
Expand All @@ -12,6 +13,9 @@ var _ CountMetric = &countMetric{}
type countMetric struct {
Metric

// logger is the logger used to log errors.
logger logging.Logger

// name is the name of the metric.
name string

Expand All @@ -27,6 +31,7 @@ type countMetric struct {

// newCountMetric creates a new CountMetric instance.
func newCountMetric(
logger logging.Logger,
registry *prometheus.Registry,
namespace string,
name string,
Expand All @@ -47,6 +52,7 @@ func newCountMetric(
)

return &countMetric{
logger: logger,
name: name,
description: description,
vec: vec,
Expand Down Expand Up @@ -74,27 +80,22 @@ func (m *countMetric) LabelFields() []string {
return m.labeler.getKeys()
}

func (m *countMetric) Increment(label ...any) error {
return m.Add(1, label...)
func (m *countMetric) Increment(label ...any) {
m.Add(1, label...)
}

func (m *countMetric) Add(value float64, label ...any) error {
if len(label) > 1 {
return fmt.Errorf("too many labels provided, expected 1, got %d", len(label))
}

func (m *countMetric) Add(value float64, label ...any) {
var l any
if len(label) == 1 {
if len(label) > 0 {
l = label[0]
}

values, err := m.labeler.extractValues(l)
if err != nil {
return fmt.Errorf("error extracting values from label for metric %s: %v", m.name, err)
m.logger.Errorf("error extracting values from label for metric %s: %v", m.name, err)
return
}

observer := m.vec.WithLabelValues(values...)
observer.Add(value)

return nil
}
18 changes: 10 additions & 8 deletions common/metrics/gauge_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package metrics

import (
"fmt"
"github.com/Layr-Labs/eigensdk-go/logging"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
Expand All @@ -12,6 +13,9 @@ var _ GaugeMetric = &gaugeMetric{}
type gaugeMetric struct {
Metric

// logger is the logger used to log errors.
logger logging.Logger

// name is the name of the metric.
name string

Expand All @@ -30,6 +34,7 @@ type gaugeMetric struct {

// newGaugeMetric creates a new GaugeMetric instance.
func newGaugeMetric(
logger logging.Logger,
registry *prometheus.Registry,
namespace string,
name string,
Expand All @@ -51,6 +56,7 @@ func newGaugeMetric(
)

return &gaugeMetric{
logger: logger,
name: name,
unit: unit,
description: description,
Expand Down Expand Up @@ -79,23 +85,19 @@ func (m *gaugeMetric) LabelFields() []string {
return m.labeler.getKeys()
}

func (m *gaugeMetric) Set(value float64, label ...any) error {
if len(label) > 1 {
return fmt.Errorf("too many labels provided, expected 1, got %d", len(label))
}

func (m *gaugeMetric) Set(value float64, label ...any) {
var l any
if len(label) == 1 {
if len(label) > 0 {
l = label[0]
}

values, err := m.labeler.extractValues(l)
if err != nil {
return fmt.Errorf("error extracting values from label for metric %s: %v", m.name, err)
m.logger.Errorf("failed to extract values from label: %v", err)
return
}

observer := m.vec.WithLabelValues(values...)

observer.Set(value)
return nil
}
18 changes: 9 additions & 9 deletions common/metrics/latency_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package metrics

import (
"fmt"
"github.com/Layr-Labs/eigensdk-go/logging"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"time"
Expand All @@ -13,6 +14,9 @@ var _ LatencyMetric = &latencyMetric{}
type latencyMetric struct {
Metric

// logger is the logger used to log errors.
logger logging.Logger

// name is the name of the metric.
name string

Expand All @@ -28,6 +32,7 @@ type latencyMetric struct {

// newLatencyMetric creates a new LatencyMetric instance.
func newLatencyMetric(
logger logging.Logger,
registry *prometheus.Registry,
namespace string,
name string,
Expand All @@ -50,6 +55,7 @@ func newLatencyMetric(
)

return &latencyMetric{
logger: logger,
name: name,
description: description,
vec: vec,
Expand Down Expand Up @@ -77,26 +83,20 @@ func (m *latencyMetric) LabelFields() []string {
return m.labeler.getKeys()
}

func (m *latencyMetric) ReportLatency(latency time.Duration, label ...any) error {
if len(label) > 1 {
return fmt.Errorf("too many labels provided, expected 1, got %d", len(label))
}

func (m *latencyMetric) ReportLatency(latency time.Duration, label ...any) {
var l any
if len(label) == 1 {
if len(label) > 0 {
l = label[0]
}

values, err := m.labeler.extractValues(l)
if err != nil {
return fmt.Errorf("error extracting values from label for metric %s: %v", m.name, err)
m.logger.Errorf("error extracting values from label: %v", err)
}

observer := m.vec.WithLabelValues(values...)

nanoseconds := float64(latency.Nanoseconds())
milliseconds := nanoseconds / float64(time.Millisecond)
observer.Observe(milliseconds)

return nil
}
8 changes: 4 additions & 4 deletions common/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ type GaugeMetric interface {
//
// The label parameter accepts zero or one label. If the label type does not match the template label type provided
// when creating the metric, an error will be returned.
Set(value float64, label ...any) error
Set(value float64, label ...any)
}

// CountMetric allows the count of a type of event to be tracked.
Expand All @@ -102,13 +102,13 @@ type CountMetric interface {
//
// The label parameter accepts zero or one label. If the label type does not match the template label type provided
// when creating the metric, an error will be returned.
Increment(label ...any) error
Increment(label ...any)

// Add increments the count by the given value.
//
// The label parameter accepts zero or one label. If the label type does not match the template label type provided
// when creating the metric, an error will be returned.
Add(value float64, label ...any) error
Add(value float64, label ...any)
}

// Quantile describes a quantile of a latency metric that should be reported. For a description of how
Expand Down Expand Up @@ -138,5 +138,5 @@ type LatencyMetric interface {
//
// The label parameter accepts zero or one label. If the label type does not match the template label type provided
// when creating the metric, an error will be returned.
ReportLatency(latency time.Duration, label ...any) error
ReportLatency(latency time.Duration, label ...any)
}
5 changes: 4 additions & 1 deletion common/metrics/metrics_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ func (m *metrics) NewLatencyMetric(
m.quantilesMap[id] = quantilesString

metric, err := newLatencyMetric(
m.logger,
m.registry,
m.config.Namespace,
name,
Expand Down Expand Up @@ -235,6 +236,7 @@ func (m *metrics) NewCountMetric(
}

metric, err := newCountMetric(
m.logger,
m.registry,
m.config.Namespace,
name, description,
Expand Down Expand Up @@ -283,6 +285,7 @@ func (m *metrics) newGaugeMetricUnsafe(
}

metric, err := newGaugeMetric(
m.logger,
m.registry,
m.config.Namespace,
name,
Expand Down Expand Up @@ -331,7 +334,7 @@ func (m *metrics) NewAutoGauge(
for m.isAlive.Load() {
value := source()

_ = gauge.Set(value, l)
gauge.Set(value, l)
<-ticker.C
}
}
Expand Down
16 changes: 8 additions & 8 deletions common/metrics/mock_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ func (m *mockCountMetric) LabelFields() []string {
return make([]string, 0)
}

func (m *mockCountMetric) Increment(label ...any) error {
return nil
func (m *mockCountMetric) Increment(label ...any) {

}

func (m *mockCountMetric) Add(value float64, label ...any) error {
return nil
func (m *mockCountMetric) Add(value float64, label ...any) {

}

var _ GaugeMetric = &mockGaugeMetric{}
Expand Down Expand Up @@ -120,8 +120,8 @@ func (m *mockGaugeMetric) LabelFields() []string {
return make([]string, 0)
}

func (m *mockGaugeMetric) Set(value float64, label ...any) error {
return nil
func (m *mockGaugeMetric) Set(value float64, label ...any) {

}

var _ LatencyMetric = &mockLatencyMetric{}
Expand Down Expand Up @@ -149,6 +149,6 @@ func (m *mockLatencyMetric) LabelFields() []string {
return make([]string, 0)
}

func (m *mockLatencyMetric) ReportLatency(latency time.Duration, label ...any) error {
return nil
func (m *mockLatencyMetric) ReportLatency(latency time.Duration, label ...any) {

}
34 changes: 8 additions & 26 deletions common/metrics/test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,45 +105,27 @@ func main() {
elapsed := now.Sub(prev)
prev = now

err = l1.ReportLatency(elapsed)
if err != nil {
panic(err)
}
l1.ReportLatency(elapsed)

err = l1.ReportLatency(elapsed/2,
l1.ReportLatency(elapsed/2,
LabelType1{
foo: "half of the normal value",
bar: "42",
baz: "true",
})
if err != nil {
panic(err)
}

err = c1.Increment()
if err != nil {
panic(err)
}
err = c1.Add(2, LabelType2{

c1.Increment()
c1.Add(2, LabelType2{
X: "2x",
})
if err != nil {
panic(err)
}
err = c2.Increment()
if err != nil {
panic(err)
}

err = g1.Set(float64(elapsed.Milliseconds()),
c2.Increment()

g1.Set(float64(elapsed.Milliseconds()),
LabelType1{
foo: "bar",
bar: "baz",
baz: "foo",
})
if err != nil {
panic(err)
}

sum.Store(sum.Load() + elapsed.Milliseconds())
}
Expand Down

0 comments on commit afe3120

Please sign in to comment.