Skip to content

Commit

Permalink
fix: added local testing behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamorosi committed Jan 7, 2022
1 parent f0c3161 commit e11c3b6
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
13 changes: 11 additions & 2 deletions packages/tracing/src/Tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ class Tracer implements TracerInterface {
private tracingEnabled: boolean = true;

public constructor(options: TracerOptions = {}) {
this.setOptions(options);
this.provider = new ProviderService();
this.setOptions(options);
}

/**
Expand Down Expand Up @@ -458,7 +458,10 @@ class Tracer implements TracerInterface {
* @returns segment - The active segment or subsegment in the current scope.
*/
public getSegment(): Segment | Subsegment {
const segment = this.provider.getSegment();
let segment = this.provider.getSegment();
if (segment === undefined && this.isTracingEnabled() === false) {
segment = new Subsegment('## Dummy segment');
}
if (segment === undefined) {
throw new Error('Failed to get the current sub/segment from the context.');
}
Expand Down Expand Up @@ -567,6 +570,8 @@ class Tracer implements TracerInterface {
* @param segment - Subsegment to set as the current segment
*/
public setSegment(segment: Segment | Subsegment): void {
if (this.isTracingEnabled() === false) return;

return this.provider.setSegment(segment);
}

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

return;
}

const customConfigValue = this.getCustomConfigService()?.getTracingEnabled();
if (customConfigValue !== undefined && customConfigValue.toLowerCase() === 'false') {
this.tracingEnabled = false;
this.provider.setContextMissingStrategy(() => undefined);

return;
}

const envVarsValue = this.getEnvVarsService()?.getTracingEnabled();
if (envVarsValue.toLowerCase() === 'false') {
this.tracingEnabled = false;
this.provider.setContextMissingStrategy(() => undefined);

return;
}

if (this.isLambdaSamCli() || this.isLambdaExecutionEnv() === false) {
this.tracingEnabled = false;
this.provider.setContextMissingStrategy(() => undefined);
}
}

Expand Down
39 changes: 35 additions & 4 deletions packages/tracing/tests/unit/Tracer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ describe('Class: Tracer', () => {

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

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

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

});

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

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

});


test('when called outside of a namespace or without parent segment, and Tracer is NOT active, it returns a dummy subsegment', () => {

// Prepare
delete process.env.AWS_EXECUTION_ENV; // This will disable the tracer, simulating local execution
const tracer: Tracer = new Tracer();

// Act
const segment = tracer.getSegment();

// Assess
expect(segment).toBeInstanceOf(Subsegment);
expect(segment.name).toBe('## Dummy segment');

});

test('when called within a namespace, it returns the parent segment', () => {

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

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

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

test('when called outside of a namespace or without parent segment, and Tracer is NOT enabled, it does nothing', () => {

// Prepare
delete process.env.AWS_EXECUTION_ENV; // This will disable the tracer, simulating local execution
const tracer: Tracer = new Tracer();
const setSegmentSpy = jest.spyOn(tracer.provider, 'setSegment');

// Act
const newSubsegment = new Subsegment('## foo.bar');
tracer.setSegment(newSubsegment);

// Assess
expect(setSegmentSpy).toBeCalledTimes(0);

});

test('when called within a namespace, it sets the segment', () => {

// Prepare
Expand Down

0 comments on commit e11c3b6

Please sign in to comment.