Files
zeekay 10e6fec4c0 debrand: the brand token is INSIGHTS_, and a wrong one stops the container
The rule table mapped HOG_ -> FN_ ahead of any POSTHOG rule, so POSTHOG_FOO
split into POST + HOG_ and came out POSTFN_FOO. Nothing sets that. The live
pods set INSIGHTS_SKIP_MIGRATION_CHECKS, which bin/migrate-check reads on the
container entrypoint, so the misspelling does not degrade a feature -- it runs
migrate --check against a database whose history this tree does not share and
the container never starts.

Both replacements are measured against the shipped tree rather than chosen:
every HOG_ name with a counterpart there is spelled INSIGHTS_ (HOG_QL ->
INSIGHTS_QL, HOG_FUNCTION_TEMPLATES -> INSIGHTS_FUNCTION_TEMPLATES, 12 of 12
determinate cases), and FN_ appears in it nowhere. Upstream carries 95 HOG_
tokens and zero FN_ ones, so repairing FN_ back out is exactly the inverse of
the bad rule and cannot collide with an upstream name.

The rule is fixed for the next convergence, and the tree it already produced is
repaired in the same commit -- the codemod is idempotent against its own output,
so it cannot undo this itself.
2026-08-01 17:42:28 -07:00

159 lines
6.0 KiB
TypeScript

import { MakeLogicType, actions, afterMount, connect, kea, listeners, path, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'
import insights from 'insights-js'
import { retryWithBackoff } from 'lib/utils/async'
import { teamLogic } from 'scenes/teamLogic'
import { ProductIntentContext, ProductKey } from '~/queries/schema/schema-general'
import type { ProductIntentProperties } from '../../../frontend/src/lib/utils/product-intents'
import { metricsHasMetricsRetrieve } from './generated/api'
import { canViewMetrics } from './metricsAccess'
const teamId = window.INSIGHTS_APP_CONTEXT?.current_team?.id
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface metricsIngestionLogicValues {
currentTeamId: number | null // teamLogic
cachedTeamHasMetrics: boolean | null
hasMetrics: boolean | undefined
teamHasMetrics: boolean | undefined
teamHasMetricsCheckFailed: boolean
teamHasMetricsLoading: boolean
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface metricsIngestionLogicActions {
addProductIntent: (properties: ProductIntentProperties) => ProductIntentProperties // teamLogic
loadTeamHasMetrics: () => any
loadTeamHasMetricsFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadTeamHasMetricsSuccess: (
teamHasMetrics: boolean | undefined,
payload?: any
) => {
teamHasMetrics: boolean | undefined
payload?: any
}
reportScrapeSnippetCopied: (variant: 'docker' | 'kubernetes') => {
variant: 'docker' | 'kubernetes'
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface metricsIngestionLogicMeta {
__keaTypeGenInternalSelectorTypes: {
hasMetrics: (teamHasMetrics: boolean | undefined, cachedTeamHasMetrics: boolean | null) => boolean | undefined
}
}
export type metricsIngestionLogicType = MakeLogicType<
metricsIngestionLogicValues,
metricsIngestionLogicActions,
Record<string, any>,
metricsIngestionLogicMeta
>
export const metricsIngestionLogic = kea<metricsIngestionLogicType>([
path(['products', 'metrics', 'frontend', 'metricsIngestionLogic']),
connect(() => ({
values: [teamLogic, ['currentTeamId']],
actions: [teamLogic, ['addProductIntent']],
})),
actions({
reportScrapeSnippetCopied: (variant: 'docker' | 'kubernetes') => ({ variant }),
}),
loaders(({ values }) => ({
teamHasMetrics: {
__default: undefined as boolean | undefined,
loadTeamHasMetrics: async (): Promise<boolean | undefined> => {
if (!canViewMetrics()) {
// No access means "unknown", not "no metrics" - false would show the setup prompt
return values.teamHasMetrics
}
const response = await retryWithBackoff(() => metricsHasMetricsRetrieve(String(values.currentTeamId)), {
maxAttempts: 3,
})
return Boolean(response.hasMetrics)
},
},
})),
reducers({
teamHasMetricsCheckFailed: [
false,
{
loadTeamHasMetrics: () => false,
loadTeamHasMetricsSuccess: () => false,
loadTeamHasMetricsFailure: () => true,
},
],
cachedTeamHasMetrics: [
null as boolean | null,
{ persist: true, prefix: `${teamId}__` },
{
// Only cache true - metrics don't disappear once ingested
loadTeamHasMetricsSuccess: (_, { teamHasMetrics }) => teamHasMetrics || null,
},
],
}),
listeners(({ actions, cache }) => ({
reportScrapeSnippetCopied: ({ variant }) => {
insights.capture('metrics scrape agent snippet copied', { variant })
// One intent per variant per session: repeated copies of the same
// snippet are retries, not new adoption signals.
cache.scrapeIntentFired ??= {}
if (cache.scrapeIntentFired[variant]) {
return
}
cache.scrapeIntentFired[variant] = true
actions.addProductIntent({
product_type: ProductKey.METRICS,
intent_context: ProductIntentContext.METRICS_SCRAPE_AGENT_SNIPPET_COPIED,
metadata: { variant },
})
},
loadTeamHasMetricsSuccess: ({ teamHasMetrics }) => {
if (!teamHasMetrics) {
// Only an observed "no metrics yet" arms the intent - an access-denied
// skip resolves with undefined and must not count.
if (teamHasMetrics === false) {
cache.sawNoMetrics = true
}
return
}
// Only an observed no-metrics -> has-metrics transition is an intent: the user
// completed external OTel setup during this session. A team whose first check
// already returns true (or was cached true) just has pre-existing metrics.
if (cache.sawNoMetrics && !cache.firstIngestIntentFired) {
cache.firstIngestIntentFired = true
actions.addProductIntent({
product_type: ProductKey.METRICS,
intent_context: ProductIntentContext.METRICS_FIRST_INGESTED,
})
}
},
})),
selectors({
hasMetrics: [
(s) => [s.teamHasMetrics, s.cachedTeamHasMetrics],
(teamHasMetrics: boolean | undefined, cachedTeamHasMetrics: boolean | null): boolean | undefined =>
teamHasMetrics ?? cachedTeamHasMetrics ?? undefined,
],
}),
afterMount(({ actions, values }) => {
if (canViewMetrics() && values.cachedTeamHasMetrics !== true) {
actions.loadTeamHasMetrics()
}
}),
])