Skip to content

fix: handle awaiting non-async functions that return a Promise #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 62 additions & 0 deletions src/decorator.injector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,68 @@ describe('DecoratorInjector', () => {
scopeSpy.mockClear();
});

it('should work with non-async promise function', async () => {
let resolve;

const promise = new Promise<number>((_resolve) => {
resolve = _resolve;
});
// given
@Injectable()
class HelloService {
@Span('hello')
hi() {
return promise;
}
}

const module: TestingModule = await Test.createTestingModule({
imports: [DatadogTraceModule.forRoot()],
providers: [HelloService],
}).compile();

const helloService = module.get<HelloService>(HelloService);
const mockSpan = { finish: jest.fn() as any } as TraceSpan;
const startSpanSpy = jest
.spyOn(tracer, 'startSpan')
.mockReturnValue(mockSpan);
const scope = {
active: jest.fn(() => null) as any,
activate: jest.fn((span: TraceSpan, fn: (...args: any[]) => any): any => {
return fn();
}) as any,
} as Scope;
const scopeSpy = jest
.spyOn(tracer, 'scope')
.mockImplementation(() => scope);

// when
const resultPromise = helloService.hi();
// The span should not be finished before the promise resolves
expect(mockSpan.finish).not.toHaveBeenCalled();
resolve(0);
const result = await resultPromise;

// then
expect(result).toBe(0);
expect(
Reflect.getMetadata(Constants.SPAN_METADATA, HelloService.prototype.hi),
).toBe('hello');
expect(
Reflect.getMetadata(
Constants.SPAN_METADATA_ACTIVE,
HelloService.prototype.hi,
),
).toBe(1);
expect(tracer.startSpan).toHaveBeenCalledWith('hello', { childOf: null });
expect(tracer.scope().active).toHaveBeenCalled();
expect(tracer.scope().activate).toHaveBeenCalled();
expect(mockSpan.finish).toHaveBeenCalled();

startSpanSpy.mockClear();
scopeSpy.mockClear();
});

it('should record exception with sync function', async () => {
// given
@Injectable()
Expand Down
11 changes: 10 additions & 1 deletion src/decorator.injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,19 @@ export class DecoratorInjector implements Injector {
} else {
try {
const result = prototype.apply(this, args);
// This handles the case where a function isn't async but returns a Promise
if (result && typeof result.then === 'function') {
return result
.catch((error) => {
DecoratorInjector.recordException(error, span);
})
.finally(() => span.finish());
}

span.finish();
return result;
} catch (error) {
DecoratorInjector.recordException(error, span);
} finally {
span.finish();
}
}
Expand Down