Skip to content

Commit 96243c3

Browse files
fix: [WIN-NPM] fix units of new latency metrics (#2018)
* fix: new latency metrics use seconds instead of milliseconds * chore: lint * fix: lower buckets to start at 8 milliseconds
1 parent b9ffaf2 commit 96243c3

File tree

6 files changed

+32
-12
lines changed

6 files changed

+32
-12
lines changed

npm/metrics/acls_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ func RecordACLLatency(timer *Timer, op OperationKind) {
77
labels := prometheus.Labels{
88
operationLabel: string(op),
99
}
10-
aclLatency.With(labels).Observe(timer.timeElapsed())
10+
aclLatency.With(labels).Observe(timer.timeElapsedSeconds())
1111
}
1212

1313
// IncACLFailures should be used in Windows DP to record the number of failures for individual ACL operations.

npm/metrics/endpoints_windows.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package metrics
22

33
func RecordListEndpointsLatency(timer *Timer) {
4-
listEndpointsLatency.Observe(timer.timeElapsed())
4+
listEndpointsLatency.Observe(timer.timeElapsedSeconds())
55
}
66

77
func IncListEndpointsFailures() {
88
listEndpointsFailures.Inc()
99
}
1010

1111
func RecordGetEndpointLatency(timer *Timer) {
12-
getEndpointLatency.Observe(timer.timeElapsed())
12+
getEndpointLatency.Observe(timer.timeElapsedSeconds())
1313
}
1414

1515
func IncGetEndpointFailures() {

npm/metrics/prometheus-metrics.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ func InitializeWindowsMetrics() {
202202
Subsystem: windowsPrefix,
203203
Help: "Latency in seconds to list HNS endpoints latency",
204204
//nolint:gomnd // default bucket consts
205-
Buckets: prometheus.ExponentialBuckets(0.016, 2, 14), // upper bounds of 16 ms to ~2 minutes
205+
Buckets: prometheus.ExponentialBuckets(0.008, 2, 14), // upper bounds of 8 ms to 65 seconds
206206
},
207207
)
208208

@@ -213,7 +213,7 @@ func InitializeWindowsMetrics() {
213213
Subsystem: windowsPrefix,
214214
Help: "Latency in seconds to get a single HNS endpoint",
215215
//nolint:gomnd // default bucket consts
216-
Buckets: prometheus.ExponentialBuckets(0.016, 2, 14), // upper bounds of 16 ms to ~2 minutes
216+
Buckets: prometheus.ExponentialBuckets(0.008, 2, 14), // upper bounds of 8 ms to 65 seconds
217217
},
218218
)
219219

@@ -224,7 +224,7 @@ func InitializeWindowsMetrics() {
224224
Subsystem: windowsPrefix,
225225
Help: "Latency in seconds to get the HNS network",
226226
//nolint:gomnd // default bucket consts
227-
Buckets: prometheus.ExponentialBuckets(0.016, 2, 14), // upper bounds of 16 ms to ~2 minutes
227+
Buckets: prometheus.ExponentialBuckets(0.008, 2, 14), // upper bounds of 8 ms to 65 seconds
228228
},
229229
)
230230

@@ -235,7 +235,7 @@ func InitializeWindowsMetrics() {
235235
Subsystem: windowsPrefix,
236236
Help: "Latency in seconds to add/update ACLs by operation label",
237237
//nolint:gomnd // default bucket consts
238-
Buckets: prometheus.ExponentialBuckets(0.016, 2, 14), // upper bounds of 16 ms to ~2 minutes
238+
Buckets: prometheus.ExponentialBuckets(0.008, 2, 14), // upper bounds of 8 ms to 65 seconds
239239
},
240240
[]string{operationLabel},
241241
)
@@ -247,7 +247,7 @@ func InitializeWindowsMetrics() {
247247
Subsystem: windowsPrefix,
248248
Help: "Latency in seconds to add/update/delete SetPolicies by operation & is_nested label",
249249
//nolint:gomnd // default bucket consts
250-
Buckets: prometheus.ExponentialBuckets(0.016, 2, 14), // upper bounds of 16 ms to ~2 minutes
250+
Buckets: prometheus.ExponentialBuckets(0.008, 2, 14), // upper bounds of 8 ms to 65 seconds
251251
},
252252
[]string{operationLabel, isNestedLabel},
253253
)

npm/metrics/setpolicies_windows.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func RecordSetPolicyLatency(timer *Timer, op OperationKind, isNested bool) {
1111
operationLabel: string(op),
1212
isNestedLabel: nested,
1313
}
14-
setPolicyLatency.With(labels).Observe(timer.timeElapsed())
14+
setPolicyLatency.With(labels).Observe(timer.timeElapsedSeconds())
1515
}
1616

1717
func IncSetPolicyFailures(op OperationKind, isNested bool) {
@@ -27,7 +27,7 @@ func IncSetPolicyFailures(op OperationKind, isNested bool) {
2727
}
2828

2929
func RecordGetNetworkLatency(timer *Timer) {
30-
getNetworkLatency.Observe(timer.timeElapsed())
30+
getNetworkLatency.Observe(timer.timeElapsedSeconds())
3131
}
3232

3333
func IncGetNetworkFailures() {

npm/metrics/timer.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"github.com/prometheus/client_golang/prometheus"
88
)
99

10+
const billion float64 = 1e+09
11+
1012
// Timer is a one-time-use tool for recording time between a start and end point
1113
type Timer struct {
1214
before int64
@@ -18,12 +20,14 @@ func StartNewTimer() *Timer {
1820
return &Timer{time.Now().UnixNano(), 0}
1921
}
2022

21-
// stopAndRecord ends a timer and records its delta in a summary
23+
// stopAndRecord uses milliseconds.
24+
// It ends a timer and records its delta in a summary
2225
func (timer *Timer) stopAndRecord(observer prometheus.Summary) {
2326
observer.Observe(timer.timeElapsed())
2427
}
2528

26-
// stopAndRecordCRUDExecTime ends a timer and records its delta in a summary (unless the operation is NoOp) with the specified operation as a label.
29+
// stopAndRecordCRUDExecTime uses milliseconds.
30+
// It ends a timer and records its delta in a summary (unless the operation is NoOp) with the specified operation as a label.
2731
func (timer *Timer) stopAndRecordCRUDExecTime(observer *prometheus.SummaryVec, op OperationKind, hadError bool) {
2832
timer.stop()
2933
if !op.isValid() {
@@ -46,10 +50,19 @@ func (timer *Timer) stop() {
4650
timer.after = time.Now().UnixNano()
4751
}
4852

53+
// timeElapsed returns milliseconds
4954
func (timer *Timer) timeElapsed() float64 {
5055
if timer.after == 0 {
5156
timer.stop()
5257
}
5358
millisecondDifference := float64(timer.after-timer.before) / 1000000.0
5459
return millisecondDifference
5560
}
61+
62+
// timeElapsedSeconds returns seconds
63+
func (timer *Timer) timeElapsedSeconds() float64 {
64+
if timer.after == 0 {
65+
timer.stop()
66+
}
67+
return float64(timer.after-timer.before) / billion
68+
}

npm/metrics/timer_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,10 @@ func TestTimeElapsed(t *testing.T) {
1919
require.FailNowf(t, "", "expected elapsed time for timer to be %f but got %f", expectedDuration, duration)
2020
}
2121
}
22+
23+
func TestTimeElapsedSeconds(t *testing.T) {
24+
timer := StartNewTimer()
25+
time.Sleep(time.Millisecond * 1100)
26+
require.GreaterOrEqual(t, timer.timeElapsedSeconds(), 1.0)
27+
require.LessOrEqual(t, timer.timeElapsedSeconds(), 3.0)
28+
}

0 commit comments

Comments
 (0)