-
Notifications
You must be signed in to change notification settings - Fork 13
/
net_metrics.go
156 lines (134 loc) · 3.93 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
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
// 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"
"slices"
"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"
}
commonLocalhostNames := []string{
"localhost",
"127.0.0.1",
"::1",
"::",
}
if slices.Contains(commonLocalhostNames, host) {
return "localhost"
}
if ip := net.ParseIP(host); ip != nil && (ip.IsLoopback() || ip.IsUnspecified()) {
return "localhost"
}
return host
}
type listenerMetrics struct {
accepted prometheus.Counter
errors 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",
}),
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) close() {
m.closed.Inc()
}
func newListenerMetricsWithNameFunc(r prometheus.Registerer, namespace string) func(name string) *listenerMetrics {
if r == nil {
r = prometheus.NewRegistry() // This registry will be discarded.
}
f := promauto.With(r)
accepted := f.NewCounterVec(prometheus.CounterOpts{
Name: "listener_accepted_total",
Namespace: namespace,
Help: "Number of accepted connections",
}, []string{"name"})
errors := f.NewCounterVec(prometheus.CounterOpts{
Name: "listener_errors_total",
Namespace: namespace,
Help: "Number of listener errors when accepting connections",
}, []string{"name"})
closed := f.NewCounterVec(prometheus.CounterOpts{
Name: "listener_closed_total",
Namespace: namespace,
Help: "Number of closed connections",
}, []string{"name"})
return func(name string) *listenerMetrics {
return &listenerMetrics{
accepted: accepted.WithLabelValues(name),
errors: errors.WithLabelValues(name),
closed: closed.WithLabelValues(name),
}
}
}