Skip to content

Commit 8d3d692

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 9e6e019 commit 8d3d692

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
@@ -83,8 +83,19 @@ impl PayloadWriter {
8383
return false;
8484
}
8585

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

89100
true
90101
}

0 commit comments

Comments
 (0)