nestjs-otel
Version:
NestJS OpenTelemetry Library
107 lines (106 loc) • 4.64 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
require("reflect-metadata");
const common_1 = require("@nestjs/common");
const api_1 = require("@opentelemetry/api");
const sdk_trace_node_1 = require("@opentelemetry/sdk-trace-node");
const traceable_1 = require("./traceable");
const TestDecoratorThatSetsMetadata = () => (0, common_1.SetMetadata)("some-metadata", true);
let TestTraceable = class TestTraceable {
methodOne() { }
methodTwo() {
return this.methodOne();
}
methodWithMetadata() { }
async asyncMethod() {
return new Promise((resolve) => setTimeout(resolve, 10));
}
};
__decorate([
TestDecoratorThatSetsMetadata(),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], TestTraceable.prototype, "methodWithMetadata", null);
TestTraceable = __decorate([
(0, traceable_1.Traceable)()
], TestTraceable);
let TestTraceableWithOptions = class TestTraceableWithOptions {
methodOne() { }
};
TestTraceableWithOptions = __decorate([
(0, traceable_1.Traceable)({ kind: api_1.SpanKind.PRODUCER })
], TestTraceableWithOptions);
describe("Traceable", () => {
let instance;
let instanceWithOptions;
let traceExporter;
let spanProcessor;
let provider;
beforeAll(async () => {
instance = new TestTraceable();
instanceWithOptions = new TestTraceableWithOptions();
traceExporter = new sdk_trace_node_1.InMemorySpanExporter();
spanProcessor = new sdk_trace_node_1.SimpleSpanProcessor(traceExporter);
provider = new sdk_trace_node_1.NodeTracerProvider({
spanProcessors: [spanProcessor],
});
provider.register();
});
afterEach(async () => {
spanProcessor.forceFlush();
traceExporter.reset();
});
afterAll(async () => {
await provider.shutdown();
});
it("should trace all methods in the class", async () => {
instance.methodOne();
const spans = traceExporter.getFinishedSpans();
expect(spans).toHaveLength(1);
expect(spans[0].name).toEqual("TestTraceable.methodOne");
});
it("should trace nested calls within the class", async () => {
instance.methodTwo();
const spans = traceExporter.getFinishedSpans();
expect(spans).toHaveLength(2);
// methodTwo calls methodOne, so we expect both spans.
// Since it's synchronous, methodOne finishes first (nested), then methodTwo.
// But wait, startActiveSpan starts a span.
// methodTwo starts span -> calls methodOne -> starts span -> methodOne ends -> span ends -> methodTwo ends -> span ends.
// getFinishedSpans returns finished spans.
// So methodOne span should be first, then methodTwo span.
expect(spans.map((s) => s.name)).toEqual([
"TestTraceable.methodOne",
"TestTraceable.methodTwo",
]);
});
it("should maintain reflect metadata", async () => {
expect(Reflect.getMetadata("some-metadata", instance.methodWithMetadata)).toEqual(true);
});
it("should apply options to all methods", async () => {
instanceWithOptions.methodOne();
const spans = traceExporter.getFinishedSpans();
expect(spans).toHaveLength(1);
expect(spans[0].name).toEqual("TestTraceableWithOptions.methodOne");
expect(spans[0].kind).toEqual(api_1.SpanKind.PRODUCER);
});
it("should preserve the original method name", () => {
expect(instance.methodOne.name).toEqual("methodOne");
});
it("should trace async methods", async () => {
await instance.asyncMethod();
const spans = traceExporter.getFinishedSpans();
expect(spans).toHaveLength(1);
expect(spans[0].name).toEqual("TestTraceable.asyncMethod");
});
});