Skip to content

Commit 7b77dfd

Browse files
committed
refactor(agent): register the rtk gauge in the typed analytics event map
Every analytics event in the app pairs its ANALYTICS_EVENTS constant with a *Properties interface and an EventPropertyMap entry (that map backs the renderer's typed track helper, and main-process events like CLOUD_STREAM_DISCONNECTED register too). RTK savings gauge was the one event missing both — add RtkSavingsGaugeProperties, the map entry, and a satisfies check at the reporter's call site so the emitted bag cannot drift from the declared shape. Generated-By: PostHog Code Task-Id: b86391cc-4634-4a2b-ae34-fe1f9c0626a0
1 parent e6bd533 commit 7b77dfd

2 files changed

Lines changed: 24 additions & 5 deletions

File tree

packages/core/src/usage/rtk-savings-reporter.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import {
77
APP_LIFECYCLE_SERVICE,
88
type IAppLifecycle,
99
} from "@posthog/platform/app-lifecycle";
10-
import { ANALYTICS_EVENTS } from "@posthog/shared/analytics-events";
10+
import {
11+
ANALYTICS_EVENTS,
12+
type RtkSavingsGaugeProperties,
13+
} from "@posthog/shared/analytics-events";
1114
import { inject, injectable, postConstruct, preDestroy } from "inversify";
1215
import { RTK_SAVINGS_HOST, type RtkSavingsHost } from "./identifiers";
1316

@@ -77,7 +80,7 @@ export class RtkSavingsReporter {
7780
cumulative_input_tokens: gauge.inputTokens,
7881
cumulative_output_tokens: gauge.outputTokens,
7982
cumulative_tokens_saved: gauge.tokensSaved,
80-
});
83+
} satisfies RtkSavingsGaugeProperties);
8184
this.logger.debug("Reported rtk savings gauge", { reading });
8285
} catch (error) {
8386
this.logger.debug("Failed to report rtk savings gauge", { error });

packages/shared/src/analytics-events.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,21 @@ export interface CloudStreamDisconnectedProperties {
275275
was_bootstrapping: boolean;
276276
}
277277

278+
/**
279+
* A gauge snapshot of rtk's cumulative machine-global savings counter,
280+
* mirroring the cloud agent's `_posthog/rtk_savings` event shape. Consumers
281+
* group by `counter_id` and difference readings (with an implicit 0 before a
282+
* counter's first reading, and a value drop treated as a reset) — never sum
283+
* event values, which would multiply-count re-reads of the shared counter.
284+
*/
285+
export interface RtkSavingsGaugeProperties {
286+
counter_id: string;
287+
cumulative_commands: number;
288+
cumulative_input_tokens: number;
289+
cumulative_output_tokens: number;
290+
cumulative_tokens_saved: number;
291+
}
292+
278293
// Permission events
279294
export interface PermissionRespondedProperties {
280295
task_id: string;
@@ -1136,9 +1151,7 @@ export const ANALYTICS_EVENTS = {
11361151
SUBSCRIPTION_STARTED: "Subscription started",
11371152
SUBSCRIPTION_CANCELLED: "Subscription cancelled",
11381153

1139-
// RTK output-compression telemetry. A gauge snapshot of rtk's cumulative
1140-
// machine-global savings counter: consumers group by counter_id and
1141-
// difference readings (never sum) — see RtkSavingsReporter in core.
1154+
// RTK output-compression telemetry — see RtkSavingsGaugeProperties.
11421155
RTK_SAVINGS_GAUGE: "RTK savings gauge",
11431156

11441157
// Project Bluebird (Channels) events
@@ -1292,6 +1305,9 @@ export type EventPropertyMap = {
12921305
[ANALYTICS_EVENTS.SUBSCRIPTION_STARTED]: SubscriptionStartedProperties;
12931306
[ANALYTICS_EVENTS.SUBSCRIPTION_CANCELLED]: SubscriptionCancelledProperties;
12941307

1308+
// RTK output-compression telemetry
1309+
[ANALYTICS_EVENTS.RTK_SAVINGS_GAUGE]: RtkSavingsGaugeProperties;
1310+
12951311
// Project Bluebird (Channels) events
12961312
[ANALYTICS_EVENTS.CHANNELS_SPACE_VIEWED]: ChannelsSpaceViewedProperties;
12971313
[ANALYTICS_EVENTS.CHANNEL_ACTION]: ChannelActionProperties;

0 commit comments

Comments
 (0)