@@ -16,9 +16,11 @@ import { LoggerProvider } from "@opentelemetry/sdk-logs";
1616describe ( "agent/agentLoader" , ( ) => {
1717 let originalEnv : NodeJS . ProcessEnv ;
1818 let sandbox : sinon . SinonSandbox ;
19+ let clock : sinon . SinonFakeTimers ;
1920 let originalOtelGlobalV1 : any ;
2021 let originalOtelGlobalV2 : any ;
2122 let originalLogsGlobal : any ;
23+ const OTEL_DETECTION_DELAY_MS = 60000 ; // 1 minute - matches the constant in agentLoader.ts
2224
2325 const defaultConfig = {
2426 azureMonitorExporterOptions : {
@@ -59,6 +61,7 @@ describe("agent/agentLoader", () => {
5961
6062 beforeEach ( ( ) => {
6163 originalEnv = process . env ;
64+ clock = sandbox . useFakeTimers ( ) ;
6265 const otelSymbolV1 = Symbol . for ( 'opentelemetry.js.api.1' ) ;
6366 const otelSymbolV2 = Symbol . for ( 'opentelemetry.js.api.2' ) ;
6467 const logsSymbol = Symbol . for ( 'io.opentelemetry.js.api.logs' ) ;
@@ -69,6 +72,7 @@ describe("agent/agentLoader", () => {
6972
7073 afterEach ( ( ) => {
7174 process . env = originalEnv ;
75+ clock . restore ( ) ;
7276 sandbox . restore ( ) ;
7377 const otelSymbolV1 = Symbol . for ( 'opentelemetry.js.api.1' ) ;
7478 const otelSymbolV2 = Symbol . for ( 'opentelemetry.js.api.2' ) ;
@@ -231,6 +235,39 @@ describe("agent/agentLoader", () => {
231235 assert . ok ( ( logged . message as string ) . includes ( "MeterProvider" ) ) ;
232236 } ) ;
233237
238+ it ( "should delay OpenTelemetry detection by 1 minute after initialize" , ( ) => {
239+ const env = {
240+ [ "APPLICATIONINSIGHTS_CONNECTION_STRING" ] : "InstrumentationKey=1aa11111-bbbb-1ccc-8ddd-eeeeffff3333" ,
241+ } ;
242+ process . env = env ;
243+ const tracerProvider = new BasicTracerProvider ( ) ;
244+ trace . setGlobalTracerProvider ( tracerProvider ) ;
245+
246+ const agent = new AgentLoader ( ) ;
247+ const detectGlobalsSpy = sandbox . spy ( agent as any , "_detectOpenTelemetryGlobals" ) ;
248+ const diagnosticLoggerStub = sandbox . stub ( agent [ "_diagnosticLogger" ] , "logMessage" ) ;
249+ sandbox . stub ( agent [ "_statusLogger" ] , "logStatus" ) ;
250+
251+ agent . initialize ( ) ;
252+
253+ // Detection should not have been called yet
254+ assert . strictEqual ( detectGlobalsSpy . callCount , 0 ) ;
255+ // Only the initialization success message should be logged
256+ assert . ok ( diagnosticLoggerStub . calledOnce ) ;
257+ assert . strictEqual ( diagnosticLoggerStub . args [ 0 ] [ 0 ] . messageId , DiagnosticMessageId . attachSuccessful ) ;
258+
259+ // Advance time by less than the delay
260+ clock . tick ( OTEL_DETECTION_DELAY_MS - 1 ) ;
261+ assert . strictEqual ( detectGlobalsSpy . callCount , 0 ) ;
262+
263+ // Advance time to trigger the detection
264+ clock . tick ( 1 ) ;
265+ assert . strictEqual ( detectGlobalsSpy . callCount , 1 ) ;
266+ // Now the conflict message should be logged
267+ assert . strictEqual ( diagnosticLoggerStub . callCount , 2 ) ;
268+ assert . strictEqual ( diagnosticLoggerStub . args [ 1 ] [ 0 ] . messageId , DiagnosticMessageId . openTelemetryConflict ) ;
269+ } ) ;
270+
234271 it ( "should log detected OpenTelemetry logger provider via logs symbol getter" , ( ) => {
235272 const env = {
236273 [ "APPLICATIONINSIGHTS_CONNECTION_STRING" ] : "InstrumentationKey=1aa11111-bbbb-1ccc-8ddd-eeeeffff3333" ,
0 commit comments