nestjs-otel
Version:
NestJS OpenTelemetry Library
253 lines (252 loc) • 10.1 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);
};
var _c;
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 span_1 = require("./span");
const TestDecoratorThatSetsMetadata = () => (0, common_1.SetMetadata)("some-metadata", true);
const symbol = Symbol("testSymbol");
class TestSpan {
singleSpan() { }
doubleSpan() {
return this.singleSpan();
}
fooProducerSpan() { }
argsInOptions(_a, _b) { }
implicitSpanNameWithOptions() { }
argsInOptionsWithImplicitName(_a, _b) { }
error() {
throw new Error("hello world");
}
metadata() { }
[_c = symbol]() { }
syncMethod() {
return "success";
}
async asyncMethod() {
return "async success";
}
errorInOnResult() {
return "success";
}
}
__decorate([
(0, span_1.Span)(),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], TestSpan.prototype, "singleSpan", null);
__decorate([
(0, span_1.Span)(),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], TestSpan.prototype, "doubleSpan", null);
__decorate([
(0, span_1.Span)("foo", { kind: api_1.SpanKind.PRODUCER }),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], TestSpan.prototype, "fooProducerSpan", null);
__decorate([
(0, span_1.Span)("bar", (a, b) => ({ attributes: { a, b } })),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Number, String]),
__metadata("design:returntype", void 0)
], TestSpan.prototype, "argsInOptions", null);
__decorate([
(0, span_1.Span)({ kind: api_1.SpanKind.PRODUCER }),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], TestSpan.prototype, "implicitSpanNameWithOptions", null);
__decorate([
(0, span_1.Span)((a, b) => ({ attributes: { a, b } })),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Number, String]),
__metadata("design:returntype", void 0)
], TestSpan.prototype, "argsInOptionsWithImplicitName", null);
__decorate([
(0, span_1.Span)(),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], TestSpan.prototype, "error", null);
__decorate([
(0, span_1.Span)(),
TestDecoratorThatSetsMetadata(),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], TestSpan.prototype, "metadata", null);
__decorate([
(0, span_1.Span)(),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], TestSpan.prototype, _c, null);
__decorate([
(0, span_1.Span)({
onResult: (result) => ({ attributes: { result } }),
}),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], TestSpan.prototype, "syncMethod", null);
__decorate([
(0, span_1.Span)({
onResult: (result) => ({ attributes: { result } }),
}),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", Promise)
], TestSpan.prototype, "asyncMethod", null);
__decorate([
(0, span_1.Span)({
onResult: (_result) => {
throw new Error("onResult error");
},
}),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], TestSpan.prototype, "errorInOnResult", null);
describe("Span", () => {
let instance;
let traceExporter;
let spanProcessor;
let provider;
beforeAll(async () => {
instance = new TestSpan();
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 maintain reflect metadataa", async () => {
expect(Reflect.getMetadata("some-metadata", instance.metadata)).toEqual(true);
});
it("should preserve the original method name", () => {
const originalFunctionName = instance.singleSpan.name;
expect(originalFunctionName).toEqual("singleSpan");
});
it("should set correct span", async () => {
instance.singleSpan();
const spans = traceExporter.getFinishedSpans();
expect(spans).toHaveLength(1);
expect(spans.map((span) => span.name)).toEqual(["TestSpan.singleSpan"]);
});
it("should set correct span options", async () => {
instance.fooProducerSpan();
const spans = traceExporter.getFinishedSpans();
expect(spans).toHaveLength(1);
expect(spans.map((span) => span.kind)).toEqual([api_1.SpanKind.PRODUCER]);
});
it("should set correct span options with implicit span name", async () => {
instance.implicitSpanNameWithOptions();
const spans = traceExporter.getFinishedSpans();
expect(spans).toHaveLength(1);
expect(spans[0].name).toEqual("TestSpan.implicitSpanNameWithOptions");
expect(spans[0].kind).toEqual(api_1.SpanKind.PRODUCER);
});
it("should set correct span options based on method params", async () => {
instance.argsInOptions(10, "bar");
const spans = traceExporter.getFinishedSpans();
expect(spans).toHaveLength(1);
expect(spans[0].attributes).toEqual({ a: 10, b: "bar" });
});
it("should set correct span options based on method params with implicit span name", async () => {
instance.argsInOptionsWithImplicitName(10, "bar");
const spans = traceExporter.getFinishedSpans();
expect(spans).toHaveLength(1);
expect(spans[0].attributes).toEqual({ a: 10, b: "bar" });
});
it("should set correct span even when calling other method with Span decorator", async () => {
instance.doubleSpan();
const spans = traceExporter.getFinishedSpans();
expect(spans).toHaveLength(2);
expect(spans.map((span) => span.name)).toEqual([
"TestSpan.singleSpan",
"TestSpan.doubleSpan",
]);
});
it("should propagate errors", () => {
expect(instance.error).toThrow("hello world");
});
it("should set setStatus to ERROR and message to error message", async () => {
expect(instance.error).toThrow("hello world");
const spans = traceExporter.getFinishedSpans();
expect(spans).toHaveLength(1);
expect(spans[0].status).toEqual({
code: api_1.SpanStatusCode.ERROR,
message: "hello world",
});
});
it("should set recordException with error", () => {
expect(instance.error).toThrow("hello world");
const spans = traceExporter.getFinishedSpans();
expect(spans).toHaveLength(1);
// Contain one exception event
expect(spans[0].events).toHaveLength(1);
expect(spans[0].events[0]).toEqual({
name: "exception",
attributes: expect.anything(),
droppedAttributesCount: 0,
time: expect.anything(),
});
});
it("should handle symbols", () => {
instance[symbol]();
const spans = traceExporter.getFinishedSpans();
expect(spans).toHaveLength(1);
expect(spans.map((span) => span.name)).toEqual([
"TestSpan.Symbol(testSymbol)",
]);
});
it("should set attributes from onResult in sync method", async () => {
const result = instance.syncMethod();
expect(result).toBe("success");
const spans = traceExporter.getFinishedSpans();
expect(spans).toHaveLength(1);
expect(spans[0].attributes).toEqual({ result: "success" });
});
it("should set attributes from onResult in async method", async () => {
const result = await instance.asyncMethod();
expect(result).toBe("async success");
const spans = traceExporter.getFinishedSpans();
expect(spans).toHaveLength(1);
expect(spans[0].attributes).toEqual({ result: "async success" });
});
it("should record exception if onResult throws", async () => {
const result = instance.errorInOnResult();
expect(result).toBe("success");
const spans = traceExporter.getFinishedSpans();
expect(spans).toHaveLength(1);
// Should have error status
expect(spans[0].status.code).toBe(api_1.SpanStatusCode.ERROR);
expect(spans[0].status.message).toBe("onResult error");
// Should have exception event
expect(spans[0].events).toHaveLength(1);
expect(spans[0].events[0].name).toBe("exception");
});
});