Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
d0ea2894
Unverified
Commit
d0ea2894
authored
Mar 28, 2024
by
Dima Voytenko
Committed by
GitHub
Mar 28, 2024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
OTEL: use the current context when creating a root span (#63825)
Fixes:
https://github.com/vercel/next.js/issues/63783
parent
3299fe0b
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
66 additions
and
3 deletions
+66
-3
tracer.ts
packages/next/src/server/lib/trace/tracer.ts
+1
-1
instrumentation-test.ts
test/e2e/opentelemetry/instrumentation-test.ts
+49
-2
opentelemetry.test.ts
test/e2e/opentelemetry/opentelemetry.test.ts
+16
-0
No files found.
packages/next/src/server/lib/trace/tracer.ts
View file @
d0ea2894
...
...
@@ -248,7 +248,7 @@ class NextTracerImpl implements NextTracer {
let
isRootSpan
=
false
if
(
!
spanContext
)
{
spanContext
=
ROOT_CONTEXT
spanContext
=
context
?.
active
()
??
ROOT_CONTEXT
isRootSpan
=
true
}
else
if
(
trace
.
getSpanContext
(
spanContext
)?.
isRemote
)
{
isRootSpan
=
true
...
...
test/e2e/opentelemetry/instrumentation-test.ts
View file @
d0ea2894
import
'./instrumentation-polyfill'
import
type
{
Context
,
TextMapGetter
,
TextMapSetter
,
TextMapPropagator
,
}
from
'@opentelemetry/api'
import
{
Resource
}
from
'@opentelemetry/resources'
import
{
SemanticResourceAttributes
}
from
'@opentelemetry/semantic-conventions'
import
{
BasicTracerProvider
}
from
'@opentelemetry/sdk-trace-base'
import
{
AsyncLocalStorageContextManager
}
from
'@opentelemetry/context-async-hooks'
import
{
Sampler
,
SamplingDecision
,
SimpleSpanProcessor
,
SpanExporter
,
ReadableSpan
,
}
from
'@opentelemetry/sdk-trace-base'
import
{
CompositePropagator
,
ExportResult
,
ExportResultCode
,
W3CTraceContextPropagator
,
hrTimeToMicroseconds
,
}
from
'@opentelemetry/core'
import
{
SavedSpan
}
from
'./constants'
const
customKey
=
Symbol
.
for
(
'opentelemetry.test/custom'
)
const
serializeSpan
=
(
span
:
ReadableSpan
):
SavedSpan
=>
({
runtime
:
process
.
env
.
NEXT_RUNTIME
,
traceId
:
span
.
spanContext
().
traceId
,
...
...
@@ -80,6 +92,7 @@ export const register = () => {
resource
:
new
Resource
({
[
SemanticResourceAttributes
.
SERVICE_NAME
]:
'test-next-app'
,
}),
sampler
:
new
CustomSampler
(),
})
if
(
!
process
.
env
.
TEST_OTEL_COLLECTOR_PORT
)
{
...
...
@@ -88,6 +101,40 @@ export const register = () => {
const
port
=
parseInt
(
process
.
env
.
TEST_OTEL_COLLECTOR_PORT
)
provider
.
addSpanProcessor
(
new
SimpleSpanProcessor
(
new
TestExporter
(
port
)))
// Make sure to register you provider
provider
.
register
({
contextManager
})
provider
.
register
({
contextManager
,
propagator
:
new
CompositePropagator
({
propagators
:
[
new
CustomPropagator
(),
new
W3CTraceContextPropagator
()],
}),
})
}
class
CustomPropagator
implements
TextMapPropagator
{
fields
():
string
[]
{
return
[
'x-custom'
]
}
inject
(
context
:
Context
,
carrier
:
unknown
,
setter
:
TextMapSetter
):
void
{}
extract
(
context
:
Context
,
carrier
:
unknown
,
getter
:
TextMapGetter
):
Context
{
const
value
=
getter
.
get
(
carrier
,
'x-custom'
)
if
(
!
value
)
{
return
context
}
return
context
.
setValue
(
customKey
,
value
)
}
}
class
CustomSampler
implements
Sampler
{
shouldSample
(
context
)
{
const
value
=
context
.
getValue
(
customKey
)
return
{
decision
:
SamplingDecision
.
RECORD_AND_SAMPLED
,
attributes
:
value
?
{
custom
:
value
}
:
{},
}
}
toString
()
{
return
'CustomSampler'
}
}
test/e2e/opentelemetry/opentelemetry.test.ts
View file @
d0ea2894
...
...
@@ -200,6 +200,22 @@ createNextDescribe(
])
})
it
(
'should propagate custom context without span'
,
async
()
=>
{
await
next
.
fetch
(
'/app/param/rsc-fetch'
,
{
...
env
.
fetchInit
,
headers
:
{
...
env
.
fetchInit
?.
headers
,
'x-custom'
:
'custom1'
},
})
await
expectTrace
(
getCollector
(),
[
{
name
:
'GET /app/[param]/rsc-fetch'
,
attributes
:
{
custom
:
'custom1'
,
},
},
])
})
it
(
'should handle RSC with fetch on edge'
,
async
()
=>
{
await
next
.
fetch
(
'/app/param/rsc-fetch/edge'
,
env
.
fetchInit
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment