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(traces): switch traces billing metrics to traces_mb_ingested (#60655)

Jon McCallum815b8b7bf24d
diff --git a/nodejs/src/common/services/quota-limiting.service.ts b/nodejs/src/common/services/quota-limiting.service.ts
index 9313d2ab4abc..c5904ed38e60 100644
--- a/nodejs/src/common/services/quota-limiting.service.ts
+++ b/nodejs/src/common/services/quota-limiting.service.ts
@@ -12,6 +12,7 @@ export type QuotaResource =
| 'workflow_destinations_dispatched'
| 'logs_mb_ingested'
| 'metrics_mb_ingested'
+ | 'traces_mb_ingested'
export const QUOTA_LIMITER_CACHE_KEY = '@posthog/quota-limits/'
diff --git a/nodejs/src/logs-ingestion/logs-ingestion-consumer.test.ts b/nodejs/src/logs-ingestion/logs-ingestion-consumer.test.ts
index 9ab5b5873ca5..9ed2c3686aae 100644
--- a/nodejs/src/logs-ingestion/logs-ingestion-consumer.test.ts
+++ b/nodejs/src/logs-ingestion/logs-ingestion-consumer.test.ts
@@ -15,6 +15,7 @@ import { APP_METRICS_OUTPUT, AppMetricsOutput } from '../ingestion/common/output
import { IngestionOutputs } from '../ingestion/outputs/ingestion-outputs'
import { SingleIngestionOutput } from '../ingestion/outputs/single-ingestion-output'
import { parseJSON } from '../utils/json-parse'
+import { getDefaultTracesIngestionConsumerConfig } from './config'
import { LogRecord, encodeLogRecords } from './log-record-avro'
import {
DEFAULT_LOGS_RETENTION_DAYS,
@@ -33,6 +34,7 @@ import { LOGS_DLQ_OUTPUT, LOGS_OUTPUT, LogsDlqOutput, LogsOutput } from './outpu
import { compileRuleSet } from './sampling/compile-rules'
import type { SamplingRulesCache } from './sampling/sampling-rules-cache'
import { BASE_REDIS_KEY } from './services/logs-rate-limiter.service'
+import { TracesIngestionConsumer } from './traces-ingestion-consumer'
const DEFAULT_TEST_TIMEOUT = 5000
jest.setTimeout(DEFAULT_TEST_TIMEOUT)
@@ -1606,4 +1608,71 @@ describe('LogsIngestionConsumer', () => {
expect(longestDelay).toBeLessThan(120)
})
})
+
+ describe('TracesIngestionConsumer billing identity', () => {
+ const createTracesIngestionConsumer = (): TracesIngestionConsumer => {
+ const tracesConsumer = new TracesIngestionConsumer(
+ // Blank the traces Redis host so it reuses the hub's pool (like the logs-consumer tests),
+ // avoiding a second eager pool that would keep handles open after the suite.
+ { ...hub, ...getDefaultTracesIngestionConsumerConfig(), TRACES_REDIS_HOST: '' },
+ {
+ ...hub,
+ outputs: new IngestionOutputs<AppMetricsOutput | LogsOutput | LogsDlqOutput>({
+ [APP_METRICS_OUTPUT]: new SingleIngestionOutput(
+ APP_METRICS_OUTPUT,
+ KAFKA_APP_METRICS_2,
+ mockProducer,
+ 'test'
+ ),
+ [LOGS_OUTPUT]: new SingleIngestionOutput(
+ LOGS_OUTPUT,
+ KAFKA_LOGS_CLICKHOUSE,
+ mockProducer,
+ 'test'
+ ),
+ [LOGS_DLQ_OUTPUT]: new SingleIngestionOutput(
+ LOGS_DLQ_OUTPUT,
+ KAFKA_LOGS_INGESTION_DLQ,
+ mockProducer,
+ 'test'
+ ),
+ }),
+ }
+ )
+ tracesConsumer['kafkaConsumer'] = {
+ connect: jest.fn(),
+ disconnect: jest.fn(),
+ isHealthy: jest.fn().mockReturnValue({ status: 'healthy' }),
+ } as any
+ return tracesConsumer
+ }
+
+ it('meters usage as traces, not logs', async () => {
+ const tracesConsumer = createTracesIngestionConsumer()
+ tracesConsumer['queueUsageMetric'](team.id, 'bytes_ingested', 500)
+ await tracesConsumer['appMetricsAggregator'].flush()
+
+ const messages = getProducedKafkaMessages().filter((m) => m.topic === KAFKA_APP_METRICS_2)
+ expect(messages).toHaveLength(1)
+ const value = Buffer.isBuffer(messages[0].value)
+ ? parseJSON(messages[0].value.toString())
+ : parseJSON(messages[0].value as string)
+ expect(value?.app_source).toBe('traces')
+ })
+
+ it('quota-limits against traces_mb_ingested, not logs_mb_ingested', async () => {
+ jest.mocked(hub.quotaLimiting.isTeamTokenQuotaLimited).mockResolvedValue(false)
+ const tracesConsumer = createTracesIngestionConsumer()
+
+ const messages = await createKafkaMessages([createLogMessage()], {
+ token: team.api_token,
+ bytes_uncompressed: '1024',
+ record_count: '5',
+ })
+ const parsed = await tracesConsumer['_parseKafkaBatch'](messages)
+ await tracesConsumer['filterQuotaLimitedMessages'](parsed)
+
+ expect(hub.quotaLimiting.isTeamTokenQuotaLimited).toHaveBeenCalledWith(team.api_token, 'traces_mb_ingested')
+ })
+ })
})
diff --git a/nodejs/src/logs-ingestion/logs-ingestion-consumer.ts b/nodejs/src/logs-ingestion/logs-ingestion-consumer.ts
index 8306280674eb..80f4cff23387 100644
--- a/nodejs/src/logs-ingestion/logs-ingestion-consumer.ts
+++ b/nodejs/src/logs-ingestion/logs-ingestion-consumer.ts
@@ -5,7 +5,7 @@ import { Counter } from 'prom-client'
import { RedisV2, createRedisV2PoolFromConfig } from '~/common/redis/redis-v2'
import { AppMetricsAggregator } from '~/common/services/app-metrics-aggregator'
-import { QuotaLimiting } from '~/common/services/quota-limiting.service'
+import { QuotaLimiting, QuotaResource } from '~/common/services/quota-limiting.service'
import { instrumentFn, instrumented } from '~/common/tracing/tracing-utils'
import { AppMetricsOutput } from '~/ingestion/common/outputs'
import { IngestionOutputs } from '~/ingestion/outputs/ingestion-outputs'
@@ -135,6 +135,9 @@ export const logsBytesDroppedByRuleCounter = new Counter({
export class LogsIngestionConsumer {
protected name = 'LogsIngestionConsumer'
+ // Billing identity for quota enforcement and usage metering; overridden by subclasses (e.g. traces).
+ protected quotaResource: QuotaResource = 'logs_mb_ingested'
+ protected appSource = 'logs'
protected kafkaConsumer: KafkaConsumerInterface
private appMetricsAggregator: AppMetricsAggregator
private redis: RedisV2
@@ -398,7 +401,7 @@ export class LogsIngestionConsumer {
(
await Promise.all(
uniqueTokens.map(async (token) =>
- (await this.deps.quotaLimiting.isTeamTokenQuotaLimited(token, 'logs_mb_ingested'))
+ (await this.deps.quotaLimiting.isTeamTokenQuotaLimited(token, this.quotaResource))
? token
: null
)
@@ -584,7 +587,7 @@ export class LogsIngestionConsumer {
}
this.appMetricsAggregator.queue({
team_id: teamId,
- app_source: 'logs',
+ app_source: this.appSource,
app_source_id: '',
instance_id: '',
metric_kind: 'usage',
@@ -604,7 +607,7 @@ export class LogsIngestionConsumer {
}
this.appMetricsAggregator.queue({
team_id: teamId,
- app_source: 'logs',
+ app_source: this.appSource,
app_source_id: '',
instance_id: ruleId,
metric_kind: 'usage',
@@ -625,7 +628,7 @@ export class LogsIngestionConsumer {
}
this.appMetricsAggregator.queue({
team_id: teamId,
- app_source: 'logs',
+ app_source: this.appSource,
app_source_id: '',
instance_id: ruleId,
metric_kind: 'usage',
diff --git a/nodejs/src/logs-ingestion/traces-ingestion-consumer.ts b/nodejs/src/logs-ingestion/traces-ingestion-consumer.ts
index e5323beb009f..86c8cd8fd3c4 100644
--- a/nodejs/src/logs-ingestion/traces-ingestion-consumer.ts
+++ b/nodejs/src/logs-ingestion/traces-ingestion-consumer.ts
@@ -3,6 +3,9 @@ import { LogsIngestionConsumer, LogsIngestionConsumerDeps } from './logs-ingesti
export class TracesIngestionConsumer extends LogsIngestionConsumer {
protected override name = 'TracesIngestionConsumer'
+ // Meter and quota-limit traces against their own billing identity, not logs'.
+ protected override quotaResource = 'traces_mb_ingested' as const
+ protected override appSource = 'traces'
constructor(config: LogsIngestionConsumerConfig & TracesIngestionConsumerConfig, deps: LogsIngestionConsumerDeps) {
// Topics are wired into `deps.outputs` by the server, so the only consumer-level