@@ -169,6 +169,12 @@ describe("shim/TelemetryClient", () => {
169169 } ) ;
170170
171171 describe ( "#manual track APIs" , ( ) => {
172+ // Allow rate limiter to recover between tests so each span-creating test
173+ // gets a fresh budget from the default RateLimitedSampler (5 traces/sec)
174+ beforeEach ( async ( ) => {
175+ await new Promise ( resolve => setTimeout ( resolve , 250 ) ) ;
176+ } ) ;
177+
172178 it ( "trackDependency http" , async ( ) => {
173179 const telemetry : DependencyTelemetry = {
174180 name : "TestName" ,
@@ -565,6 +571,35 @@ describe("shim/TelemetryClient", () => {
565571 } ) ;
566572 } ) ;
567573
574+ describe ( "#rate limiting behavior" , ( ) => {
575+ it ( "should rate limit spans when creating many rapidly" , async ( ) => {
576+ // Wait for rate limiter to fully recover
577+ await new Promise ( resolve => setTimeout ( resolve , 500 ) ) ;
578+ testProcessor . spansProcessed = [ ] ;
579+
580+ const totalSpans = 20 ;
581+ // Create many spans as fast as possible (far exceeding 5/sec default)
582+ for ( let i = 0 ; i < totalSpans ; i ++ ) {
583+ client . trackDependency ( {
584+ name : `RateLimitTest-${ i } ` ,
585+ duration : 100 ,
586+ resultCode : "200" ,
587+ data : "http://test.com" ,
588+ dependencyTypeName : "HTTP" ,
589+ success : true ,
590+ } ) ;
591+ }
592+ await tracerProvider . forceFlush ( ) ;
593+ const sampledCount = testProcessor . spansProcessed . length ;
594+
595+ // Rate limiter should allow some but not all spans through
596+ assert . ok ( sampledCount >= 1 ,
597+ `Expected at least 1 span to be sampled, got ${ sampledCount } ` ) ;
598+ assert . ok ( sampledCount < totalSpans ,
599+ `Expected rate limiting to drop some spans when creating ${ totalSpans } rapidly, but all ${ sampledCount } were sampled` ) ;
600+ } ) ;
601+ } ) ;
602+
568603 describe ( "initialization modes" , ( ) => {
569604 it ( "does not call useAzureMonitor for isolated clients" , async ( ) => {
570605 const useAzureMonitorStub = sandbox . stub ( main , "useAzureMonitor" ) ;
0 commit comments