Skip to content

Commit

Permalink
Set the default operation name and resource name for web requests (#57)
Browse files Browse the repository at this point in the history
* Set the default operation name and resource name for web requests

This change sets the default 'operation.name' and 'resource.name' attributes for web requests.
The 'resource.name' is set to the concatenation of the HTTP method and the HTTP route.
The 'operation.name' is set to the string 'web.request'

The effect is that we can view all associated web requests in the UI by filtering on the 'operation.name' attribute.

* Add changeset

* Remove vercel.env (#58)

* missing vercel.env
  • Loading branch information
gaspar09 authored Mar 21, 2024
1 parent cc4f6b0 commit 84e2e04
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 37 deletions.
7 changes: 7 additions & 0 deletions .changeset/plenty-crabs-warn.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion packages/otel/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
- `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:
Expand Down
40 changes: 23 additions & 17 deletions packages/otel/src/processor/composite-span-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions packages/otel/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
2 changes: 1 addition & 1 deletion packages/otel/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/test/vercel-deployment/api-inbound.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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: {
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/test/vercel-deployment/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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: {
Expand Down
8 changes: 4 additions & 4 deletions tests/e2e/test/vercel-deployment/render-middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
12 changes: 6 additions & 6 deletions tests/e2e/test/vercel-deployment/render.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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: [
{
Expand Down Expand Up @@ -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",
Expand All @@ -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: [
{
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/test/vercel-deployment/vercel-collector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe(
resource: {
"service.name": "sample-app",
"vercel.runtime": "nodejs",
"vercel.env": "test",
env: "test",
},
attributes: {
scope: "next.js",
Expand Down Expand Up @@ -79,7 +79,7 @@ describe(
resource: {
"service.name": "sample-app",
"vercel.runtime": "edge",
"vercel.env": "test",
env: "test",
},
attributes: {
scope: "next.js",
Expand Down

0 comments on commit 84e2e04

Please sign in to comment.