UNPKG

@opentelemetry/instrumentation-runtime-node

Version:
58 lines 2.38 kB
"use strict"; /* * Copyright The OpenTelemetry Authors * SPDX-License-Identifier: Apache-2.0 */ Object.defineProperty(exports, "__esModule", { value: true }); exports.GCCollector = void 0; const perf_hooks = require("node:perf_hooks"); const api_1 = require("@opentelemetry/api"); const baseCollector_1 = require("./baseCollector"); const semconv_1 = require("../semconv"); const DEFAULT_GC_DURATION_BUCKETS = [ 0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1, 2.5, 5, 7.5, 10, ]; const kinds = []; kinds[perf_hooks.constants.NODE_PERFORMANCE_GC_MAJOR] = 'major'; kinds[perf_hooks.constants.NODE_PERFORMANCE_GC_MINOR] = 'minor'; kinds[perf_hooks.constants.NODE_PERFORMANCE_GC_INCREMENTAL] = 'incremental'; kinds[perf_hooks.constants.NODE_PERFORMANCE_GC_WEAKCB] = 'weakcb'; class GCCollector extends baseCollector_1.BaseCollector { _gcDurationByKindHistogram; _observer; constructor(config = {}) { super(config); this._observer = new perf_hooks.PerformanceObserver(list => { if (!this._config.enabled) return; const entry = list.getEntries()[0]; // Node < 16 uses entry.kind // Node >= 16 uses entry.detail.kind // See: https://nodejs.org/docs/latest-v16.x/api/deprecations.html#deprecations_dep0152_extension_performanceentry_properties // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore const kind = entry.detail ? kinds[entry.detail.kind] : kinds[entry.kind]; this._gcDurationByKindHistogram?.record(entry.duration / 1000, { [semconv_1.ATTR_V8JS_GC_TYPE]: kind, }); }); } updateMetricInstruments(meter) { this._gcDurationByKindHistogram = meter.createHistogram(semconv_1.METRIC_V8JS_GC_DURATION, { description: 'Garbage collection duration by kind, one of major, minor, incremental or weakcb.', unit: 's', valueType: api_1.ValueType.DOUBLE, advice: { explicitBucketBoundaries: DEFAULT_GC_DURATION_BUCKETS, }, }); } internalEnable() { this._observer.observe({ entryTypes: ['gc'] }); } internalDisable() { this._observer.disconnect(); } } exports.GCCollector = GCCollector; //# sourceMappingURL=gcCollector.js.map