Repository: microsoft/ApplicationInsights-node.js
Pull request #1138 (2023-05-05 23:46:47)

[Beta] Removing Native Metrics functionality
Native metrics is not fully validated, there are some issues causing memory errors in build executions, removing for now we will enable back once we fix those issues


Patch:
diff --git a/src/metrics/handlers/performanceCounterMetricsHandler.ts b/src/metrics/handlers/performanceCounterMetricsHandler.ts
index 5f9d628..83537d6 100644
--- a/src/metrics/handlers/performanceCounterMetricsHandler.ts
+++ b/src/metrics/handlers/performanceCounterMetricsHandler.ts
@@ -31,7 +31,6 @@ export class PerformanceCounterMetricsHandler {
     private _meter: Meter;
     private _processMetrics: ProcessMetrics;
     private _requestMetrics: RequestMetrics;
-    private _nativeMetrics: NativePerformanceMetrics;
 
     constructor(config: ApplicationInsightsConfig, options?: { collectionInterval: number }) {
         this._config = config;
@@ -50,7 +49,6 @@ export class PerformanceCounterMetricsHandler {
         this._meter = this._meterProvider.getMeter("ApplicationInsightsPerfMetricsMeter");
         this._processMetrics = new ProcessMetrics(this._meter);
         this._requestMetrics = new RequestMetrics(this._meter);
-        this._nativeMetrics = new NativePerformanceMetrics(this._meter);
     }
 
      /** 
@@ -64,7 +62,6 @@ export class PerformanceCounterMetricsHandler {
         this._meterProvider.shutdown();
         this._processMetrics.shutdown();
         this._requestMetrics.shutdown();
-        this._nativeMetrics.shutdown();
     }
 
     public recordSpan(span: ReadableSpan): void {
@@ -143,54 +140,6 @@ export class PerformanceCounterMetricsHandler {
                 aggregation: new DropAggregation(),
             })
         );
-        if (!this._config.extendedMetrics?.gc) {
-            views.push(
-                new View({
-                    instrumentName: NativeMetricsCounter.GARBAGE_COLLECTION_INCREMENTAL_MARKING,
-                    aggregation: new DropAggregation(),
-                })
-            );
-            views.push(
-                new View({
-                    instrumentName: NativeMetricsCounter.GARBAGE_COLLECTION_SCAVENGE,
-                    aggregation: new DropAggregation(),
-                })
-            );
-            views.push(
-                new View({
-                    instrumentName: NativeMetricsCounter.GARBAGE_COLLECTION_SWEEP_COMPACT,
-                    aggregation: new DropAggregation(),
-                })
-            );
-        }
-        if (!this._config.extendedMetrics?.heap) {
-            views.push(
-                new View({
-                    instrumentName: NativeMetricsCounter.HEAP_MEMORY_TOTAL,
-                    aggregation: new DropAggregation(),
-                })
-            );
-            views.push(
-                new View({
-                    instrumentName: NativeMetricsCounter.HEAP_MEMORY_USAGE,
-                    aggregation: new DropAggregation(),
-                })
-            );
-            views.push(
-                new View({
-                    instrumentName: NativeMetricsCounter.MEMORY_USAGE_NON_HEAP,
-                    aggregation: new DropAggregation(),
-                })
-            );
-        }
-        if (!this._config.extendedMetrics?.loop) {
-            views.push(
-                new View({
-                    instrumentName: NativeMetricsCounter.EVENT_LOOP_CPU,
-                    aggregation: new DropAggregation(),
-                })
-            );
-        }
         return views;
     }
 


Test patch:
diff --git a/test/unitTests/metrics/nativePerformance.tests.ts b/test/unitTests/metrics/nativePerformance.tests.ts
index e0bd29d..1bdeeec 100644
--- a/test/unitTests/metrics/nativePerformance.tests.ts
+++ b/test/unitTests/metrics/nativePerformance.tests.ts
@@ -1,44 +1,44 @@
-import * as assert from "assert";
-import * as sinon from "sinon";
-import { Meter } from "@opentelemetry/api";
-import { MeterProvider } from "@opentelemetry/sdk-metrics";
-import { NativePerformanceMetrics } from "../../../src/metrics/collection/nativePerformanceMetrics";
+// import * as assert from "assert";
+// import * as sinon from "sinon";
+// import { Meter } from "@opentelemetry/api";
+// import { MeterProvider } from "@opentelemetry/sdk-metrics";
+// import { NativePerformanceMetrics } from "../../../src/metrics/collection/nativePerformanceMetrics";
 
-class TestEmitter {
-    enable() { }
-    disable() { }
-    getLoopData() { }
-    getGCData() { }
-}
+// class TestEmitter {
+//     enable() { }
+//     disable() { }
+//     getLoopData() { }
+//     getGCData() { }
+// }
 
-describe("AutoCollection/NativePerformance", () => {
-    let sandbox: sinon.SinonSandbox;
-    let testMeter: Meter;
+// describe("AutoCollection/NativePerformance", () => {
+//     let sandbox: sinon.SinonSandbox;
+//     let testMeter: Meter;
 
-    before(() => {
-        sandbox = sinon.createSandbox();
-        const testProvider = new MeterProvider();
-        testMeter = testProvider.getMeter("test");
-    });
+//     before(() => {
+//         sandbox = sinon.createSandbox();
+//         const testProvider = new MeterProvider();
+//         testMeter = testProvider.getMeter("test");
+//     });
 
-    afterEach(() => {
-        sandbox.restore();
-    });
+//     afterEach(() => {
+//         sandbox.restore();
+//     });
 
-    describe("#Metrics", () => {
-        it("init should auto collection interval if native metrics packages is installed", () => {
-            const nativePerformance = new NativePerformanceMetrics(testMeter);
-            nativePerformance["_emitter"] = new TestEmitter();
-            assert.ok(!nativePerformance["_handle"]); // Package is not installed in test execution, TODO: Add test where this is available
-        });
+//     describe("#Metrics", () => {
+//         it("init should auto collection interval if native metrics packages is installed", () => {
+//             const nativePerformance = new NativePerformanceMetrics(testMeter);
+//             nativePerformance["_emitter"] = new TestEmitter();
+//             assert.ok(!nativePerformance["_handle"]); // Package is not installed in test execution, TODO: Add test where this is available
+//         });
 
-        it("Calling enable when metrics are not available should fail gracefully", () => {
-            let nativePerformance = null;
+//         it("Calling enable when metrics are not available should fail gracefully", () => {
+//             let nativePerformance = null;
 
-            assert.doesNotThrow(
-                () => nativePerformance = new NativePerformanceMetrics(testMeter),
-                "Does not throw when native metrics are not available"
-            );
-        });
-    });
-});
+//             assert.doesNotThrow(
+//                 () => nativePerformance = new NativePerformanceMetrics(testMeter),
+//                 "Does not throw when native metrics are not available"
+//             );
+//         });
+//     });
+// });
diff --git a/test/unitTests/metrics/performance.tests.ts b/test/unitTests/metrics/performance.tests.ts
index b581e5c..7bf24ed 100644
--- a/test/unitTests/metrics/performance.tests.ts
+++ b/test/unitTests/metrics/performance.tests.ts
@@ -46,35 +46,6 @@ describe("PerformanceCounterMetricsHandler", () => {
                 autoCollect["_requestMetrics"]["_requestRateGauge"],
                 "_dependencyDurationGauge not available"
             );
-
-            assert.ok(
-                autoCollect["_nativeMetrics"]["_eventLoopHistogram"],
-                "_eventLoopHistogram not available"
-            );
-            assert.ok(
-                autoCollect["_nativeMetrics"]["_garbageCollectionScavenge"],
-                "_garbageCollectionScavenge not available"
-            );
-            assert.ok(
-                autoCollect["_nativeMetrics"]["_garbageCollectionMarkSweepCompact"],
-                "_garbageCollectionMarkSweepCompact not available"
-            );
-            assert.ok(
-                autoCollect["_nativeMetrics"]["_garbageCollectionIncrementalMarking"],
-                "_garbageCollectionIncrementalMarking not available"
-            );
-            assert.ok(
-                autoCollect["_nativeMetrics"]["_heapMemoryTotalGauge"],
-                "_heapMemoryTotalGauge not available"
-            );
-            assert.ok(
-                autoCollect["_nativeMetrics"]["_heapMemoryUsageGauge"],
-                "_heapMemoryUsageGauge not available"
-            );
-            assert.ok(
-                autoCollect["_nativeMetrics"]["_memoryUsageNonHeapGauge"],
-                "_memoryUsageNonHeapGauge not available"
-            );
         });
 
         it("should observe instruments during collection", async () => {
@@ -85,29 +56,13 @@ describe("PerformanceCounterMetricsHandler", () => {
             const scopeMetrics = resourceMetrics.scopeMetrics;
             assert.strictEqual(scopeMetrics.length, 1, "scopeMetrics count");
             let metrics = scopeMetrics[0].metrics;
-            assert.strictEqual(metrics.length, 13, "metrics count");
+            assert.strictEqual(metrics.length, 6, "metrics count");
             assert.equal(metrics[0].descriptor.name, PerformanceCounter.PRIVATE_BYTES);
             assert.equal(metrics[1].descriptor.name, PerformanceCounter.AVAILABLE_BYTES);
             assert.equal(metrics[2].descriptor.name, PerformanceCounter.PROCESSOR_TIME);
             assert.equal(metrics[3].descriptor.name, PerformanceCounter.PROCESS_TIME);
             assert.equal(metrics[4].descriptor.name, PerformanceCounter.REQUEST_DURATION);
             assert.equal(metrics[5].descriptor.name, PerformanceCounter.REQUEST_RATE);
-            assert.equal(metrics[6].descriptor.name, NativeMetricsCounter.EVENT_LOOP_CPU);
-            assert.equal(
-                metrics[7].descriptor.name,
-                NativeMetricsCounter.GARBAGE_COLLECTION_SCAVENGE
-            );
-            assert.equal(
-                metrics[8].descriptor.name,
-                NativeMetricsCounter.GARBAGE_COLLECTION_SWEEP_COMPACT
-            );
-            assert.equal(
-                metrics[9].descriptor.name,
-                NativeMetricsCounter.GARBAGE_COLLECTION_INCREMENTAL_MARKING
-            );
-            assert.equal(metrics[10].descriptor.name, NativeMetricsCounter.HEAP_MEMORY_TOTAL);
-            assert.equal(metrics[11].descriptor.name, NativeMetricsCounter.HEAP_MEMORY_USAGE);
-            assert.equal(metrics[12].descriptor.name, NativeMetricsCounter.MEMORY_USAGE_NON_HEAP);
         });
 
         it("should not collect when disabled", async () => {
@@ -125,11 +80,6 @@ describe("PerformanceCounterMetricsHandler", () => {
             config.extendedMetrics.gc = false;
             const autoCollect = new PerformanceCounterMetricsHandler(config);
             let views = autoCollect["_getViews"]();
-            assert.equal(views.length, 18); // All Native metrics ignore views are added
-            config.extendedMetrics.heap = true;
-            config.extendedMetrics.loop = true;
-            config.extendedMetrics.gc = true;
-            views = autoCollect["_getViews"]();
             assert.equal(views.length, 11);
         });
     });