: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 type { TopHogRegistry } from '~/ingestion/framework/extensions/tophog'
import type { ImageFetchBlockReason } from './block-reason'
import type { FetchCandidate } from './collected-urls-record'
import { deduplicateFetchCandidates } from './fetch-candidate-queue'
import type { FetchAttempt } from './fetch-runner'
import type { SchedulerWaitScope } from './metrics'
const TOP_DOMAIN_FACTORS = 20
const MAX_TRACKED_DOMAIN_FACTORS = 2_000
type Recorder = { record(key: Record<string, string>, value: number): void }
export class ImageFetchTopHogMetrics {
private readonly attempts: Recorder
private readonly blockEvents: Recorder
private readonly blockedMs: Recorder
private readonly filteredUrls: Recorder
constructor(topHog: TopHogRegistry) {
this.attempts = topHog.registerSum('ai_research_v4_image_fetch_attempts_by_registrable_domain', {
topN: TOP_DOMAIN_FACTORS,
maxKeys: MAX_TRACKED_DOMAIN_FACTORS,
})
this.blockEvents = topHog.registerSum('ai_research_v4_image_fetch_block_events_by_registrable_domain', {
topN: TOP_DOMAIN_FACTORS,
maxKeys: MAX_TRACKED_DOMAIN_FACTORS,
})
this.blockedMs = topHog.registerSum('ai_research_v4_image_fetch_blocked_ms_by_registrable_domain', {
topN: TOP_DOMAIN_FACTORS,
maxKeys: MAX_TRACKED_DOMAIN_FACTORS,
})
this.filteredUrls = topHog.registerSum('ai_research_v4_image_fetch_filtered_urls_by_registrable_domain', {
topN: TOP_DOMAIN_FACTORS,
maxKeys: MAX_TRACKED_DOMAIN_FACTORS,
})
}
/**
* The first pass of a URL shows what the recorder sends for a domain. The later passes show what the fetcher sends
* back to itself, so the two together separate new demand from republish churn. Only the `received` stage reads
* demand, because the later stages see a URL after duplicates merge, with the highest pass and one route.
*/
public recordFilteredUrl(
candidate: FetchCandidate,
stage: 'received' | 'store_deduped' | 'not_ready' | 'fetchable'
): void {
this.recordForSourcePartitions(
this.filteredUrls,
candidate.sourcePartitions,
{
registrable_domain: candidate.registrableDomain,
stage,
pass: candidate.republishCount === 0 ? 'first' : 'republished',
},
1
)
}
public recordAttempt(attempt: FetchAttempt): void {
this.recordForSourcePartitions(
this.attempts,
attempt.candidate.sourcePartitions,
{
registrable_domain: attempt.candidate.registrableDomain,
disposition: attempt.finished ? 'completed' : 'republished',
outcome: attempt.outcome,
},
1
)
if (attempt.block) {
this.recordBlock(
attempt.candidate.registrableDomain,
attempt.candidate.sourcePartitions,
attempt.block.reason,
1,
attempt.block.waitMs
)
}
}
public recordConcurrencyLimitedUrls(
candidates: FetchCandidate[],
availableConnections: (registrableDomain: string) => number
): void {
const candidatesByRegistrableDomain = new Map<
string,
{ candidateCount: number; sourcePartitions: Set<number> }
>()
for (const candidate of deduplicateFetchCandidates(candidates).candidates) {
const domainCandidates = candidatesByRegistrableDomain.get(candidate.registrableDomain) ?? {
candidateCount: 0,
sourcePartitions: new Set<number>(),
}
domainCandidates.candidateCount += 1
for (const sourcePartition of candidate.sourcePartitions ?? []) {
domainCandidates.sourcePartitions.add(sourcePartition)
}
candidatesByRegistrableDomain.set(candidate.registrableDomain, domainCandidates)
}
for (const [registrableDomain, domainCandidates] of candidatesByRegistrableDomain) {
const limitedUrls = Math.max(0, domainCandidates.candidateCount - availableConnections(registrableDomain))
if (limitedUrls > 0) {
this.recordBlock(
registrableDomain,
[...domainCandidates.sourcePartitions],
'domain_concurrency',
limitedUrls,
0
)
}
}
}
public recordSchedulerWait(
registrableDomain: string,
sourcePartitions: readonly number[] | undefined,
scope: SchedulerWaitScope,
waitMs: number
): void {
if (waitMs <= 0) {
return
}
this.recordBlock(registrableDomain, sourcePartitions, scope, 1, waitMs)
}
private recordBlock(
registrableDomain: string,
sourcePartitions: readonly number[] | undefined,
reason: ImageFetchBlockReason,
eventCount: number,
waitMs: number
): void {
const key = { registrable_domain: registrableDomain, reason }
this.recordForSourcePartitions(this.blockEvents, sourcePartitions, key, eventCount)
if (waitMs > 0) {
this.recordForSourcePartitions(this.blockedMs, sourcePartitions, key, waitMs)
}
}
private recordForSourcePartitions(
recorder: Recorder,
sourcePartitions: readonly number[] | undefined,
key: Record<string, string>,
value: number
): void {
const partitions = sourcePartitions?.length ? new Set(sourcePartitions) : new Set(['unattributed'])
for (const partition of partitions) {
recorder.record({ ...key, partition: String(partition) }, value)
}
}
}