github.com/ethersphere/bee/v2@v2.2.0/pkg/pingpong/metrics.go (about)

     1  // Copyright 2020 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package pingpong
     6  
     7  import (
     8  	m "github.com/ethersphere/bee/v2/pkg/metrics"
     9  	"github.com/prometheus/client_golang/prometheus"
    10  )
    11  
    12  type metrics struct {
    13  	// all metrics fields must be exported
    14  	// to be able to return them by Metrics()
    15  	// using reflection
    16  	PingSentCount     prometheus.Counter
    17  	PongSentCount     prometheus.Counter
    18  	PingReceivedCount prometheus.Counter
    19  	PongReceivedCount prometheus.Counter
    20  }
    21  
    22  func newMetrics() metrics {
    23  	subsystem := "pingpong"
    24  
    25  	return metrics{
    26  		PingSentCount: prometheus.NewCounter(prometheus.CounterOpts{
    27  			Namespace: m.Namespace,
    28  			Subsystem: subsystem,
    29  			Name:      "ping_sent_count",
    30  			Help:      "Number ping requests sent.",
    31  		}),
    32  		PongSentCount: prometheus.NewCounter(prometheus.CounterOpts{
    33  			Namespace: m.Namespace,
    34  			Subsystem: subsystem,
    35  			Name:      "pong_sent_count",
    36  			Help:      "Number of pong responses sent.",
    37  		}),
    38  		PingReceivedCount: prometheus.NewCounter(prometheus.CounterOpts{
    39  			Namespace: m.Namespace,
    40  			Subsystem: subsystem,
    41  			Name:      "ping_received_count",
    42  			Help:      "Number ping requests received.",
    43  		}),
    44  		PongReceivedCount: prometheus.NewCounter(prometheus.CounterOpts{
    45  			Namespace: m.Namespace,
    46  			Subsystem: subsystem,
    47  			Name:      "pong_received_count",
    48  			Help:      "Number of pong responses received.",
    49  		}),
    50  	}
    51  }
    52  
    53  func (s *Service) Metrics() []prometheus.Collector {
    54  	return m.PrometheusCollectorsFromFields(s.metrics)
    55  }