Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update GCP metric exporter to use an ExponentialHistogramAggregation #398

Merged
merged 2 commits into from
Jun 12, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(100, true);
}
return new DefaultAggregation();
};
exporter.selectAggregationTemporality = (
instrumentType: InstrumentType
) => {
return AggregationTemporality.DELTA;
};
return exporter;
}
}

export function __getMetricExporterForTesting(): InMemoryMetricExporter {
Expand Down
Loading