Skip to content

Commit 43b2c9b

Browse files
[mq] [skip ddci] working branch - merge 1df1be4 on top of master at 71aa16f
{"baseBranch":"master","baseCommit":"71aa16f323565f4496264515ddb1a21322d00703","createdAt":"2026-06-02T12:13:19.584549Z","headSha":"1df1be48de3df3474ee5cbf9948b388c85d6abec","id":"3a97872b-8591-4097-bc14-d6dccd69d606","priority":"200","pullRequestNumber":"311","queuedAt":"2026-06-02T12:13:19.583537Z","status":"STATUS_QUEUED"}
2 parents 293f3ce + 1df1be4 commit 43b2c9b

2 files changed

Lines changed: 29 additions & 6 deletions

File tree

dogstatsd-http-core/src/main/java/com/datadoghq/dogstatsd/http/serializer/PayloadBuilder.java

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@
1212
import java.util.Collections;
1313
import java.util.List;
1414

15-
/** Build metrics payloads in a format accepted by the agent or the intake. */
15+
/**
16+
* Build metrics payloads in a format accepted by the agent or the intake.
17+
*
18+
* <p>If any {@link Metric} method throws, the in-progress metric is no longer valid; the caller
19+
* must call {@link #resetMetric()} to discard the partial state before encoding any further
20+
* metrics. Metrics that are ended without any successfully-added points are silently dropped from
21+
* the payload.
22+
*/
1623
public class PayloadBuilder {
1724
private static final int DEFAULT_MAX_PAYLOAD_SIZE = 256 * 1024;
1825
private static final int METRIC_DATA_FIELD_ID = 3;
@@ -205,6 +212,10 @@ void endMetric() {
205212
}
206213

207214
try {
215+
if (timestamps.length() == 0) {
216+
return;
217+
}
218+
208219
m.encodeIndependentFields();
209220
m.encodeDependentFields();
210221

@@ -221,14 +232,24 @@ void endMetric() {
221232
}
222233
payload.put(record);
223234
} finally {
224-
record.clear();
225-
timestamps.clear();
226-
values.clear();
227-
counts.clear();
228-
metricInProgress = null;
235+
resetMetric();
229236
}
230237
}
231238

239+
/**
240+
* Discard the in-progress metric without adding it to the payload.
241+
*
242+
* <p>Must be called after any {@link Metric} method throws — the metric is no longer valid and
243+
* its partial state must be discarded before encoding any further metrics.
244+
*/
245+
public void resetMetric() {
246+
record.clear();
247+
timestamps.clear();
248+
values.clear();
249+
counts.clear();
250+
metricInProgress = null;
251+
}
252+
232253
ColumnarBuffer currentRecord() {
233254
return record;
234255
}

dogstatsd-http-core/src/test/java/com/datadoghq/dogstatsd/http/serializer/PayloadBuilderTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public void handle(byte[] p) {
4343

4444
b.gauge("defgh").addPoint(100, 0).close();
4545

46+
b.gauge("noop").setTags(Arrays.asList(new String[] {"foo", "bar"})).setInterval(10).close();
47+
4648
Sketch sketch1 = new Sketch();
4749
sketch1.build(new long[] {1, 2, 2}, 1.0);
4850
Sketch sketch2 = new Sketch();

0 commit comments

Comments
 (0)