: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import { ChunkProcessingStep } from '~/ingestion/framework/builders'
import { drop, ok } from '~/ingestion/framework/results'
import { metricMessageDroppedCounter } from './metrics'
import { MetricsUsageAccumulator, kafkaMessageUsageIdentity } from './metrics-usage'
import { MetricsRateLimiterService } from './services/metrics-rate-limiter.service'
import { MetricsIngestionMessage } from './types'
/** Allowed usage is recorded here, before the produce, so a message that later goes to the DLQ is still billed. */
export function createRateLimitMetricsStep<T extends MetricsIngestionMessage & { usage: MetricsUsageAccumulator }>(
rateLimiter: Pick<MetricsRateLimiterService, 'filterMessages'>
): ChunkProcessingStep<T, T> {
return async function rateLimitMetricsStep(values) {
const { dropped } = await rateLimiter.filterMessages(values)
const droppedSet = new Set<T>(dropped)
return values.map((value) => {
if (droppedSet.has(value)) {
value.usage.recordDropped(value.teamId, value.bytesUncompressed, value.recordCount)
metricMessageDroppedCounter.inc({ reason: 'rate_limited', team_id: value.teamId.toString() })
return drop<T>('rate_limited')
}
value.usage.recordAllowed(
value.teamId,
kafkaMessageUsageIdentity(value.message),
value.bytesUncompressed,
value.recordCount,
value.bytesUncompressedRecords
)
return ok(value)
})
}
}