From be6475891713be2068755f87adb6324d647d99bb Mon Sep 17 00:00:00 2001 From: Bryan Atkinson Date: Wed, 12 Jun 2024 14:54:34 -0400 Subject: [PATCH] Update GCP metric exporter to use an ExponentialHistogramAggregation (#398) * Also switch to using DELTA temporality and reduce export frequency to 5m intervals by default. --- .../google-cloud/src/gcpOpenTelemetry.ts | 29 +++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/js/plugins/google-cloud/src/gcpOpenTelemetry.ts b/js/plugins/google-cloud/src/gcpOpenTelemetry.ts index 43ec46dbf..7d819998d 100644 --- a/js/plugins/google-cloud/src/gcpOpenTelemetry.ts +++ b/js/plugins/google-cloud/src/gcpOpenTelemetry.ts @@ -27,7 +27,10 @@ import { WinstonInstrumentation } from '@opentelemetry/instrumentation-winston'; import { Resource } from '@opentelemetry/resources'; import { AggregationTemporality, + DefaultAggregation, + ExponentialHistogramAggregation, InMemoryMetricExporter, + InstrumentType, PeriodicExportingMetricReader, PushMetricExporter, } from '@opentelemetry/sdk-metrics'; @@ -145,14 +148,12 @@ export class GcpOpenTelemetry implements TelemetryConfig { * Creates a {MetricReader} for pushing metrics out to GCP via OpenTelemetry. */ private createMetricReader(): PeriodicExportingMetricReader { - metricExporter = this.shouldExportMetrics() - ? new MetricExporter({ projectId: this.options.projectId }) - : new InMemoryMetricExporter(AggregationTemporality.CUMULATIVE); + metricExporter = this.buildMetricExporter(); return new PeriodicExportingMetricReader({ exportIntervalMillis: - this.options?.telemetryConfig?.metricExportIntervalMillis || 60_000, + this.options?.telemetryConfig?.metricExportIntervalMillis || 300_000, exportTimeoutMillis: - this.options?.telemetryConfig?.metricExportTimeoutMillis || 60_000, + this.options?.telemetryConfig?.metricExportTimeoutMillis || 300_000, exporter: metricExporter, }); } @@ -188,6 +189,24 @@ export class GcpOpenTelemetry implements TelemetryConfig { new PinoInstrumentation({ logHook: this.gcpTraceLogHook }), ]; } + + private buildMetricExporter(): PushMetricExporter { + const exporter: PushMetricExporter = this.shouldExportMetrics() + ? new MetricExporter({ projectId: this.options.projectId }) + : new InMemoryMetricExporter(AggregationTemporality.DELTA); + exporter.selectAggregation = (instrumentType: InstrumentType) => { + if (instrumentType === InstrumentType.HISTOGRAM) { + return new ExponentialHistogramAggregation(); + } + return new DefaultAggregation(); + }; + exporter.selectAggregationTemporality = ( + instrumentType: InstrumentType + ) => { + return AggregationTemporality.DELTA; + }; + return exporter; + } } export function __getMetricExporterForTesting(): InMemoryMetricExporter {