@@ -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.
7435type Handler interface {
@@ -308,18 +269,20 @@ func (c *connection) Call(ctx context.Context, body *Body) (*Body, error) {
308269func (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.
606569func 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}
0 commit comments