The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Count app runs and platform events in a 90-day metric table (#28288)
martmullccc712729470
diff --git a/packages/twenty-server/src/database/clickhouse/migrations/016-create-application-metric-event-table.sql b/packages/twenty-server/src/database/clickhouse/migrations/016-create-application-metric-event-table.sqlnew file mode 100644index 00000000000..979a0416e3a--- /dev/null+++ b/packages/twenty-server/src/database/clickhouse/migrations/016-create-application-metric-event-table.sql@@ -0,0 +1,20 @@+CREATE TABLE IF NOT EXISTS applicationMetricEvent+(+ `timestamp` DateTime64(3) NOT NULL,+ `applicationRegistrationId` String NOT NULL,+ `workspaceId` String NOT NULL,+ `applicationId` String DEFAULT '',+ `applicationVersion` String DEFAULT '',+ `source` LowCardinality(String) NOT NULL,+ `logicFunctionId` String DEFAULT '',+ `logicFunctionName` String DEFAULT '',+ `level` LowCardinality(String) NOT NULL,+ `errorCode` LowCardinality(String) DEFAULT '',+ `durationMs` UInt32 DEFAULT 0,+ `occurrenceCount` UInt32 DEFAULT 1+)+ ENGINE = MergeTree+PARTITION BY toYYYYMM(timestamp)+ORDER BY (applicationRegistrationId, timestamp, workspaceId)+TTL toDateTime(timestamp) + INTERVAL 90 DAY DELETE+SETTINGS ttl_only_drop_parts = 1;diff --git a/packages/twenty-server/src/engine/core-modules/application/application-install/__tests__/application-install.service.spec.ts b/packages/twenty-server/src/engine/core-modules/application/application-install/__tests__/application-install.service.spec.tsindex e059b05d4d3..adba918c431 100644--- a/packages/twenty-server/src/engine/core-modules/application/application-install/__tests__/application-install.service.spec.ts+++ b/packages/twenty-server/src/engine/core-modules/application/application-install/__tests__/application-install.service.spec.ts@@ -95,7 +95,9 @@ describe('ApplicationInstallService', () => {{} as MessageQueueService,{} as WorkspaceCacheService,{ incrementCounterBy: jest.fn() } as unknown as MetricsService,- { recordApplicationLogs: jest.fn() } as unknown as EventLogEmitterService,+ {+ recordApplicationPlatformEvents: jest.fn(),+ } as unknown as EventLogEmitterService,);const install = () =>diff --git a/packages/twenty-server/src/engine/core-modules/application/application-install/application-install.service.ts b/packages/twenty-server/src/engine/core-modules/application/application-install/application-install.service.tsindex 9f6025ebf94..c78d5421fe8 100644--- a/packages/twenty-server/src/engine/core-modules/application/application-install/application-install.service.ts+++ b/packages/twenty-server/src/engine/core-modules/application/application-install/application-install.service.ts@@ -238,7 +238,7 @@ export class ApplicationInstallService {{ targetVersion: params.version },);} catch (error) {- await this.eventLogEmitterService.recordApplicationLogs([+ await this.eventLogEmitterService.recordApplicationPlatformEvents([buildApplicationLifecycleLogEntry({source: isVersionUpgrade ? 'UPGRADE' : 'INSTALL',workspaceId: params.workspaceId,@@ -308,7 +308,7 @@ export class ApplicationInstallService {attributes,});- await this.eventLogEmitterService.recordApplicationLogs([+ await this.eventLogEmitterService.recordApplicationPlatformEvents([buildApplicationLifecycleLogEntry({...lifecycleLogContext,applicationId: application.id,@@ -317,7 +317,7 @@ export class ApplicationInstallService {return true;} catch (error) {- await this.eventLogEmitterService.recordApplicationLogs([+ await this.eventLogEmitterService.recordApplicationPlatformEvents([buildApplicationLifecycleLogEntry({...lifecycleLogContext,applicationId: existingApplication?.id ?? '',diff --git a/packages/twenty-server/src/engine/core-modules/application/application-install/services/application-uninstall-runner.service.ts b/packages/twenty-server/src/engine/core-modules/application/application-install/services/application-uninstall-runner.service.tsindex 39276d6dd08..d40eff794db 100644--- a/packages/twenty-server/src/engine/core-modules/application/application-install/services/application-uninstall-runner.service.ts+++ b/packages/twenty-server/src/engine/core-modules/application/application-install/services/application-uninstall-runner.service.ts@@ -108,7 +108,7 @@ export class ApplicationUninstallRunnerService {return;}- await this.eventLogEmitterService.recordApplicationLogs([+ await this.eventLogEmitterService.recordApplicationPlatformEvents([buildApplicationLifecycleLogEntry({source: 'UNINSTALL',workspaceId,diff --git a/packages/twenty-server/src/engine/core-modules/event-logs/emit/application-log-deduplication.service.ts b/packages/twenty-server/src/engine/core-modules/event-logs/emit/application-log-deduplication.service.tsindex b1e84481384..20a30162e1e 100644--- a/packages/twenty-server/src/engine/core-modules/event-logs/emit/application-log-deduplication.service.ts+++ b/packages/twenty-server/src/engine/core-modules/event-logs/emit/application-log-deduplication.service.ts@@ -49,7 +49,9 @@ export class ApplicationLogDeduplicationService {});if (isDefined(ownerToken)) {- await this.eventLogEmitterService.recordApplicationLogs([entry]);+ await this.eventLogEmitterService.recordApplicationPlatformEvents([+ entry,+ ]);}}}diff --git a/packages/twenty-server/src/engine/core-modules/event-logs/emit/event-log-emitter.service.ts b/packages/twenty-server/src/engine/core-modules/event-logs/emit/event-log-emitter.service.tsindex 7b2f5d74bed..a735cfd0509 100644--- a/packages/twenty-server/src/engine/core-modules/event-logs/emit/event-log-emitter.service.ts+++ b/packages/twenty-server/src/engine/core-modules/event-logs/emit/event-log-emitter.service.ts@@ -14,6 +14,9 @@ import {import { WorkspaceEventSinkService } from 'src/engine/core-modules/event-logs/ingest/workspace-event-sink.service';import { type ApplicationLogEntry } from 'src/engine/core-modules/event-logs/producers/application-log/application-log-entry.interface';import { buildApplicationLogEnvelopes } from 'src/engine/core-modules/event-logs/producers/application-log/build-application-log-envelopes';+import { type ApplicationMetricEvent } from 'src/engine/core-modules/event-logs/producers/application-metric-event/application-metric-event.type';+import { buildApplicationMetricEventEnvelopes } from 'src/engine/core-modules/event-logs/producers/application-metric-event/build-application-metric-event-envelopes';+import { toApplicationMetricEvent } from 'src/engine/core-modules/event-logs/producers/application-metric-event/to-application-metric-event';import {type EventContextFields,type WorkspaceEventEnvelope,@@ -40,10 +43,35 @@ export class EventLogEmitterService {}async recordApplicationLogs(entries: ApplicationLogEntry[]): Promise<void> {+ await this.dispatchOrLogError(() => buildApplicationLogEnvelopes(entries));+ }++ async recordApplicationMetricEvents(+ events: ApplicationMetricEvent[],+ ): Promise<void> {+ await this.dispatchOrLogError(() =>+ buildApplicationMetricEventEnvelopes(events),+ );+ }++ async recordApplicationPlatformEvents(+ entries: ApplicationLogEntry[],+ ): Promise<void> {+ await this.dispatchOrLogError(() => [+ ...buildApplicationLogEnvelopes(entries),+ ...buildApplicationMetricEventEnvelopes(+ entries.map(toApplicationMetricEvent),+ ),+ ]);+ }++ private async dispatchOrLogError(+ buildEnvelopes: () => WorkspaceEventEnvelope[],+ ): Promise<void> {try {- await this.dispatch(buildApplicationLogEnvelopes(entries));+ await this.dispatch(buildEnvelopes());} catch (error) {- this.logger.error('Failed to record application logs', error);+ this.logger.error('Failed to record application events', error);}}diff --git a/packages/twenty-server/src/engine/core-modules/event-logs/ingest/console-event.sink.ts b/packages/twenty-server/src/engine/core-modules/event-logs/ingest/console-event.sink.tsindex 9b3e595bab6..3007f3936e5 100644--- a/packages/twenty-server/src/engine/core-modules/event-logs/ingest/console-event.sink.ts+++ b/packages/twenty-server/src/engine/core-modules/event-logs/ingest/console-event.sink.ts@@ -11,6 +11,10 @@ export class ConsoleEventSink implements EventSink {async write(events: WorkspaceEventEnvelope[]): Promise<void> {for (const event of events) {+ if (event.table === 'applicationMetricEvent') {+ continue;+ }+if (event.table === 'applicationLog') {const context = isNonEmptyString(event.row.logicFunctionName)? `${event.row.logicFunctionName}:${event.row.executionId}`diff --git a/packages/twenty-server/src/engine/core-modules/event-logs/producers/application-metric-event/application-metric-event.type.ts b/packages/twenty-server/src/engine/core-modules/event-logs/producers/application-metric-event/application-metric-event.type.tsnew file mode 100644index 00000000000..f0fdc1eb6d7--- /dev/null+++ b/packages/twenty-server/src/engine/core-modules/event-logs/producers/application-metric-event/application-metric-event.type.ts@@ -0,0 +1,15 @@+import { type ApplicationLogSource } from 'twenty-shared/types';++export type ApplicationMetricEvent = {+ timestamp: Date;+ applicationRegistrationId: string;+ workspaceId: string;+ applicationId: string;+ applicationVersion: string;+ source: ApplicationLogSource;+ logicFunctionId: string;+ logicFunctionName: string;+ level: string;+ errorCode: string;+ durationMs: number;+};diff --git a/packages/twenty-server/src/engine/core-modules/event-logs/producers/application-metric-event/build-application-metric-event-envelopes.spec.ts b/packages/twenty-server/src/engine/core-modules/event-logs/producers/application-metric-event/build-application-metric-event-envelopes.spec.tsnew file mode 100644index 00000000000..5d165c844af--- /dev/null+++ b/packages/twenty-server/src/engine/core-modules/event-logs/producers/application-metric-event/build-application-metric-event-envelopes.spec.ts@@ -0,0 +1,39 @@+import { type ApplicationMetricEvent } from 'src/engine/core-modules/event-logs/producers/application-metric-event/application-metric-event.type';+import { buildApplicationMetricEventEnvelopes } from 'src/engine/core-modules/event-logs/producers/application-metric-event/build-application-metric-event-envelopes';++const EVENT: ApplicationMetricEvent = {+ timestamp: new Date('2026-10-19T08:41:12.204Z'),+ applicationRegistrationId: 'registration-1',+ workspaceId: 'workspace-1',+ applicationId: 'application-1',+ applicationVersion: '1.2.3',+ source: 'LOGIC_FUNCTION',+ logicFunctionId: 'function-1',+ logicFunctionName: 'syncContacts',+ level: 'ERROR',+ errorCode: 'TypeError',+ durationMs: 1240.6,+};++describe('buildApplicationMetricEventEnvelopes', () => {+ it('maps an event to an applicationMetricEvent row with a whole duration', () => {+ expect(buildApplicationMetricEventEnvelopes([EVENT])).toEqual([+ {+ table: 'applicationMetricEvent',+ row: {+ ...EVENT,+ timestamp: '2026-10-19 08:41:12.204',+ durationMs: 1241,+ },+ },+ ]);+ });++ it('drops events that are not tied to an app registration', () => {+ expect(+ buildApplicationMetricEventEnvelopes([+ { ...EVENT, applicationRegistrationId: '' },+ ]),+ ).toEqual([]);+ });+});diff --git a/packages/twenty-server/src/engine/core-modules/event-logs/producers/application-metric-event/build-application-metric-event-envelopes.ts b/packages/twenty-server/src/engine/core-modules/event-logs/producers/application-metric-event/build-application-metric-event-envelopes.tsnew file mode 100644index 00000000000..049fcea39bf--- /dev/null+++ b/packages/twenty-server/src/engine/core-modules/event-logs/producers/application-metric-event/build-application-metric-event-envelopes.ts@@ -0,0 +1,19 @@+import { isNonEmptyString } from '@sniptt/guards';++import { formatDateTimeForClickHouse } from 'src/database/clickhouse/utils/format-date-time-for-clickhouse.util';+import { type ApplicationMetricEvent } from 'src/engine/core-modules/event-logs/producers/application-metric-event/application-metric-event.type';+import { type WorkspaceEventEnvelope } from 'src/engine/core-modules/event-logs/types/workspace-event-envelope.type';++export const buildApplicationMetricEventEnvelopes = (+ events: ApplicationMetricEvent[],+): WorkspaceEventEnvelope[] =>+ events+ .filter((event) => isNonEmptyString(event.applicationRegistrationId))+ .map((event) => ({+ table: 'applicationMetricEvent',+ row: {+ ...event,+ timestamp: formatDateTimeForClickHouse(event.timestamp),+ durationMs: Math.max(0, Math.round(event.durationMs)),+ },+ }));diff --git a/packages/twenty-server/src/engine/core-modules/event-logs/producers/application-metric-event/to-application-metric-event.spec.ts b/packages/twenty-server/src/engine/core-modules/event-logs/producers/application-metric-event/to-application-metric-event.spec.tsnew file mode 100644index 00000000000..d3b44864de7--- /dev/null+++ b/packages/twenty-server/src/engine/core-modules/event-logs/producers/application-metric-event/to-application-metric-event.spec.ts@@ -0,0 +1,36 @@+import { toApplicationMetricEvent } from 'src/engine/core-modules/event-logs/producers/application-metric-event/to-application-metric-event';++describe('toApplicationMetricEvent', () => {+ it('counts a platform log entry without duration or message', () => {+ const timestamp = new Date('2026-10-19T07:55:00.120Z');++ expect(+ toApplicationMetricEvent({+ timestamp,+ workspaceId: 'workspace-1',+ applicationId: 'application-1',+ applicationRegistrationId: 'registration-1',+ applicationVersion: '1.2.2',+ source: 'API_RATE_LIMIT',+ logicFunctionId: '',+ logicFunctionName: '',+ executionId: '',+ level: 'WARN',+ message: 'API rate limit reached. Requests are rejected for 2s.',+ errorCode: 'RATE_LIMITED',+ }),+ ).toEqual({+ timestamp,+ applicationRegistrationId: 'registration-1',+ workspaceId: 'workspace-1',+ applicationId: 'application-1',+ applicationVersion: '1.2.2',+ source: 'API_RATE_LIMIT',+ logicFunctionId: '',+ logicFunctionName: '',+ level: 'WARN',+ errorCode: 'RATE_LIMITED',+ durationMs: 0,+ });+ });+});diff --git a/packages/twenty-server/src/engine/core-modules/event-logs/producers/application-metric-event/to-application-metric-event.ts b/packages/twenty-server/src/engine/core-modules/event-logs/producers/application-metric-event/to-application-metric-event.tsnew file mode 100644index 00000000000..b4068747df3--- /dev/null+++ b/packages/twenty-server/src/engine/core-modules/event-logs/producers/application-metric-event/to-application-metric-event.ts@@ -0,0 +1,18 @@+import { type ApplicationLogEntry } from 'src/engine/core-modules/event-logs/producers/application-log/application-log-entry.interface';+import { type ApplicationMetricEvent } from 'src/engine/core-modules/event-logs/producers/application-metric-event/application-metric-event.type';++export const toApplicationMetricEvent = (+ entry: ApplicationLogEntry,+): ApplicationMetricEvent => ({+ timestamp: entry.timestamp,+ applicationRegistrationId: entry.applicationRegistrationId,+ workspaceId: entry.workspaceId,+ applicationId: entry.applicationId,+ applicationVersion: entry.applicationVersion,+ source: entry.source,+ logicFunctionId: entry.logicFunctionId,+ logicFunctionName: entry.logicFunctionName,+ level: entry.level,+ errorCode: entry.errorCode ?? '',+ durationMs: 0,+});diff --git a/packages/twenty-server/src/engine/core-modules/event-logs/types/workspace-event-envelope.type.ts b/packages/twenty-server/src/engine/core-modules/event-logs/types/workspace-event-envelope.type.tsindex 39461953d1f..c299a9c439b 100644--- a/packages/twenty-server/src/engine/core-modules/event-logs/types/workspace-event-envelope.type.ts+++ b/packages/twenty-server/src/engine/core-modules/event-logs/types/workspace-event-envelope.type.ts@@ -58,12 +58,27 @@ export type ApplicationLogRow = {properties: Record<string, unknown>;};+export type ApplicationMetricEventRow = {+ timestamp: string;+ applicationRegistrationId: string;+ workspaceId: string;+ applicationId: string;+ applicationVersion: string;+ source: ApplicationLogSource;+ logicFunctionId: string;+ logicFunctionName: string;+ level: string;+ errorCode: string;+ durationMs: number;+};+// The `table` literals must match the registry's clickHouseTable values (EVENT_LOG_TYPES).export type WorkspaceEventEnvelope =| { table: 'workspaceEvent'; row: AnalyticsEventRow }| { table: 'pageview'; row: PageviewRow }| { table: 'objectEvent'; row: ObjectEventRow }| { table: 'usageEvent'; row: UsageEventRow }- | { table: 'applicationLog'; row: ApplicationLogRow };+ | { table: 'applicationLog'; row: ApplicationLogRow }+ | { table: 'applicationMetricEvent'; row: ApplicationMetricEventRow };export type WorkspaceEventTable = WorkspaceEventEnvelope['table'];diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-executor/logic-function-executor.service.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-executor/logic-function-executor.service.tsindex bb8fbbf6a60..86498773266 100644--- a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-executor/logic-function-executor.service.ts+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-executor/logic-function-executor.service.ts@@ -41,6 +41,7 @@ import { parseApplicationLogLines } from 'src/engine/core-modules/event-logs/proimport { LogicFunctionDriverFactory } from 'src/engine/core-modules/logic-function/logic-function-drivers/logic-function-driver.factory';import { buildLogicFunctionErrorLogLine } from 'src/engine/core-modules/logic-function/logic-function-executor/utils/build-logic-function-error-log-line.util';import { buildLogicFunctionExecutionUsage } from 'src/engine/core-modules/logic-function/logic-function-executor/utils/build-logic-function-execution-usage.util';+import { buildLogicFunctionRunMetricEvent } from 'src/engine/core-modules/logic-function/logic-function-executor/utils/build-logic-function-run-metric-event.util';import { LogicFunctionPrebuiltWarmUpService } from 'src/engine/core-modules/logic-function/logic-function-prebuilt-warm-up/logic-function-prebuilt-warm-up.service';import { SecretEncryptionService } from 'src/engine/core-modules/secret-encryption/secret-encryption.service';import {@@ -599,6 +600,15 @@ export class LogicFunctionExecutorService {})),);+ void this.eventLogEmitterService.recordApplicationMetricEvents([+ buildLogicFunctionRunMetricEvent({+ result,+ flatApplication,+ flatLogicFunction,+ workspaceId,+ }),+ ]);+void this.publishLogicFunctionLogsToCli({result,flatApplication,diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-executor/utils/__tests__/build-logic-function-run-metric-event.util.spec.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-executor/utils/__tests__/build-logic-function-run-metric-event.util.spec.tsnew file mode 100644index 00000000000..7729549d8b4--- /dev/null+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-executor/utils/__tests__/build-logic-function-run-metric-event.util.spec.ts@@ -0,0 +1,65 @@+import { buildLogicFunctionRunMetricEvent } from 'src/engine/core-modules/logic-function/logic-function-executor/utils/build-logic-function-run-metric-event.util';+import { LogicFunctionExecutionStatus } from 'src/engine/metadata-modules/logic-function/dtos/logic-function-execution-result.dto';++const flatApplication = {+ id: 'application-1',+ applicationRegistrationId: 'registration-1',+ version: '1.2.3',+};++const flatLogicFunction = {+ id: 'function-1',+ name: 'syncContacts',+};++describe('buildLogicFunctionRunMetricEvent', () => {+ it('counts a successful run with its duration', () => {+ expect(+ buildLogicFunctionRunMetricEvent({+ result: {+ data: null,+ logs: '',+ duration: 420,+ billedDurationMs: 420,+ status: LogicFunctionExecutionStatus.SUCCESS,+ },+ flatApplication,+ flatLogicFunction,+ workspaceId: 'workspace-1',+ }),+ ).toMatchObject({+ applicationRegistrationId: 'registration-1',+ workspaceId: 'workspace-1',+ applicationId: 'application-1',+ applicationVersion: '1.2.3',+ source: 'LOGIC_FUNCTION',+ logicFunctionId: 'function-1',+ logicFunctionName: 'syncContacts',+ level: 'INFO',+ errorCode: '',+ durationMs: 420,+ });+ });++ it('counts a failed run with its error type', () => {+ expect(+ buildLogicFunctionRunMetricEvent({+ result: {+ data: null,+ logs: '',+ duration: 30_000,+ billedDurationMs: 30_000,+ status: LogicFunctionExecutionStatus.ERROR,+ error: {+ errorType: 'TimeoutError',+ errorMessage: 'Function execution timed out after 30s',+ stackTrace: [],+ },+ },+ flatApplication,+ flatLogicFunction,+ workspaceId: 'workspace-1',+ }),+ ).toMatchObject({ level: 'ERROR', errorCode: 'TimeoutError' });+ });+});diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-executor/utils/build-logic-function-run-metric-event.util.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-executor/utils/build-logic-function-run-metric-event.util.tsnew file mode 100644index 00000000000..3c03e42afe6--- /dev/null+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-executor/utils/build-logic-function-run-metric-event.util.ts@@ -0,0 +1,33 @@+import { type FlatApplication } from 'src/engine/core-modules/application/types/flat-application.type';+import { type ApplicationMetricEvent } from 'src/engine/core-modules/event-logs/producers/application-metric-event/application-metric-event.type';+import { type LogicFunctionExecuteResult } from 'src/engine/core-modules/logic-function/logic-function-drivers/interfaces/logic-function-driver.interface';+import { LogicFunctionExecutionStatus } from 'src/engine/metadata-modules/logic-function/dtos/logic-function-execution-result.dto';+import { type FlatLogicFunction } from 'src/engine/metadata-modules/logic-function/types/flat-logic-function.type';++export const buildLogicFunctionRunMetricEvent = ({+ result,+ flatApplication,+ flatLogicFunction,+ workspaceId,+}: {+ result: LogicFunctionExecuteResult;+ flatApplication: Pick<+ FlatApplication,+ 'id' | 'applicationRegistrationId' | 'version'+ >;+ flatLogicFunction: Pick<FlatLogicFunction, 'id' | 'name'>;+ workspaceId: string;+}): ApplicationMetricEvent => ({+ timestamp: new Date(),+ applicationRegistrationId: flatApplication.applicationRegistrationId ?? '',+ workspaceId,+ applicationId: flatApplication.id,+ applicationVersion: flatApplication.version ?? '',+ source: 'LOGIC_FUNCTION',+ logicFunctionId: flatLogicFunction.id,+ logicFunctionName: flatLogicFunction.name,+ level:+ result.status === LogicFunctionExecutionStatus.ERROR ? 'ERROR' : 'INFO',+ errorCode: result.error?.errorType ?? '',+ durationMs: result.duration,+});