Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(logs): suppress empty log batches instead of producing them (#77478)

Frank Hamand329b05d05a04
diff --git a/nodejs/src/logs/log-record-avro.ts b/nodejs/src/logs/log-record-avro.ts
index 1aa2112e7308..ea10d3d66b7a 100644
--- a/nodejs/src/logs/log-record-avro.ts
+++ b/nodejs/src/logs/log-record-avro.ts
@@ -364,16 +364,19 @@ export const processLogMessageBuffer = instrumented({
const { kept, stats } = await runPipelineStages(records, stages)
+ if (kept.length === 0) {
+ // No surviving records — signal the caller to suppress the message rather than forward or
+ // re-encode an empty batch downstream. Checked before the visitor-only shortcut below so a
+ // zero-record batch decoded solely for metric-rule tallying is suppressed too.
+ // `stats.droppedBy` distinguishes a filter drop (sampling / transformations) from an
+ // already-empty batch so the caller can attribute it correctly.
+ return { value: null, pii, drops: stats }
+ }
if (!mustReencode) {
- // Only a visitor ran — the buffer is unchanged, so forward it without re-encoding.
+ // Only a visitor ran and records survive — the buffer is unchanged, so forward it without
+ // re-encoding.
return { value: buffer, pii, drops: stats }
}
- if (kept.length === 0 && stats.droppedBy) {
- // A filter stage emptied the batch — signal the caller to suppress it. A batch that was
- // *already* empty (nothing dropped) still re-encodes to a schema-only buffer, matching the
- // pre-pipeline behavior; suppressing those is a separate change.
- return { value: null, pii, drops: stats }
- }
const value = await encodeLogRecordsInstrumented(logRecordType, codec, kept)
return { value, pii, drops: stats }
diff --git a/nodejs/src/logs/logs-ingestion-consumer.ts b/nodejs/src/logs/logs-ingestion-consumer.ts
index 6abc6181db7c..baf449c59a33 100644
--- a/nodejs/src/logs/logs-ingestion-consumer.ts
+++ b/nodejs/src/logs/logs-ingestion-consumer.ts
@@ -430,7 +430,7 @@ export class LogsIngestionConsumer {
}
| {
outcome: 'all_dropped'
- reason: 'sampling_all_dropped' | 'transformations_all_dropped'
+ reason: 'sampling_all_dropped' | 'transformations_all_dropped' | 'empty_batch'
pii: PiiScrubStats
recordsDropped: number
recordsDroppedByRuleId: Map<string, number>
@@ -510,9 +510,17 @@ export class LogsIngestionConsumer {
}
if (value === null) {
+ // `droppedBy` tells us which filter emptied the batch; its absence means the batch decoded
+ // to zero records to begin with, so attribute it to an empty batch rather than sampling.
+ const reason =
+ drops.droppedBy === 'transformations'
+ ? 'transformations_all_dropped'
+ : drops.droppedBy === 'sampling'
+ ? 'sampling_all_dropped'
+ : 'empty_batch'
return {
outcome: 'all_dropped',
- reason: drops.droppedBy === 'transformations' ? 'transformations_all_dropped' : 'sampling_all_dropped',
+ reason,
pii,
recordsDropped: drops.recordsDropped,
recordsDroppedByRuleId: drops.recordsDroppedByRuleId,