-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from peternewnham/fix-span-parent-ids
fix: spans not taking the parentId of their parent
- Loading branch information
Showing
2 changed files
with
70 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |