-
Notifications
You must be signed in to change notification settings - Fork 13
/
net_metrics.go
124 lines (105 loc) · 2.98 KB
/
net_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
// Copyright 2022-2024 Sauce Labs Inc., all rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package forwarder
import (
"net"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
type dialerMetrics struct {
dialed *prometheus.CounterVec
errors *prometheus.CounterVec
closed *prometheus.CounterVec
}
func newDialerMetrics(r prometheus.Registerer, namespace string) *dialerMetrics {
if r == nil {
r = prometheus.NewRegistry() // This registry will be discarded.
}
f := promauto.With(r)
l := []string{"host"}
return &dialerMetrics{
dialed: f.NewCounterVec(prometheus.CounterOpts{
Name: "dialer_dialed_total",
Namespace: namespace,
Help: "Number of dialed connections",
}, l),
errors: f.NewCounterVec(prometheus.CounterOpts{
Name: "dialer_errors_total",
Namespace: namespace,
Help: "Number of dialer errors",
}, l),
closed: f.NewCounterVec(prometheus.CounterOpts{
Name: "dialer_closed_total",
Namespace: namespace,
Help: "Number of closed connections",
}, l),
}
}
func (m *dialerMetrics) dial(addr string) {
m.dialed.WithLabelValues(addr2Host(addr)).Inc()
}
func (m *dialerMetrics) error(addr string) {
m.errors.WithLabelValues(addr2Host(addr)).Inc()
}
func (m *dialerMetrics) close(addr string) {
m.closed.WithLabelValues(addr2Host(addr)).Inc()
}
func addr2Host(addr string) string {
host, _, err := net.SplitHostPort(addr)
if err != nil {
return "unknown"
}
if isLocalhost(host) {
return "localhost"
}
return host
}
type listenerMetrics struct {
accepted prometheus.Counter
errors prometheus.Counter
tlsErrors prometheus.Counter
closed prometheus.Counter
}
func newListenerMetrics(r prometheus.Registerer, namespace string) *listenerMetrics {
if r == nil {
r = prometheus.NewRegistry() // This registry will be discarded.
}
f := promauto.With(r)
return &listenerMetrics{
accepted: f.NewCounter(prometheus.CounterOpts{
Name: "listener_accepted_total",
Namespace: namespace,
Help: "Number of accepted connections",
}),
errors: f.NewCounter(prometheus.CounterOpts{
Name: "listener_errors_total",
Namespace: namespace,
Help: "Number of listener errors when accepting connections",
}),
tlsErrors: f.NewCounter(prometheus.CounterOpts{
Name: "listener_tls_errors_total",
Namespace: namespace,
Help: "Number of TLS handshake errors",
}),
closed: f.NewCounter(prometheus.CounterOpts{
Name: "listener_closed_total",
Namespace: namespace,
Help: "Number of closed connections",
}),
}
}
func (m *listenerMetrics) accept() {
m.accepted.Inc()
}
func (m *listenerMetrics) error() {
m.errors.Inc()
}
func (m *listenerMetrics) tlsError() {
m.tlsErrors.Inc()
}
func (m *listenerMetrics) close() {
m.closed.Inc()
}