@@ -120,7 +120,7 @@ func newShadowsocksMetrics(ipCountryDB *geoip2.Reader) *shadowsocksMetrics {
120
120
tcpProbes : prometheus .NewHistogramVec (prometheus.HistogramOpts {
121
121
Namespace : "shadowsocks" ,
122
122
Name : "tcp_probes" ,
123
- Buckets : []float64 {0 , 48 , 49 , 50 , 51 , 52 , 72 , 73 , 90 , 91 , 220 , 221 },
123
+ Buckets : []float64 {0 , 49 , 50 , 51 , 73 , 91 },
124
124
Help : "Histogram of number of bytes from client to proxy, for detecting possible probes" ,
125
125
}, []string {"location" , "port" , "status" , "error" }),
126
126
timeToCipherMs : prometheus .NewHistogramVec (
@@ -215,14 +215,21 @@ func isFound(accessKey string) string {
215
215
return fmt .Sprintf ("%t" , accessKey != "" )
216
216
}
217
217
218
+ // addIfNonZero helps avoid the creation of series that are always zero.
219
+ func addIfNonZero (counter prometheus.Counter , value int64 ) {
220
+ if value > 0 {
221
+ counter .Add (float64 (value ))
222
+ }
223
+ }
224
+
218
225
func (m * shadowsocksMetrics ) AddClosedTCPConnection (clientLocation , accessKey , status string , data ProxyMetrics , timeToCipher , duration time.Duration ) {
219
226
m .tcpClosedConnections .WithLabelValues (clientLocation , status , accessKey ).Inc ()
220
227
m .tcpConnectionDurationMs .WithLabelValues (status ).Observe (duration .Seconds () * 1000 )
221
228
m .timeToCipherMs .WithLabelValues ("tcp" , isFound (accessKey )).Observe (timeToCipher .Seconds () * 1000 )
222
- m .dataBytes .WithLabelValues ("c>p" , "tcp" , clientLocation , status , accessKey ). Add ( float64 ( data .ClientProxy ) )
223
- m .dataBytes .WithLabelValues ("p>t" , "tcp" , clientLocation , status , accessKey ). Add ( float64 ( data .ProxyTarget ) )
224
- m .dataBytes .WithLabelValues ("p<t" , "tcp" , clientLocation , status , accessKey ). Add ( float64 ( data .TargetProxy ) )
225
- m .dataBytes .WithLabelValues ("c<p" , "tcp" , clientLocation , status , accessKey ). Add ( float64 ( data .ProxyClient ) )
229
+ addIfNonZero ( m .dataBytes .WithLabelValues ("c>p" , "tcp" , clientLocation , status , accessKey ), data .ClientProxy )
230
+ addIfNonZero ( m .dataBytes .WithLabelValues ("p>t" , "tcp" , clientLocation , status , accessKey ), data .ProxyTarget )
231
+ addIfNonZero ( m .dataBytes .WithLabelValues ("p<t" , "tcp" , clientLocation , status , accessKey ), data .TargetProxy )
232
+ addIfNonZero ( m .dataBytes .WithLabelValues ("c<p" , "tcp" , clientLocation , status , accessKey ), data .ProxyClient )
226
233
}
227
234
228
235
func (m * shadowsocksMetrics ) AddTCPProbe (clientLocation , status , drainResult string , port int , data ProxyMetrics ) {
@@ -231,13 +238,13 @@ func (m *shadowsocksMetrics) AddTCPProbe(clientLocation, status, drainResult str
231
238
232
239
func (m * shadowsocksMetrics ) AddUDPPacketFromClient (clientLocation , accessKey , status string , clientProxyBytes , proxyTargetBytes int , timeToCipher time.Duration ) {
233
240
m .timeToCipherMs .WithLabelValues ("udp" , isFound (accessKey )).Observe (timeToCipher .Seconds () * 1000 )
234
- m .dataBytes .WithLabelValues ("c>p" , "udp" , clientLocation , status , accessKey ). Add ( float64 (clientProxyBytes ))
235
- m .dataBytes .WithLabelValues ("p>t" , "udp" , clientLocation , status , accessKey ). Add ( float64 (proxyTargetBytes ))
241
+ addIfNonZero ( m .dataBytes .WithLabelValues ("c>p" , "udp" , clientLocation , status , accessKey ), int64 (clientProxyBytes ))
242
+ addIfNonZero ( m .dataBytes .WithLabelValues ("p>t" , "udp" , clientLocation , status , accessKey ), int64 (proxyTargetBytes ))
236
243
}
237
244
238
245
func (m * shadowsocksMetrics ) AddUDPPacketFromTarget (clientLocation , accessKey , status string , targetProxyBytes , proxyClientBytes int ) {
239
- m .dataBytes .WithLabelValues ("p<t" , "udp" , clientLocation , status , accessKey ). Add ( float64 (targetProxyBytes ))
240
- m .dataBytes .WithLabelValues ("c<p" , "udp" , clientLocation , status , accessKey ). Add ( float64 (proxyClientBytes ))
246
+ addIfNonZero ( m .dataBytes .WithLabelValues ("p<t" , "udp" , clientLocation , status , accessKey ), int64 (targetProxyBytes ))
247
+ addIfNonZero ( m .dataBytes .WithLabelValues ("c<p" , "udp" , clientLocation , status , accessKey ), int64 (proxyClientBytes ))
241
248
}
242
249
243
250
func (m * shadowsocksMetrics ) AddUDPNatEntry () {
0 commit comments