Skip to content

Commit 8269318

Browse files
authored
Merge pull request #91 from Jigsaw-Code/fortuna-bytes-metric
Reduce cost of shadowsocks_data_bytes and shadowsocks_tcp_probes_bucket metrics
2 parents e82cf98 + 90bbbe2 commit 8269318

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

service/metrics/metrics.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func newShadowsocksMetrics(ipCountryDB *geoip2.Reader) *shadowsocksMetrics {
120120
tcpProbes: prometheus.NewHistogramVec(prometheus.HistogramOpts{
121121
Namespace: "shadowsocks",
122122
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},
124124
Help: "Histogram of number of bytes from client to proxy, for detecting possible probes",
125125
}, []string{"location", "port", "status", "error"}),
126126
timeToCipherMs: prometheus.NewHistogramVec(
@@ -215,14 +215,21 @@ func isFound(accessKey string) string {
215215
return fmt.Sprintf("%t", accessKey != "")
216216
}
217217

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+
218225
func (m *shadowsocksMetrics) AddClosedTCPConnection(clientLocation, accessKey, status string, data ProxyMetrics, timeToCipher, duration time.Duration) {
219226
m.tcpClosedConnections.WithLabelValues(clientLocation, status, accessKey).Inc()
220227
m.tcpConnectionDurationMs.WithLabelValues(status).Observe(duration.Seconds() * 1000)
221228
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)
226233
}
227234

228235
func (m *shadowsocksMetrics) AddTCPProbe(clientLocation, status, drainResult string, port int, data ProxyMetrics) {
@@ -231,13 +238,13 @@ func (m *shadowsocksMetrics) AddTCPProbe(clientLocation, status, drainResult str
231238

232239
func (m *shadowsocksMetrics) AddUDPPacketFromClient(clientLocation, accessKey, status string, clientProxyBytes, proxyTargetBytes int, timeToCipher time.Duration) {
233240
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))
236243
}
237244

238245
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))
241248
}
242249

243250
func (m *shadowsocksMetrics) AddUDPNatEntry() {

0 commit comments

Comments
 (0)