@@ -21,7 +21,7 @@ import (
21
21
"time"
22
22
23
23
onet "github.com/Jigsaw-Code/outline-ss-server/net"
24
- "github.com/oschwald/geoip2-golang"
24
+ geoip2 "github.com/oschwald/geoip2-golang"
25
25
"github.com/prometheus/client_golang/prometheus"
26
26
)
27
27
@@ -30,21 +30,25 @@ type ShadowsocksMetrics interface {
30
30
GetLocation (net.Addr ) (string , error )
31
31
32
32
SetNumAccessKeys (numKeys int , numPorts int )
33
- AddUDPPacketFromClient ( clientLocation , accessKey , status string , clientProxyBytes , proxyTargetBytes int )
34
- AddUDPPacketFromTarget ( clientLocation , accessKey , status string , targetProxyBytes , proxyClientBytes int )
33
+
34
+ // TCP metrics
35
35
AddOpenTCPConnection (clientLocation string )
36
- AddClosedTCPConnection (clientLocation , accessKey , status string , data ProxyMetrics , duration time.Duration )
36
+ AddClosedTCPConnection (clientLocation , accessKey , status string , data ProxyMetrics , timeToCipher , duration time.Duration )
37
37
38
- AddUdpNatEntry ()
39
- RemoveUdpNatEntry ()
38
+ // UDP metrics
39
+ AddUDPPacketFromClient (clientLocation , accessKey , status string , clientProxyBytes , proxyTargetBytes int , timeToCipher time.Duration )
40
+ AddUDPPacketFromTarget (clientLocation , accessKey , status string , targetProxyBytes , proxyClientBytes int )
41
+ AddUDPNatEntry ()
42
+ RemoveUDPNatEntry ()
40
43
}
41
44
42
45
type shadowsocksMetrics struct {
43
46
ipCountryDB * geoip2.Reader
44
47
45
- accessKeys prometheus.Gauge
46
- ports prometheus.Gauge
47
- dataBytes * prometheus.CounterVec
48
+ accessKeys prometheus.Gauge
49
+ ports prometheus.Gauge
50
+ dataBytes * prometheus.CounterVec
51
+ timeToCipherMs * prometheus.SummaryVec
48
52
// TODO: Add time to first byte.
49
53
50
54
tcpOpenConnections * prometheus.CounterVec
@@ -87,14 +91,21 @@ func NewShadowsocksMetrics(ipCountryDB *geoip2.Reader) ShadowsocksMetrics {
87
91
Subsystem : "tcp" ,
88
92
Name : "connection_duration_ms" ,
89
93
Help : "TCP connection duration distributions." ,
90
- Objectives : map [float64 ]float64 {0.5 : 0.05 , 0.9 : 0.01 , 0.99 : 0.001 },
94
+ Objectives : map [float64 ]float64 {0.5 : 0.02 , 0.9 : 0.01 , 0.99 : 0.005 },
91
95
}, []string {"location" , "status" , "access_key" }),
92
96
dataBytes : prometheus .NewCounterVec (
93
97
prometheus.CounterOpts {
94
98
Namespace : "shadowsocks" ,
95
99
Name : "data_bytes" ,
96
100
Help : "Bytes transferred by the proxy" ,
97
101
}, []string {"dir" , "proto" , "location" , "status" , "access_key" }),
102
+ timeToCipherMs : prometheus .NewSummaryVec (
103
+ prometheus.SummaryOpts {
104
+ Namespace : "shadowsocks" ,
105
+ Name : "time_to_cipher_ms" ,
106
+ Help : "Time needed to find the cipher" ,
107
+ Objectives : map [float64 ]float64 {0.5 : 0.02 , 0.9 : 0.01 , 0.99 : 0.005 },
108
+ }, []string {"proto" , "location" , "status" , "access_key" }),
98
109
udpAddedNatEntries : prometheus .NewCounter (
99
110
prometheus.CounterOpts {
100
111
Namespace : "shadowsocks" ,
@@ -112,7 +123,7 @@ func NewShadowsocksMetrics(ipCountryDB *geoip2.Reader) ShadowsocksMetrics {
112
123
}
113
124
// TODO: Is it possible to pass where to register the collectors?
114
125
prometheus .MustRegister (m .accessKeys , m .ports , m .tcpOpenConnections , m .tcpClosedConnections , m .tcpConnectionDurationMs ,
115
- m .dataBytes , m .udpAddedNatEntries , m .udpRemovedNatEntries )
126
+ m .dataBytes , m .timeToCipherMs , m . udpAddedNatEntries , m .udpRemovedNatEntries )
116
127
return m
117
128
}
118
129
@@ -163,16 +174,18 @@ func (m *shadowsocksMetrics) AddOpenTCPConnection(clientLocation string) {
163
174
m .tcpOpenConnections .WithLabelValues (clientLocation ).Inc ()
164
175
}
165
176
166
- func (m * shadowsocksMetrics ) AddClosedTCPConnection (clientLocation , accessKey , status string , data ProxyMetrics , duration time.Duration ) {
177
+ func (m * shadowsocksMetrics ) AddClosedTCPConnection (clientLocation , accessKey , status string , data ProxyMetrics , timeToCipher , duration time.Duration ) {
167
178
m .tcpClosedConnections .WithLabelValues (clientLocation , status , accessKey ).Inc ()
168
179
m .tcpConnectionDurationMs .WithLabelValues (clientLocation , status , accessKey ).Observe (duration .Seconds () * 1000 )
180
+ m .timeToCipherMs .WithLabelValues ("tcp" , clientLocation , status , accessKey ).Observe (timeToCipher .Seconds () * 1000 )
169
181
m .dataBytes .WithLabelValues ("c>p" , "tcp" , clientLocation , status , accessKey ).Add (float64 (data .ClientProxy ))
170
182
m .dataBytes .WithLabelValues ("p>t" , "tcp" , clientLocation , status , accessKey ).Add (float64 (data .ProxyTarget ))
171
183
m .dataBytes .WithLabelValues ("p<t" , "tcp" , clientLocation , status , accessKey ).Add (float64 (data .TargetProxy ))
172
184
m .dataBytes .WithLabelValues ("c<p" , "tcp" , clientLocation , status , accessKey ).Add (float64 (data .ProxyClient ))
173
185
}
174
186
175
- func (m * shadowsocksMetrics ) AddUDPPacketFromClient (clientLocation , accessKey , status string , clientProxyBytes , proxyTargetBytes int ) {
187
+ func (m * shadowsocksMetrics ) AddUDPPacketFromClient (clientLocation , accessKey , status string , clientProxyBytes , proxyTargetBytes int , timeToCipher time.Duration ) {
188
+ m .timeToCipherMs .WithLabelValues ("udp" , clientLocation , status , accessKey ).Observe (timeToCipher .Seconds () * 1000 )
176
189
m .dataBytes .WithLabelValues ("c>p" , "udp" , clientLocation , status , accessKey ).Add (float64 (clientProxyBytes ))
177
190
m .dataBytes .WithLabelValues ("p>t" , "udp" , clientLocation , status , accessKey ).Add (float64 (proxyTargetBytes ))
178
191
}
@@ -182,11 +195,11 @@ func (m *shadowsocksMetrics) AddUDPPacketFromTarget(clientLocation, accessKey, s
182
195
m .dataBytes .WithLabelValues ("c<p" , "udp" , clientLocation , status , accessKey ).Add (float64 (proxyClientBytes ))
183
196
}
184
197
185
- func (m * shadowsocksMetrics ) AddUdpNatEntry () {
198
+ func (m * shadowsocksMetrics ) AddUDPNatEntry () {
186
199
m .udpAddedNatEntries .Inc ()
187
200
}
188
201
189
- func (m * shadowsocksMetrics ) RemoveUdpNatEntry () {
202
+ func (m * shadowsocksMetrics ) RemoveUDPNatEntry () {
190
203
m .udpRemovedNatEntries .Inc ()
191
204
}
192
205
0 commit comments