Skip to content

Commit f8cf6cc

Browse files
authored
feat(tracing): enable tracing channels for unstorage (#4226)
1 parent 0bbbb79 commit f8cf6cc

4 files changed

Lines changed: 46 additions & 1 deletion

File tree

src/build/virtual/storage.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,13 @@ export default function storage(nitro: Nitro) {
2424

2525
const driverImports = [...new Set(mounts.map((m) => m.driver))];
2626

27+
const tracingEnabled = !!(
28+
typeof nitro.options.tracingChannel === "object" && nitro.options.tracingChannel?.unstorage
29+
);
30+
2731
return /* js */ `
2832
import { createStorage } from 'unstorage'
33+
${tracingEnabled ? `import { withTracing } from 'unstorage/tracing'` : ""}
2934
import { assets } from '#nitro/virtual/server-assets'
3035
3136
${driverImports.map((i) => genImport(i, genSafeVariableName(i))).join("\n")}
@@ -39,7 +44,7 @@ export function initStorage() {
3944
`storage.mount('${m.path}', ${genSafeVariableName(m.driver)}(${JSON.stringify(m.opts)}))`
4045
)
4146
.join("\n")}
42-
return storage
47+
return ${tracingEnabled ? "withTracing(storage)" : "storage"}
4348
}
4449
`;
4550
},

src/config/resolvers/tracing.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export async function resolveTracingOptions(options: NitroOptions) {
55
options.tracingChannel = {
66
srvx: true,
77
h3: true,
8+
unstorage: true,
89
...(typeof options.tracingChannel === "object" ? options.tracingChannel : {}),
910
};
1011
options.plugins = options.plugins || [];

src/types/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,7 @@ export interface ServerAssetDir {
10381038
export interface TracingOptions {
10391039
srvx?: boolean;
10401040
h3?: boolean;
1041+
unstorage?: boolean;
10411042
}
10421043

10431044
/**

test/unit/virtual-storage.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { describe, expect, it } from "vitest";
2+
import type { Nitro } from "nitro/types";
3+
4+
import storage from "../../src/build/virtual/storage.ts";
5+
6+
function createNitroStub(tracingChannel: Nitro["options"]["tracingChannel"]): Nitro {
7+
return {
8+
options: {
9+
dev: true,
10+
preset: "nitro-dev",
11+
storage: {},
12+
devStorage: {},
13+
tracingChannel,
14+
},
15+
} as unknown as Nitro;
16+
}
17+
18+
describe("virtual/storage template", () => {
19+
it("does not wrap storage when tracingChannel is disabled", () => {
20+
const template = storage(createNitroStub(undefined)).template();
21+
expect(template).not.toContain("withTracing");
22+
expect(template).not.toContain("unstorage/tracing");
23+
expect(template).toContain("return storage");
24+
});
25+
26+
it("does not wrap storage when tracingChannel.unstorage is false", () => {
27+
const template = storage(
28+
createNitroStub({ srvx: true, h3: true, unstorage: false })
29+
).template();
30+
expect(template).not.toContain("withTracing");
31+
});
32+
33+
it("wraps storage with withTracing when tracingChannel.unstorage is true", () => {
34+
const template = storage(createNitroStub({ srvx: true, h3: true, unstorage: true })).template();
35+
expect(template).toContain(`import { withTracing } from 'unstorage/tracing'`);
36+
expect(template).toContain("return withTracing(storage)");
37+
});
38+
});

0 commit comments

Comments
 (0)