统一 Node 22 与后端安全运行基线(07-15-production-trust-closeout-cleanup)
这个提交包含在:
@@ -0,0 +1,90 @@
|
||||
const { initTracing, shutdownTracing } = require("../../../src/observability/tracing");
|
||||
|
||||
function buildDependencies({ constructError = null } = {}) {
|
||||
const instances = [];
|
||||
const resourceFromAttributes = vi.fn((attributes) => ({ attributes }));
|
||||
const getNodeAutoInstrumentations = vi.fn((options) => ({ options }));
|
||||
const logger = { log: vi.fn(), error: vi.fn() };
|
||||
|
||||
class TestNodeSDK {
|
||||
constructor(options) {
|
||||
if (constructError) {
|
||||
throw constructError;
|
||||
}
|
||||
this.options = options;
|
||||
this.start = vi.fn();
|
||||
this.shutdown = vi.fn(async () => {});
|
||||
instances.push(this);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
overrides: {
|
||||
NodeSDK: TestNodeSDK,
|
||||
ConsoleSpanExporter: class TestConsoleSpanExporter {},
|
||||
resourceFromAttributes,
|
||||
getNodeAutoInstrumentations,
|
||||
logger,
|
||||
},
|
||||
instances,
|
||||
resourceFromAttributes,
|
||||
getNodeAutoInstrumentations,
|
||||
logger,
|
||||
};
|
||||
}
|
||||
|
||||
describe("observability tracing", () => {
|
||||
afterEach(async () => {
|
||||
await shutdownTracing();
|
||||
});
|
||||
|
||||
it("does not initialize the SDK when traces are disabled", () => {
|
||||
const dependencies = buildDependencies();
|
||||
|
||||
expect(initTracing({ otelTracesEnabled: false }, dependencies.overrides)).toBeNull();
|
||||
expect(dependencies.instances).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("initializes the SDK with the service resource and filesystem instrumentation disabled", () => {
|
||||
const dependencies = buildDependencies();
|
||||
|
||||
const result = initTracing({ otelTracesEnabled: true }, dependencies.overrides);
|
||||
|
||||
expect(result).toBe(dependencies.instances[0]);
|
||||
expect(dependencies.resourceFromAttributes).toHaveBeenCalledWith({ "service.name": "hyb-oa-server" });
|
||||
expect(dependencies.getNodeAutoInstrumentations).toHaveBeenCalledWith({
|
||||
"@opentelemetry/instrumentation-fs": { enabled: false },
|
||||
});
|
||||
expect(result.start).toHaveBeenCalledOnce();
|
||||
expect(dependencies.logger.log).toHaveBeenCalledWith("OTel tracing initialized");
|
||||
});
|
||||
|
||||
it("supports a distinct worker service name", () => {
|
||||
const dependencies = buildDependencies();
|
||||
|
||||
initTracing({ otelTracesEnabled: true }, {
|
||||
...dependencies.overrides,
|
||||
serviceName: "hyb-oa-worker",
|
||||
});
|
||||
|
||||
expect(dependencies.resourceFromAttributes).toHaveBeenCalledWith({ "service.name": "hyb-oa-worker" });
|
||||
});
|
||||
|
||||
it("contains initialization failures and leaves tracing disabled", () => {
|
||||
const error = new Error("otel_init_failed");
|
||||
const dependencies = buildDependencies({ constructError: error });
|
||||
|
||||
expect(initTracing({ otelTracesEnabled: true }, dependencies.overrides)).toBeNull();
|
||||
expect(dependencies.logger.error).toHaveBeenCalledWith("Error initializing OTel tracing", error);
|
||||
});
|
||||
|
||||
it("shuts down an initialized SDK once", async () => {
|
||||
const dependencies = buildDependencies();
|
||||
const instance = initTracing({ otelTracesEnabled: true }, dependencies.overrides);
|
||||
|
||||
await shutdownTracing();
|
||||
await shutdownTracing();
|
||||
|
||||
expect(instance.shutdown).toHaveBeenCalledOnce();
|
||||
});
|
||||
});
|
||||
在新工单中引用
屏蔽一个用户