diff --git a/CHANGELOG.md b/CHANGELOG.md index 628a95e8..cccae9b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,21 @@ Changes to Deployyy, newest first. Each feature works in the console at [deployyy.com](https://deployyy.com), the API and the MCP tools. Exceptions are noted per entry. +## 2026-12-02 + +### Traces + +- **Each request of a Next.js or GraphCommerce server is one span, named by + its route, in the trace of the gateway.** Its `fetch` calls to a Magento + backend carry the `traceparent`, so the PHP request continues the same + trace. A GraphQL call is named by its operation, such as + `GET graphql CategoryPage`. The Requests page of the environment lists the + requests. +- **The platform sets `OTEL_EXPORTER_OTLP_ENDPOINT`, `OTEL_TRACES_SAMPLER` + and `OTEL_SERVICE_NAME` on each process.** `OTEL_SERVICE_NAME` is the name + of the process, such as `magento-web`. A stored value of these variables + has no effect. + ## 2026-11-30 ### Profiles diff --git a/README.md b/README.md index 56933cdf..d30ceb3b 100644 --- a/README.md +++ b/README.md @@ -318,9 +318,14 @@ Laravel writes to stderr with `LOG_CHANNEL=stderr`, a value of the recipe. Each PHP-FPM request of a Magento, Mage-OS or Laravel image on PHP 8.2 or later is one span, a child of the `traceparent` of the request. The SDK sends the -spans to `OTEL_EXPORTER_OTLP_ENDPOINT`, a value of the recipe that names the -collector of the platform. Set the variable to your own OTLP/HTTP endpoint, or -leave it empty to send no spans. +spans to the collector of the platform. The platform sets these variables on +each process: + +| Variable | Value | +|---|---| +| `OTEL_EXPORTER_OTLP_ENDPOINT` | the OTLP/HTTP endpoint of the collector | +| `OTEL_TRACES_SAMPLER` | `parentbased_always_off`: a process records a request with a sampled `traceparent`, such as each request through the gateway. A health check of the kubelet has none. | +| `OTEL_SERVICE_NAME` | the name of the process, such as `magento-web` | The spans of a request: @@ -356,8 +361,6 @@ no bundle and configures OpenTelemetry itself. | Variable | Image value | |---|---| -| `OTEL_SERVICE_NAME` | `magento` or `laravel` | -| `OTEL_TRACES_SAMPLER` | `parentbased_always_off`: PHP-FPM records a request with a sampled `traceparent`, such as each request through the gateway. A health check of the kubelet has none. | | `OTEL_EXPORTER_OTLP_TIMEOUT` | `1000` (milliseconds) | | `OTEL_PHP_DISABLED_INSTRUMENTATIONS` | none; `pdo,guzzle` turns those off | @@ -411,6 +414,31 @@ Every variable and secret is available to `next build` and, at runtime, to the server in `process.env`. As on Vercel, only `NEXT_PUBLIC_*` values end up in the browser bundle. +### Traces + +Each request of the Next.js server is one span of the instrumentation +`next.js`, named by its method and its route, such as `GET /products/[slug]`, +a child of the `traceparent` of the request. Each `fetch` and each `http` +call of the server is a child span. A call to an address inside the platform, +a host name without a dot or under `.svc`, carries the `traceparent`, so the +PHP request of a Magento backend continues the trace. A call to another host +carries no `traceparent`. A GraphQL call is named as the PHP request that +answers it, such as `GET graphql CategoryPage`, with +`graphql.operation.name` and `graphql.operation.type`. The variables of the +platform are in [Traces](#traces). + +The build bundles [`otel.mjs`](recipes/nextjs/platform/otel.mjs), which +calls `registerOTel` of [`@vercel/otel`](https://github.com/vercel/otel), +with the packages of +[`otel-package.json`](recipes/nextjs/platform/otel-package.json) into one file +of the image. [`inject-instrumentation.mjs`](recipes/nextjs/platform/inject-instrumentation.mjs) +writes the instrumentation file of Next.js, which loads that file. Your own +`instrumentation.ts` or `.js` stays: the build renames it to +`instrumentation.project.ts`, and its `register()` and its other exports run +as before. A project that depends on `@vercel/otel` or an +`@opentelemetry/sdk-*` package gets no instrumentation file and configures +OpenTelemetry itself. + ### Magento backend for a storefront A GraphCommerce storefront paired with a Magento project builds against one diff --git a/recipes/laravel/Dockerfile b/recipes/laravel/Dockerfile index 81dc1a3f..4b5dd414 100644 --- a/recipes/laravel/Dockerfile +++ b/recipes/laravel/Dockerfile @@ -103,9 +103,9 @@ ENV PHP_EXTENSION_GD=1 PHP_EXTENSION_INTL=1 PHP_EXTENSION_BCMATH=1 \ PHP_EXTENSION_OPCACHE=1 PHP_INI_OPCACHE__ENABLE=1 \ PHP_INI_MEMORY_LIMIT=512M PHP_INI_DATE__TIMEZONE=Europe/Amsterdam \ TEMPLATE_PHP_INI=production -# The settings of the OpenTelemetry SDK. OTEL_EXPORTER_OTLP_ENDPOINT, a value of the -# recipe, starts the traces of PHP-FPM (.platform/otel-bootstrap.php). -ENV PHP_EXTENSION_OPENTELEMETRY=1 PHP_EXTENSION_EXCIMER=1 OTEL_PHP_AUTOLOAD_ENABLED=true OTEL_TRACES_SAMPLER=parentbased_always_off OTEL_SERVICE_NAME=laravel \ +# The settings of the OpenTelemetry SDK. OTEL_EXPORTER_OTLP_ENDPOINT, a platform variable, +# starts the traces of PHP-FPM (.platform/otel-bootstrap.php). +ENV PHP_EXTENSION_OPENTELEMETRY=1 PHP_EXTENSION_EXCIMER=1 OTEL_PHP_AUTOLOAD_ENABLED=true \ OTEL_PHP_PSR3_MODE=inject OTEL_METRICS_EXPORTER=none OTEL_LOGS_EXPORTER=none \ OTEL_EXPORTER_OTLP_TIMEOUT=1000 # `php artisan migrate` loads a schema dump (database/schema/) with the client diff --git a/recipes/magento/Dockerfile b/recipes/magento/Dockerfile index 4e052789..a7768e43 100644 --- a/recipes/magento/Dockerfile +++ b/recipes/magento/Dockerfile @@ -326,9 +326,9 @@ ENV PHP_EXTENSION_GD=1 PHP_EXTENSION_INTL=1 PHP_EXTENSION_OPCACHE=1 \ PHP_INI_OPCACHE__MEMORY_CONSUMPTION=512 PHP_INI_OPCACHE__INTERNED_STRINGS_BUFFER=64 \ PHP_INI_OPCACHE__MAX_ACCELERATED_FILES=130000 PHP_INI_OPCACHE__VALIDATE_TIMESTAMPS=0 \ TEMPLATE_PHP_INI=production -# The settings of the OpenTelemetry SDK. OTEL_EXPORTER_OTLP_ENDPOINT, a value of the -# recipe, starts the traces of PHP-FPM (.platform/otel-bootstrap.php). -ENV PHP_EXTENSION_OPENTELEMETRY=1 PHP_EXTENSION_EXCIMER=1 OTEL_PHP_AUTOLOAD_ENABLED=true OTEL_TRACES_SAMPLER=parentbased_always_off OTEL_SERVICE_NAME=magento \ +# The settings of the OpenTelemetry SDK. OTEL_EXPORTER_OTLP_ENDPOINT, a platform variable, +# starts the traces of PHP-FPM (.platform/otel-bootstrap.php). +ENV PHP_EXTENSION_OPENTELEMETRY=1 PHP_EXTENSION_EXCIMER=1 OTEL_PHP_AUTOLOAD_ENABLED=true \ OTEL_PHP_PSR3_MODE=inject OTEL_METRICS_EXPORTER=none OTEL_LOGS_EXPORTER=none \ OTEL_EXPORTER_OTLP_TIMEOUT=1000 USER root diff --git a/recipes/nextjs/Dockerfile b/recipes/nextjs/Dockerfile index 7dee62ce..4c15d14f 100644 --- a/recipes/nextjs/Dockerfile +++ b/recipes/nextjs/Dockerfile @@ -24,6 +24,19 @@ # build and never stored in a layer. ARG NODE_VERSION=22 +# The OpenTelemetry SDK of the Next.js server (README, "Traces"): .platform/otel.mjs with the +# packages of .platform/otel-package.json, bundled into one file. A fixed Node.js version keeps +# the cache of this stage independent of the project. +FROM node:22-bookworm-slim AS otel +WORKDIR /otel +COPY .platform/otel-package.json package.json +COPY .platform/otel-package-lock.json package-lock.json +COPY .platform/otel.mjs otel.mjs +RUN npm ci --ignore-scripts --no-audit --no-fund \ + && node_modules/.bin/esbuild otel.mjs --bundle --platform=node --format=esm --target=node18 \ + --main-fields=module,main --outfile=deployyy-otel.mjs \ + --banner:js="import { createRequire } from 'node:module'; const require = createRequire(import.meta.url);" + FROM node:${NODE_VERSION}-bookworm-slim AS builder ENV NEXT_TELEMETRY_DISABLED=1 COREPACK_ENABLE_DOWNLOAD_PROMPT=0 CI=1 WORKDIR /app @@ -32,9 +45,10 @@ RUN corepack enable # and postinstall code generation (GraphCommerce) all need more than the # lockfile, and a partial copy fails in ways that are hard to see. COPY . . -# The platform cache handler and the Next config wrapper (docs in each file). +# The platform cache handler, the Next config wrapper and the instrumentation file (docs in each file). RUN cp .platform/cache-handler.mjs deployyy-cache-handler.mjs \ - && node .platform/inject-next-config.mjs + && node .platform/inject-next-config.mjs \ + && node .platform/inject-instrumentation.mjs # The commit sha as the Next build id (inject-next-config.mjs). ARG DEPLOYYY_BUILD_ID="" RUN --mount=type=secret,id=build-env,required=false \ @@ -84,6 +98,7 @@ RUN groupadd --system --gid 1001 nextjs \ COPY --from=builder --chown=1001:1001 /app/.next/standalone ./ COPY --from=builder --chown=1001:1001 /app/.next/static ./.next/static COPY --from=builder --chown=1001:1001 /app/public ./public +COPY --from=otel /otel/deployyy-otel.mjs /usr/local/lib/deployyy/deployyy-otel.mjs # Numeric, so the kubelet can verify runAsNonRoot without a securityContext. LABEL app.deployyy.pod-security="restricted" USER 1001:1001 diff --git a/recipes/nextjs/platform/inject-instrumentation.mjs b/recipes/nextjs/platform/inject-instrumentation.mjs new file mode 100644 index 00000000..726a6ee9 --- /dev/null +++ b/recipes/nextjs/platform/inject-instrumentation.mjs @@ -0,0 +1,59 @@ +/** + * Platform build step for Next.js and GraphCommerce projects: load the OpenTelemetry SDK of the + * platform (otel.mjs, bundled into deployyy-otel.mjs) in the Next.js server, so a project carries + * no tracing package and no tracing code of its own. + * + * Runs in the Dockerfile builder stage, in the project root, before `next build`. It writes the + * instrumentation file of Next.js (`instrumentation.js`, in `src/` when the pages or the app + * directory is in `src/`). Its `register()` imports deployyy-otel.mjs in the Node.js runtime while + * OTEL_EXPORTER_OTLP_ENDPOINT is set. The import names an absolute path in the runtime image, and + * Next.js does not bundle it. + * + * A project with its own instrumentation file keeps it: the file is renamed to + * `instrumentation.project.`, the generated file under the original name exports each export + * of it, and its `register()` runs after the SDK of the platform. + * + * A project that depends on @vercel/otel or an OpenTelemetry SDK configures OpenTelemetry itself + * and gets no generated file. + */ +import fs from 'node:fs' +import path from 'node:path' + +const root = process.cwd() +// The runtime stage of the Dockerfile copies the bundle to this path. +const BUNDLE = '/usr/local/lib/deployyy/deployyy-otel.mjs' + +const pkg = JSON.parse(fs.readFileSync(path.join(root, 'package.json'), 'utf8')) +const dependencies = { ...pkg.dependencies, ...pkg.devDependencies } +const sdk = Object.keys(dependencies).find((name) => name === '@vercel/otel' || name.startsWith('@opentelemetry/sdk-')) +if (sdk) { + console.log(`Traces: the project depends on ${sdk}. The build adds no instrumentation file.`) + process.exit(0) +} + +const exists = (...parts) => fs.existsSync(path.join(root, ...parts)) +// Next.js reads the instrumentation file from src/ when the pages or the app directory is there. +const dir = !exists('pages') && !exists('app') && (exists('src', 'pages') || exists('src', 'app')) ? 'src' : '.' +// The default page extensions of Next.js. +const own = ['ts', 'tsx', 'js', 'jsx'].map((ext) => `instrumentation.${ext}`).find((f) => exists(dir, f)) +const ext = own ? path.extname(own) : '.js' +const projectFile = own ? `instrumentation.project${ext}` : null +if (own) fs.renameSync(path.join(root, dir, own), path.join(root, dir, projectFile)) + +const projectModule = projectFile ? `./${path.basename(projectFile, ext)}` : null +const body = `/* eslint-disable */ +${ext.startsWith('.ts') ? '// @ts-nocheck\n' : ''}// GENERATED AT BUILD TIME by the deployyy platform build (inject-instrumentation.mjs). +${projectModule ? `// Your own instrumentation is in ${projectFile}; edit that file, not this one. +import * as project from '${projectModule}' +export * from '${projectModule}' +` : ''} +export async function register() { + if (process.env.NEXT_RUNTIME === 'nodejs' && process.env.OTEL_EXPORTER_OTLP_ENDPOINT) { + await import(/* webpackIgnore: true */ /* turbopackIgnore: true */ '${BUNDLE}') + } +${projectModule ? ' await project.register?.()\n' : ''}} +` + +const generated = path.join(dir, `instrumentation${ext}`) +fs.writeFileSync(path.join(root, generated), body) +console.log(`Traces: ${generated} loads the OpenTelemetry SDK of the platform${own ? `, then ${path.join(dir, projectFile)}` : ''}.`) diff --git a/recipes/nextjs/platform/inject-next-config.mjs b/recipes/nextjs/platform/inject-next-config.mjs index 159bb8a1..8b420c70 100644 --- a/recipes/nextjs/platform/inject-next-config.mjs +++ b/recipes/nextjs/platform/inject-next-config.mjs @@ -6,7 +6,7 @@ * Runs in the Dockerfile builder stage, in the project root, before * `next build`. It renames the project's config file to * `next.config.project.` and writes a wrapper under the original config - * name that imports it and adds three settings: + * name that imports it and adds these settings: * * cacheHandler -> /deployyy-cache-handler.mjs (the platform * handler, copied next to the config by the @@ -18,6 +18,9 @@ * the operator repo). * output -> 'standalone' when the project sets no output. The * runtime image runs `node server.js`. + * experimental.instrumentationHook -> true on Next.js 13 and 14, which + * load the instrumentation file of + * inject-instrumentation.mjs only with this flag. * generateBuildId -> the commit sha (DEPLOYYY_BUILD_ID), when the * project sets none. The cache seed and the runtime * image are two build targets; a fixed build id @@ -85,8 +88,10 @@ const body = `${isTs ? '// @ts-nocheck\n' : ''}// GENERATED AT BUILD TIME by the // Your own config is in ${projectFile ?? '(none)'}; edit that file, not this one. ${importLine} import path from 'node:path' +import { createRequire } from 'node:module' import { fileURLToPath } from 'node:url' +const nextMajor = Number(createRequire(import.meta.url)('next/package.json').version.split('.')[0]) const handler = path.join(path.dirname(fileURLToPath(import.meta.url)), '${HANDLER}') const loaded${isTs ? ': any' : ''} = (project${isTs ? ' as any' : ''}).default ?? project @@ -97,6 +102,7 @@ export default async function deployyyConfig(phase${isTs ? ': string' : ''}, ctx throw new Error("deployyy: output: 'export' produces static files without a server. Remove output: 'export' from your next.config.") } if (!config.output) config.output = 'standalone' + if (nextMajor < 15) config.experimental = { ...config.experimental, instrumentationHook: true } if (!config.generateBuildId && process.env.DEPLOYYY_BUILD_ID) { const buildId = process.env.DEPLOYYY_BUILD_ID config.generateBuildId = () => buildId diff --git a/recipes/nextjs/platform/otel-package-lock.json b/recipes/nextjs/platform/otel-package-lock.json new file mode 100644 index 00000000..aebd97e1 --- /dev/null +++ b/recipes/nextjs/platform/otel-package-lock.json @@ -0,0 +1,718 @@ +{ + "name": "deployyy-otel", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "deployyy-otel", + "dependencies": { + "@opentelemetry/api": "1.9.1", + "@opentelemetry/api-logs": "0.223.2", + "@opentelemetry/instrumentation": "0.223.2", + "@opentelemetry/resources": "2.12.1", + "@opentelemetry/sdk-logs": "0.223.2", + "@opentelemetry/sdk-metrics": "2.12.1", + "@opentelemetry/sdk-trace-base": "2.12.1", + "@vercel/otel": "2.3.0", + "esbuild": "0.28.2" + } + }, + "node_modules/@esbuild/aix-ppc64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.28.2.tgz", + "integrity": "sha512-XExcO+dvLKvVtNTibSTBej1NCAbaGhWn9Ww1ZPx80qsahhPFe/8jgWP0IchNe0F3HwkU7n8ejhH8bjonqht8mQ==", + "cpu": [ + "ppc64" + ], + "license": "MIT", + "optional": true, + "os": [ + "aix" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-arm": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.28.2.tgz", + "integrity": "sha512-kXXoiPVVGQcnIYGOeaovwOURpniDBpSq4A03qkQ+BMQqtGG6HYap3xne9C1O1yo4TR3qxlCX5IqqmX6fFo2Lqg==", + "cpu": [ + "arm" + ], + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm64/-/android-arm64-0.28.2.tgz", + "integrity": "sha512-5YfKeeI8qWfBZIX+u2xZC3Zlb3Os/gLS2sbEKM+I4ZOcsWmHS2WLysCcQZDAFRslDUU5Oiq44gf6PYN1vGwG5A==", + "cpu": [ + "arm64" + ], + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/android-x64/-/android-x64-0.28.2.tgz", + "integrity": "sha512-O387ite7SzUyCcy3JQX4P4bLtEA7bLLkx+esve5JHnyYfNTxcVpXZo9jhdB0lTKN44gztELTdU7nS8Nr16Fs1Q==", + "cpu": [ + "x64" + ], + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/darwin-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.28.2.tgz", + "integrity": "sha512-n4KqkOQrraxHJcgjM1RvwbigfQKIKJVpM7xp+KsxiyUSrRdIXnt73VhrPAx0fV44hgfmIVKjxMN9J1t5jySVkw==", + "cpu": [ + "arm64" + ], + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/darwin-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-x64/-/darwin-x64-0.28.2.tgz", + "integrity": "sha512-uq6suIWYP37qzGddBKPw5QEQPi6HiLGsO7UmkpfyaYNQ3D+rN6w6WfwH+nuqcGXWvawGwxOEroO4YGnFh95azw==", + "cpu": [ + "x64" + ], + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/freebsd-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-arm64/-/freebsd-arm64-0.28.2.tgz", + "integrity": "sha512-n+I0BTSRIoy+d6RPKnEVwql5UwBJolytvY4mAOIEJorKlqgPII8ix6slVVrfZ5Tnj7glIZvloylbB/EJPMWEXw==", + "cpu": [ + "arm64" + ], + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/freebsd-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-x64/-/freebsd-x64-0.28.2.tgz", + "integrity": "sha512-78XJTJkvPs0kz2w61301PJjXl4g7q3JqiYMZ/M/yVI73EHBrCRTgkhu9oqG7vPqq+a/yadEW8aD+agKlk5xrmg==", + "cpu": [ + "x64" + ], + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-arm": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm/-/linux-arm-0.28.2.tgz", + "integrity": "sha512-XlDnu2q5yoqems+xay6wSAcg9DDD7K9RLKZEBOMZm3ckNpJBvOX20tSfby8KfrrhINDyv9V2YVZKY/SpoGJI8w==", + "cpu": [ + "arm" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.28.2.tgz", + "integrity": "sha512-pW4AC0P3it8c7do9MVM4p51FzHzdM/TZrerurgRcHJ2WTa1VQ1CIq18xncfpBJw4ojkiZZrKW2yIBWBP92j6Ug==", + "cpu": [ + "arm64" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-ia32": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ia32/-/linux-ia32-0.28.2.tgz", + "integrity": "sha512-CYbnj78HsIeA+DhgUKgFCfvNsTHFhMMrinUrMZpDXJXKN8T3XViTZ/+wtHeVxEWY8ewSzTFN+nRmSwO2tZaLUQ==", + "cpu": [ + "ia32" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-loong64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.28.2.tgz", + "integrity": "sha512-buwkd8nsph4R+ajRvw0qM5Hja/TXQow3ptzWO2EbG/cqcIkHloRrdlBtQlshyYGTNFvfkfJ5tpPLVkY4DtsPfQ==", + "cpu": [ + "loong64" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-mips64el": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-mips64el/-/linux-mips64el-0.28.2.tgz", + "integrity": "sha512-ZVykbDyk7519VwiNb9Lcj9m8XM6v5V9uKPvrEMkkEedVewf+0itkhahp4HDpgERXhwLRpWFypsGbG/J8s0QjJA==", + "cpu": [ + "mips64el" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-ppc64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ppc64/-/linux-ppc64-0.28.2.tgz", + "integrity": "sha512-CAXl+Dtd9UUuJd8pKKdwh6MLm3MUMiqMPmhZ3tTSXPqfyQ3vDl6R5hZdZ/kYojK4ofXtdfSv1tFq8XzWx3heNQ==", + "cpu": [ + "ppc64" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-riscv64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-riscv64/-/linux-riscv64-0.28.2.tgz", + "integrity": "sha512-GeXCej4IQtU1B+QlDV8W/RRvbzI3O/Stss+/bCXv4lZls5WGRtu2a+3JkA3i4qIUlMXpcHebWpF8AkJhATowuA==", + "cpu": [ + "riscv64" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-s390x": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-s390x/-/linux-s390x-0.28.2.tgz", + "integrity": "sha512-3H1weTYZPxt/WOhByszQZybS9w5lKzUn1FDMsgEChbHWQwHYQQRfBxgCcZvPhjHfKyJjIievvMmEUawJrdY9Dg==", + "cpu": [ + "s390x" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.28.2.tgz", + "integrity": "sha512-4xTZr1FUmSoQW4XIWmit3tzQrUTZM+N3P0XV8xROKYF50XfI7xeO90+1bZvNwxIufQ9hDQVRJH5YhgPVF8A/HQ==", + "cpu": [ + "x64" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/netbsd-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/netbsd-arm64/-/netbsd-arm64-0.28.2.tgz", + "integrity": "sha512-sSATRjPeDBg3pdgHoQfoYBob11Kk1FGa9lui5RIHZCoCkJa9QKlvl3/vKz2usCmYYjs7ymJR/2Nnsqe+Hjt5nw==", + "cpu": [ + "arm64" + ], + "license": "MIT", + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/netbsd-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/netbsd-x64/-/netbsd-x64-0.28.2.tgz", + "integrity": "sha512-lqnzCV+mM0gIADaKihiCg6ifgfU2L3h5E33rNQBN1Y4MaVGnzryzmvvf7UHxprpQdE8hpqLolJ9Rl+SkIRDpyw==", + "cpu": [ + "x64" + ], + "license": "MIT", + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/openbsd-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/openbsd-arm64/-/openbsd-arm64-0.28.2.tgz", + "integrity": "sha512-AL2qJILH7lNjrDmCQDvdxMfAUIv8KMNZOvrwAQ8i8//ntL9FflhOyMJ8OZSMBb8/AWXe3/5v5S20y3zCoZWKoQ==", + "cpu": [ + "arm64" + ], + "license": "MIT", + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/openbsd-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/openbsd-x64/-/openbsd-x64-0.28.2.tgz", + "integrity": "sha512-QtiuPytchRyC4rwUKhexJdQKvDuZ6hWloi3igqPQNUJCS1/v9EiO3UTOXR6A3FoMo4fnAKbWJdqaIwhOzh8qEw==", + "cpu": [ + "x64" + ], + "license": "MIT", + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/openharmony-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/openharmony-arm64/-/openharmony-arm64-0.28.2.tgz", + "integrity": "sha512-WkhYDmpTjLvGlScA1rwjRUmhl4k8oXR3cIbtqWmELgU/dFeHHlEllxDvdWcNJV9rbzCexB5vz8gtNewWLgCT7Q==", + "cpu": [ + "arm64" + ], + "license": "MIT", + "optional": true, + "os": [ + "openharmony" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/sunos-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/sunos-x64/-/sunos-x64-0.28.2.tgz", + "integrity": "sha512-GPMSkTOtMnv2U2F8gxe4Io6qmVs+YKyp832Etqqxr0hFngmXQ3rzwytelm3GIn7T4VviRUlf3sOgBOiTdvaf7g==", + "cpu": [ + "x64" + ], + "license": "MIT", + "optional": true, + "os": [ + "sunos" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/win32-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/win32-arm64/-/win32-arm64-0.28.2.tgz", + "integrity": "sha512-PIhhEkE9uPBleRBrQEJpUn7MBnibZzbGzYWPmY3x+YoVg/95zbjB4CxPPOQ8l5tYYM4mMaCthF8/1DIfBQQyWQ==", + "cpu": [ + "arm64" + ], + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/win32-ia32": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/win32-ia32/-/win32-ia32-0.28.2.tgz", + "integrity": "sha512-YmJbfTlvU7Sdn9BB+4PRES4oB6pxgS37MAONj+hBr/cpXS1aBPKXxNnDbu+QCWPj0o9dgyxeq79g6c5P8KeuYA==", + "cpu": [ + "ia32" + ], + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/win32-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.28.2.tgz", + "integrity": "sha512-5ebpxr3nWMzrL/rnUI755Jkuee0bHL/Gq0WTF9lvcpv73wAp5eu8MfBUgWK9bhWvZjj7yX8etf/8tI8Ney695g==", + "cpu": [ + "x64" + ], + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@opentelemetry/api": { + "version": "1.9.1", + "resolved": "https://registry.npmjs.org/@opentelemetry/api/-/api-1.9.1.tgz", + "integrity": "sha512-gLyJlPHPZYdAk1JENA9LeHejZe1Ti77/pTeFm/nMXmQH/HFZlcS/O2XJB+L8fkbrNSqhdtlvjBVjxwUYanNH5Q==", + "license": "Apache-2.0", + "peer": true, + "engines": { + "node": ">=8.0.0" + } + }, + "node_modules/@opentelemetry/api-logs": { + "version": "0.223.2", + "resolved": "https://registry.npmjs.org/@opentelemetry/api-logs/-/api-logs-0.223.2.tgz", + "integrity": "sha512-pKr/0bxkzNPhWKTqqSP2KtPVxqmC2oQrWjUjr4EDHrJCCiRkGIN52m2c7ILhCnQkYKmlTQBiESIEeyg0hlmotQ==", + "license": "Apache-2.0", + "peer": true, + "dependencies": { + "@opentelemetry/api": "^1.3.0" + }, + "engines": { + "node": ">=8.0.0" + } + }, + "node_modules/@opentelemetry/core": { + "version": "2.12.1", + "resolved": "https://registry.npmjs.org/@opentelemetry/core/-/core-2.12.1.tgz", + "integrity": "sha512-3hr6Pj0/G0oVgIGb97IVcw9BjZ6eudonKjrmQxw7JquyU/XmVbJelkmyoU0sT/OvHGkv+r1tcrwv8MbSX7aKHw==", + "license": "Apache-2.0", + "dependencies": { + "@opentelemetry/semantic-conventions": "^1.29.0" + }, + "engines": { + "node": "^18.19.0 || >=20.6.0" + }, + "peerDependencies": { + "@opentelemetry/api": ">=1.0.0 <1.10.0" + } + }, + "node_modules/@opentelemetry/instrumentation": { + "version": "0.223.2", + "resolved": "https://registry.npmjs.org/@opentelemetry/instrumentation/-/instrumentation-0.223.2.tgz", + "integrity": "sha512-2tRpOgQ91+wC2/h6IDt+dsQNuUlfUR+CVwABceAEtiWtON9JumZR6YQcYCsisNRTX942TnCjPDuBI0xxGgVLHQ==", + "license": "Apache-2.0", + "peer": true, + "dependencies": { + "@opentelemetry/api-logs": "0.223.2", + "import-in-the-middle": "^3.0.0", + "require-in-the-middle": "^8.0.0" + }, + "engines": { + "node": "^18.19.0 || >=20.6.0" + }, + "peerDependencies": { + "@opentelemetry/api": "^1.3.0" + } + }, + "node_modules/@opentelemetry/resources": { + "version": "2.12.1", + "resolved": "https://registry.npmjs.org/@opentelemetry/resources/-/resources-2.12.1.tgz", + "integrity": "sha512-IwrNcuLCvnJAnNLkXxx0Uw+rjBkVW6VGQrhd7PanNf0LavOzkTk9sDAjdqegM7p/VdxefNRrii6d+wBRJfz35g==", + "license": "Apache-2.0", + "peer": true, + "dependencies": { + "@opentelemetry/core": "2.12.1", + "@opentelemetry/semantic-conventions": "^1.29.0" + }, + "engines": { + "node": "^18.19.0 || >=20.6.0" + }, + "peerDependencies": { + "@opentelemetry/api": ">=1.3.0 <1.10.0" + } + }, + "node_modules/@opentelemetry/sdk-logs": { + "version": "0.223.2", + "resolved": "https://registry.npmjs.org/@opentelemetry/sdk-logs/-/sdk-logs-0.223.2.tgz", + "integrity": "sha512-gMCNZzXvb89w8MxZWYZLIRImgromnT6iloTQaSbQNyIdjtl9KPiZMrfBv3fYbR3sQl5EYBDJhFN/W3gycbEICw==", + "license": "Apache-2.0", + "peer": true, + "dependencies": { + "@opentelemetry/api-logs": "0.223.2", + "@opentelemetry/core": "2.12.1", + "@opentelemetry/resources": "2.12.1", + "@opentelemetry/semantic-conventions": "^1.29.0" + }, + "engines": { + "node": "^18.19.0 || >=20.6.0" + }, + "peerDependencies": { + "@opentelemetry/api": ">=1.4.0 <1.10.0" + } + }, + "node_modules/@opentelemetry/sdk-metrics": { + "version": "2.12.1", + "resolved": "https://registry.npmjs.org/@opentelemetry/sdk-metrics/-/sdk-metrics-2.12.1.tgz", + "integrity": "sha512-HeSblYhpds4QWliBE7ANCfoa2p3QH83uUxX4vO2qtk6BhIcqP7qDYYT36z3LOXftEBpdfbDStZxv+Cw5mqKX5g==", + "license": "Apache-2.0", + "peer": true, + "dependencies": { + "@opentelemetry/core": "2.12.1", + "@opentelemetry/resources": "2.12.1" + }, + "engines": { + "node": "^18.19.0 || >=20.6.0" + }, + "peerDependencies": { + "@opentelemetry/api": ">=1.9.0 <1.10.0" + } + }, + "node_modules/@opentelemetry/sdk-trace": { + "version": "2.12.1", + "resolved": "https://registry.npmjs.org/@opentelemetry/sdk-trace/-/sdk-trace-2.12.1.tgz", + "integrity": "sha512-vANdmCYnn+Ka/alBKxwICMl4Koa+aQBlRC9zAZy+Ig1Obbo3RZt2oiyv+8/gET5rrBBYrO3YniPcXfwZrN01rg==", + "license": "Apache-2.0", + "dependencies": { + "@opentelemetry/core": "2.12.1", + "@opentelemetry/resources": "2.12.1", + "@opentelemetry/semantic-conventions": "^1.29.0" + }, + "engines": { + "node": "^18.19.0 || >=20.6.0" + }, + "peerDependencies": { + "@opentelemetry/api": ">=1.3.0 <1.10.0" + } + }, + "node_modules/@opentelemetry/sdk-trace-base": { + "version": "2.12.1", + "resolved": "https://registry.npmjs.org/@opentelemetry/sdk-trace-base/-/sdk-trace-base-2.12.1.tgz", + "integrity": "sha512-CMrcqFxTikYmYQCuHToBO4kQvWIseRk2ztzWidTd7VutIxj1rljvJZCZnFPJEGNOS7FvaSmRL21Jeuu3/wrCAQ==", + "license": "Apache-2.0", + "peer": true, + "dependencies": { + "@opentelemetry/core": "2.12.1", + "@opentelemetry/resources": "2.12.1", + "@opentelemetry/sdk-trace": "2.12.1", + "@opentelemetry/semantic-conventions": "^1.29.0" + }, + "engines": { + "node": "^18.19.0 || >=20.6.0" + }, + "peerDependencies": { + "@opentelemetry/api": ">=1.3.0 <1.10.0" + } + }, + "node_modules/@opentelemetry/semantic-conventions": { + "version": "1.45.0", + "resolved": "https://registry.npmjs.org/@opentelemetry/semantic-conventions/-/semantic-conventions-1.45.0.tgz", + "integrity": "sha512-aOP45mbcOKbQVvFWfQmgoOPT8R2iwbbbgl83k4CujGV9BrJhFr3qG55MFpwFs4YLabpuW3HhzUY9eQxPlDqaZw==", + "license": "Apache-2.0", + "engines": { + "node": ">=14" + } + }, + "node_modules/@vercel/otel": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/@vercel/otel/-/otel-2.3.0.tgz", + "integrity": "sha512-A9OSxGMeemIE2rUzw2zlj9dFXDCp54tzuY6kvKixR+WcxMDPSB/xVV4TDHkGqUXsHbKARR8aeenJm+Or/NSnTA==", + "license": "MIT", + "engines": { + "node": "^18.19.0 || >=20.6.0" + }, + "peerDependencies": { + "@opentelemetry/api": ">=1.9.0 <2.0.0", + "@opentelemetry/api-logs": ">=0.200.0 <0.300.0", + "@opentelemetry/instrumentation": ">=0.200.0 <0.300.0", + "@opentelemetry/resources": ">=2.0.0 <3.0.0", + "@opentelemetry/sdk-logs": ">=0.200.0 <0.300.0", + "@opentelemetry/sdk-metrics": ">=2.0.0 <3.0.0", + "@opentelemetry/sdk-trace-base": ">=2.0.0 <3.0.0" + } + }, + "node_modules/cjs-module-lexer": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/cjs-module-lexer/-/cjs-module-lexer-2.3.0.tgz", + "integrity": "sha512-lmNyBzi6iYiqyrExTctfmjd8tInx+dHu9PBomTfH5RXS53c27JMFJgzNv5TyAnm3PgGf+MLy0+Ws41/HlVxdKQ==", + "license": "MIT" + }, + "node_modules/debug": { + "version": "4.4.3", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.4.3.tgz", + "integrity": "sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA==", + "license": "MIT", + "dependencies": { + "ms": "^2.1.3" + }, + "engines": { + "node": ">=6.0" + }, + "peerDependenciesMeta": { + "supports-color": { + "optional": true + } + } + }, + "node_modules/es-module-lexer": { + "version": "3.0.5", + "resolved": "https://registry.npmjs.org/es-module-lexer/-/es-module-lexer-3.0.5.tgz", + "integrity": "sha512-RNq2lKJw53c7z9QRHeiKeA6Ez0oikOqiBGdmxQhKDxEiOX1g5QJR4IoFVwicn8uCZcRWNrXC3NZaJihgK7aEbg==", + "license": "MIT" + }, + "node_modules/esbuild": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.28.2.tgz", + "integrity": "sha512-HKVLS8dvII+xoKW9kmqxbRKrnWEXfJJr/FZhhJmiqIB0e053QNYFqOBouTMO/k5sID4MvCiUCvv8b9M4h32wIA==", + "hasInstallScript": true, + "license": "MIT", + "bin": { + "esbuild": "bin/esbuild" + }, + "engines": { + "node": ">=18" + }, + "optionalDependencies": { + "@esbuild/aix-ppc64": "0.28.2", + "@esbuild/android-arm": "0.28.2", + "@esbuild/android-arm64": "0.28.2", + "@esbuild/android-x64": "0.28.2", + "@esbuild/darwin-arm64": "0.28.2", + "@esbuild/darwin-x64": "0.28.2", + "@esbuild/freebsd-arm64": "0.28.2", + "@esbuild/freebsd-x64": "0.28.2", + "@esbuild/linux-arm": "0.28.2", + "@esbuild/linux-arm64": "0.28.2", + "@esbuild/linux-ia32": "0.28.2", + "@esbuild/linux-loong64": "0.28.2", + "@esbuild/linux-mips64el": "0.28.2", + "@esbuild/linux-ppc64": "0.28.2", + "@esbuild/linux-riscv64": "0.28.2", + "@esbuild/linux-s390x": "0.28.2", + "@esbuild/linux-x64": "0.28.2", + "@esbuild/netbsd-arm64": "0.28.2", + "@esbuild/netbsd-x64": "0.28.2", + "@esbuild/openbsd-arm64": "0.28.2", + "@esbuild/openbsd-x64": "0.28.2", + "@esbuild/openharmony-arm64": "0.28.2", + "@esbuild/sunos-x64": "0.28.2", + "@esbuild/win32-arm64": "0.28.2", + "@esbuild/win32-ia32": "0.28.2", + "@esbuild/win32-x64": "0.28.2" + } + }, + "node_modules/import-in-the-middle": { + "version": "3.7.1", + "resolved": "https://registry.npmjs.org/import-in-the-middle/-/import-in-the-middle-3.7.1.tgz", + "integrity": "sha512-Bxnb2pIpS1C2PdtpQH6rar03EPLcOWUSb1xH+3MLKcEBwe/OiKide3RzMYbTAiasYjzJriehz5rdPZ5KFJSN6w==", + "license": "Apache-2.0", + "dependencies": { + "cjs-module-lexer": "^2.2.0", + "es-module-lexer": "^3.0.4", + "module-details-from-path": "^1.0.4" + }, + "engines": { + "node": ">=18" + } + }, + "node_modules/module-details-from-path": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/module-details-from-path/-/module-details-from-path-1.0.4.tgz", + "integrity": "sha512-EGWKgxALGMgzvxYF1UyGTy0HXX/2vHLkw6+NvDKW2jypWbHpjQuj4UMcqQWXHERJhVGKikolT06G3bcKe4fi7w==", + "license": "MIT" + }, + "node_modules/ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", + "license": "MIT" + }, + "node_modules/require-in-the-middle": { + "version": "8.0.1", + "resolved": "https://registry.npmjs.org/require-in-the-middle/-/require-in-the-middle-8.0.1.tgz", + "integrity": "sha512-QT7FVMXfWOYFbeRBF6nu+I6tr2Tf3u0q8RIEjNob/heKY/nh7drD/k7eeMFmSQgnTtCzLDcCu/XEnpW2wk4xCQ==", + "license": "MIT", + "dependencies": { + "debug": "^4.3.5", + "module-details-from-path": "^1.0.3" + }, + "engines": { + "node": ">=9.3.0 || >=8.10.0 <9.0.0" + } + } + } +} diff --git a/recipes/nextjs/platform/otel-package.json b/recipes/nextjs/platform/otel-package.json new file mode 100644 index 00000000..c4c19447 --- /dev/null +++ b/recipes/nextjs/platform/otel-package.json @@ -0,0 +1,17 @@ +{ + "name": "deployyy-otel", + "description": "OpenTelemetry for the Next.js processes of a recipe. The Dockerfile bundles otel.mjs into deployyy-otel.mjs.", + "private": true, + "type": "module", + "dependencies": { + "@opentelemetry/api": "1.9.1", + "@opentelemetry/api-logs": "0.223.2", + "@opentelemetry/instrumentation": "0.223.2", + "@opentelemetry/resources": "2.12.1", + "@opentelemetry/sdk-logs": "0.223.2", + "@opentelemetry/sdk-metrics": "2.12.1", + "@opentelemetry/sdk-trace-base": "2.12.1", + "@vercel/otel": "2.3.0", + "esbuild": "0.28.2" + } +} diff --git a/recipes/nextjs/platform/otel.mjs b/recipes/nextjs/platform/otel.mjs new file mode 100644 index 00000000..967a3acc --- /dev/null +++ b/recipes/nextjs/platform/otel.mjs @@ -0,0 +1,69 @@ +// The OpenTelemetry SDK of a Next.js process. The Dockerfile bundles this file with its packages +// into deployyy-otel.mjs, and the instrumentation file of inject-instrumentation.mjs loads it. +import { registerOTel } from '@vercel/otel' + +registerOTel({ + instrumentationConfig: { + fetch: { + // Only an address inside the cluster gets the traceparent: a Service name without a dot, + // or a name under .svc. + propagateContextUrls: [ + /^https?:\/\/[^/?#.:]+(:\d+)?([/?#]|$)/, + /^https?:\/\/[^/?#:]+\.svc(\.cluster\.local)?(:\d+)?([/?#]|$)/, + ], + }, + }, +}) + +const graphqlDocument = /^\s*(#[^\n]*\n\s*)*(query|mutation|subscription|fragment|\{)/ +const namedOperation = /\b(query|mutation|subscription)\s+([_A-Za-z][_0-9A-Za-z]*)/ + +// The GraphQL operation of a call: the operationName and the query of the URL (GET) or of the +// JSON body (POST). +function graphqlOperation(url, body) { + let name = url.searchParams.get('operationName') + let document = url.searchParams.get('query') + if (document === null && typeof body === 'string' && body.startsWith('{')) { + try { + const json = JSON.parse(body) + name ||= typeof json.operationName === 'string' ? json.operationName : null + document = typeof json.query === 'string' ? json.query : null + } catch { + return undefined + } + } + const isDocument = typeof document === 'string' && graphqlDocument.test(document) + if (!isDocument && !name) return undefined + const [, type, parsed] = (isDocument && namedOperation.exec(document)) || [] + return { name: name || parsed, type: type ?? (isDocument && /^\s*\{/.test(document) ? 'query' : undefined) } +} + +// A GraphQL call has the span name of the PHP request that answers it, such as +// `GET graphql CategoryPage`. The fetch of @vercel/otel reads the name from `opentelemetry`. +const instrumentedFetch = globalThis.fetch +globalThis.fetch = function fetch(input, init) { + if (!init?.opentelemetry?.spanName) { + try { + const request = input instanceof Request ? input : undefined + const url = new URL(request ? request.url : input instanceof URL ? input.href : String(input)) + const operation = graphqlOperation(url, init?.body) + if (operation) { + const method = (init?.method ?? request?.method ?? 'GET').toUpperCase() + const attributes = {} + if (operation.name) attributes['graphql.operation.name'] = operation.name + if (operation.type) attributes['graphql.operation.type'] = operation.type + init = { + ...init, + opentelemetry: { + ...init?.opentelemetry, + spanName: [method, 'graphql', operation.name].filter(Boolean).join(' '), + attributes: { ...attributes, ...init?.opentelemetry?.attributes }, + }, + } + } + } catch { + // A relative or invalid URL: fetch refuses it with its own error. + } + } + return instrumentedFetch(input, init) +}