Skip to content

Commit 370ba8d

Browse files
povilasvdevjiwonchoiijjk
authored
[OTel] fix: Root span name should not include high cardinality URL (#75416)
> HTTP span names SHOULD be {method} {target} if there is a (low-cardinality) target available. If there is no (low-cardinality) {target} available, HTTP span names SHOULD be {method}. Some root spans don't have the `next.route` name available. In that case, we used the `req.url` in the name. However, the OTel docs recommend using only the method if the target is high cardinality. Therefore, removed the usage of `req.url` in the name of root spans. x-ref: [OTel docs](https://opentelemetry.io/docs/specs/semconv/http/http-spans/#:~:text=HTTP%20span%20names%20SHOULD%20be%20%7Bmethod%7D%20%7Btarget%7D%20if%20there%20is%20a%20(low%2Dcardinality)%20target%20available.%20If%20there%20is%20no%20(low%2Dcardinality)%20%7Btarget%7D%20available%2C%20HTTP%20span%20names%20SHOULD%20be%20%7Bmethod%7D.) Related: #59645 --------- Co-authored-by: Jiwon Choi <devjiwonchoi@gmail.com> Co-authored-by: JJ Kasper <jj@jjsweb.site>
1 parent fc307c4 commit 370ba8d

File tree

7 files changed

+21
-25
lines changed

7 files changed

+21
-25
lines changed

packages/next/src/build/templates/app-page.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ export async function handler(
459459
})
460460
span.updateName(name)
461461
} else {
462-
span.updateName(`${method} ${req.url}`)
462+
span.updateName(`${method}`)
463463
}
464464
})
465465
}
@@ -1396,7 +1396,7 @@ export async function handler(
13961396
tracer.trace(
13971397
BaseServerSpan.handleRequest,
13981398
{
1399-
spanName: `${method} ${req.url}`,
1399+
spanName: `${method}`,
14001400
kind: SpanKind.SERVER,
14011401
attributes: {
14021402
'http.method': method,

packages/next/src/build/templates/app-route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ export async function handler(
258258
})
259259
span.updateName(name)
260260
} else {
261-
span.updateName(`${method} ${req.url}`)
261+
span.updateName(`${method}`)
262262
}
263263
})
264264
}
@@ -454,7 +454,7 @@ export async function handler(
454454
tracer.trace(
455455
BaseServerSpan.handleRequest,
456456
{
457-
spanName: `${method} ${req.url}`,
457+
spanName: `${method}`,
458458
kind: SpanKind.SERVER,
459459
attributes: {
460460
'http.method': method,

packages/next/src/build/templates/edge-ssr-app.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ async function requestHandler(
317317
})
318318
span.updateName(name)
319319
} else {
320-
span.updateName(`${req.method} ${relativeUrl}`)
320+
span.updateName(`${req.method}`)
321321
}
322322
})
323323

@@ -340,7 +340,7 @@ async function requestHandler(
340340
tracer.trace(
341341
BaseServerSpan.handleRequest,
342342
{
343-
spanName: `${req.method} ${relativeUrl}`,
343+
spanName: `${req.method}`,
344344
kind: SpanKind.SERVER,
345345
attributes: {
346346
'http.method': req.method,

packages/next/src/build/templates/edge-ssr.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ async function requestHandler(
296296
})
297297
span.updateName(name)
298298
} else {
299-
span.updateName(`${req.method} ${relativeUrl}`)
299+
span.updateName(`${req.method}`)
300300
}
301301
})
302302

@@ -343,7 +343,7 @@ async function requestHandler(
343343
tracer.trace(
344344
BaseServerSpan.handleRequest,
345345
{
346-
spanName: `${req.method} ${relativeUrl}`,
346+
spanName: `${req.method}`,
347347
kind: SpanKind.SERVER,
348348
attributes: {
349349
'http.method': req.method,

packages/next/src/build/templates/pages-api.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export async function handler(
133133
})
134134
span.updateName(name)
135135
} else {
136-
span.updateName(`${method} ${req.url}`)
136+
span.updateName(`${method}`)
137137
}
138138
})
139139

@@ -146,7 +146,7 @@ export async function handler(
146146
tracer.trace(
147147
BaseServerSpan.handleRequest,
148148
{
149-
spanName: `${method} ${req.url}`,
149+
spanName: `${method}`,
150150
kind: SpanKind.SERVER,
151151
attributes: {
152152
'http.method': method,

packages/next/src/server/base-server.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -882,13 +882,13 @@ export default abstract class Server<
882882
): Promise<void> {
883883
await this.prepare()
884884
const method = req.method.toUpperCase()
885-
886885
const tracer = getTracer()
886+
887887
return tracer.withPropagatedContext(req.headers, () => {
888888
return tracer.trace(
889889
BaseServerSpan.handleRequest,
890890
{
891-
spanName: `${method} ${req.url}`,
891+
spanName: `${method}`,
892892
kind: SpanKind.SERVER,
893893
attributes: {
894894
'http.method': method,
@@ -944,11 +944,7 @@ export default abstract class Server<
944944
})
945945
span.updateName(name)
946946
} else {
947-
span.updateName(
948-
isRSCRequest
949-
? `RSC ${method} ${req.url}`
950-
: `${method} ${req.url}`
951-
)
947+
span.updateName(isRSCRequest ? `RSC ${method}` : `${method}`)
952948
}
953949
})
954950
)

test/e2e/opentelemetry/instrumentation/opentelemetry.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,10 @@ describe('opentelemetry', () => {
333333
traceId: env.span.traceId,
334334
parentId: env.span.rootParentId,
335335
runtime: 'nodejs',
336-
name: 'GET /app/param/rsc-fetch/edge',
336+
name: 'GET',
337337
kind: 1,
338338
attributes: {
339-
'next.span_name': 'GET /app/param/rsc-fetch/edge',
339+
'next.span_name': 'GET',
340340
'next.span_type': 'BaseServer.handleRequest',
341341
'http.method': 'GET',
342342
'http.target': '/app/param/rsc-fetch/edge',
@@ -468,10 +468,10 @@ describe('opentelemetry', () => {
468468
runtime: 'nodejs',
469469
traceId: env.span.traceId,
470470
parentId: env.span.rootParentId,
471-
name: 'GET /api/app/param/data/edge',
471+
name: 'GET',
472472
kind: 1,
473473
attributes: {
474-
'next.span_name': 'GET /api/app/param/data/edge',
474+
'next.span_name': 'GET',
475475
'next.span_type': 'BaseServer.handleRequest',
476476
'http.method': 'GET',
477477
'http.target': '/api/app/param/data/edge',
@@ -831,10 +831,10 @@ describe('opentelemetry', () => {
831831
runtime: 'nodejs',
832832
traceId: env.span.traceId,
833833
parentId: env.span.rootParentId,
834-
name: 'GET /pages/param/edge/getServerSideProps',
834+
name: 'GET',
835835
kind: 1,
836836
attributes: {
837-
'next.span_name': 'GET /pages/param/edge/getServerSideProps',
837+
'next.span_name': 'GET',
838838
'next.span_type': 'BaseServer.handleRequest',
839839
'http.method': 'GET',
840840
'http.target': '/pages/param/edge/getServerSideProps',
@@ -1100,10 +1100,10 @@ describe('opentelemetry', () => {
11001100
runtime: 'nodejs',
11011101
traceId: env.span.traceId,
11021102
parentId: env.span.rootParentId,
1103-
name: 'GET /api/pages/param/edge',
1103+
name: 'GET',
11041104
kind: 1,
11051105
attributes: {
1106-
'next.span_name': 'GET /api/pages/param/edge',
1106+
'next.span_name': 'GET',
11071107
'next.span_type': 'BaseServer.handleRequest',
11081108
'http.method': 'GET',
11091109
'http.target': '/api/pages/param/edge',

0 commit comments

Comments
 (0)