@azure/monitor-opentelemetry
Version:
Azure Monitor OpenTelemetry (Node.js)
97 lines • 3.51 kB
JavaScript
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { context, diag } from "@opentelemetry/api";
import { AggregationTemporality, InstrumentType } from "@opentelemetry/sdk-metrics";
import { ExportResultCode, suppressTracing } from "@opentelemetry/core";
import { QuickpulseSender } from "./sender.js";
import { getTransmissionTime, resourceMetricsToQuickpulseDataPoint } from "../utils.js";
/**
* Quickpulse Metric Exporter.
*/
export class QuickpulseMetricExporter {
sender;
postCallback;
getDocumentsFn;
// Monitoring data point with common properties
baseMonitoringDataPoint;
etag;
getErrorsFn;
getDerivedMetricValuesFn;
/**
* Initializes a new instance of the AzureMonitorMetricExporter class.
* @param AzureExporterConfig - Exporter configuration.
*/
constructor(options) {
this.sender = new QuickpulseSender({
endpointUrl: options.endpointUrl,
instrumentationKey: options.instrumentationKey,
credential: options.credential,
credentialScopes: options.credentialScopes,
});
this.postCallback = options.postCallback;
this.getDocumentsFn = options.getDocumentsFn;
this.baseMonitoringDataPoint = options.baseMonitoringDataPoint;
this.getErrorsFn = options.getErrorsFn;
this.etag = "";
this.getDerivedMetricValuesFn = options.getDerivedMetricValuesFn;
diag.debug("QuickpulseMetricExporter was successfully setup");
}
/**
* Export OpenTelemetry resource metrics.
* @param metrics - Resource metrics to export.
* @param resultCallback - Result callback.
*/
// eslint-disable-next-line @typescript-eslint/no-misused-promises
async export(metrics, resultCallback) {
diag.info(`Exporting Live metrics(s). Converting to envelopes...`);
const optionalParams = {
monitoringDataPoints: resourceMetricsToQuickpulseDataPoint(metrics, this.baseMonitoringDataPoint, this.getDocumentsFn(), this.getErrorsFn(), this.getDerivedMetricValuesFn()),
transmissionTime: getTransmissionTime(),
configurationEtag: this.etag,
};
// Supress tracing until OpenTelemetry Metrics SDK support it
await context.with(suppressTracing(context.active()), async () => {
try {
this.postCallback(await this.sender.publish(optionalParams));
resultCallback({ code: ExportResultCode.SUCCESS });
}
catch (error) {
this.postCallback(undefined);
resultCallback({ code: ExportResultCode.FAILED });
}
});
}
/**
* Shutdown Exporter.
*/
async shutdown() {
diag.info("QuickpulseMetricExporter shutting down");
return Promise.resolve();
}
/**
* Select aggregation temporality
*/
selectAggregationTemporality(instrumentType) {
if (instrumentType === InstrumentType.UP_DOWN_COUNTER ||
instrumentType === InstrumentType.OBSERVABLE_UP_DOWN_COUNTER) {
return AggregationTemporality.CUMULATIVE;
}
return AggregationTemporality.DELTA;
}
/**
* Force flush
*/
async forceFlush() {
return Promise.resolve();
}
/**
* Get Sender
*/
getSender() {
return this.sender;
}
setEtag(etag) {
this.etag = etag;
}
}
//# sourceMappingURL=exporter.js.map