nestjs-otel
Version:
NestJS OpenTelemetry Library
400 lines (399 loc) • 16.8 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";
}
async asyncError() {
throw new Error("async hello world");
}
lastLazyThenable;
returnsLazyThenable() {
this.lastLazyThenable = makeLazyThenable("lazy result");
return this.lastLazyThenable;
}
returnsFailingLazyThenable() {
return makeFailingLazyThenable(new Error("lazy rejection"));
}
lazyThenableOnResult() {
return makeLazyThenable({ rows: [1, 2, 3] });
}
}
__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);
__decorate([
(0, span_1.Span)(),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", Promise)
], TestSpan.prototype, "asyncError", null);
__decorate([
(0, span_1.Span)(),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], TestSpan.prototype, "returnsLazyThenable", null);
__decorate([
(0, span_1.Span)(),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], TestSpan.prototype, "returnsFailingLazyThenable", null);
__decorate([
(0, span_1.Span)({
onResult: (_result) => ({ attributes: { count: _result.rows.length } }),
}),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], TestSpan.prototype, "lazyThenableOnResult", null);
function makeLazyThenable(value) {
const thenable = {
triggered: false,
// biome-ignore lint/suspicious/noThenProperty: <We are testing the behavior of the thenable>
then(onFulfilled, onRejected) {
thenable.triggered = true;
try {
const result = onFulfilled ? onFulfilled(value) : value;
return Promise.resolve(result);
}
catch (error) {
if (onRejected) {
return Promise.resolve(onRejected(error));
}
return Promise.reject(error);
}
},
catch(onRejected) {
return thenable.then(undefined, onRejected);
},
};
return thenable;
}
function makeFailingLazyThenable(error) {
const thenable = {
triggered: false,
// biome-ignore lint/suspicious/noThenProperty: <We are testing the behavior of the thenable>
then(_onFulfilled, onRejected) {
thenable.triggered = true;
if (onRejected) {
return Promise.resolve(onRejected(error));
}
return Promise.reject(error);
},
catch(onRejected) {
return thenable.then(undefined, onRejected);
},
};
return thenable;
}
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");
});
it("should call onResult with the thenable (not resolved value) for lazy thenables", () => {
// The decorator hands the lazy thenable to onResult untouched (it cannot
// know it is a deferred query). Accessing fields on the resolved shape
// (e.g. `.rows`) therefore throws synchronously, and the decorator records
// that as an exception on the span.
const returned = instance.lazyThenableOnResult();
// Caller still receives the untouched thenable.
expect(returned.triggered).toBe(false);
const spans = traceExporter.getFinishedSpans();
expect(spans).toHaveLength(1);
expect(spans[0].status.code).toBe(api_1.SpanStatusCode.ERROR);
expect(spans[0].status.message).toMatch("Cannot read properties of undefined (reading 'length')");
expect(spans[0].events).toHaveLength(1);
expect(spans[0].events[0].name).toBe("exception");
expect(spans[0].events[0].attributes?.["exception.type"]).toBe("TypeError");
});
it("should not trigger a lazy thenable returned by the wrapped method", () => {
const returned = instance.returnsLazyThenable();
// The decorator must hand the lazy thenable back to the caller untouched.
// It must NOT subscribe via .then(), which would force-execute query
// builders such as Knex, Mongoose Query, or Drizzle queries that the
// caller intended to defer (or compose further) before awaiting.
expect(instance.lastLazyThenable?.triggered).toBe(false);
expect(returned).toBe(instance.lastLazyThenable);
});
it("should end the span synchronously before the caller awaits a lazy thenable", () => {
instance.returnsLazyThenable();
// The span closes as soon as the decorated method returns, before the
// caller subscribes to the thenable, because the decorator cannot know
// whether the caller will compose it further or await it at all.
const spans = traceExporter.getFinishedSpans();
expect(spans).toHaveLength(1);
expect(spans[0].name).toBe("TestSpan.returnsLazyThenable");
});
it("should not record errors from a lazy thenable that rejects after the span has ended", async () => {
const returned = instance.returnsFailingLazyThenable();
// Span is already closed — no error recorded yet.
const spans = traceExporter.getFinishedSpans();
expect(spans).toHaveLength(1);
expect(spans[0].status.code).toBe(api_1.SpanStatusCode.UNSET);
expect(spans[0].events).toHaveLength(0);
// Caller awaits the thenable now — it rejects — but the span is gone.
await expect(Promise.resolve(returned)).rejects.toThrow("lazy rejection");
// Known limitation: the rejection is invisible to the span.
expect(traceExporter.getFinishedSpans()[0].events).toHaveLength(0);
});
it("should still track results from async (real Promise) methods", async () => {
const result = await instance.asyncMethod();
expect(result).toBe("async success");
const spans = traceExporter.getFinishedSpans();
expect(spans).toHaveLength(1);
expect(spans[0].name).toBe("TestSpan.asyncMethod");
expect(spans[0].status.code).toBe(api_1.SpanStatusCode.UNSET);
expect(spans[0].attributes).toEqual({ result: "async success" });
});
it("should still track errors thrown by async (real Promise) methods", async () => {
await expect(instance.asyncError()).rejects.toThrow("async hello world");
const spans = traceExporter.getFinishedSpans();
expect(spans).toHaveLength(1);
expect(spans[0].name).toBe("TestSpan.asyncError");
expect(spans[0].status).toEqual({
code: api_1.SpanStatusCode.ERROR,
message: "async hello world",
});
expect(spans[0].events).toHaveLength(1);
expect(spans[0].events[0].name).toBe("exception");
});
});