Skip to content

Commit abdfacf

Browse files
committed
dogstatds: consolidate metrics in a single payload until max_payload_len
This avoids sending each metric in a separate UDP packet and saves CPU cycles.
1 parent 7618c56 commit abdfacf

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

metrics-exporter-dogstatsd/src/writer.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,19 @@ impl PayloadWriter {
8888
return false;
8989
}
9090

91-
// Track the new offset.
92-
self.offsets.push(self.buf.len());
91+
// Offset update
92+
if current_len + self.last_offset() <= self.max_payload_len {
93+
// If the current metric can be written within the max_payload_len
94+
// replace the last offset (if there is valid offset)
95+
if let Some(last_offset) = self.offsets.last_mut() {
96+
*last_offset = self.buf.len();
97+
} else {
98+
self.offsets.push(self.buf.len());
99+
}
100+
} else {
101+
// - else add a new offset to send current metric in a new Packet
102+
self.offsets.push(self.buf.len());
103+
}
93104

94105
true
95106
}

0 commit comments

Comments
 (0)