Skip to content

Commit e11c3b6

Browse files
committed
fix: added local testing behavior
1 parent f0c3161 commit e11c3b6

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

packages/tracing/src/Tracer.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ class Tracer implements TracerInterface {
128128
private tracingEnabled: boolean = true;
129129

130130
public constructor(options: TracerOptions = {}) {
131-
this.setOptions(options);
132131
this.provider = new ProviderService();
132+
this.setOptions(options);
133133
}
134134

135135
/**
@@ -458,7 +458,10 @@ class Tracer implements TracerInterface {
458458
* @returns segment - The active segment or subsegment in the current scope.
459459
*/
460460
public getSegment(): Segment | Subsegment {
461-
const segment = this.provider.getSegment();
461+
let segment = this.provider.getSegment();
462+
if (segment === undefined && this.isTracingEnabled() === false) {
463+
segment = new Subsegment('## Dummy segment');
464+
}
462465
if (segment === undefined) {
463466
throw new Error('Failed to get the current sub/segment from the context.');
464467
}
@@ -567,6 +570,8 @@ class Tracer implements TracerInterface {
567570
* @param segment - Subsegment to set as the current segment
568571
*/
569572
public setSegment(segment: Segment | Subsegment): void {
573+
if (this.isTracingEnabled() === false) return;
574+
570575
return this.provider.setSegment(segment);
571576
}
572577

@@ -730,26 +735,30 @@ class Tracer implements TracerInterface {
730735
private setTracingEnabled(enabled?: boolean): void {
731736
if (enabled !== undefined && enabled === false) {
732737
this.tracingEnabled = enabled;
738+
this.provider.setContextMissingStrategy(() => undefined);
733739

734740
return;
735741
}
736742

737743
const customConfigValue = this.getCustomConfigService()?.getTracingEnabled();
738744
if (customConfigValue !== undefined && customConfigValue.toLowerCase() === 'false') {
739745
this.tracingEnabled = false;
746+
this.provider.setContextMissingStrategy(() => undefined);
740747

741748
return;
742749
}
743750

744751
const envVarsValue = this.getEnvVarsService()?.getTracingEnabled();
745752
if (envVarsValue.toLowerCase() === 'false') {
746753
this.tracingEnabled = false;
754+
this.provider.setContextMissingStrategy(() => undefined);
747755

748756
return;
749757
}
750758

751759
if (this.isLambdaSamCli() || this.isLambdaExecutionEnv() === false) {
752760
this.tracingEnabled = false;
761+
this.provider.setContextMissingStrategy(() => undefined);
753762
}
754763
}
755764

packages/tracing/tests/unit/Tracer.test.ts

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ describe('Class: Tracer', () => {
274274

275275
describe('Method: getSegment', () => {
276276

277-
test('when called outside of a namespace or without parent segment, it throws an error', () => {
277+
test('when called outside of a namespace or without parent segment, and Tracer is active, it throws an error', () => {
278278

279279
// Prepare
280280
const tracer: Tracer = new Tracer();
@@ -286,7 +286,7 @@ describe('Class: Tracer', () => {
286286

287287
});
288288

289-
test('when called outside of a namespace or without parent segment, it throws an error', () => {
289+
test('when called and no segment is returned, while Tracer is active, it throws an error', () => {
290290

291291
// Prepare
292292
const tracer: Tracer = new Tracer();
@@ -298,7 +298,22 @@ describe('Class: Tracer', () => {
298298
}).toThrow('Failed to get the current sub/segment from the context.');
299299

300300
});
301-
301+
302+
test('when called outside of a namespace or without parent segment, and Tracer is NOT active, it returns a dummy subsegment', () => {
303+
304+
// Prepare
305+
delete process.env.AWS_EXECUTION_ENV; // This will disable the tracer, simulating local execution
306+
const tracer: Tracer = new Tracer();
307+
308+
// Act
309+
const segment = tracer.getSegment();
310+
311+
// Assess
312+
expect(segment).toBeInstanceOf(Subsegment);
313+
expect(segment.name).toBe('## Dummy segment');
314+
315+
});
316+
302317
test('when called within a namespace, it returns the parent segment', () => {
303318

304319
// Prepare
@@ -320,7 +335,7 @@ describe('Class: Tracer', () => {
320335
});
321336

322337
describe('Method: setSegment', () => {
323-
test('when called outside of a namespace or without parent segment, it throws an error', () => {
338+
test('when called outside of a namespace or without parent segment, and Tracer is enabled, it throws an error', () => {
324339

325340
// Prepare
326341
const tracer: Tracer = new Tracer();
@@ -332,6 +347,22 @@ describe('Class: Tracer', () => {
332347
}).toThrow('No context available. ns.run() or ns.bind() must be called first.');
333348
});
334349

350+
test('when called outside of a namespace or without parent segment, and Tracer is NOT enabled, it does nothing', () => {
351+
352+
// Prepare
353+
delete process.env.AWS_EXECUTION_ENV; // This will disable the tracer, simulating local execution
354+
const tracer: Tracer = new Tracer();
355+
const setSegmentSpy = jest.spyOn(tracer.provider, 'setSegment');
356+
357+
// Act
358+
const newSubsegment = new Subsegment('## foo.bar');
359+
tracer.setSegment(newSubsegment);
360+
361+
// Assess
362+
expect(setSegmentSpy).toBeCalledTimes(0);
363+
364+
});
365+
335366
test('when called within a namespace, it sets the segment', () => {
336367

337368
// Prepare

0 commit comments

Comments
 (0)