From bb8f0597eed532f1c398e635b3ac9cce93abc6c8 Mon Sep 17 00:00:00 2001 From: Gaspar Garcia Jr Date: Mon, 25 Mar 2024 09:33:27 -0700 Subject: [PATCH] =?UTF-8?q?Revert=20"Revert=20"Set=20the=20default=20opera?= =?UTF-8?q?tion=20name=20and=20resource=20name=20for=20web=20requ=E2=80=A6?= =?UTF-8?q?"=20(#60)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit b68b270497ee98f7009dedfd18c7f9fada3da6a3. --- .changeset/plenty-crabs-warn.md | 7 ++++ packages/otel/README.md | 2 +- .../src/processor/composite-span-processor.ts | 40 +++++++++++-------- packages/otel/src/sdk.ts | 4 +- packages/otel/src/types.ts | 2 +- .../vercel-deployment/api-inbound.test.ts | 4 +- tests/e2e/test/vercel-deployment/api.test.ts | 4 +- .../render-middleware.test.ts | 8 ++-- .../e2e/test/vercel-deployment/render.test.ts | 12 +++--- .../vercel-collector.test.ts | 4 +- 10 files changed, 50 insertions(+), 37 deletions(-) create mode 100644 .changeset/plenty-crabs-warn.md diff --git a/.changeset/plenty-crabs-warn.md b/.changeset/plenty-crabs-warn.md new file mode 100644 index 0000000..8e696ec --- /dev/null +++ b/.changeset/plenty-crabs-warn.md @@ -0,0 +1,7 @@ +--- +"@vercel/otel": minor +--- + +Set the default 'operation.name' attribute for web requests to 'web.request'. + +Update the default 'resource.name' attribute for web requests to the concatenation of the HTTP method and the HTTP route. diff --git a/packages/otel/README.md b/packages/otel/README.md index abb2b39..f6684fa 100644 --- a/packages/otel/README.md +++ b/packages/otel/README.md @@ -47,7 +47,7 @@ Registers the OpenTelemetry SDK with the specified service name and the default Registers the OpenTelemetry SDK with the specified configuration. Configuration options include: - `serviceName`: The name of your service, used as the app name in many OpenTelemetry backends. -- `attributes`: The resource attributes. By default, `@vercel/otel` configures relevant Vercel attributes based on [the environment](https://vercel.com/docs/projects/environment-variables/system-environment-variables), such as `vercel.env`, `vercel.runtime`, `vercel.host`, etc. The specified attributes are merged with default Vercel environment attributes. +- `attributes`: The resource attributes. By default, `@vercel/otel` configures relevant Vercel attributes based on [the environment](https://vercel.com/docs/projects/environment-variables/system-environment-variables), such as `env`, `vercel.runtime`, `vercel.host`, etc. - `instrumentations`: A set of instrumentations. By default, `@vercel/otel` configures "fetch" instrumentation. - `instrumentationConfig`: Customize configuration for predefined instrumentations: - `fetch`: Customize configuration of the predefined "fetch" instrumentation: diff --git a/packages/otel/src/processor/composite-span-processor.ts b/packages/otel/src/processor/composite-span-processor.ts index a272bbc..c4cdc27 100644 --- a/packages/otel/src/processor/composite-span-processor.ts +++ b/packages/otel/src/processor/composite-span-processor.ts @@ -153,37 +153,43 @@ function getResourceAttributes(span: ReadableSpan): Attributes | undefined { return undefined; } + const resourceNameResolved = + resourceName ?? + (httpMethod && + typeof httpMethod === "string" && + httpRoute && + typeof httpRoute === "string" + ? `${httpMethod} ${httpRoute}` + : httpRoute); + + if ( + span.kind === SpanKind.SERVER && + httpMethod && + httpRoute && + typeof httpMethod === "string" && + typeof httpRoute === "string" + ) { + return { + "operation.name": "web.request", + "resource.name": resourceNameResolved, + }; + } + // Per https://github.com/DataDog/datadog-agent/blob/main/pkg/config/config_template.yaml, // the default operation.name is "library name + span kind". const libraryName = span.instrumentationLibrary.name; - const spanType = nextSpanType ?? spanTypeAttr; if (spanType && typeof spanType === "string") { const nextOperationName = toOperationName(libraryName, spanType); if (httpRoute) { return { "operation.name": nextOperationName, - "resource.name": resourceName ?? httpRoute, + "resource.name": resourceNameResolved, }; } return { "operation.name": nextOperationName }; } - if ( - httpMethod && - httpRoute && - typeof httpMethod === "string" && - typeof httpRoute === "string" - ) { - return { - "operation.name": toOperationName( - libraryName, - `http.${SPAN_KIND_NAME[kind] || "internal"}.${httpMethod}` - ), - "resource.name": resourceName ?? httpRoute, - }; - } - return { "operation.name": toOperationName( libraryName, diff --git a/packages/otel/src/sdk.ts b/packages/otel/src/sdk.ts index 9ff6e62..40d10c0 100644 --- a/packages/otel/src/sdk.ts +++ b/packages/otel/src/sdk.ts @@ -103,8 +103,8 @@ export class Sdk { // Vercel. // https://vercel.com/docs/projects/environment-variables/system-environment-variables - "vercel.env": - process.env.VERCEL_ENV || process.env.NEXT_PUBLIC_VERCEL_ENV, + // Vercel Env set as top level attribute for simplicity. One of 'production', 'preview' or 'development'. + env: process.env.VERCEL_ENV || process.env.NEXT_PUBLIC_VERCEL_ENV, "vercel.region": process.env.VERCEL_REGION, "vercel.runtime": runtime, "vercel.sha": diff --git a/packages/otel/src/types.ts b/packages/otel/src/types.ts index 01b4e32..47d63d0 100644 --- a/packages/otel/src/types.ts +++ b/packages/otel/src/types.ts @@ -62,7 +62,7 @@ export interface Configuration { * including: * - `service.name` - the service name. * - `node.env` - the value of `NODE_ENV` environment variable. - * - `vercel.env` - the Vercel deployment environment such as "production" or "preview" (`VERCEL_ENV` environment variable). + * - `env` - the Vercel deployment environment such as "production" or "preview" (`VERCEL_ENV` environment variable). * - `vercel.region` - the Vercel deployment region (`VERCEL_REGION` environment variable). * - `vercel.runtime` - "nodejs" or "edge" (`NEXT_RUNTIME` environment variable). * - `vercel.sha` - the Vercel deployment Git SHA (`VERCEL_GIT_COMMIT_SHA` environment variable). diff --git a/tests/e2e/test/vercel-deployment/api-inbound.test.ts b/tests/e2e/test/vercel-deployment/api-inbound.test.ts index 283818c..cff36ae 100644 --- a/tests/e2e/test/vercel-deployment/api-inbound.test.ts +++ b/tests/e2e/test/vercel-deployment/api-inbound.test.ts @@ -23,7 +23,7 @@ describe("vercel deployment: api inbound", {}, (props) => { resource: { "service.name": "sample-app", "vercel.runtime": "nodejs", - "vercel.env": "test", + env: "test", }, attributes: { scope: "next.js", @@ -87,7 +87,7 @@ describe("vercel deployment: api inbound", {}, (props) => { resource: { "service.name": "sample-app", "vercel.runtime": "edge", - "vercel.env": "test", + env: "test", }, kind: SpanKind.INTERNAL, attributes: { diff --git a/tests/e2e/test/vercel-deployment/api.test.ts b/tests/e2e/test/vercel-deployment/api.test.ts index fd1820c..f3e397e 100644 --- a/tests/e2e/test/vercel-deployment/api.test.ts +++ b/tests/e2e/test/vercel-deployment/api.test.ts @@ -19,7 +19,7 @@ describe("vercel deployment: api", {}, (props) => { resource: { "service.name": "sample-app", "vercel.runtime": "nodejs", - "vercel.env": "test", + env: "test", }, attributes: { scope: "next.js", @@ -68,7 +68,7 @@ describe("vercel deployment: api", {}, (props) => { resource: { "service.name": "sample-app", "vercel.runtime": "edge", - "vercel.env": "test", + env: "test", }, kind: SpanKind.INTERNAL, attributes: { diff --git a/tests/e2e/test/vercel-deployment/render-middleware.test.ts b/tests/e2e/test/vercel-deployment/render-middleware.test.ts index d7c3323..d65234f 100644 --- a/tests/e2e/test/vercel-deployment/render-middleware.test.ts +++ b/tests/e2e/test/vercel-deployment/render-middleware.test.ts @@ -15,7 +15,7 @@ describe("vercel deployment: middleware", {}, (props) => { resource: { "service.name": "sample-app", "vercel.runtime": "edge", - "vercel.env": "test", + env: "test", }, attributes: { scope: "next.js", @@ -43,7 +43,7 @@ describe("vercel deployment: middleware", {}, (props) => { resource: { "service.name": "sample-app", "vercel.runtime": "nodejs", - "vercel.env": "test", + env: "test", }, attributes: { scope: "next.js", @@ -88,7 +88,7 @@ describe("vercel deployment: middleware", {}, (props) => { resource: { "service.name": "sample-app", "vercel.runtime": "edge", - "vercel.env": "test", + env: "test", }, attributes: { scope: "next.js", @@ -116,7 +116,7 @@ describe("vercel deployment: middleware", {}, (props) => { resource: { "service.name": "sample-app", "vercel.runtime": "edge", - "vercel.env": "test", + env: "test", }, attributes: { scope: "next.js", diff --git a/tests/e2e/test/vercel-deployment/render.test.ts b/tests/e2e/test/vercel-deployment/render.test.ts index 8adcee4..f253e07 100644 --- a/tests/e2e/test/vercel-deployment/render.test.ts +++ b/tests/e2e/test/vercel-deployment/render.test.ts @@ -17,7 +17,7 @@ describe("vercel deployment: render", {}, (props) => { resource: { "service.name": "sample-app", "vercel.runtime": "nodejs", - "vercel.env": "test", + env: "test", }, attributes: { scope: "next.js", @@ -29,8 +29,8 @@ describe("vercel deployment: render", {}, (props) => { "http.status_code": 200, "next.route": "/slugs/[slug]", "http.route": "/slugs/[slug]", - "operation.name": "next_js.BaseServer.handleRequest", - "resource.name": "/slugs/[slug]", + "operation.name": "web.request", + "resource.name": "GET /slugs/[slug]", }, spans: [ { @@ -73,7 +73,7 @@ describe("vercel deployment: render", {}, (props) => { resource: { "service.name": "sample-app", "vercel.runtime": "edge", - "vercel.env": "test", + env: "test", }, attributes: { scope: "next.js", @@ -84,8 +84,8 @@ describe("vercel deployment: render", {}, (props) => { "http.status_code": 200, "next.route": "/slugs/[slug]/edge", "http.route": "/slugs/[slug]/edge", - "operation.name": "next_js.BaseServer.handleRequest", - "resource.name": "/slugs/[slug]/edge", + "operation.name": "web.request", + "resource.name": "GET /slugs/[slug]/edge", }, spans: [ { diff --git a/tests/e2e/test/vercel-deployment/vercel-collector.test.ts b/tests/e2e/test/vercel-deployment/vercel-collector.test.ts index 6763ad4..6e5ee3d 100644 --- a/tests/e2e/test/vercel-deployment/vercel-collector.test.ts +++ b/tests/e2e/test/vercel-deployment/vercel-collector.test.ts @@ -31,7 +31,7 @@ describe( resource: { "service.name": "sample-app", "vercel.runtime": "nodejs", - "vercel.env": "test", + env: "test", }, attributes: { scope: "next.js", @@ -79,7 +79,7 @@ describe( resource: { "service.name": "sample-app", "vercel.runtime": "edge", - "vercel.env": "test", + env: "test", }, attributes: { scope: "next.js",