instrumentation.go 3.48 KB
Newer Older
1 2
package node

ale's avatar
ale committed
3 4 5 6 7 8
import (
	"net/http"

	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promhttp"
)
9 10

var (
ale's avatar
ale committed
11
	// Proxy metrics.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
	streamSentBytes = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Name: "bytes_sent",
			Help: "Bytes proxied to the client, by stream.",
		},
		[]string{"stream"},
	)
	streamRcvdBytes = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Name: "bytes_received",
			Help: "Bytes received from the client, by stream.",
		},
		[]string{"stream"},
	)
	streamListeners = prometheus.NewGaugeVec(
		prometheus.GaugeOpts{
			Name: "listeners",
			Help: "Number of current listeners.",
		},
		[]string{"stream"},
	)
ale's avatar
ale committed
33 34 35 36 37 38 39
	proxyConnectErrs = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Name: "proxy_connect_errors",
			Help: "Proxy connection errors (client-side).",
		},
		[]string{"stream", "upstream"},
	)
ale's avatar
ale committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

	// Node metrics.
	icecastUpdateFailed = prometheus.NewGauge(
		prometheus.GaugeOpts{
			Name: "icecast_update_failed",
			Help: "Status of the last attempt to reload Icecast.",
		},
	)
	icecastIsLeader = prometheus.NewGauge(
		prometheus.GaugeOpts{
			Name: "icecast_is_leader",
			Help: "Icecast leader state.",
		},
	)

55 56 57 58 59 60 61 62 63 64 65 66 67 68
	// Status protocol (gossip) metrics.
	gossipNumNodes = prometheus.NewGauge(
		prometheus.GaugeOpts{
			Name: "gossip_peer_count",
			Help: "Number of peers seen by the gossip protocol.",
		},
	)
	gossipOldestTS = prometheus.NewGauge(
		prometheus.GaugeOpts{
			Name: "gossip_oldest_ts",
			Help: "Timestamp of the oldest update from any active peer.",
		},
	)

ale's avatar
ale committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
	// Descriptors for the nodeCollector below.
	numListenersDesc = prometheus.NewDesc(
		"status_num_listeners",
		"Number of total listeners.",
		nil, nil,
	)
	maxListenersDesc = prometheus.NewDesc(
		"status_max_listeners",
		"Maximum number of total listeners (for utilization).",
		nil, nil,
	)
	curBandwidthDesc = prometheus.NewDesc(
		"status_cur_bandwidth",
		"Current bandwidth usage (for utilization).",
		nil, nil,
	)
	maxBandwidthDesc = prometheus.NewDesc(
		"status_max_bandwidth",
		"Maximum bandwidth usage (for utilization).",
		nil, nil,
	)
	icecastOkDesc = prometheus.NewDesc(
		"status_icecast_ok",
		"Status of the node / Icecast connection.",
		nil, nil,
	)
95 96
)

ale's avatar
ale committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
// Prometheus Collector that exports the node's Status protobuf as metrics.
type nodeCollector struct {
	*Node
}

func (nc nodeCollector) Describe(ch chan<- *prometheus.Desc) {
	ch <- numListenersDesc
	ch <- maxListenersDesc
	ch <- curBandwidthDesc
	ch <- maxBandwidthDesc
	ch <- icecastOkDesc
}

func (nc nodeCollector) Collect(ch chan<- prometheus.Metric) {
	status := nc.Node.getStatus()
	ch <- prometheus.MustNewConstMetric(
		numListenersDesc,
		prometheus.GaugeValue,
		float64(status.NumListeners),
	)
	ch <- prometheus.MustNewConstMetric(
		maxListenersDesc,
		prometheus.GaugeValue,
		float64(status.MaxListeners),
	)
	ch <- prometheus.MustNewConstMetric(
		curBandwidthDesc,
		prometheus.GaugeValue,
		float64(status.CurBandwidth),
	)
	ch <- prometheus.MustNewConstMetric(
		maxBandwidthDesc,
		prometheus.GaugeValue,
		float64(status.MaxBandwidth),
	)
	var okVal float64
	if status.IcecastOk {
		okVal = 1
	}
	ch <- prometheus.MustNewConstMetric(
		icecastOkDesc,
		prometheus.GaugeValue,
		okVal,
	)
}

143 144 145 146 147
func init() {
	prometheus.MustRegister(
		streamSentBytes,
		streamRcvdBytes,
		streamListeners,
ale's avatar
ale committed
148
		proxyConnectErrs,
ale's avatar
ale committed
149 150
		icecastUpdateFailed,
		icecastIsLeader,
151 152
	)
}
ale's avatar
ale committed
153 154 155 156 157 158

func newMetricsHandler() http.Handler {
	mux := http.NewServeMux()
	mux.Handle("/metrics", promhttp.Handler())
	return mux
}