Skip to content

Commit

Permalink
Update GCP metric exporter to use an ExponentialHistogramAggregation (#…
Browse files Browse the repository at this point in the history
…398)

* Also switch to using DELTA temporality and reduce export frequency to 5m intervals by default.
  • Loading branch information
bryanatkinson authored Jun 12, 2024
1 parent 71efe6a commit be64758
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions js/plugins/google-cloud/src/gcpOpenTelemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
});
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit be64758

Please sign in to comment.