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.

fix(logs-ingestion): add retention days to usage metrics and update tests (#60678)

Jon McCallum431907187739
diff --git a/nodejs/src/logs-ingestion/logs-ingestion-consumer.test.ts b/nodejs/src/logs-ingestion/logs-ingestion-consumer.test.ts
index 6490895dce2f..9ab5b5873ca5 100644
--- a/nodejs/src/logs-ingestion/logs-ingestion-consumer.test.ts
+++ b/nodejs/src/logs-ingestion/logs-ingestion-consumer.test.ts
@@ -1131,6 +1131,7 @@ describe('LogsIngestionConsumer', () => {
bytesDropped: 200,
recordsDropped: 2,
piiReplacements: 0,
+ retentionDays: 30,
},
],
])
@@ -1139,7 +1140,7 @@ describe('LogsIngestionConsumer', () => {
const messages = getProducedKafkaMessages().filter((m) => m.topic === KAFKA_APP_METRICS_2)
- expect(messages).toHaveLength(6)
+ expect(messages).toHaveLength(7)
const metricNames = messages.map((m) => parseMetricValue(m.value)?.metric_name)
expect(metricNames).toContain('bytes_received')
@@ -1148,8 +1149,47 @@ describe('LogsIngestionConsumer', () => {
expect(metricNames).toContain('records_ingested')
expect(metricNames).toContain('bytes_dropped')
expect(metricNames).toContain('records_dropped')
+ expect(metricNames).toContain('bytes_ingested_retention_30d')
})
+ it.each([
+ { retentionDays: 14, absent: ['bytes_ingested_retention_30d', 'bytes_ingested_retention_90d'] },
+ { retentionDays: 30, absent: ['bytes_ingested_retention_14d', 'bytes_ingested_retention_90d'] },
+ { retentionDays: 90, absent: ['bytes_ingested_retention_14d', 'bytes_ingested_retention_30d'] },
+ ])(
+ 'should emit only bytes_ingested_retention_${retentionDays}d for the matching tier',
+ async ({ retentionDays, absent }) => {
+ const usageStats = new Map([
+ [
+ team.id,
+ {
+ bytesReceived: 500,
+ recordsReceived: 5,
+ bytesAllowed: 500,
+ recordsAllowed: 5,
+ bytesDropped: 0,
+ recordsDropped: 0,
+ piiReplacements: 0,
+ retentionDays,
+ },
+ ],
+ ])
+
+ await consumer['emitUsageMetrics'](usageStats)
+
+ const messages = getProducedKafkaMessages().filter((m) => m.topic === KAFKA_APP_METRICS_2)
+ const retentionMetric = messages
+ .map((m) => parseMetricValue(m.value))
+ .find((m) => m?.metric_name === `bytes_ingested_retention_${retentionDays}d`)
+
+ expect(retentionMetric?.count).toBe(500)
+ const metricNames = messages.map((m) => parseMetricValue(m.value)?.metric_name)
+ for (const name of absent) {
+ expect(metricNames).not.toContain(name)
+ }
+ }
+ )
+
it('should skip zero-count metrics', async () => {
const usageStats = new Map([
[
@@ -1162,6 +1202,7 @@ describe('LogsIngestionConsumer', () => {
bytesDropped: 0,
recordsDropped: 0,
piiReplacements: 0,
+ retentionDays: 14,
},
],
])
@@ -1170,11 +1211,12 @@ describe('LogsIngestionConsumer', () => {
const messages = getProducedKafkaMessages().filter((m) => m.topic === KAFKA_APP_METRICS_2)
- // Should only have 4 metrics (no dropped)
- expect(messages).toHaveLength(4)
+ // 4 non-zero base metrics (no dropped) + 1 retention tier row
+ expect(messages).toHaveLength(5)
const metricNames = messages.map((m) => parseMetricValue(m.value)?.metric_name)
expect(metricNames).not.toContain('bytes_dropped')
expect(metricNames).not.toContain('records_dropped')
+ expect(metricNames).toContain('bytes_ingested_retention_14d')
})
it('should handle empty usageStats', async () => {
@@ -1199,6 +1241,7 @@ describe('LogsIngestionConsumer', () => {
bytesDropped: 0,
recordsDropped: 0,
piiReplacements: 0,
+ retentionDays: 30,
},
],
[
@@ -1211,6 +1254,7 @@ describe('LogsIngestionConsumer', () => {
bytesDropped: 0,
recordsDropped: 0,
piiReplacements: 0,
+ retentionDays: 90,
},
],
])
@@ -1219,13 +1263,13 @@ describe('LogsIngestionConsumer', () => {
const messages = getProducedKafkaMessages().filter((m) => m.topic === KAFKA_APP_METRICS_2)
- // 4 metrics per team (no dropped) = 8 total
- expect(messages).toHaveLength(8)
+ // 4 base metrics + 1 retention tier per team (no dropped) = 10 total
+ expect(messages).toHaveLength(10)
const team1Messages = messages.filter((m) => parseMetricValue(m.value)?.team_id === team.id)
const team2Messages = messages.filter((m) => parseMetricValue(m.value)?.team_id === team2.id)
- expect(team1Messages).toHaveLength(4)
- expect(team2Messages).toHaveLength(4)
+ expect(team1Messages).toHaveLength(5)
+ expect(team2Messages).toHaveLength(5)
})
})
diff --git a/nodejs/src/logs-ingestion/logs-ingestion-consumer.ts b/nodejs/src/logs-ingestion/logs-ingestion-consumer.ts
index e9c7f513c173..8306280674eb 100644
--- a/nodejs/src/logs-ingestion/logs-ingestion-consumer.ts
+++ b/nodejs/src/logs-ingestion/logs-ingestion-consumer.ts
@@ -40,6 +40,16 @@ export interface LogsIngestionConsumerDeps {
outputs: IngestionOutputs<LogsOutput | LogsDlqOutput | AppMetricsOutput>
}
+/** Ingestion default when `logs_settings.retention_days` is unset; must be in `TeamSerializer.VALID_RETENTION_DAYS`. */
+export const DEFAULT_LOGS_RETENTION_DAYS = 14
+
+/** Retention tiers that get their own per-tier usage metric; total = sum across all tiers. */
+const RETENTION_USAGE_TIERS = new Set([14, 30, 90])
+
+function retentionBytesMetricName(retentionDays: number): string | null {
+ return RETENTION_USAGE_TIERS.has(retentionDays) ? `bytes_ingested_retention_${retentionDays}d` : null
+}
+
export type UsageStats = {
bytesReceived: number
recordsReceived: number
@@ -48,6 +58,7 @@ export type UsageStats = {
bytesDropped: number
recordsDropped: number
piiReplacements: number
+ retentionDays: number
}
const DEFAULT_USAGE_STATS: UsageStats = {
@@ -58,13 +69,11 @@ const DEFAULT_USAGE_STATS: UsageStats = {
bytesDropped: 0,
recordsDropped: 0,
piiReplacements: 0,
+ retentionDays: DEFAULT_LOGS_RETENTION_DAYS,
}
export type UsageStatsByTeam = Map<number, UsageStats>
-/** Ingestion default when `logs_settings.retention_days` is unset; must be in `TeamSerializer.VALID_RETENTION_DAYS`. */
-export const DEFAULT_LOGS_RETENTION_DAYS = 14
-
/** Cap concurrent per-message processing within a single kafka batch. */
const MAX_CONCURRENT_MESSAGE_PROCESSES = 50
@@ -448,6 +457,12 @@ export class LogsIngestionConsumer {
const jsonParse = logsSettings.json_parse_logs ?? false
const retentionDays = logsSettings.retention_days ?? DEFAULT_LOGS_RETENTION_DAYS
+ // Retention is uniform per team; stash for retention-bucketed usage emit
+ const teamStats = usageStats.get(message.teamId)
+ if (teamStats) {
+ teamStats.retentionDays = retentionDays
+ }
+
// ignore empty messages
if (message.message.value === null) {
return Promise.resolve()
@@ -548,6 +563,11 @@ export class LogsIngestionConsumer {
this.queueUsageMetric(teamId, 'bytes_dropped', stats.bytesDropped)
this.queueUsageMetric(teamId, 'records_dropped', stats.recordsDropped)
this.queueUsageMetric(teamId, 'pii_replacements', stats.piiReplacements)
+
+ const retentionMetric = retentionBytesMetricName(stats.retentionDays)
+ if (retentionMetric) {
+ this.queueUsageMetric(teamId, retentionMetric, stats.bytesAllowed)
+ }
}
// Best-effort: don't let metric failures block ingestion