Skip to content

Commit 2d2709e

Browse files
avitenzerclaude
andauthored
perf(metrics): update the rpc_endpoint_* series positionally, without a label map per call (MAG-4244) (#609)
Sixteen rpc_endpoint_* metrics went through MappedLabelsCounterVec and MappedLabelsGaugeVec, wrappers written for "a dynamic number of labels based on flags given upon startup". No metric has that: every label set is fixed. Yet each update built a map[string]string of its labels, and the wrapper then built a []string to put them back in order. These are per-relay updates (relays serviced, in flight) and per-poll ones. They are now plain *prometheus.CounterVec / *prometheus.GaugeVec, registered through registerOrReuse like the metrics beside them, and updated with positional WithLabelValues. The three wrapper files and the wrapper's own test are deleted (MAG-4244 section 7). registerOrReuse is a true drop-in here, checked rather than assumed: all sixteen passed prometheus.DefaultRegisterer, which is the registry registerOrReuse uses, and both reuse an already-registered collector of the same type and panic otherwise. Measured, steady-state allocations per update: relay serviced, in flight, fetch success, tracker request 1 -> 0 latest block 2 -> 1 url relay serviced 3 -> 1 What could go wrong, and what rules it out: - A wrong count of positional values makes Prometheus panic, where the wrapper filled a missing label with "" and dropped an extra one. Every one of the 17 update sites supplied exactly its metric's declared keys, so nothing relied on either; the conversion derived each positional order from the declared labels and refused any key-set mismatch. - A swapped order would mislabel series the stuck-tip and per-url alerts read, silently. Proved absent: a fixed sequence of updates through every wrapped metric, including a url that two providers share and a url that is then forgotten, renders 43 series whose names, help text, label sets and values are BYTE-IDENTICAL to main's (only answered_until's value is redacted, being time.Now()-derived). Order now matters where it did not, so it gets a durable guard. TestPositionalLabels_EveryWrappedMetric drives every metric with a different value in every label position and requires each value under its own name, then requires ForgetEndpointURLLatestBlock to remove the url series. It passes on main's code too, so it pins the labels main already produces. Against six swaps in the new code (spec with apiInterface; endpoint_id with function, twice; endpoint_id with kind; url with internal_path in the url helper; a forget that deletes nothing) it fails every one. urlSeriesLabelValuesLocked returns the url labels positionally for both updates and DeleteLabelValues; its comment names the test that guards its order. The nil guard comment on AddEndpointTrackerRequest named the deleted wrapper type; it now names prometheus.CounterVec, whose WithLabelValues dereferences its receiver all the same. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 924600c commit 2d2709e

10 files changed

Lines changed: 279 additions & 458 deletions

‎protocol/metrics/cancelled_relay_metrics_test.go‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ import (
1010
// MAG-2648. A relay-race loser must balance the in-flight gauge and land in its own
1111
// counter, without polluting the error counter or the router request-group counters.
1212

13-
func cancelledTestLabels() map[string]string {
14-
return map[string]string{"spec": "ETH1", "apiInterface": "jsonrpc", "endpoint_id": "ep1", "function": "eth_sendRawTransaction"}
13+
// cancelledTestLabels is the series under test, in endpointFunctionLabels order
14+
// (spec, apiInterface, endpoint_id, function).
15+
func cancelledTestLabels() []string {
16+
return []string{"ETH1", "jsonrpc", "ep1", "eth_sendRawTransaction"}
1517
}
1618

1719
// The trap this fix had to route around: the in-flight gauge is incremented at relay
@@ -23,7 +25,7 @@ func TestCancelledRelay_InFlightGaugeReturnsToZero(t *testing.T) {
2325
m.RecordDirectRelayStart("ETH1", "jsonrpc", "ep1", "eth_sendRawTransaction")
2426
m.RecordDirectRelayEnd("ETH1", "jsonrpc", "ep1", "eth_sendRawTransaction", 5, RelayOutcomeCancelled, &RelayMetrics{IsWrite: true})
2527
}
26-
require.Equal(t, float64(0), testutil.ToFloat64(m.endpointInFlight.WithLabelValues(cancelledTestLabels())),
28+
require.Equal(t, float64(0), testutil.ToFloat64(m.endpointInFlight.WithLabelValues(cancelledTestLabels()...)),
2729
"every cancelled relay must still decrement the in-flight gauge")
2830
}
2931

@@ -33,7 +35,7 @@ func TestCancelledRelay_InFlightBalancesAcrossMixedOutcomes(t *testing.T) {
3335
m.RecordDirectRelayStart("ETH1", "jsonrpc", "ep1", "eth_sendRawTransaction")
3436
m.RecordDirectRelayEnd("ETH1", "jsonrpc", "ep1", "eth_sendRawTransaction", 5, outcome, &RelayMetrics{IsWrite: true})
3537
}
36-
require.Equal(t, float64(0), testutil.ToFloat64(m.endpointInFlight.WithLabelValues(cancelledTestLabels())))
38+
require.Equal(t, float64(0), testutil.ToFloat64(m.endpointInFlight.WithLabelValues(cancelledTestLabels()...)))
3739
}
3840

3941
// The customer-visible symptom: errored relays were almost all eth_sendRawTransaction.
@@ -45,9 +47,9 @@ func TestCancelledRelay_DoesNotCountAsErrored(t *testing.T) {
4547
m.RecordDirectRelayEnd("ETH1", "jsonrpc", "ep1", "eth_sendRawTransaction", 5, RelayOutcomeCancelled, &RelayMetrics{IsWrite: true})
4648
}
4749

48-
require.Equal(t, float64(0), testutil.ToFloat64(m.endpointTotalErrored.WithLabelValues(labels)))
49-
require.Equal(t, float64(10), testutil.ToFloat64(m.endpointTotalCancelled.WithLabelValues(labels)))
50-
require.Equal(t, float64(0), testutil.ToFloat64(m.endpointTotalRelaysServiced.WithLabelValues(labels)))
50+
require.Equal(t, float64(0), testutil.ToFloat64(m.endpointTotalErrored.WithLabelValues(labels...)))
51+
require.Equal(t, float64(10), testutil.ToFloat64(m.endpointTotalCancelled.WithLabelValues(labels...)))
52+
require.Equal(t, float64(0), testutil.ToFloat64(m.endpointTotalRelaysServiced.WithLabelValues(labels...)))
5153
}
5254

5355
// COLLATERAL GUARD: a real error must still be counted as errored. If this ever fails the
@@ -58,8 +60,8 @@ func TestErroredRelay_StillCountsAsErrored(t *testing.T) {
5860

5961
m.RecordDirectRelayEnd("ETH1", "jsonrpc", "ep1", "eth_sendRawTransaction", 5, RelayOutcomeError, &RelayMetrics{IsWrite: true})
6062

61-
require.Equal(t, float64(1), testutil.ToFloat64(m.endpointTotalErrored.WithLabelValues(labels)))
62-
require.Equal(t, float64(0), testutil.ToFloat64(m.endpointTotalCancelled.WithLabelValues(labels)))
63+
require.Equal(t, float64(1), testutil.ToFloat64(m.endpointTotalErrored.WithLabelValues(labels...)))
64+
require.Equal(t, float64(0), testutil.ToFloat64(m.endpointTotalCancelled.WithLabelValues(labels...)))
6365
}
6466

6567
// COLLATERAL GUARD: success accounting is untouched by the signature change.
@@ -70,8 +72,8 @@ func TestSuccessRelay_StillCountsAsServiced(t *testing.T) {
7072

7173
m.RecordDirectRelayEnd("ETH1", "jsonrpc", "ep1", "eth_sendRawTransaction", 5, RelayOutcomeSuccess, &RelayMetrics{IsWrite: true})
7274

73-
require.Equal(t, float64(1), testutil.ToFloat64(m.endpointTotalRelaysServiced.WithLabelValues(labels)))
74-
require.Equal(t, float64(0), testutil.ToFloat64(m.endpointTotalErrored.WithLabelValues(labels)))
75+
require.Equal(t, float64(1), testutil.ToFloat64(m.endpointTotalRelaysServiced.WithLabelValues(labels...)))
76+
require.Equal(t, float64(0), testutil.ToFloat64(m.endpointTotalErrored.WithLabelValues(labels...)))
7577
require.Equal(t, float64(1), testutil.ToFloat64(m.routerRequestsTotal.WithLabelValues(reqLabels...)))
7678
require.Equal(t, float64(1), testutil.ToFloat64(m.routerRequestsSuccess.WithLabelValues(reqLabels...)))
7779
require.Equal(t, float64(1), testutil.ToFloat64(m.routerRequestsWrite.WithLabelValues(reqLabels...)))
@@ -126,7 +128,7 @@ func TestCancelledRelay_StillNormalisesBatchLabel(t *testing.T) {
126128
raw := "eth_call&eth_call&eth_getBalance"
127129
m.RecordDirectRelayEnd("ETH1", "jsonrpc", "ep1", raw, 5, RelayOutcomeCancelled, &RelayMetrics{IsBatch: true})
128130

129-
collapsed := map[string]string{"spec": "ETH1", "apiInterface": "jsonrpc", "endpoint_id": "ep1", "function": "batch:eth_call+eth_getBalance"}
130-
require.Equal(t, float64(1), testutil.ToFloat64(m.endpointTotalCancelled.WithLabelValues(collapsed)),
131+
collapsed := []string{"ETH1", "jsonrpc", "ep1", "batch:eth_call+eth_getBalance"}
132+
require.Equal(t, float64(1), testutil.ToFloat64(m.endpointTotalCancelled.WithLabelValues(collapsed...)),
131133
"the cancelled path must collapse batch labels like every other recorder")
132134
}

‎protocol/metrics/endpoint_url_latest_block_test.go‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,14 @@ func TestURLLatestBlock_OneSeriesPerURL(t *testing.T) {
8181
m.SetEndpointURLLatestBlock("TON", "rest", tonV2URL, 1000, DefaultURLAnswerTimeout) // frozen, still answering
8282
m.SetEndpointURLLatestBlock("TON", "rest", tonV3URL, v3, DefaultURLAnswerTimeout)
8383
require.Equal(t, map[string]float64{"/v2": 1000, "/v3": float64(v3)},
84-
byPath(t, urlPoints(t, m.endpointURLLatestBlock.GaugeVec, "TON", "chainstack")))
84+
byPath(t, urlPoints(t, m.endpointURLLatestBlock, "TON", "chainstack")))
8585
}
86-
points := urlPoints(t, m.endpointURLLatestBlock.GaugeVec, "TON", "chainstack")
86+
points := urlPoints(t, m.endpointURLLatestBlock, "TON", "chainstack")
8787
require.NotEqual(t, points[0].url, points[1].url, "/v2 and /v3 share a host; the url label still tells them apart")
8888
for _, p := range points {
8989
require.Equal(t, map[string]string{"/v2": URLFingerprint(tonV2URL), "/v3": URLFingerprint(tonV3URL)}[p.internalPath], p.url)
9090
}
91-
require.Empty(t, urlPoints(t, m.endpointLatestBlock.GaugeVec, "TON", "chainstack"), "the provider series is written elsewhere")
91+
require.Empty(t, urlPoints(t, m.endpointLatestBlock, "TON", "chainstack"), "the provider series is written elsewhere")
9292
}
9393

9494
// Every write moves the url's answered-until time to now plus the answer timeout its caller
@@ -108,10 +108,10 @@ func TestURLLatestBlock_AnsweredUntilIsTheLastAnswerPlusTheTimeout(t *testing.T)
108108
require.GreaterOrEqual(t, got, float64(before.Add(timeout).Unix()))
109109
require.LessOrEqual(t, got, float64(after.Add(timeout).Unix()))
110110
}
111-
ton := byPath(t, urlPoints(t, m.endpointURLAnsweredUntil.GaugeVec, "TON", "chainstack"))
111+
ton := byPath(t, urlPoints(t, m.endpointURLAnsweredUntil, "TON", "chainstack"))
112112
within(ton["/v2"], 10*time.Minute)
113113
within(ton["/v3"], 20*time.Minute)
114-
within(byPath(t, urlPoints(t, m.endpointURLAnsweredUntil.GaugeVec, "ETH1", "solo"))[""], DefaultURLAnswerTimeout)
114+
within(byPath(t, urlPoints(t, m.endpointURLAnsweredUntil, "ETH1", "solo"))[""], DefaultURLAnswerTimeout)
115115
}
116116

117117
// A shared url is one series per provider configured with it; the relay harvest moves only the
@@ -122,8 +122,8 @@ func TestURLLatestBlock_SharedURLIsASeriesPerProvider(t *testing.T) {
122122
m.SetEndpointURLLatestBlock("TON", "rest", tonV2URL, 1000, DefaultURLAnswerTimeout)
123123
m.SetProviderURLLatestBlock("TON", "rest", "chainstack", tonV2URL, 1001, DefaultURLAnswerTimeout)
124124

125-
require.Equal(t, map[string]float64{"/v2": 1001}, byPath(t, urlPoints(t, m.endpointURLLatestBlock.GaugeVec, "TON", "chainstack")))
126-
require.Equal(t, map[string]float64{"/v2": 1000}, byPath(t, urlPoints(t, m.endpointURLLatestBlock.GaugeVec, "TON", "chainstack-backup")))
125+
require.Equal(t, map[string]float64{"/v2": 1001}, byPath(t, urlPoints(t, m.endpointURLLatestBlock, "TON", "chainstack")))
126+
require.Equal(t, map[string]float64{"/v2": 1000}, byPath(t, urlPoints(t, m.endpointURLLatestBlock, "TON", "chainstack-backup")))
127127
}
128128

129129
// An unregistered url would name itself as its provider, and no part of a url is safe to export.
@@ -133,7 +133,7 @@ func TestURLLatestBlock_UnregisteredURLGetsNoSeries(t *testing.T) {
133133
m.SetEndpointURLLatestBlock("ETH1", "jsonrpc", url, 10, DefaultURLAnswerTimeout)
134134
m.SetProviderURLLatestBlock("ETH1", "jsonrpc", "p", url, 10, DefaultURLAnswerTimeout)
135135
m.AddEndpointURLRelayServiced("ETH1", "jsonrpc", "p", url)
136-
for _, c := range []prometheus.Collector{m.endpointURLLatestBlock.GaugeVec, m.endpointURLAnsweredUntil.GaugeVec, m.endpointURLRelaysServiced.CounterVec} {
136+
for _, c := range []prometheus.Collector{m.endpointURLLatestBlock, m.endpointURLAnsweredUntil, m.endpointURLRelaysServiced} {
137137
ch := make(chan prometheus.Metric, 4)
138138
c.Collect(ch)
139139
close(ch)
@@ -148,7 +148,7 @@ func TestURLRelaysServiced_CountsPerURL(t *testing.T) {
148148
m.AddEndpointURLRelayServiced("TON", "rest", "chainstack", tonV2URL)
149149
}
150150
m.AddEndpointURLRelayServiced("TON", "rest", "chainstack", tonV3URL)
151-
require.Equal(t, map[string]float64{"/v2": 3, "/v3": 1}, byPath(t, urlPoints(t, m.endpointURLRelaysServiced.CounterVec, "TON", "chainstack")))
151+
require.Equal(t, map[string]float64{"/v2": 3, "/v3": 1}, byPath(t, urlPoints(t, m.endpointURLRelaysServiced, "TON", "chainstack")))
152152
}
153153

154154
// A removed url loses every per-url series, and nothing else: a series left behind stands still
@@ -161,9 +161,9 @@ func TestURLLatestBlock_ForgetDeletesTheURLsSeries(t *testing.T) {
161161
m.AddEndpointURLRelayServiced("TON", "rest", "chainstack", tonV3URL)
162162

163163
m.ForgetEndpointURLLatestBlock("TON", "rest", tonV2URL)
164-
require.Equal(t, map[string]float64{"/v3": 1200}, byPath(t, urlPoints(t, m.endpointURLLatestBlock.GaugeVec, "TON", "chainstack")))
165-
require.Len(t, urlPoints(t, m.endpointURLAnsweredUntil.GaugeVec, "TON", "chainstack"), 1)
166-
require.Equal(t, map[string]float64{"/v3": 1}, byPath(t, urlPoints(t, m.endpointURLRelaysServiced.CounterVec, "TON", "chainstack")))
164+
require.Equal(t, map[string]float64{"/v3": 1200}, byPath(t, urlPoints(t, m.endpointURLLatestBlock, "TON", "chainstack")))
165+
require.Len(t, urlPoints(t, m.endpointURLAnsweredUntil, "TON", "chainstack"), 1)
166+
require.Equal(t, map[string]float64{"/v3": 1}, byPath(t, urlPoints(t, m.endpointURLRelaysServiced, "TON", "chainstack")))
167167

168168
m.ForgetEndpointURLLatestBlock("TON", "rest", "https://never.registered.example")
169169
var nilManager *SmartRouterMetricsManager

‎protocol/metrics/mapped_labels_counter_vec.go‎

Lines changed: 0 additions & 47 deletions
This file was deleted.

‎protocol/metrics/mapped_labels_gauge_vec.go‎

Lines changed: 0 additions & 47 deletions
This file was deleted.

‎protocol/metrics/mapped_labels_metric_common.go‎

Lines changed: 0 additions & 22 deletions
This file was deleted.

‎protocol/metrics/mapped_labels_registerer_test.go‎

Lines changed: 0 additions & 97 deletions
This file was deleted.

0 commit comments

Comments
 (0)