UNPKG

solarwinds-apm

Version:

OpenTelemetry-based SolarWinds APM library

192 lines (189 loc) 7.21 kB
/* Copyright 2023-2025 SolarWinds Worldwide, LLC. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ var _a; import util from "node:util"; import { ExportResultCode, } from "@opentelemetry/core"; import { AggregationTemporality, DataPointType, InstrumentType, } from "@opentelemetry/sdk-metrics"; import { oboe } from "@solarwinds-apm/bindings"; import { explicit, exponential, } from "@solarwinds-apm/histogram"; import { componentLogger } from "../../shared/logger.js"; const MAX_TAGS = 50; export class AppopticsMetricExporter { #logger = componentLogger(_a); #reporter; constructor(reporter) { this.#reporter = reporter; } export(metrics, resultCallback) { for (const scopeMetric of metrics.scopeMetrics) { const scopeTags = this.#scopeTags(scopeMetric.scope); for (const metric of scopeMetric.metrics) { const name = metric.descriptor.name; const temporality = metric.aggregationTemporality; const descriptorTags = this.#descriptorTags(metric.descriptor); for (const dataPoint of metric.dataPoints) { const dataPointTags = this.#dataPointTags(dataPoint.attributes); const [tags, tagCount] = this.#oboeTags({ ...scopeTags, ...descriptorTags, ...dataPointTags, }); switch (metric.dataPointType) { case DataPointType.SUM: { if (temporality === AggregationTemporality.DELTA) { this.#exportCounter(dataPoint.value, name, tags, tagCount); } else { this.#exportSummary(dataPoint.value, name, tags, tagCount); } break; } case DataPointType.GAUGE: { if (temporality === AggregationTemporality.CUMULATIVE) { this.#exportSummary(dataPoint.value, name, tags, tagCount); } else { this.#logger.warn("gauges with delta aggregation are not supported"); } break; } case DataPointType.HISTOGRAM: { if (temporality === AggregationTemporality.DELTA) { const histogram = explicit(dataPoint.value); this.#exportHistogram(histogram, name, tags, tagCount); } else { this.#logger.warn("histograms with cumulative aggregation are not supported"); } break; } case DataPointType.EXPONENTIAL_HISTOGRAM: { if (temporality === AggregationTemporality.DELTA) { const histogram = exponential(dataPoint.value); this.#exportHistogram(histogram, name, tags, tagCount); } else { this.#logger.warn("histograms with cumulative aggregation are not supported"); } break; } } } } } resultCallback({ code: ExportResultCode.SUCCESS, }); } selectAggregationTemporality(instrumentType) { switch (instrumentType) { case InstrumentType.HISTOGRAM: { return AggregationTemporality.DELTA; } default: { return AggregationTemporality.CUMULATIVE; } } } forceFlush() { this.#reporter.flush(); return Promise.resolve(); } shutdown() { oboe.Context.shutdown(); return Promise.resolve(); } #exportCounter(value, name, tags, tagCount) { oboe.CustomMetrics.increment({ name, count: value, tags, tags_count: tagCount, host_tag: 1, service_name: null, }); } #exportSummary(value, name, tags, tagCount) { oboe.CustomMetrics.summary({ name, value, count: 1, tags, tags_count: tagCount, host_tag: 1, service_name: null, }); } #exportHistogram(histogram, name, tags, tagCount) { const stats = { count: histogram.count, min: histogram.min, max: histogram.max, p50: histogram.getPercentile(50), p95: histogram.getPercentile(95), p99: histogram.getPercentile(99), }; const filtered = Object.entries(stats).filter(([, v]) => v !== undefined); for (const [stat, value] of filtered) { oboe.CustomMetrics.summary({ name: `${name}.${stat}`, value: value, count: 1, tags, tags_count: tagCount, host_tag: 1, service_name: null, }); } } #oboeTags(tags) { const entries = Object.entries(tags); const count = Math.min(entries.length, MAX_TAGS); entries.length = count; const oboeTags = new oboe.MetricTags(count); for (const [i, [k, v]] of entries.entries()) { oboeTags.add(i, k, v); } return [oboeTags, count]; } #scopeTags(scope) { const tags = { "scope.name": scope.name }; if (scope.version) { tags["scope.version"] = scope.version; } return tags; } #descriptorTags(descriptor) { return { description: descriptor.description, unit: descriptor.unit, }; } #dataPointTags(attributes) { return Object.fromEntries(Object.entries(attributes) .filter(([, v]) => v !== undefined) .map(([k, v]) => { if (Array.isArray(v)) { return [ k, util.inspect(v, { breakLength: Infinity, compact: true }), ]; } else { return [k, v.toString()]; } })); } } _a = AppopticsMetricExporter; //# sourceMappingURL=metrics.js.map