Skip to content

Commit 453ddfc

Browse files
misteriaudclaude
andcommitted
fix(pkg/aggregator): gate hookBatch append on HasSubscribers()
The unconditional append inside sample() was copying a 72-byte MetricSampleSnapshot struct on every DogStatsD metric even when no observer was listening. SMP profiler comparison (main vs branch with 0 subscribers) showed +12ms in (*TimeSampler).sample() from this copy alone. Gating on HasSubscribers() makes the idle path (0 subscribers) match main exactly: one atomic read, then nothing. The append only runs when someone is actually subscribed. Before: noop/0sub/1sub/5sub all ~82 ns (append unconditional) After: noop/0sub ~77 ns; 1sub ~82 ns; 5sub ~86 ns Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
1 parent 17b9bb1 commit 453ddfc

1 file changed

Lines changed: 10 additions & 8 deletions

File tree

pkg/aggregator/time_sampler.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,16 @@ func (s *TimeSampler) sample(metricSample *metrics.MetricSample, timestamp float
9797
contextKey := s.contextResolver.trackContext(metricSample, int64(timestamp), filterList)
9898
bucketStart := s.calculateBucketStart(timestamp)
9999

100-
s.hookBatch = append(s.hookBatch, hook.MetricSampleSnapshot{
101-
Name: metricSample.Name,
102-
Value: metricSample.Value,
103-
RawTags: metricSample.Tags,
104-
Timestamp: timestamp,
105-
SampleRate: metricSample.SampleRate,
106-
ContextKey: uint64(contextKey),
107-
})
100+
if s.metricHook.HasSubscribers() {
101+
s.hookBatch = append(s.hookBatch, hook.MetricSampleSnapshot{
102+
Name: metricSample.Name,
103+
Value: metricSample.Value,
104+
RawTags: metricSample.Tags,
105+
Timestamp: timestamp,
106+
SampleRate: metricSample.SampleRate,
107+
ContextKey: uint64(contextKey),
108+
})
109+
}
108110

109111
switch metricSample.Mtype {
110112
case metrics.DistributionType:

0 commit comments

Comments
 (0)