Skip to content

Commit 5644761

Browse files
make sure that instrumentation files work
1 parent 12d385d commit 5644761

File tree

19 files changed

+413
-61
lines changed

19 files changed

+413
-61
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@opennextjs/cloudflare": patch
3+
---
4+
5+
make sure that instrumentation files work
6+
7+
currently [instrumentation files](https://nextjs.org/docs/app/building-your-application/optimizing/instrumentation)
8+
in applications built using the adapter are ignored, the changes here
9+
make sure that those are instead properly included in the applications

examples/common/apps.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const apps = [
77
"vercel-blog-starter",
88
"vercel-commerce",
99
"ssg-app",
10+
"instrumentation-app",
1011
// e2e
1112
"app-pages-router",
1213
"app-router",
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.*
7+
.yarn/*
8+
!.yarn/patches
9+
!.yarn/plugins
10+
!.yarn/releases
11+
!.yarn/versions
12+
13+
# testing
14+
/coverage
15+
16+
# next.js
17+
/.next/
18+
/out/
19+
20+
# production
21+
/build
22+
23+
# misc
24+
.DS_Store
25+
*.pem
26+
27+
# debug
28+
npm-debug.log*
29+
yarn-debug.log*
30+
yarn-error.log*
31+
.pnpm-debug.log*
32+
33+
# env files (can opt-in for committing if needed)
34+
.env*
35+
36+
# vercel
37+
.vercel
38+
39+
# typescript
40+
*.tsbuildinfo
41+
next-env.d.ts
42+
43+
# playwright
44+
/test-results/
45+
/playwright-report/
46+
/blob-report/
47+
/playwright/.cache/
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { NextResponse } from "next/server";
2+
3+
export function GET() {
4+
return NextResponse.json({
5+
"nodejs-instrumentation-setup": globalThis["__NODEJS_INSTRUMENTATION_SETUP"] ?? "undefined",
6+
"edge-instrumentation-setup": globalThis["__EDGE_INSTRUMENTATION_SETUP"] ?? "undefined",
7+
});
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default function RootLayout({ children }: { children: React.ReactNode }) {
2+
return (
3+
<html lang="en">
4+
<body>{children}</body>
5+
</html>
6+
);
7+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import Link from "next/link";
2+
3+
export default function () {
4+
return (
5+
<div>
6+
<p>
7+
See{" "}
8+
<Link href="/api/hello">
9+
<code>/api/hello</code>
10+
</Link>
11+
</p>
12+
<p>
13+
See{" "}
14+
<Link href="/middleware">
15+
<code>/middleware</code>
16+
</Link>
17+
</p>
18+
</div>
19+
);
20+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { test, expect } from "@playwright/test";
2+
import { describe } from "node:test";
3+
4+
test.describe("instrumentation", () => {
5+
test("the instrumentation register hook should work for the nodejs runtime", async ({ page }) => {
6+
const res = await page.request.get("/api/hello");
7+
const respJson: Record<string, string> = await res.json();
8+
expect(respJson["nodejs-instrumentation-setup"]).toEqual(
9+
"this value has been set by calling the instrumentation `register` callback in the nodejs runtime"
10+
);
11+
});
12+
13+
test("the instrumentation register hook should work for the edge runtime", async ({ page }) => {
14+
const res = await page.request.get("/middleware");
15+
const respJson: Record<string, string> = await res.json();
16+
expect(respJson["edge-instrumentation-setup"]).toEqual(
17+
"this value has been set by calling the instrumentation `register` callback in the edge runtime"
18+
);
19+
});
20+
21+
// Note: we cannot test this since currently both runtimes share the same global scope
22+
// (see: https://github.com/opennextjs/opennextjs-cloudflare/issues/408)
23+
describe.skip("isolation", () => {
24+
test("the instrumentation register hook nodejs logic should not effect edge routes", async ({ page }) => {
25+
const res = await page.request.get("/middleware");
26+
const respJson: Record<string, string> = await res.json();
27+
expect(respJson["nodejs-instrumentation-setup"]).toEqual("undefined");
28+
});
29+
30+
test("the instrumentation register hook edge logic should not effect nodejs routes", async ({ page }) => {
31+
const res = await page.request.get("/api/hello");
32+
const respJson: Record<string, string> = await res.json();
33+
expect(respJson["edge-instrumentation-setup"]).toEqual("undefined");
34+
});
35+
});
36+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { configurePlaywright } from "../../common/config-e2e";
2+
3+
export default configurePlaywright("instrumentation-app", { isCI: !!process.env.CI });
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export function register() {
2+
// Note: we register instrumentation for both the nodejs and edge runtime, we do that using the NEXT_RUNTIME env
3+
// variable as recommended in the official docs:
4+
// https://nextjs.org/docs/app/building-your-application/optimizing/instrumentation#importing-runtime-specific-code
5+
6+
if (process.env.NEXT_RUNTIME === "nodejs") {
7+
globalThis["__NODEJS_INSTRUMENTATION_SETUP"] =
8+
"this value has been set by calling the instrumentation `register` callback in the nodejs runtime";
9+
}
10+
11+
if (process.env.NEXT_RUNTIME === "edge") {
12+
globalThis["__EDGE_INSTRUMENTATION_SETUP"] =
13+
"this value has been set by calling the instrumentation `register` callback in the edge runtime";
14+
}
15+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { NextResponse } from "next/server";
2+
3+
export function middleware() {
4+
return NextResponse.json({
5+
"nodejs-instrumentation-setup": globalThis["__NODEJS_INSTRUMENTATION_SETUP"] ?? "undefined",
6+
"edge-instrumentation-setup": globalThis["__EDGE_INSTRUMENTATION_SETUP"] ?? "undefined",
7+
});
8+
}
9+
10+
export const config = {
11+
matcher: ["/middleware"],
12+
};

0 commit comments

Comments
 (0)