Skip to content

Commit

Permalink
improve: simplify metric labels handling
Browse files Browse the repository at this point in the history
  • Loading branch information
0x416e746f6e committed Mar 18, 2024
1 parent fa3d573 commit 271ce1b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 44 deletions.
4 changes: 3 additions & 1 deletion prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ func NewMeter(ctx context.Context, name string) (
return nil, err
}

exporter, err := prometheus.New(prometheus.WithNamespace("node-monitor"))
exporter, err := prometheus.New(
prometheus.WithNamespace("node-monitor"),
)
if err != nil {
return nil, err
}
Expand Down
66 changes: 24 additions & 42 deletions server/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
)

const (
defaultGroup = "__default"
groupEndpoint = "__group"
defaultTargetGroup = "__default"
groupVirtualEndpoint = "__group"

keyTargetEndpoint = "node_monitor_target_endpoint"
keyTargetGroup = "node_monitor_target_group"
Expand All @@ -37,33 +37,24 @@ func (s *Server) handleEventEthNewHeader(
e := g.Endpoint(ename)

e.RegisterBlock(block, ts)
latency := g.RegisterBlockAndGetLatency(block, ts)

var attrs []attribute.KeyValue
if gname != "" {
attrs = []attribute.KeyValue{
{Key: keyTargetEndpoint, Value: attribute.StringValue(ename)},
{Key: keyTargetGroup, Value: attribute.StringValue(gname)},
{Key: keyTargetID, Value: attribute.StringValue(utils.MakeELEndpointID(gname, ename))},
}
} else {
attrs = []attribute.KeyValue{
{Key: keyTargetEndpoint, Value: attribute.StringValue(ename)},
{Key: keyTargetGroup, Value: attribute.StringValue(defaultGroup)},
{Key: keyTargetID, Value: attribute.StringValue(utils.MakeELEndpointID(gname, ename))},
}
latency_s := g.RegisterBlockAndGetLatency(block, ts).Seconds()

attrs := []attribute.KeyValue{
{Key: keyTargetEndpoint, Value: attribute.StringValue(ename)},
{Key: keyTargetGroup, Value: attribute.StringValue(normalisedGroup(gname))},
{Key: keyTargetID, Value: attribute.StringValue(utils.MakeELEndpointID(gname, ename))},
}

s.metrics.newBlockLatency.Record(ctx,
latency.Seconds(),
latency_s,
metric.WithAttributes(attrs...),
)

l.Debug("Received new header",
zap.Float64("latency_s", latency_s),
zap.String("block", block.String()),
zap.String("endpoint_group", gname),
zap.String("endpoint_name", ename),
zap.Duration("latency_s", latency),
)
}

Expand All @@ -73,17 +64,9 @@ func (s *Server) handleHealthcheck(w http.ResponseWriter, r *http.Request) {

func (s *Server) handleEventPrometheusObserve(_ context.Context, o metric.Observer) error {
s.state.IterateELGroupsRO(func(gname string, g *state.ELGroup) {
var attrs []attribute.KeyValue
if gname != "" {
attrs = []attribute.KeyValue{
{Key: keyTargetEndpoint, Value: attribute.StringValue(groupEndpoint)},
{Key: keyTargetGroup, Value: attribute.StringValue(gname)},
}
} else {
attrs = []attribute.KeyValue{
{Key: keyTargetGroup, Value: attribute.StringValue(defaultGroup)},
{Key: keyTargetEndpoint, Value: attribute.StringValue(groupEndpoint)},
}
attrs := []attribute.KeyValue{
{Key: keyTargetEndpoint, Value: attribute.StringValue(groupVirtualEndpoint)},
{Key: keyTargetGroup, Value: attribute.StringValue(normalisedGroup(gname))},
}

blockGroup, tsBlockGroup := g.TimeSinceHighestBlock()
Expand All @@ -95,18 +78,10 @@ func (s *Server) handleEventPrometheusObserve(_ context.Context, o metric.Observ
o.ObserveFloat64(s.metrics.timeSinceLastBlock, tsBlockGroup.Seconds(), metric.WithAttributes(attrs...))

g.IterateEndpointsRO(func(ename string, e *state.ELEndpoint) {
if gname != "" {
attrs = []attribute.KeyValue{
{Key: keyTargetEndpoint, Value: attribute.StringValue(ename)},
{Key: keyTargetGroup, Value: attribute.StringValue(gname)},
{Key: keyTargetID, Value: attribute.StringValue(utils.MakeELEndpointID(gname, ename))},
}
} else {
attrs = []attribute.KeyValue{
{Key: keyTargetEndpoint, Value: attribute.StringValue(ename)},
{Key: keyTargetGroup, Value: attribute.StringValue(defaultGroup)},
{Key: keyTargetID, Value: attribute.StringValue(utils.MakeELEndpointID(gname, ename))},
}
attrs := []attribute.KeyValue{
{Key: keyTargetEndpoint, Value: attribute.StringValue(ename)},
{Key: keyTargetGroup, Value: attribute.StringValue(normalisedGroup(gname))},
{Key: keyTargetID, Value: attribute.StringValue(utils.MakeELEndpointID(gname, ename))},
}

blockEndpoint, tsBlockEndpoint := e.TimeSinceHighestBlock()
Expand All @@ -128,3 +103,10 @@ func (s *Server) handleEventPrometheusObserve(_ context.Context, o metric.Observ

return nil
}

func normalisedGroup(gname string) string {
if gname == "" {
return defaultTargetGroup
}
return gname
}
2 changes: 1 addition & 1 deletion server/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (m *metrics) setup(meter otelapi.Meter, observe func(ctx context.Context, o
newBlockLatency, err := meter.Float64Histogram(metricNewBlockLatency,
metric.WithExplicitBucketBoundaries(
0.01171875, // 1/1024
0.0234375, // 1//512
0.0234375, // 1/512
0.046875, // 1/256
0.09375, // 1/128
0.1875, // 1/64
Expand Down

0 comments on commit 271ce1b

Please sign in to comment.