Skip to content

Commit cca0303

Browse files
committed
go/runtime/host/protocol: Refactor metrics
1 parent c074b74 commit cca0303

2 files changed

Lines changed: 77 additions & 59 deletions

File tree

go/runtime/host/protocol/connection.go

Lines changed: 18 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -28,47 +28,8 @@ const (
2828
connReadyTimeout = 5 * time.Second
2929
)
3030

31-
var (
32-
// ErrNotReady is the error reported when the Runtime Host Protocol is not initialized.
33-
ErrNotReady = errors.New(moduleName, 1, "rhp: not ready")
34-
35-
rhpLatency = prometheus.NewSummaryVec(
36-
prometheus.SummaryOpts{
37-
Name: "oasis_rhp_latency",
38-
Help: "Runtime Host call latency (seconds).",
39-
},
40-
[]string{"call"},
41-
)
42-
rhpCallSuccesses = prometheus.NewCounterVec(
43-
prometheus.CounterOpts{
44-
Name: "oasis_rhp_successes",
45-
Help: "Number of successful Runtime Host calls.",
46-
},
47-
[]string{"call"},
48-
)
49-
rhpCallFailures = prometheus.NewCounterVec(
50-
prometheus.CounterOpts{
51-
Name: "oasis_rhp_failures",
52-
Help: "Number of failed Runtime Host calls.",
53-
},
54-
[]string{"call"},
55-
)
56-
rhpCallTimeouts = prometheus.NewCounter(
57-
prometheus.CounterOpts{
58-
Name: "oasis_rhp_timeouts",
59-
Help: "Number of timed out Runtime Host calls.",
60-
},
61-
)
62-
63-
rhpCollectors = []prometheus.Collector{
64-
rhpLatency,
65-
rhpCallSuccesses,
66-
rhpCallFailures,
67-
rhpCallTimeouts,
68-
}
69-
70-
metricsOnce sync.Once
71-
)
31+
// ErrNotReady is the error reported when the Runtime Host Protocol is not initialized.
32+
var ErrNotReady = errors.New(moduleName, 1, "rhp: not ready")
7233

7334
// Handler is a protocol message handler interface.
7435
type Handler interface {
@@ -308,18 +269,20 @@ func (c *connection) Call(ctx context.Context, body *Body) (*Body, error) {
308269
func (c *connection) call(ctx context.Context, body *Body) (result *Body, err error) {
309270
start := time.Now()
310271
defer func() {
311-
if metrics.Enabled() {
312-
rhpLatency.With(prometheus.Labels{"call": body.Type()}).Observe(time.Since(start).Seconds())
313-
if err != nil {
314-
rhpCallFailures.With(prometheus.Labels{"call": body.Type()}).Inc()
315-
316-
// Specifically measure timeouts.
317-
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
318-
rhpCallTimeouts.Inc()
319-
}
320-
} else {
321-
rhpCallSuccesses.With(prometheus.Labels{"call": body.Type()}).Inc()
272+
if !metrics.Enabled() {
273+
return
274+
}
275+
276+
rhpLatency.With(prometheus.Labels{"call": body.Type()}).Observe(time.Since(start).Seconds())
277+
if err != nil {
278+
rhpCallFailures.With(prometheus.Labels{"call": body.Type()}).Inc()
279+
280+
// Specifically measure timeouts.
281+
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
282+
rhpCallTimeouts.Inc()
322283
}
284+
} else {
285+
rhpCallSuccesses.With(prometheus.Labels{"call": body.Type()}).Inc()
323286
}
324287
}()
325288

@@ -604,11 +567,9 @@ func (c *connection) InitHost(ctx context.Context, conn net.Conn, hi *HostInfo)
604567

605568
// NewConnection creates a new uninitialized RHP connection.
606569
func NewConnection(logger *logging.Logger, runtimeID common.Namespace, handler Handler) (Connection, error) {
607-
metricsOnce.Do(func() {
608-
prometheus.MustRegister(rhpCollectors...)
609-
})
570+
initMetrics()
610571

611-
c := &connection{
572+
return &connection{
612573
runtimeID: runtimeID,
613574
handler: handler,
614575
state: stateUninitialized,
@@ -617,7 +578,5 @@ func NewConnection(logger *logging.Logger, runtimeID common.Namespace, handler H
617578
outCh: make(chan *Message),
618579
closeCh: make(chan struct{}),
619580
logger: logger,
620-
}
621-
622-
return c, nil
581+
}, nil
623582
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package protocol
2+
3+
import (
4+
"sync"
5+
6+
"github.com/prometheus/client_golang/prometheus"
7+
8+
"github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common/metrics"
9+
)
10+
11+
var (
12+
rhpLatency = prometheus.NewSummaryVec(
13+
prometheus.SummaryOpts{
14+
Name: "oasis_rhp_latency",
15+
Help: "Runtime Host call latency (seconds).",
16+
},
17+
[]string{"call"},
18+
)
19+
rhpCallSuccesses = prometheus.NewCounterVec(
20+
prometheus.CounterOpts{
21+
Name: "oasis_rhp_successes",
22+
Help: "Number of successful Runtime Host calls.",
23+
},
24+
[]string{"call"},
25+
)
26+
rhpCallFailures = prometheus.NewCounterVec(
27+
prometheus.CounterOpts{
28+
Name: "oasis_rhp_failures",
29+
Help: "Number of failed Runtime Host calls.",
30+
},
31+
[]string{"call"},
32+
)
33+
rhpCallTimeouts = prometheus.NewCounter(
34+
prometheus.CounterOpts{
35+
Name: "oasis_rhp_timeouts",
36+
Help: "Number of timed out Runtime Host calls.",
37+
},
38+
)
39+
40+
rhpCollectors = []prometheus.Collector{
41+
rhpLatency,
42+
rhpCallSuccesses,
43+
rhpCallFailures,
44+
rhpCallTimeouts,
45+
}
46+
47+
metricsOnce sync.Once
48+
)
49+
50+
// initMetrics registers the metrics collectors.
51+
func initMetrics() {
52+
if !metrics.Enabled() {
53+
return
54+
}
55+
56+
metricsOnce.Do(func() {
57+
prometheus.MustRegister(rhpCollectors...)
58+
})
59+
}

0 commit comments

Comments
 (0)