From 2740b45ae8ebd2d5a3a95a990819b8a571f42503 Mon Sep 17 00:00:00 2001 From: Jackson Weber Date: Fri, 29 Aug 2025 17:16:34 -0700 Subject: [PATCH] Add support for ignoring the log/trace OTLP export values. --- src/agent/aksLoader.ts | 23 +++++ test/unitTests/agent/aksLoader.tests.ts | 107 ++++++++++++++++++++++++ 2 files changed, 130 insertions(+) diff --git a/src/agent/aksLoader.ts b/src/agent/aksLoader.ts index 642328900..8b96ae2b9 100644 --- a/src/agent/aksLoader.ts +++ b/src/agent/aksLoader.ts @@ -52,4 +52,27 @@ export class AKSLoader extends AgentLoader { ); } } + + public initialize() { + // For AKS auto attach scenario, temporarily store and remove OTEL_TRACES_EXPORTER and OTEL_LOGS_EXPORTER + // to ensure they don't interfere with useAzureMonitor setup + const originalTracesExporter = process.env.OTEL_TRACES_EXPORTER; + const originalLogsExporter = process.env.OTEL_LOGS_EXPORTER; + + delete process.env.OTEL_TRACES_EXPORTER; + delete process.env.OTEL_LOGS_EXPORTER; + + try { + // Call parent initialize method + super.initialize(); + } finally { + // Restore the original environment variables after useAzureMonitor is complete + if (originalTracesExporter !== undefined) { + process.env.OTEL_TRACES_EXPORTER = originalTracesExporter; + } + if (originalLogsExporter !== undefined) { + process.env.OTEL_LOGS_EXPORTER = originalLogsExporter; + } + } + } } diff --git a/test/unitTests/agent/aksLoader.tests.ts b/test/unitTests/agent/aksLoader.tests.ts index d3786c96f..599427e71 100644 --- a/test/unitTests/agent/aksLoader.tests.ts +++ b/test/unitTests/agent/aksLoader.tests.ts @@ -60,4 +60,111 @@ describe("agent/AKSLoader", () => { let loggerProvider = logs.getLoggerProvider() as any; assert.equal(loggerProvider.constructor.name, "LoggerProvider"); }); + + describe("OTEL environment variable handling", () => { + it("should remove OTEL_TRACES_EXPORTER and OTEL_LOGS_EXPORTER during initialization", () => { + const env = { + ["APPLICATIONINSIGHTS_CONNECTION_STRING"]: "InstrumentationKey=1aa11111-bbbb-1ccc-8ddd-eeeeffff3333", + ["OTEL_TRACES_EXPORTER"]: "jaeger", + ["OTEL_LOGS_EXPORTER"]: "console" + }; + process.env = env; + + const agent = new AKSLoader(); + + // Spy on the parent initialize method to check environment during call + let envDuringParentCall: { traces?: string; logs?: string } = {}; + const originalInitialize = Object.getPrototypeOf(Object.getPrototypeOf(agent)).initialize; + const parentInitializeSpy = sandbox.stub(Object.getPrototypeOf(Object.getPrototypeOf(agent)), 'initialize').callsFake(function() { + envDuringParentCall.traces = process.env.OTEL_TRACES_EXPORTER; + envDuringParentCall.logs = process.env.OTEL_LOGS_EXPORTER; + return originalInitialize.call(this); + }); + + agent.initialize(); + + // Verify that environment variables were undefined during parent initialize call + assert.strictEqual(envDuringParentCall.traces, undefined, "OTEL_TRACES_EXPORTER should be undefined during parent initialize"); + assert.strictEqual(envDuringParentCall.logs, undefined, "OTEL_LOGS_EXPORTER should be undefined during parent initialize"); + + // Verify parent initialize was called + assert.ok(parentInitializeSpy.calledOnce, "Parent initialize should be called once"); + + parentInitializeSpy.restore(); + }); + + it("should restore OTEL_TRACES_EXPORTER and OTEL_LOGS_EXPORTER after initialization", () => { + const env = { + ["APPLICATIONINSIGHTS_CONNECTION_STRING"]: "InstrumentationKey=1aa11111-bbbb-1ccc-8ddd-eeeeffff3333", + ["OTEL_TRACES_EXPORTER"]: "jaeger", + ["OTEL_LOGS_EXPORTER"]: "console" + }; + process.env = env; + + const agent = new AKSLoader(); + agent.initialize(); + + // Verify environment variables are restored after initialization + assert.strictEqual(process.env.OTEL_TRACES_EXPORTER, "jaeger", "OTEL_TRACES_EXPORTER should be restored"); + assert.strictEqual(process.env.OTEL_LOGS_EXPORTER, "console", "OTEL_LOGS_EXPORTER should be restored"); + }); + + it("should handle cases where OTEL environment variables are not set", () => { + const env = { + ["APPLICATIONINSIGHTS_CONNECTION_STRING"]: "InstrumentationKey=1aa11111-bbbb-1ccc-8ddd-eeeeffff3333" + // OTEL variables intentionally not set + }; + process.env = env; + + const agent = new AKSLoader(); + agent.initialize(); + + // Verify that undefined variables remain undefined + assert.strictEqual(process.env.OTEL_TRACES_EXPORTER, undefined, "OTEL_TRACES_EXPORTER should remain undefined"); + assert.strictEqual(process.env.OTEL_LOGS_EXPORTER, undefined, "OTEL_LOGS_EXPORTER should remain undefined"); + }); + + it("should restore environment variables even if parent initialize throws", () => { + const env = { + ["APPLICATIONINSIGHTS_CONNECTION_STRING"]: "InstrumentationKey=1aa11111-bbbb-1ccc-8ddd-eeeeffff3333", + ["OTEL_TRACES_EXPORTER"]: "jaeger", + ["OTEL_LOGS_EXPORTER"]: "console" + }; + process.env = env; + + const agent = new AKSLoader(); + + // Stub parent initialize to throw an error + const parentInitializeStub = sandbox.stub(Object.getPrototypeOf(Object.getPrototypeOf(agent)), 'initialize').throws(new Error("Test error")); + + try { + agent.initialize(); + assert.fail("Expected initialize to throw an error"); + } catch (error: any) { + assert.strictEqual(error.message, "Test error", "Should propagate the error from parent initialize"); + } + + // Verify environment variables are still restored despite the error + assert.strictEqual(process.env.OTEL_TRACES_EXPORTER, "jaeger", "OTEL_TRACES_EXPORTER should be restored even after error"); + assert.strictEqual(process.env.OTEL_LOGS_EXPORTER, "console", "OTEL_LOGS_EXPORTER should be restored even after error"); + + parentInitializeStub.restore(); + }); + + it("should handle partial environment variable sets correctly", () => { + const env = { + ["APPLICATIONINSIGHTS_CONNECTION_STRING"]: "InstrumentationKey=1aa11111-bbbb-1ccc-8ddd-eeeeffff3333", + ["OTEL_TRACES_EXPORTER"]: "jaeger" + // OTEL_LOGS_EXPORTER intentionally not set + }; + process.env = env; + + const agent = new AKSLoader(); + agent.initialize(); + + // Verify that only the set variable is restored + assert.strictEqual(process.env.OTEL_TRACES_EXPORTER, "jaeger", "OTEL_TRACES_EXPORTER should be restored"); + assert.strictEqual(process.env.OTEL_LOGS_EXPORTER, undefined, "OTEL_LOGS_EXPORTER should remain undefined"); + }); + }); });