Skip to content

The open alternative to Salesforce, designed for AI.

feat(server): make the OTLP metrics temporality configurable (#26857)

Etiennede5d5e27ec1d
diff --git a/packages/twenty-server/.env.example b/packages/twenty-server/.env.example
index 54d071cbb44..d312329f74d 100644
--- a/packages/twenty-server/.env.example
+++ b/packages/twenty-server/.env.example
@@ -45,6 +45,7 @@ FRONTEND_URL=http://localhost:3001
# EXCEPTION_HANDLER_DRIVER=sentry
# METER_DRIVER=opentelemetry,console
# METER_EXPORT_INTERVAL_MS=30000
+# METER_TEMPORALITY=delta
# SENTRY_ENVIRONMENT=main
# SENTRY_DSN=https://[email protected]/xxx
# SENTRY_FRONT_DSN=https://[email protected]/xxx
diff --git a/packages/twenty-server/src/engine/core-modules/metrics/types/meter-temporality.type.ts b/packages/twenty-server/src/engine/core-modules/metrics/types/meter-temporality.type.ts
new file mode 100644
index 00000000000..b63fdaf2fbe
--- /dev/null
+++ b/packages/twenty-server/src/engine/core-modules/metrics/types/meter-temporality.type.ts
@@ -0,0 +1,4 @@
+export enum MeterTemporality {
+ Delta = 'delta',
+ Cumulative = 'cumulative',
+}
diff --git a/packages/twenty-server/src/engine/core-modules/twenty-config/config-variables.ts b/packages/twenty-server/src/engine/core-modules/twenty-config/config-variables.ts
index c988c54be05..4110bc43db6 100644
--- a/packages/twenty-server/src/engine/core-modules/twenty-config/config-variables.ts
+++ b/packages/twenty-server/src/engine/core-modules/twenty-config/config-variables.ts
@@ -42,6 +42,7 @@ import {
type TwentyLogLevel,
} from 'src/engine/core-modules/logger/interfaces';
import { type MeterDriver } from 'src/engine/core-modules/metrics/types/meter-driver.type';
+import { MeterTemporality } from 'src/engine/core-modules/metrics/types/meter-temporality.type';
import { CastToLogLevelArray } from 'src/engine/core-modules/twenty-config/decorators/cast-to-log-level-array.decorator';
import { CastToMeterDriverArray } from 'src/engine/core-modules/twenty-config/decorators/cast-to-meter-driver.decorator';
import { CastToPositiveNumber } from 'src/engine/core-modules/twenty-config/decorators/cast-to-positive-number.decorator';
@@ -1257,6 +1258,18 @@ export class ConfigVariables {
@IsOptional()
METER_EXPORT_INTERVAL_MS = 30_000;
+ @ConfigVariablesMetadata({
+ group: ConfigVariablesGroup.LOGGING,
+ description:
+ 'Temporality of the metrics pushed by the opentelemetry driver: delta sends what changed since the previous export, cumulative sends running totals on every export. Read before the config store is available, so it cannot be overridden from the database.',
+ type: ConfigVariableType.ENUM,
+ options: Object.values(MeterTemporality),
+ isEnvOnly: true,
+ })
+ @IsOptional()
+ @IsEnum(MeterTemporality)
+ METER_TEMPORALITY: MeterTemporality = MeterTemporality.Delta;
+
@ConfigVariablesMetadata({
group: ConfigVariablesGroup.LOGGING,
description: 'Driver used for logging (only console for now)',
diff --git a/packages/twenty-server/src/instrument.ts b/packages/twenty-server/src/instrument.ts
index 5498e60f41c..fe3fa265e42 100644
--- a/packages/twenty-server/src/instrument.ts
+++ b/packages/twenty-server/src/instrument.ts
@@ -19,6 +19,7 @@ import { NodeEnvironment } from 'src/engine/core-modules/twenty-config/interface
import { ExceptionHandlerDriver } from 'src/engine/core-modules/exception-handler/interfaces';
import { POD_NAME } from 'src/engine/core-modules/metrics/constants/pod-name.constant';
import { MeterDriver } from 'src/engine/core-modules/metrics/types/meter-driver.type';
+import { MeterTemporality } from 'src/engine/core-modules/metrics/types/meter-temporality.type';
import { parseArrayEnvVar } from 'src/utils/parse-array-env-var';
const meterDrivers = parseArrayEnvVar(
@@ -52,6 +53,11 @@ const metricExportIntervalMillis =
? parsedExportInterval
: 30_000;
+const metricTemporality =
+ process.env.METER_TEMPORALITY === MeterTemporality.Cumulative
+ ? AggregationTemporality.CUMULATIVE
+ : AggregationTemporality.DELTA;
+
if (process.env.EXCEPTION_HANDLER_DRIVER === ExceptionHandlerDriver.SENTRY) {
const tracesSampleRate = parseSampleRate({
value: process.env.SENTRY_TRACES_SAMPLE_RATE,
@@ -151,7 +157,7 @@ const meterProvider = new MeterProvider({
new PeriodicExportingMetricReader({
exporter: new OTLPMetricExporter({
url: process.env.OTLP_COLLECTOR_METRICS_ENDPOINT_URL,
- temporalityPreference: AggregationTemporality.DELTA,
+ temporalityPreference: metricTemporality,
}),
exportIntervalMillis: metricExportIntervalMillis,
}),