: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 { MakeLogicType, afterMount, connect, kea, path, selectors } from 'kea'
import { loaders } from 'kea-loaders'
import { teamLogic } from 'scenes/teamLogic'
import { urls } from 'scenes/urls'
import { logsMetricRulesList } from 'products/logs/frontend/generated/api'
import type { LogsMetricRuleApi } from 'products/logs/frontend/generated/api.schemas'
/**
* Deep link into the Logs product scoped to the rule's own filters. The stored filter_group is
* already the two-level group shape the logs scene's urlToAction decodes, so it goes out verbatim.
*/
export function logsUrlForMetricRule(rule: LogsMetricRuleApi): string {
if (!rule.filter_group) {
return urls.logs()
}
return `${urls.logs()}?filterGroup=${encodeURIComponent(JSON.stringify(rule.filter_group))}`
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface metricsLogsSourceLogicValues {
currentTeamId: number | null // teamLogic
logsMetricRules: LogsMetricRuleApi[]
logsMetricRulesLoading: boolean
ruleByMetricName: Record<string, LogsMetricRuleApi>
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface metricsLogsSourceLogicActions {
loadLogsMetricRules: () => any
loadLogsMetricRulesFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadLogsMetricRulesSuccess: (
logsMetricRules: LogsMetricRuleApi[],
payload?: any
) => {
logsMetricRules: LogsMetricRuleApi[]
payload?: any
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface metricsLogsSourceLogicMeta {
__keaTypeGenInternalSelectorTypes: {
ruleByMetricName: (logsMetricRules: LogsMetricRuleApi[]) => Record<string, LogsMetricRuleApi>
}
}
export type metricsLogsSourceLogicType = MakeLogicType<
metricsLogsSourceLogicValues,
metricsLogsSourceLogicActions,
Record<string, any>,
metricsLogsSourceLogicMeta
>
/**
* Loads the team's logs metric rules so the viewer can mark metrics that are generated from logs
* and link back to their source. Rules are matched by metric_name, which is unique per team and
* immutable, so it is a stable join key.
*/
export const metricsLogsSourceLogic = kea<metricsLogsSourceLogicType>([
path(['products', 'metrics', 'frontend', 'components', 'metricsLogsSourceLogic']),
connect(() => ({
values: [teamLogic, ['currentTeamId']],
})),
loaders(({ values }) => ({
logsMetricRules: [
[] as LogsMetricRuleApi[],
{
loadLogsMetricRules: async () => {
// The endpoint is flag-gated server-side; a team without logs metric rules
// must see a viewer without the tag, not an error.
try {
const response = await logsMetricRulesList(String(values.currentTeamId))
return response.results
} catch {
return []
}
},
},
],
})),
selectors({
ruleByMetricName: [
(s) => [s.logsMetricRules],
(rules: LogsMetricRuleApi[]): Record<string, LogsMetricRuleApi> =>
Object.fromEntries(rules.map((rule) => [rule.metric_name, rule])),
],
}),
afterMount(({ actions }) => {
actions.loadLogsMetricRules()
}),
])