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.

4.5 KB
Raw Blame
import type { LogsSettings } from '~/types'

import { LogsIngestionConsumerConfig, TracesIngestionConsumerConfig } from './config'
import { LogsIngestionConsumer, LogsIngestionConsumerDeps } from './logs-ingestion-consumer'
import type { MetricRuleSource } from './metrics-rules/compile-metric-rules'
import type { RetentionRuleSource } from './retention/compile-retention-rules'
import { DEFAULT_TRACES_RETENTION_DAYS, TracingConfigCache } from './retention/tracing-config-cache'

export interface TracesIngestionConsumerDeps extends LogsIngestionConsumerDeps {
    /** Per-team default span retention. Without it, traces fall back to the built-in default. */
    tracingConfigCache?: TracingConfigCache
}

export class TracesIngestionConsumer extends LogsIngestionConsumer {
    protected override name = 'TracesIngestionConsumer'
    // Traces bill against the same bucket as logs: one product, one free tier, one price. The
    // inherited logs_mb_ingested quota resource is that bucket, so it is deliberately not overridden.
    protected override appSource = 'traces'
    // Traces records are spans, so this consumer tallies the `spans` metric rules.
    protected override metricRuleSource: MetricRuleSource = 'spans'
    // Span retention rules live in their own table, which this source selects.
    protected override retentionRuleSource: RetentionRuleSource = 'spans'
    private tracingConfigCache?: TracingConfigCache

    constructor(
        config: LogsIngestionConsumerConfig & TracesIngestionConsumerConfig,
        deps: TracesIngestionConsumerDeps
    ) {
        // Topics are wired into `deps.outputs` by the server, so the only consumer-level
        // overrides left are the consume topic / group / Redis / rate-limiter settings.
        super(
            config,
            deps,
            {
                LOGS_INGESTION_CONSUMER_GROUP_ID: config.TRACES_INGESTION_CONSUMER_GROUP_ID,
                LOGS_INGESTION_CONSUMER_CONSUME_TOPIC: config.TRACES_INGESTION_CONSUMER_CONSUME_TOPIC,
                LOGS_REDIS_HOST: config.TRACES_REDIS_HOST,
                LOGS_REDIS_PORT: config.TRACES_REDIS_PORT,
                LOGS_REDIS_PASSWORD: config.TRACES_REDIS_PASSWORD,
                LOGS_REDIS_TLS: config.TRACES_REDIS_TLS,
                LOGS_LIMITER_ENABLED_TEAMS: config.TRACES_LIMITER_ENABLED_TEAMS,
                LOGS_LIMITER_DISABLED_FOR_TEAMS: config.TRACES_LIMITER_DISABLED_FOR_TEAMS,
                LOGS_LIMITER_BUCKET_SIZE_KB: config.TRACES_LIMITER_BUCKET_SIZE_KB,
                LOGS_LIMITER_REFILL_RATE_KB_PER_SECOND: config.TRACES_LIMITER_REFILL_RATE_KB_PER_SECOND,
                LOGS_LIMITER_TTL_SECONDS: config.TRACES_LIMITER_TTL_SECONDS,
                LOGS_LIMITER_TEAM_BUCKET_SIZE_KB: config.TRACES_LIMITER_TEAM_BUCKET_SIZE_KB,
                LOGS_LIMITER_TEAM_REFILL_RATE_KB_PER_SECOND: config.TRACES_LIMITER_TEAM_REFILL_RATE_KB_PER_SECOND,
                // Span metric rules gate on traces-specific env config so they roll out
                // independently of log metric rules.
                LOGS_METRICS_RULES_ENABLED_TEAMS: config.TRACES_METRICS_RULES_ENABLED_TEAMS,
                LOGS_METRICS_RULES_KILLSWITCH: config.TRACES_METRICS_RULES_KILLSWITCH,
                LOGS_METRICS_RULES_EXPORT_URL: config.TRACES_METRICS_RULES_EXPORT_URL,
                LOGS_DEFAULT_METRIC_RULES_ENABLED_TEAMS: config.TRACES_DEFAULT_METRIC_RULES_ENABLED_TEAMS,
                // Span retention gates on traces-specific env config so it rolls out
                // independently of log retention.
                LOGS_RETENTION_ENABLED_TEAMS: config.TRACES_RETENTION_ENABLED_TEAMS,
                LOGS_RETENTION_KILLSWITCH: config.TRACES_RETENTION_KILLSWITCH,
                // Internal batches are logs only, so a signed traces batch is still billed.
                LOGS_INTERNAL_INGESTION_SIGNING_KEYS: '',
                LOGS_INTERNAL_INGESTION_ENABLED_TEAMS: '',
            },
            // Own Redis key namespace so traces token buckets don't share per-team state with logs.
            'traces-rate-limiter'
        )
        this.tracingConfigCache = deps.tracingConfigCache
    }

    // Spans keep their own default period on the tracing team extension, so a change to the logs
    // retention setting must not move it.
    protected override async defaultRetentionDays(teamId: number, _logsSettings: LogsSettings): Promise<number> {
        if (!this.tracingConfigCache) {
            return DEFAULT_TRACES_RETENTION_DAYS
        }
        return await this.tracingConfigCache.getRetentionDays(teamId)
    }
}