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
7c33a217
Unverified
Commit
7c33a217
authored
Jul 11, 2021
by
Tim Neutkens
Committed by
GitHub
Jul 11, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add batching to zipkin reporter (#27082)
parent
ad56e81f
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
57 additions
and
19 deletions
+57
-19
to-zipkin.ts
packages/next/telemetry/trace/report/to-zipkin.ts
+57
-19
No files found.
packages/next/telemetry/trace/report/to-zipkin.ts
View file @
7c33a217
import
retry
from
'next/dist/compiled/async-retry'
import
{
randomBytes
}
from
'crypto'
import
fetch
from
'node-fetch'
import
*
as
Log
from
'../../../build/output/log'
let
traceId
=
process
.
env
.
TRACE_ID
let
batch
:
ReturnType
<
typeof
batcher
>
|
undefined
const
localEndpoint
=
{
serviceName
:
'nextjs'
,
...
...
@@ -12,6 +14,34 @@ const localEndpoint = {
const
zipkinUrl
=
`http://
${
localEndpoint
.
ipv4
}
:
${
localEndpoint
.
port
}
`
const
zipkinAPI
=
`
${
zipkinUrl
}
/api/v2/spans`
type
Event
=
{
traceId
:
string
parentId
?:
string
name
:
string
id
:
string
timestamp
:
number
duration
:
number
localEndpoint
:
typeof
localEndpoint
tags
?:
Object
}
// Batch events as zipkin allows for multiple events to be sent in one go
function
batcher
(
reportEvents
:
(
evts
:
Event
[])
=>
void
)
{
const
events
:
Event
[]
=
[]
let
timeout
:
ReturnType
<
typeof
setTimeout
>
|
undefined
return
(
event
:
Event
)
=>
{
events
.
push
(
event
)
// setTimeout is used instead of setInterval to ensure events sending does not block exiting the program
if
(
!
timeout
)
{
timeout
=
setTimeout
(()
=>
{
reportEvents
(
events
.
slice
())
events
.
length
=
0
timeout
=
undefined
},
1500
)
}
}
}
const
reportToLocalHost
=
(
name
:
string
,
duration
:
number
,
...
...
@@ -26,25 +56,33 @@ const reportToLocalHost = (
`Zipkin trace will be available on
${
zipkinUrl
}
/zipkin/traces/
${
traceId
}
`
)
}
const
body
=
[
{
traceId
,
parentId
,
name
,
id
,
timestamp
,
duration
,
localEndpoint
,
tags
:
attrs
,
},
]
// We intentionally do not block on I/O here.
fetch
(
zipkinAPI
,
{
method
:
'POST'
,
headers
:
{
'Content-Type'
:
'application/json'
},
body
:
JSON
.
stringify
(
body
),
}).
catch
(
console
.
log
)
if
(
!
batch
)
{
batch
=
batcher
((
events
)
=>
{
// Ensure ECONNRESET error is retried 3 times before erroring out
retry
(
()
=>
// Send events to zipkin
fetch
(
zipkinAPI
,
{
method
:
'POST'
,
headers
:
{
'Content-Type'
:
'application/json'
},
body
:
JSON
.
stringify
(
events
),
}),
{
minTimeout
:
500
,
retries
:
3
,
factor
:
1
}
).
catch
(
console
.
log
)
})
}
batch
({
traceId
,
parentId
,
name
,
id
,
timestamp
,
duration
,
localEndpoint
,
tags
:
attrs
,
})
}
export
default
reportToLocalHost
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