forked from minio/sidekick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
174 lines (152 loc) · 4.78 KB
/
metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
95
96
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright (c) 2020 MinIO, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/atomic"
)
func init() {
prometheus.MustRegister(newSidekickCollector())
}
// newSidekickCollector describes the collector
// and returns reference of sidekickCollector
// It creates the Prometheus Description which is used
// to define metric and help string
func newSidekickCollector() *sidekickCollector {
return &sidekickCollector{
desc: prometheus.NewDesc("sidekick_stats", "Statistics exposed by Sidekick loadbalancer", nil, nil),
}
}
// sidekickCollector is the Custom Collector
type sidekickCollector struct {
desc *prometheus.Desc
}
// Describe sends the super-set of all possible descriptors of metrics
func (c *sidekickCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.desc
}
// Collect is called by the Prometheus registry when collecting metrics.
func (c *sidekickCollector) Collect(ch chan<- prometheus.Metric) {
for _, c := range globalConnStats {
if c == nil {
continue
}
// total calls per node
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName("sidekick", "requests", "total"),
"Total number of calls in current SideKick server instance",
[]string{"endpoint"}, nil),
prometheus.CounterValue,
float64(c.totalCalls.Load()),
c.endpoint,
)
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName("sidekick", "errors", "total"),
"Total number of failed calls in current SideKick server instance",
[]string{"endpoint"}, nil),
prometheus.CounterValue,
float64(c.totalFailedCalls.Load()),
c.endpoint,
)
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName("sidekick", "rx", "bytes_total"),
"Total number of bytes received by current SideKick server instance",
[]string{"endpoint"}, nil),
prometheus.CounterValue,
float64(c.getTotalInputBytes()),
c.endpoint,
)
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName("sidekick", "tx", "bytes_total"),
"Total number of bytes sent by current SideKick server instance",
[]string{"endpoint"}, nil),
prometheus.CounterValue,
float64(c.getTotalOutputBytes()),
c.endpoint,
)
}
}
func metricsHandler() (http.Handler, error) {
registry := prometheus.NewRegistry()
err := registry.Register(newSidekickCollector())
if err != nil {
return nil, err
}
gatherers := prometheus.Gatherers{
prometheus.DefaultGatherer,
registry,
}
// Delegate http serving to Prometheus client library, which will call collector.Collect.
return promhttp.InstrumentMetricHandler(
registry,
promhttp.HandlerFor(gatherers,
promhttp.HandlerOpts{
ErrorHandling: promhttp.ContinueOnError,
}),
), nil
}
// ConnStats - statistics on backend
type ConnStats struct {
endpoint string
totalInputBytes atomic.Uint64
totalOutputBytes atomic.Uint64
totalCalls atomic.Uint64
totalFailedCalls atomic.Uint64
minLatency atomic.Duration
maxLatency atomic.Duration
}
// Store current total input bytes
func (s *ConnStats) setInputBytes(n int64) {
s.totalInputBytes.Store(uint64(n))
}
// Store current total output bytes
func (s *ConnStats) setOutputBytes(n int64) {
s.totalOutputBytes.Store(uint64(n))
}
// Return total input bytes
func (s *ConnStats) getTotalInputBytes() uint64 {
return s.totalInputBytes.Load()
}
// Store current total calls
func (s *ConnStats) setTotalCalls(n int64) {
s.totalCalls.Store(uint64(n))
}
// Store current total call failures
func (s *ConnStats) setTotalCallFailures(n int64) {
s.totalFailedCalls.Store(uint64(n))
}
// set min latency
func (s *ConnStats) setMinLatency(mn time.Duration) {
s.minLatency.Store(mn)
}
// set max latency
func (s *ConnStats) setMaxLatency(mx time.Duration) {
s.maxLatency.Store(mx)
}
// Return total output bytes
func (s *ConnStats) getTotalOutputBytes() uint64 {
return s.totalOutputBytes.Load()
}
// Prepare new ConnStats structure
func newConnStats(endpoint string) *ConnStats {
return &ConnStats{endpoint: endpoint}
}