Skip to content

Commit b07ce76

Browse files
fix: Improve telemetry allocations (#2185)
This PR: - moves OTEL counter initialization to a single place - removes startTime and endTime non-counter metrics - introduces duration metric - removes client-level cardinality from the metrics, preventing memory accumulation
1 parent 4aaab28 commit b07ce76

File tree

10 files changed

+352
-292
lines changed

10 files changed

+352
-292
lines changed

scheduler/metrics/duration.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package metrics
2+
3+
import (
4+
"sync"
5+
"time"
6+
)
7+
8+
type durationMeasurement struct {
9+
startTime time.Time
10+
started bool
11+
duration time.Duration
12+
sem sync.Mutex
13+
}
14+
15+
func (dm *durationMeasurement) Start(start time.Time) {
16+
// If we have already started, don't start again. This can happen for relational tables that are resolved multiple times (per parent resource)
17+
dm.sem.Lock()
18+
defer dm.sem.Unlock()
19+
if dm.started {
20+
return
21+
}
22+
23+
dm.started = true
24+
dm.startTime = start
25+
}
26+
27+
// End calculates, updates and returns the delta duration for updating OTEL counters.
28+
func (dm *durationMeasurement) End(end time.Time) time.Duration {
29+
var delta time.Duration
30+
newDuration := end.Sub(dm.startTime)
31+
32+
dm.sem.Lock()
33+
defer dm.sem.Unlock()
34+
35+
delta = newDuration - dm.duration
36+
dm.duration = newDuration
37+
return delta
38+
}

0 commit comments

Comments
 (0)