Skip to content
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

fix: spans not taking the parentId of their parent #33

Merged
merged 1 commit into from
Apr 5, 2022
Merged
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
16 changes: 7 additions & 9 deletions src/Trace/Injectors/BaseTraceInjector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,32 +59,30 @@ export class BaseTraceInjector {
const method = {
[prototype.name]: function (...args: any[]) {
const tracer = trace.getTracer('default');
const currentSpan =
trace.getSpan(context.active()) ?? tracer.startSpan('default');
const currentSpan = tracer.startSpan(traceName);

return context.with(
trace.setSpan(context.active(), currentSpan),
() => {
const span = tracer.startSpan(traceName);
span.setAttributes(attributes);
currentSpan.setAttributes(attributes);
if (prototype.constructor.name === 'AsyncFunction') {
return prototype
.apply(this, args)
.catch((error) =>
BaseTraceInjector.recordException(error, span),
BaseTraceInjector.recordException(error, currentSpan),
)
.finally(() => {
span.end();
currentSpan.end();
});
} else {
try {
const result = prototype.apply(this, args);
span.end();
currentSpan.end();
return result;
} catch (error) {
BaseTraceInjector.recordException(error, span);
BaseTraceInjector.recordException(error, currentSpan);
} finally {
span.end();
currentSpan.end();
}
}
},
Expand Down
63 changes: 63 additions & 0 deletions src/Trace/Tests/BaseTraceInjectorTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Test } from '@nestjs/testing';
import { OpenTelemetryModule } from '../../OpenTelemetryModule';
import { NoopSpanProcessor } from '@opentelemetry/sdk-trace-base';
import { Controller, Get, Injectable } from '@nestjs/common';
import { Span } from '../Decorators/Span';
import * as request from 'supertest';
import { ControllerInjector } from '../Injectors/ControllerInjector';

describe('Base Trace Injector Test', () => {
const exporter = new NoopSpanProcessor();
const exporterSpy = jest.spyOn(exporter, 'onStart');

const sdkModule = OpenTelemetryModule.forRoot({
spanProcessor: exporter,
traceAutoInjectors: [ControllerInjector],
});

beforeEach(() => {
exporterSpy.mockClear();
exporterSpy.mockReset();
});

it('should create spans that inherit the ids of their parents', async () => {
// given
@Injectable()
class HelloService {
@Span()
hello() {
this.helloAgain();
}
@Span()
helloAgain() {} // eslint-disable-line @typescript-eslint/no-empty-function
}

@Controller('hello')
class HelloController {
constructor(private service: HelloService) {}
@Get()
hi() {
return this.service.hello();
}
}

const context = await Test.createTestingModule({
imports: [sdkModule],
providers: [HelloService],
controllers: [HelloController],
}).compile();
const app = context.createNestApplication();
await app.init();

//when
await request(app.getHttpServer()).get('/hello').send().expect(200);

//then
const [[parent], [childOfParent], [childOfChild]] = exporterSpy.mock.calls;
expect(parent.parentSpanId).toBeUndefined();
expect(childOfParent.parentSpanId).toBe(parent.spanContext().spanId);
expect(childOfChild.parentSpanId).toBe(childOfParent.spanContext().spanId);

await app.close();
});
});