Skip to content
Merged
Changes from all commits
Commits
File filter

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();
}
return new DefaultAggregation();
};
exporter.selectAggregationTemporality = (
instrumentType: InstrumentType
) => {
return AggregationTemporality.DELTA;
};
return exporter;
}
}

export function __getMetricExporterForTesting(): InMemoryMetricExporter {
Expand Down