metrics.go 1.54 KB
Newer Older
1 2 3 4
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

Janos Guljas's avatar
Janos Guljas committed
5 6 7
package pingpong

import (
8
	m "github.com/ethersphere/bee/pkg/metrics"
Janos Guljas's avatar
Janos Guljas committed
9 10 11 12 13 14 15 16 17 18 19 20 21
	"github.com/prometheus/client_golang/prometheus"
)

type metrics struct {
	// all metrics fields must be exported
	// to be able to return them by Metrics()
	// using reflection
	PingSentCount     prometheus.Counter
	PongSentCount     prometheus.Counter
	PingReceivedCount prometheus.Counter
	PongReceivedCount prometheus.Counter
}

22 23 24
func newMetrics() metrics {
	subsystem := "pingpong"

Janos Guljas's avatar
Janos Guljas committed
25 26
	return metrics{
		PingSentCount: prometheus.NewCounter(prometheus.CounterOpts{
27 28 29 30
			Namespace: m.Namespace,
			Subsystem: subsystem,
			Name:      "ping_sent_count",
			Help:      "Number ping requests sent.",
Janos Guljas's avatar
Janos Guljas committed
31 32
		}),
		PongSentCount: prometheus.NewCounter(prometheus.CounterOpts{
33
			Namespace: m.Namespace,
34
			Subsystem: subsystem,
35 36
			Name:      "pong_sent_count",
			Help:      "Number of pong responses sent.",
Janos Guljas's avatar
Janos Guljas committed
37 38
		}),
		PingReceivedCount: prometheus.NewCounter(prometheus.CounterOpts{
39 40 41 42
			Namespace: m.Namespace,
			Subsystem: subsystem,
			Name:      "ping_received_count",
			Help:      "Number ping requests received.",
Janos Guljas's avatar
Janos Guljas committed
43 44
		}),
		PongReceivedCount: prometheus.NewCounter(prometheus.CounterOpts{
45 46 47 48
			Namespace: m.Namespace,
			Subsystem: subsystem,
			Name:      "pong_received_count",
			Help:      "Number of pong responses received.",
Janos Guljas's avatar
Janos Guljas committed
49 50 51 52
		}),
	}
}

53 54
func (s *Service) Metrics() []prometheus.Collector {
	return m.PrometheusCollectorsFromFields(s.metrics)
Janos Guljas's avatar
Janos Guljas committed
55
}