UNPKG

@aws/aws-distro-opentelemetry-node-autoinstrumentation

Version:

This package provides Amazon Web Services distribution of the OpenTelemetry Node Instrumentation, which allows for auto-instrumentation of NodeJS applications.

175 lines 6.19 kB
import { Attributes } from '@opentelemetry/api'; import { AggregationSelector, AggregationTemporality, AggregationTemporalitySelector, InstrumentType, PushMetricExporter, ResourceMetrics } from '@opentelemetry/sdk-metrics'; import type { AggregationOption } from '@opentelemetry/sdk-metrics'; import { ExportResult } from '@opentelemetry/core'; import type { LogEvent } from '@aws-sdk/client-cloudwatch-logs'; /** * Intermediate format for metric data before converting to EMF */ export interface MetricRecord { name: string; unit: string; description: string; timestamp: number; attributes: Attributes; sumData?: number; histogramData?: HistogramMetricRecordData; expHistogramData?: ExponentialHistogramMetricRecordData; value?: number; } interface HistogramMetricRecordData { Count: number; Sum: number; Max: number; Min: number; } interface ExponentialHistogramMetricRecordData { Values: number[]; Counts: number[]; Count: number; Sum: number; Max: number; Min: number; } export interface EMFLog { _aws: _Aws; [key: `otel.resource.${string}`]: string; [metricName: string]: any; Version: string; } interface _Aws { CloudWatchMetrics: CloudWatchMetric[]; Timestamp: number; } interface CloudWatchMetric { Namespace: string; Dimensions?: string[][]; Metrics: Metric[]; } interface Metric { Name: string; Unit?: string; } export declare abstract class EMFExporterBase implements PushMetricExporter { private namespace; private aggregationTemporalitySelector; private aggregationSelector; private EMF_SUPPORTED_UNITS; private UNIT_MAPPING; constructor(namespace?: string, aggregationTemporalitySelector?: AggregationTemporalitySelector, aggregationSelector?: AggregationSelector); /** * Get CloudWatch unit from unit in MetricRecord */ private getUnit; /** * Extract dimension names from attributes. * For now, use all attributes as dimensions for the dimension selection logic. */ private getDimensionNames; /** * Check if Application Signals EMF dimensions should be added. * * Returns true by default, unless OTEL_METRICS_ADD_APPLICATION_SIGNALS_DIMENSIONS is explicitly set to 'false'. * Note: When Agent Observability is enabled, the configurator sets this env var to 'false' by default. */ private static shouldAddApplicationSignalsDimensions; /** * Get the default environment based on the cloud platform. */ private getDefaultEnvironmentForPlatform; /** * Check if dimension already exists (case-insensitive match). */ private hasDimensionCaseInsensitive; /** * Add Service and Environment dimensions if Application Signals dimensions are enabled * and the dimensions are not already present (case-insensitive check). */ private addApplicationSignalsDimensions; /** * Create a hashable key from attributes for grouping metrics. */ private getAttributesKey; /** * Normalize an OpenTelemetry timestamp to milliseconds for CloudWatch. */ private normalizeTimestamp; /** * Create a base metric record with instrument information. */ private createMetricRecord; /** * Convert a Gauge or Sum metric datapoint to a metric record. */ private convertGaugeAndSum; /** * Convert a Histogram metric datapoint to a metric record. * * https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/exporter/awsemfexporter/datapoint.go#L87 */ private convertHistogram; /** * Convert an ExponentialHistogram metric datapoint to a metric record. * * This function follows the logic of CalculateDeltaDatapoints in the Go implementation, * converting exponential buckets to their midpoint values. * Ref: https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/22626 */ private convertExpHistogram; /** * Create EMF log from metric records. * * Since metricRecords is already grouped by attributes, this function * creates a single EMF log for all records. */ private createEmfLog; /** * Group metric record by attributes and timestamp. * * @param record The metric record * @param timestampMs The timestamp in milliseconds * @returns {[string, number]} Values for the key to group metrics */ private groupByAttributesAndTimestamp; /** * Method to handle safely pushing a MetricRecord into a Map of a Map of a list of MetricRecords * * @param groupedMetrics * @param groupAttribute * @param groupTimestamp * @param record */ private pushMetricRecordIntoGroupedMetrics; /** * Export metrics as EMF logs. * Groups metrics by attributes and timestamp before creating EMF logs. * * @param resourceMetrics Resource Metrics data containing scope metrics * @param resultCallback callback for when the export has completed * @returns {Promise<void>} */ export(resourceMetrics: ResourceMetrics, resultCallback: (result: ExportResult) => void): Promise<void>; /** * Send a log event to the destination (CloudWatch Logs, console, etc.). * This method must be implemented by subclasses to define where the EMF logs are sent. * * @param logEvent The log event to send * @returns {Promise<void>} */ protected abstract sendLogEvent(logEvent: Required<LogEvent>): Promise<void>; /** * Force flush any pending metrics. * This method must be implemented by subclasses to forceFlush pending metrics. */ abstract forceFlush(): Promise<void>; /** * Shutdown the exporter. * This method must be implemented by subclasses to gracefully shutdown * and clean-up the exporter. */ abstract shutdown(): Promise<void>; selectAggregationTemporality(instrumentType: InstrumentType): AggregationTemporality; selectAggregation(instrumentType: InstrumentType): AggregationOption; } export {}; //# sourceMappingURL=emf-exporter-base.d.ts.map