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): skip retention_byte_days usage metric for the default retention (#96452)

Frank Hamand2a1b0f11c5d2
diff --git a/nodejs/src/logs/logs-ingestion-consumer.test.ts b/nodejs/src/logs/logs-ingestion-consumer.test.ts
index f0c0f63d64ba..48b92a915324 100644
--- a/nodejs/src/logs/logs-ingestion-consumer.test.ts
+++ b/nodejs/src/logs/logs-ingestion-consumer.test.ts
@@ -1511,13 +1511,13 @@ describe('LogsIngestionConsumer', () => {
})
it.each([
- { retentionDays: 14, tierMetric: 'bytes_ingested_retention_14d' },
- { retentionDays: 30, tierMetric: 'bytes_ingested_retention_30d' },
- { retentionDays: 90, tierMetric: 'bytes_ingested_retention_90d' },
- { retentionDays: 45, tierMetric: null },
+ { retentionDays: 14, tierMetric: 'bytes_ingested_retention_14d', byteDays: undefined },
+ { retentionDays: 30, tierMetric: 'bytes_ingested_retention_30d', byteDays: 500 * 30 },
+ { retentionDays: 90, tierMetric: 'bytes_ingested_retention_90d', byteDays: 500 * 90 },
+ { retentionDays: 45, tierMetric: null, byteDays: 500 * 45 },
])(
- 'should emit retention_byte_days as bytes_ingested * $retentionDays alongside the per-tier metric',
- async ({ retentionDays, tierMetric }) => {
+ 'should emit retention_byte_days as $byteDays for $retentionDays days alongside the per-tier metric',
+ async ({ retentionDays, tierMetric, byteDays }) => {
const usageStats = new Map([
[
team.id,
@@ -1540,7 +1540,7 @@ describe('LogsIngestionConsumer', () => {
const messages = getProducedKafkaMessages().filter((m) => m.topic === KAFKA_APP_METRICS_2)
const parsed = messages.map((m) => parseMetricValue(m.value))
- expect(parsed.find((m) => m?.metric_name === 'retention_byte_days')?.count).toBe(500 * retentionDays)
+ expect(parsed.find((m) => m?.metric_name === 'retention_byte_days')?.count).toBe(byteDays)
const tierMetrics = parsed.filter((m) => m?.metric_name?.startsWith('bytes_ingested_retention_'))
if (tierMetric) {
@@ -1575,13 +1575,13 @@ describe('LogsIngestionConsumer', () => {
const messages = getProducedKafkaMessages().filter((m) => m.topic === KAFKA_APP_METRICS_2)
- // 4 non-zero base metrics (no dropped) + per-tier retention + retention_byte_days.
- expect(messages).toHaveLength(6)
+ // 4 non-zero base metrics (no dropped) + per-tier retention; no retention_byte_days for the default tier.
+ 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')
- expect(metricNames).toContain('retention_byte_days')
+ expect(metricNames).not.toContain('retention_byte_days')
})
it('should handle empty usageStats', async () => {
diff --git a/nodejs/src/logs/logs-ingestion-consumer.ts b/nodejs/src/logs/logs-ingestion-consumer.ts
index f571213a4f62..2dc98670a271 100644
--- a/nodejs/src/logs/logs-ingestion-consumer.ts
+++ b/nodejs/src/logs/logs-ingestion-consumer.ts
@@ -1167,11 +1167,14 @@ export class LogsIngestionConsumer {
if (retentionMetric) {
this.queueUsageMetric(teamId, retentionMetric, stats.bytesAllowed)
}
- // Byte-days: ingested bytes weighted by retention days. Summed over a period it is total
- // storage-duration and scales to any retention day count (average retention =
- // retention_byte_days / bytes_ingested). Uses credit-adjusted `bytesAllowed` to reconcile
- // with `bytes_ingested`. Runs beside the per-tier metric above until billing leaves fixed tiers.
- this.queueUsageMetric(teamId, 'retention_byte_days', stats.bytesAllowed * stats.retentionDays)
+ // Byte-days: ingested bytes weighted by the full retention day count, only for teams that
+ // chose a retention longer than the default. The default tier is covered by `bytes_ingested`,
+ // so it must not be billed a second time here. Uses credit-adjusted `bytesAllowed` to
+ // reconcile with `bytes_ingested`. Runs beside the per-tier metric above until billing
+ // leaves fixed tiers.
+ if (stats.retentionDays !== DEFAULT_LOGS_RETENTION_DAYS) {
+ this.queueUsageMetric(teamId, 'retention_byte_days', stats.bytesAllowed * stats.retentionDays)
+ }
const source = this.appSource === 'traces' ? 'apm_traces' : 'logs'
// These records are per-flush aggregates, not one per billed thing, so there is no
// stable identity to reproduce. A fresh ID per flush is what keeps two pods flushing
diff --git a/posthog/tasks/usage_report.py b/posthog/tasks/usage_report.py
index b8df6df24a74..6111d447998d 100644
--- a/posthog/tasks/usage_report.py
+++ b/posthog/tasks/usage_report.py
@@ -369,8 +369,8 @@ class UsageReportCounters:
logs_retention_30d_mb_in_period: int
logs_retention_90d_mb_in_period: int
# Byte-days of retention floored to whole MB-days (retention_byte_days // 1_000_000): ingested bytes
- # weighted by retention days, so it scales to any retention day count. Report-only, like
- # logs_mb_in_period. Average retention days = logs_retention_mb_days_in_period / logs_mb_in_period.
+ # weighted by the full retention day count, so it scales to any retention day count. Zero for teams
+ # on the default retention; they are covered by logs_mb_in_period alone. Report-only.
logs_retention_mb_days_in_period: int
# Per-SDK split of logs_records_in_period, which on its own has no SDK dimension. Keyed off the
# telemetry.sdk.name resource attribute each SDK sets on every record. See SDK_TELEMETRY_NAMES.
@@ -2601,10 +2601,11 @@ def get_teams_with_logs_retention_byte_days_in_period(
Returns byte-days of log retention grouped by team: ingested bytes weighted by retention days.
The consumer emits one `retention_byte_days` metric into `app_metrics2`
- (`retention_byte_days = bytes_ingested * retention_days`, summed per flush). Summed over the period
- it is total storage-duration and scales to any retention day count. Average retention days =
- `retention_byte_days` / `bytes_ingested`. Each `(team_id, count)` tuple is ready for
- `convert_team_usage_rows_to_dict`.
+ (`retention_byte_days = bytes_ingested * retention_days`, summed per flush) only for teams on a
+ non-default retention; default-retention teams emit nothing, so their storage is billed through
+ `bytes_ingested` alone. Summed over the period it is the storage-duration of the logs kept beyond
+ the default, not of all logs, and it scales to any retention day count. Each `(team_id, count)`
+ tuple is ready for `convert_team_usage_rows_to_dict`.
"""
with tags_context(product=Product.LOGS, feature=Feature.USAGE_REPORT):
return sync_execute(