@@ -190,6 +190,44 @@ export class SpanBuilder {
190190 }
191191}
192192
193+ // ─── No-op Span Builder ────────────────────────────────────────────────────────
194+
195+ const NOOP_SPAN : Span = {
196+ traceId : '0' . repeat ( 32 ) ,
197+ spanId : '0' . repeat ( 16 ) ,
198+ name : 'noop' ,
199+ kind : 'internal' ,
200+ startTime : 0 ,
201+ endTime : 0 ,
202+ duration : 0 ,
203+ status : 'unset' ,
204+ attributes : { } ,
205+ events : [ ] ,
206+ links : [ ] ,
207+ resource : { } ,
208+ } ;
209+
210+ /**
211+ * No-op SpanBuilder — all methods are safe to call but do nothing.
212+ * Used when sampling is disabled to avoid null-checks in caller code.
213+ */
214+ export class NoopSpanBuilder extends SpanBuilder {
215+ constructor ( ) {
216+ super ( 'noop' , { } , ( ) => { } ) ;
217+ }
218+
219+ override setKind ( ) : this { return this ; }
220+ override setAttribute ( ) : this { return this ; }
221+ override setAttributes ( ) : this { return this ; }
222+ override addEvent ( ) : this { return this ; }
223+ override addLink ( ) : this { return this ; }
224+ override getContext ( ) : SpanContext { return { traceId : '0' . repeat ( 32 ) , spanId : '0' . repeat ( 16 ) , traceFlags : 0 } ; }
225+ override end ( ) : Span { return NOOP_SPAN ; }
226+ }
227+
228+ /** Singleton no-op span builder */
229+ export const NOOP_SPAN_BUILDER = new NoopSpanBuilder ( ) ;
230+
193231// ─── Span Manager ──────────────────────────────────────────────────────────────
194232
195233/**
@@ -199,7 +237,7 @@ export class SpanManager {
199237 private buffer : Span [ ] = [ ] ;
200238 private exporter ?: SpanExporter ;
201239 private resource : SpanAttributes ;
202- private config : Required < Pick < TelemetryConfig , 'samplingRate' | 'maxBufferSize' | 'maxAttributes' | 'maxEvents' > > ;
240+ private config : Required < Pick < TelemetryConfig , 'samplingRate' | 'maxBufferSize' | 'maxAttributes' | 'maxEvents' > > & { batchSize : number } ;
203241 private exportTimer ?: ReturnType < typeof setInterval > ;
204242 private totalSpansCreated = 0 ;
205243 private totalSpansExported = 0 ;
@@ -212,6 +250,7 @@ export class SpanManager {
212250 maxBufferSize : config . maxBufferSize ?? 2048 ,
213251 maxAttributes : config . maxAttributes ?? 128 ,
214252 maxEvents : config . maxEvents ?? 128 ,
253+ batchSize : config . exporter ?. batchSize ?? 512 ,
215254 } ;
216255
217256 this . resource = {
@@ -275,8 +314,7 @@ export class SpanManager {
275314 this . buffer . push ( span ) ;
276315
277316 // Auto-flush when batch size is reached
278- const batchSize = 512 ;
279- if ( this . buffer . length >= batchSize && this . exporter ) {
317+ if ( this . buffer . length >= this . config . batchSize && this . exporter ) {
280318 this . flush ( ) . catch ( ( ) => { /* swallow */ } ) ;
281319 }
282320 }
0 commit comments