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
9316dc0a
Unverified
Commit
9316dc0a
authored
Aug 25, 2021
by
Tim Neutkens
Committed by
GitHub
Aug 25, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add default trace format that is exported automatically (#28461)
Co-authored-by:
Steven
<
steven@ceriously.com
>
parent
706547e0
Hide whitespace changes
Inline
Side-by-side
Showing
15 changed files
with
258 additions
and
80 deletions
+258
-80
index.ts
packages/next/build/babel/loader/index.ts
+1
-1
types.d.ts
packages/next/build/babel/loader/types.d.ts
+1
-1
index.ts
packages/next/build/index.ts
+3
-1
webpack-config.ts
packages/next/build/webpack-config.ts
+1
-1
index.ts
...ext/build/webpack/loaders/next-serverless-loader/index.ts
+47
-51
next-swc-loader.js
packages/next/build/webpack/loaders/next-swc-loader.js
+1
-1
build-stats-plugin.ts
packages/next/build/webpack/plugins/build-stats-plugin.ts
+1
-1
css-minimizer-plugin.ts
packages/next/build/webpack/plugins/css-minimizer-plugin.ts
+3
-4
profiling-plugin.ts
packages/next/build/webpack/plugins/profiling-plugin.ts
+17
-8
hot-reloader.ts
packages/next/server/dev/hot-reloader.ts
+6
-0
index.ts
packages/next/telemetry/trace/report/index.ts
+34
-5
to-jaeger.ts
packages/next/telemetry/trace/report/to-jaeger.ts
+2
-2
to-json.ts
packages/next/telemetry/trace/report/to-json.ts
+71
-0
to-zipkin.ts
packages/next/telemetry/trace/report/to-zipkin.ts
+5
-4
send-trace-to-jaeger.mjs
scripts/send-trace-to-jaeger.mjs
+65
-0
No files found.
packages/next/build/babel/loader/index.ts
View file @
9316dc0a
...
@@ -40,7 +40,7 @@ const nextBabelLoaderOuter = function nextBabelLoaderOuter(
...
@@ -40,7 +40,7 @@ const nextBabelLoaderOuter = function nextBabelLoaderOuter(
)
{
)
{
const
callback
=
this
.
async
()
const
callback
=
this
.
async
()
const
loaderSpan
=
t
race
(
'next-babel-turbo-loader'
,
this
.
currentTraceSpan
?.
id
)
const
loaderSpan
=
t
his
.
currentTraceSpan
.
traceChild
(
'next-babel-turbo-loader'
)
loaderSpan
loaderSpan
.
traceAsyncFn
(()
=>
.
traceAsyncFn
(()
=>
nextBabelLoader
.
call
(
this
,
loaderSpan
,
inputSource
,
inputSourceMap
)
nextBabelLoader
.
call
(
this
,
loaderSpan
,
inputSource
,
inputSourceMap
)
...
...
packages/next/build/babel/loader/types.d.ts
View file @
9316dc0a
...
@@ -2,7 +2,7 @@ import { loader } from 'next/dist/compiled/webpack/webpack'
...
@@ -2,7 +2,7 @@ import { loader } from 'next/dist/compiled/webpack/webpack'
import
{
Span
}
from
'../../../telemetry/trace'
import
{
Span
}
from
'../../../telemetry/trace'
export
interface
NextJsLoaderContext
extends
loader
.
LoaderContext
{
export
interface
NextJsLoaderContext
extends
loader
.
LoaderContext
{
currentTraceSpan
?
:
Span
currentTraceSpan
:
Span
}
}
export
interface
NextBabelLoaderOptions
{
export
interface
NextBabelLoaderOptions
{
...
...
packages/next/build/index.ts
View file @
9316dc0a
...
@@ -130,11 +130,13 @@ export default async function build(
...
@@ -130,11 +130,13 @@ export default async function build(
const
config
:
NextConfigComplete
=
await
nextBuildSpan
const
config
:
NextConfigComplete
=
await
nextBuildSpan
.
traceChild
(
'load-next-config'
)
.
traceChild
(
'load-next-config'
)
.
traceAsyncFn
(()
=>
loadConfig
(
PHASE_PRODUCTION_BUILD
,
dir
,
conf
))
.
traceAsyncFn
(()
=>
loadConfig
(
PHASE_PRODUCTION_BUILD
,
dir
,
conf
))
const
distDir
=
path
.
join
(
dir
,
config
.
distDir
)
setGlobal
(
'distDir'
,
distDir
)
const
{
target
}
=
config
const
{
target
}
=
config
const
buildId
:
string
=
await
nextBuildSpan
const
buildId
:
string
=
await
nextBuildSpan
.
traceChild
(
'generate-buildid'
)
.
traceChild
(
'generate-buildid'
)
.
traceAsyncFn
(()
=>
generateBuildId
(
config
.
generateBuildId
,
nanoid
))
.
traceAsyncFn
(()
=>
generateBuildId
(
config
.
generateBuildId
,
nanoid
))
const
distDir
=
path
.
join
(
dir
,
config
.
distDir
)
const
customRoutes
:
CustomRoutes
=
await
nextBuildSpan
const
customRoutes
:
CustomRoutes
=
await
nextBuildSpan
.
traceChild
(
'load-custom-routes'
)
.
traceChild
(
'load-custom-routes'
)
...
...
packages/next/build/webpack-config.ts
View file @
9316dc0a
...
@@ -240,7 +240,7 @@ export default async function getBaseWebpackConfig(
...
@@ -240,7 +240,7 @@ export default async function getBaseWebpackConfig(
entrypoints
:
WebpackEntrypoints
entrypoints
:
WebpackEntrypoints
rewrites
:
CustomRoutes
[
'rewrites'
]
rewrites
:
CustomRoutes
[
'rewrites'
]
isDevFallback
?:
boolean
isDevFallback
?:
boolean
runWebpackSpan
?
:
Span
runWebpackSpan
:
Span
}
}
):
Promise
<
webpack
.
Configuration
>
{
):
Promise
<
webpack
.
Configuration
>
{
const
hasRewrites
=
const
hasRewrites
=
...
...
packages/next/build/webpack/loaders/next-serverless-loader/index.ts
View file @
9316dc0a
...
@@ -11,7 +11,6 @@ import {
...
@@ -11,7 +11,6 @@ import {
ROUTES_MANIFEST
,
ROUTES_MANIFEST
,
REACT_LOADABLE_MANIFEST
,
REACT_LOADABLE_MANIFEST
,
}
from
'../../../../shared/lib/constants'
}
from
'../../../../shared/lib/constants'
import
{
trace
}
from
'../../../../telemetry/trace'
export
type
ServerlessLoaderQuery
=
{
export
type
ServerlessLoaderQuery
=
{
page
:
string
page
:
string
...
@@ -34,63 +33,61 @@ export type ServerlessLoaderQuery = {
...
@@ -34,63 +33,61 @@ export type ServerlessLoaderQuery = {
}
}
const
nextServerlessLoader
:
webpack
.
loader
.
Loader
=
function
()
{
const
nextServerlessLoader
:
webpack
.
loader
.
Loader
=
function
()
{
const
loaderSpan
=
trace
(
'next-serverless-loader'
)
const
{
return
loaderSpan
.
traceFn
(()
=>
{
distDir
,
const
{
absolutePagePath
,
distDir
,
page
,
absolutePagePath
,
buildId
,
page
,
canonicalBase
,
buildId
,
assetPrefix
,
canonicalBase
,
absoluteAppPath
,
assetPrefix
,
absoluteDocumentPath
,
absoluteAppPath
,
absoluteErrorPath
,
absoluteDocumentPath
,
absolute404Path
,
absoluteErrorPath
,
generateEtags
,
absolute404Path
,
poweredByHeader
,
generateEtags
,
basePath
,
poweredByHeader
,
runtimeConfig
,
basePath
,
previewProps
,
runtimeConfig
,
loadedEnvFiles
,
previewProps
,
i18n
,
loadedEnvFiles
,
}:
ServerlessLoaderQuery
=
i18n
,
typeof
this
.
query
===
'string'
?
parse
(
this
.
query
.
substr
(
1
))
:
this
.
query
}:
ServerlessLoaderQuery
=
typeof
this
.
query
===
'string'
?
parse
(
this
.
query
.
substr
(
1
))
:
this
.
query
const
buildManifest
=
join
(
distDir
,
BUILD_MANIFEST
).
replace
(
/
\\
/g
,
'/'
)
const
reactLoadableManifest
=
join
(
distDir
,
REACT_LOADABLE_MANIFEST
).
replace
(
const
buildManifest
=
join
(
distDir
,
BUILD_MANIFEST
).
replace
(
/
\\
/g
,
'/'
)
/
\\
/g
,
const
reactLoadableManifest
=
join
(
'/'
distDir
,
)
REACT_LOADABLE_MANIFEST
const
routesManifest
=
join
(
distDir
,
ROUTES_MANIFEST
).
replace
(
/
\\
/g
,
'/'
)
).
replace
(
/
\\
/g
,
'/'
)
const
routesManifest
=
join
(
distDir
,
ROUTES_MANIFEST
).
replace
(
/
\\
/g
,
'/'
)
const
escapedBuildId
=
escapeRegexp
(
buildId
)
const
pageIsDynamicRoute
=
isDynamicRoute
(
page
)
const
escapedBuildId
=
escapeRegexp
(
buildId
)
const
pageIsDynamicRoute
=
isDynamicRoute
(
page
)
const
encodedPreviewProps
=
devalue
(
JSON
.
parse
(
previewProps
)
as
__ApiPreviewProps
const
encodedPreviewProps
=
devalue
(
)
JSON
.
parse
(
previewProps
)
as
__ApiPreviewProps
)
const
envLoading
=
`
const
envLoading
=
`
const { processEnv } = require('@next/env')
const { processEnv } = require('@next/env')
processEnv(
${
Buffer
.
from
(
loadedEnvFiles
,
'base64'
).
toString
()}
)
processEnv(
${
Buffer
.
from
(
loadedEnvFiles
,
'base64'
).
toString
()}
)
`
`
const
runtimeConfigImports
=
runtimeConfig
const
runtimeConfigImports
=
runtimeConfig
?
`
?
`
const { setConfig } = require('next/config')
const { setConfig } = require('next/config')
`
`
:
''
:
''
const
runtimeConfigSetter
=
runtimeConfig
const
runtimeConfigSetter
=
runtimeConfig
?
`
?
`
const runtimeConfig =
${
runtimeConfig
}
const runtimeConfig =
${
runtimeConfig
}
setConfig(runtimeConfig)
setConfig(runtimeConfig)
`
`
:
'const runtimeConfig = {}'
:
'const runtimeConfig = {}'
if
(
page
.
match
(
API_ROUTE
))
{
if
(
page
.
match
(
API_ROUTE
))
{
return
`
return
`
${
envLoading
}
${
envLoading
}
${
runtimeConfigImports
}
${
runtimeConfigImports
}
${
${
...
@@ -125,8 +122,8 @@ const nextServerlessLoader: webpack.loader.Loader = function () {
...
@@ -125,8 +122,8 @@ const nextServerlessLoader: webpack.loader.Loader = function () {
})
})
export default apiHandler
export default apiHandler
`
`
}
else
{
}
else
{
return
`
return
`
import 'next/dist/server/node-polyfill-fetch'
import 'next/dist/server/node-polyfill-fetch'
import routesManifest from '
${
routesManifest
}
'
import routesManifest from '
${
routesManifest
}
'
import buildManifest from '
${
buildManifest
}
'
import buildManifest from '
${
buildManifest
}
'
...
@@ -206,8 +203,7 @@ const nextServerlessLoader: webpack.loader.Loader = function () {
...
@@ -206,8 +203,7 @@ const nextServerlessLoader: webpack.loader.Loader = function () {
})
})
export { renderReqToHTML, render }
export { renderReqToHTML, render }
`
`
}
}
})
}
}
export
default
nextServerlessLoader
export
default
nextServerlessLoader
packages/next/build/webpack/loaders/next-swc-loader.js
View file @
9316dc0a
...
@@ -123,7 +123,7 @@ async function loaderTransform(parentTrace, source, inputSourceMap) {
...
@@ -123,7 +123,7 @@ async function loaderTransform(parentTrace, source, inputSourceMap) {
}
}
export
default
function
swcLoader
(
inputSource
,
inputSourceMap
)
{
export
default
function
swcLoader
(
inputSource
,
inputSourceMap
)
{
const
loaderSpan
=
t
race
(
'next-swc-loader'
,
this
.
currentTraceSpan
?.
id
)
const
loaderSpan
=
t
his
.
currentTraceSpan
.
traceChild
(
'next-swc-loader'
)
const
callback
=
this
.
async
()
const
callback
=
this
.
async
()
loaderSpan
loaderSpan
.
traceAsyncFn
(()
=>
.
traceAsyncFn
(()
=>
...
...
packages/next/build/webpack/plugins/build-stats-plugin.ts
View file @
9316dc0a
...
@@ -118,7 +118,7 @@ export default class BuildStatsPlugin {
...
@@ -118,7 +118,7 @@ export default class BuildStatsPlugin {
async
(
stats
,
callback
)
=>
{
async
(
stats
,
callback
)
=>
{
const
compilerSpan
=
spans
.
get
(
compiler
)
const
compilerSpan
=
spans
.
get
(
compiler
)
try
{
try
{
const
writeStatsSpan
=
trace
(
'NextJsBuildStats'
,
compilerSpan
?.
id
)
const
writeStatsSpan
=
compilerSpan
!
.
traceChild
(
'NextJsBuildStats'
)
await
writeStatsSpan
.
traceAsyncFn
(()
=>
{
await
writeStatsSpan
.
traceAsyncFn
(()
=>
{
return
new
Promise
((
resolve
,
reject
)
=>
{
return
new
Promise
((
resolve
,
reject
)
=>
{
const
statsJson
=
reduceSize
(
const
statsJson
=
reduceSize
(
...
...
packages/next/build/webpack/plugins/css-minimizer-plugin.ts
View file @
9316dc0a
...
@@ -71,9 +71,8 @@ export class CssMinimizerPlugin {
...
@@ -71,9 +71,8 @@ export class CssMinimizerPlugin {
},
},
async
(
assets
:
any
)
=>
{
async
(
assets
:
any
)
=>
{
const
compilerSpan
=
spans
.
get
(
compiler
)
const
compilerSpan
=
spans
.
get
(
compiler
)
const
cssMinimizerSpan
=
trace
(
const
cssMinimizerSpan
=
compilerSpan
!
.
traceChild
(
'css-minimizer-plugin'
,
'css-minimizer-plugin'
compilerSpan
?.
id
)
)
cssMinimizerSpan
.
setAttribute
(
'webpackVersion'
,
5
)
cssMinimizerSpan
.
setAttribute
(
'webpackVersion'
,
5
)
...
@@ -83,7 +82,7 @@ export class CssMinimizerPlugin {
...
@@ -83,7 +82,7 @@ export class CssMinimizerPlugin {
files
files
.
filter
((
file
)
=>
CSS_REGEX
.
test
(
file
))
.
filter
((
file
)
=>
CSS_REGEX
.
test
(
file
))
.
map
(
async
(
file
)
=>
{
.
map
(
async
(
file
)
=>
{
const
assetSpan
=
trace
(
'minify-css'
,
cssMinimizerSpan
.
id
)
const
assetSpan
=
cssMinimizerSpan
.
traceChild
(
'minify-css'
)
assetSpan
.
setAttribute
(
'file'
,
file
)
assetSpan
.
setAttribute
(
'file'
,
file
)
return
assetSpan
.
traceAsyncFn
(
async
()
=>
{
return
assetSpan
.
traceAsyncFn
(
async
()
=>
{
...
...
packages/next/build/webpack/plugins/profiling-plugin.ts
View file @
9316dc0a
...
@@ -15,9 +15,9 @@ function getNormalModuleLoaderHook(compilation: any) {
...
@@ -15,9 +15,9 @@ function getNormalModuleLoaderHook(compilation: any) {
export
class
ProfilingPlugin
{
export
class
ProfilingPlugin
{
compiler
:
any
compiler
:
any
runWebpackSpan
:
Span
|
undefined
runWebpackSpan
:
Span
constructor
({
runWebpackSpan
}:
{
runWebpackSpan
:
Span
|
undefined
})
{
constructor
({
runWebpackSpan
}:
{
runWebpackSpan
:
Span
})
{
this
.
runWebpackSpan
=
runWebpackSpan
this
.
runWebpackSpan
=
runWebpackSpan
}
}
apply
(
compiler
:
any
)
{
apply
(
compiler
:
any
)
{
...
@@ -35,14 +35,17 @@ export class ProfilingPlugin {
...
@@ -35,14 +35,17 @@ export class ProfilingPlugin {
attrs
,
attrs
,
onSetSpan
,
onSetSpan
,
}:
{
}:
{
parentSpan
?:
()
=>
Span
|
undefined
parentSpan
?:
()
=>
Span
attrs
?:
any
attrs
?:
any
onSetSpan
?:
(
span
:
Span
)
=>
void
onSetSpan
?:
(
span
:
Span
)
=>
void
}
=
{}
}
=
{}
)
{
)
{
let
span
:
Span
|
undefined
let
span
:
Span
|
undefined
startHook
.
tap
(
pluginName
,
()
=>
{
startHook
.
tap
(
pluginName
,
()
=>
{
span
=
trace
(
spanName
,
parentSpan
?.()?.
id
,
attrs
?
attrs
()
:
attrs
)
span
=
parentSpan
?
parentSpan
().
traceChild
(
spanName
,
attrs
?
attrs
()
:
attrs
)
:
trace
(
spanName
,
undefined
,
attrs
?
attrs
()
:
attrs
)
onSetSpan
?.(
span
)
onSetSpan
?.(
span
)
})
})
stopHook
.
tap
(
pluginName
,
()
=>
{
stopHook
.
tap
(
pluginName
,
()
=>
{
...
@@ -103,10 +106,16 @@ export class ProfilingPlugin {
...
@@ -103,10 +106,16 @@ export class ProfilingPlugin {
const
issuerModule
=
compilation
?.
moduleGraph
?.
getIssuer
(
module
)
const
issuerModule
=
compilation
?.
moduleGraph
?.
getIssuer
(
module
)
const
span
=
trace
(
let
span
:
Span
`build-module
${
moduleType
?
`-
${
moduleType
}
`
:
''
}
`
,
issuerModule
?
spans
.
get
(
issuerModule
)?.
id
:
compilerSpan
.
id
const
spanName
=
`build-module
${
moduleType
?
`-
${
moduleType
}
`
:
''
}
`
)
const
issuerSpan
:
Span
|
undefined
=
issuerModule
&&
spans
.
get
(
issuerModule
)
if
(
issuerSpan
)
{
span
=
issuerSpan
.
traceChild
(
spanName
)
}
else
{
span
=
compilerSpan
.
traceChild
(
spanName
)
}
span
.
setAttribute
(
'name'
,
module
.
userRequest
)
span
.
setAttribute
(
'name'
,
module
.
userRequest
)
spans
.
set
(
module
,
span
)
spans
.
set
(
module
,
span
)
})
})
...
...
packages/next/server/dev/hot-reloader.ts
View file @
9316dc0a
...
@@ -27,6 +27,7 @@ import { difference } from '../../build/utils'
...
@@ -27,6 +27,7 @@ import { difference } from '../../build/utils'
import
{
NextConfigComplete
}
from
'../config-shared'
import
{
NextConfigComplete
}
from
'../config-shared'
import
{
CustomRoutes
}
from
'../../lib/load-custom-routes'
import
{
CustomRoutes
}
from
'../../lib/load-custom-routes'
import
{
DecodeError
}
from
'../../shared/lib/utils'
import
{
DecodeError
}
from
'../../shared/lib/utils'
import
{
Span
,
trace
}
from
'../../telemetry/trace'
export
async
function
renderScriptError
(
export
async
function
renderScriptError
(
res
:
ServerResponse
,
res
:
ServerResponse
,
...
@@ -143,6 +144,7 @@ export default class HotReloader {
...
@@ -143,6 +144,7 @@ export default class HotReloader {
private
watcher
:
any
private
watcher
:
any
private
rewrites
:
CustomRoutes
[
'rewrites'
]
private
rewrites
:
CustomRoutes
[
'rewrites'
]
private
fallbackWatcher
:
any
private
fallbackWatcher
:
any
private
hotReloaderSpan
:
Span
public
isWebpack5
:
any
public
isWebpack5
:
any
constructor
(
constructor
(
...
@@ -174,6 +176,7 @@ export default class HotReloader {
...
@@ -174,6 +176,7 @@ export default class HotReloader {
this
.
previewProps
=
previewProps
this
.
previewProps
=
previewProps
this
.
rewrites
=
rewrites
this
.
rewrites
=
rewrites
this
.
isWebpack5
=
isWebpack5
this
.
isWebpack5
=
isWebpack5
this
.
hotReloaderSpan
=
trace
(
'hot-reloader'
)
}
}
public
async
run
(
public
async
run
(
...
@@ -283,6 +286,7 @@ export default class HotReloader {
...
@@ -283,6 +286,7 @@ export default class HotReloader {
pagesDir
:
this
.
pagesDir
,
pagesDir
:
this
.
pagesDir
,
rewrites
:
this
.
rewrites
,
rewrites
:
this
.
rewrites
,
entrypoints
:
entrypoints
.
client
,
entrypoints
:
entrypoints
.
client
,
runWebpackSpan
:
this
.
hotReloaderSpan
,
}),
}),
getBaseWebpackConfig
(
this
.
dir
,
{
getBaseWebpackConfig
(
this
.
dir
,
{
dev
:
true
,
dev
:
true
,
...
@@ -292,6 +296,7 @@ export default class HotReloader {
...
@@ -292,6 +296,7 @@ export default class HotReloader {
pagesDir
:
this
.
pagesDir
,
pagesDir
:
this
.
pagesDir
,
rewrites
:
this
.
rewrites
,
rewrites
:
this
.
rewrites
,
entrypoints
:
entrypoints
.
server
,
entrypoints
:
entrypoints
.
server
,
runWebpackSpan
:
this
.
hotReloaderSpan
,
}),
}),
])
])
}
}
...
@@ -300,6 +305,7 @@ export default class HotReloader {
...
@@ -300,6 +305,7 @@ export default class HotReloader {
if
(
this
.
fallbackWatcher
)
return
if
(
this
.
fallbackWatcher
)
return
const
fallbackConfig
=
await
getBaseWebpackConfig
(
this
.
dir
,
{
const
fallbackConfig
=
await
getBaseWebpackConfig
(
this
.
dir
,
{
runWebpackSpan
:
this
.
hotReloaderSpan
,
dev
:
true
,
dev
:
true
,
isServer
:
false
,
isServer
:
false
,
config
:
this
.
config
,
config
:
this
.
config
,
...
...
packages/next/telemetry/trace/report/index.ts
View file @
9316dc0a
...
@@ -3,6 +3,7 @@ import reportToConsole from './to-console'
...
@@ -3,6 +3,7 @@ import reportToConsole from './to-console'
import
reportToZipkin
from
'./to-zipkin'
import
reportToZipkin
from
'./to-zipkin'
import
reportToJaeger
from
'./to-jaeger'
import
reportToJaeger
from
'./to-jaeger'
import
reportToTelemetry
from
'./to-telemetry'
import
reportToTelemetry
from
'./to-telemetry'
import
reportToJson
from
'./to-json'
type
Reporter
=
{
type
Reporter
=
{
flushAll
:
()
=>
Promise
<
void
>
|
void
flushAll
:
()
=>
Promise
<
void
>
|
void
...
@@ -16,6 +17,31 @@ type Reporter = {
...
@@ -16,6 +17,31 @@ type Reporter = {
)
=>
void
)
=>
void
}
}
class
MultiReporter
implements
Reporter
{
private
reporters
:
Reporter
[]
=
[]
constructor
(
reporters
:
Reporter
[])
{
this
.
reporters
=
reporters
}
async
flushAll
()
{
await
Promise
.
all
(
this
.
reporters
.
map
((
reporter
)
=>
reporter
.
flushAll
()))
}
report
(
spanName
:
string
,
duration
:
number
,
timestamp
:
number
,
id
:
SpanId
,
parentId
?:
SpanId
,
attrs
?:
Object
)
{
this
.
reporters
.
forEach
((
reporter
)
=>
reporter
.
report
(
spanName
,
duration
,
timestamp
,
id
,
parentId
,
attrs
)
)
}
}
const
target
=
const
target
=
process
.
env
.
TRACE_TARGET
&&
process
.
env
.
TRACE_TARGET
in
TARGET
process
.
env
.
TRACE_TARGET
&&
process
.
env
.
TRACE_TARGET
in
TARGET
?
TARGET
[
process
.
env
.
TRACE_TARGET
as
TARGET
]
?
TARGET
[
process
.
env
.
TRACE_TARGET
as
TARGET
]
...
@@ -27,14 +53,17 @@ if (process.env.TRACE_TARGET && !target) {
...
@@ -27,14 +53,17 @@ if (process.env.TRACE_TARGET && !target) {
)
)
}
}
export
let
r
eporter
:
Reporter
let
traceTargetR
eporter
:
Reporter
if
(
target
===
TARGET
.
CONSOLE
)
{
if
(
target
===
TARGET
.
CONSOLE
)
{
r
eporter
=
reportToConsole
traceTargetR
eporter
=
reportToConsole
}
else
if
(
target
===
TARGET
.
ZIPKIN
)
{
}
else
if
(
target
===
TARGET
.
ZIPKIN
)
{
r
eporter
=
reportToZipkin
traceTargetR
eporter
=
reportToZipkin
}
else
if
(
target
===
TARGET
.
JAEGER
)
{
}
else
if
(
target
===
TARGET
.
JAEGER
)
{
r
eporter
=
reportToJaeger
traceTargetR
eporter
=
reportToJaeger
}
else
{
}
else
{
r
eporter
=
reportToTelemetry
traceTargetR
eporter
=
reportToTelemetry
}
}
// JSON is always reported to allow for diagnostics
export
const
reporter
=
new
MultiReporter
([
reportToJson
,
traceTargetReporter
])
packages/next/telemetry/trace/report/to-jaeger.ts
View file @
9316dc0a
...
@@ -5,7 +5,7 @@ import * as Log from '../../../build/output/log'
...
@@ -5,7 +5,7 @@ import * as Log from '../../../build/output/log'
// Jaeger uses Zipkin's reporting
// Jaeger uses Zipkin's reporting
import
{
batcher
}
from
'./to-zipkin'
import
{
batcher
}
from
'./to-zipkin'
let
traceId
=
process
.
env
.
TRACE_ID
let
traceId
:
string
let
batch
:
ReturnType
<
typeof
batcher
>
|
undefined
let
batch
:
ReturnType
<
typeof
batcher
>
|
undefined
const
localEndpoint
=
{
const
localEndpoint
=
{
...
@@ -33,7 +33,7 @@ const reportToLocalHost = (
...
@@ -33,7 +33,7 @@ const reportToLocalHost = (
attrs
?:
Object
attrs
?:
Object
)
=>
{
)
=>
{
if
(
!
traceId
)
{
if
(
!
traceId
)
{
traceId
=
process
.
env
.
TRACE_ID
=
randomBytes
(
8
).
toString
(
'hex'
)
traceId
=
process
.
env
.
TRACE_ID
||
randomBytes
(
8
).
toString
(
'hex'
)
logWebUrl
()
logWebUrl
()
}
}
...
...
packages/next/telemetry/trace/report/to-json.ts
0 → 100644
View file @
9316dc0a
import
{
randomBytes
}
from
'crypto'
import
{
batcher
}
from
'./to-zipkin'
import
{
traceGlobals
}
from
'../shared'
import
fs
from
'fs'
import
path
from
'path'
let
writeStream
:
fs
.
WriteStream
let
traceId
:
string
let
batch
:
ReturnType
<
typeof
batcher
>
|
undefined
const
reportToLocalHost
=
(
name
:
string
,
duration
:
number
,
timestamp
:
number
,
id
:
string
,
parentId
?:
string
,
attrs
?:
Object
)
=>
{
const
distDir
=
traceGlobals
.
get
(
'distDir'
)
if
(
!
distDir
)
{
return
}
if
(
!
traceId
)
{
traceId
=
process
.
env
.
TRACE_ID
||
randomBytes
(
8
).
toString
(
'hex'
)
}
if
(
!
batch
)
{
batch
=
batcher
(
async
(
events
)
=>
{
if
(
!
writeStream
)
{
const
tracesDir
=
path
.
join
(
distDir
,
'traces'
)
await
fs
.
promises
.
mkdir
(
tracesDir
,
{
recursive
:
true
})
const
file
=
path
.
join
(
distDir
,
'trace'
)
writeStream
=
fs
.
createWriteStream
(
file
,
{
flags
:
'a'
,
encoding
:
'utf8'
,
})
}
const
eventsJson
=
JSON
.
stringify
(
events
)
try
{
await
new
Promise
<
void
>
((
resolve
,
reject
)
=>
{
writeStream
.
write
(
eventsJson
+
'
\
n'
,
'utf8'
,
(
err
)
=>
{
err
?
reject
(
err
)
:
resolve
()
})
})
}
catch
(
err
)
{
console
.
log
(
err
)
}
})
}
batch
.
report
({
traceId
,
parentId
,
name
,
id
,
timestamp
,
duration
,
tags
:
attrs
,
})
}
export
default
{
flushAll
:
()
=>
batch
?
batch
.
flushAll
().
then
(()
=>
{
writeStream
.
end
(
''
,
'utf8'
)
})
:
undefined
,
report
:
reportToLocalHost
,
}
packages/next/telemetry/trace/report/to-zipkin.ts
View file @
9316dc0a
...
@@ -3,7 +3,7 @@ import { randomBytes } from 'crypto'
...
@@ -3,7 +3,7 @@ import { randomBytes } from 'crypto'
import
fetch
from
'node-fetch'
import
fetch
from
'node-fetch'
import
*
as
Log
from
'../../../build/output/log'
import
*
as
Log
from
'../../../build/output/log'
let
traceId
=
process
.
env
.
TRACE_ID
let
traceId
:
string
let
batch
:
ReturnType
<
typeof
batcher
>
|
undefined
let
batch
:
ReturnType
<
typeof
batcher
>
|
undefined
const
localEndpoint
=
{
const
localEndpoint
=
{
...
@@ -21,7 +21,7 @@ type Event = {
...
@@ -21,7 +21,7 @@ type Event = {
id
:
string
id
:
string
timestamp
:
number
timestamp
:
number
duration
:
number
duration
:
number
localEndpoint
:
typeof
localEndpoint
localEndpoint
?
:
typeof
localEndpoint
tags
?:
Object
tags
?:
Object
}
}
...
@@ -42,8 +42,9 @@ export function batcher(reportEvents: (evts: Event[]) => Promise<void>) {
...
@@ -42,8 +42,9 @@ export function batcher(reportEvents: (evts: Event[]) => Promise<void>) {
events
.
push
(
event
)
events
.
push
(
event
)
if
(
events
.
length
>
100
)
{
if
(
events
.
length
>
100
)
{
const
report
=
reportEvents
(
events
.
slice
()
)
const
evts
=
events
.
slice
(
)
events
.
length
=
0
events
.
length
=
0
const
report
=
reportEvents
(
evts
)
queue
.
add
(
report
)
queue
.
add
(
report
)
report
.
then
(()
=>
queue
.
delete
(
report
))
report
.
then
(()
=>
queue
.
delete
(
report
))
}
}
...
@@ -60,7 +61,7 @@ const reportToLocalHost = (
...
@@ -60,7 +61,7 @@ const reportToLocalHost = (
attrs
?:
Object
attrs
?:
Object
)
=>
{
)
=>
{
if
(
!
traceId
)
{
if
(
!
traceId
)
{
traceId
=
process
.
env
.
TRACE_ID
=
randomBytes
(
8
).
toString
(
'hex'
)
traceId
=
process
.
env
.
TRACE_ID
||
randomBytes
(
8
).
toString
(
'hex'
)
Log
.
info
(
Log
.
info
(
`Zipkin trace will be available on
${
zipkinUrl
}
/zipkin/traces/
${
traceId
}
`
`Zipkin trace will be available on
${
zipkinUrl
}
/zipkin/traces/
${
traceId
}
`
)
)
...
...
scripts/send-trace-to-jaeger.mjs
0 → 100644
View file @
9316dc0a
import
fs
from
'fs'
import
eventStream
from
'event-stream'
import
retry
from
'async-retry'
import
fetch
from
'node-fetch'
const
file
=
fs
.
createReadStream
(
process
.
argv
[
2
])
const
localEndpoint
=
{
serviceName
:
'nextjs'
,
ipv4
:
'127.0.0.1'
,
port
:
9411
,
}
// Jaeger supports Zipkin's reporting API
const
zipkinUrl
=
`http://
${
localEndpoint
.
ipv4
}
:
${
localEndpoint
.
port
}
`
const
jaegerWebUiUrl
=
`http://
${
localEndpoint
.
ipv4
}
:16686`
const
zipkinAPI
=
`
${
zipkinUrl
}
/api/v2/spans`
let
loggedUrl
=
false
function
logWebUrl
(
traceId
)
{
console
.
log
(
`Jaeger trace will be available on
${
jaegerWebUiUrl
}
/trace/
${
traceId
}
`
)
}
file
.
pipe
(
eventStream
.
split
()).
pipe
(
eventStream
.
map
((
data
,
cb
)
=>
{
if
(
data
===
''
)
{
return
cb
(
null
,
''
)
}
const
eventsJson
=
JSON
.
parse
(
data
).
map
((
item
)
=>
{
item
.
localEndpoint
=
localEndpoint
return
item
})
if
(
!
loggedUrl
)
{
logWebUrl
(
eventsJson
[
0
].
traceId
)
loggedUrl
=
true
}
retry
(
()
=>
// Send events to zipkin
fetch
(
zipkinAPI
,
{
method
:
'POST'
,
headers
:
{
'Content-Type'
:
'application/json'
},
body
:
JSON
.
stringify
(
eventsJson
),
}),
{
minTimeout
:
500
,
retries
:
3
,
factor
:
1
}
)
.
then
(
async
(
res
)
=>
{
if
(
res
.
status
!==
202
)
{
console
.
log
({
status
:
res
.
status
,
body
:
await
res
.
text
(),
events
:
eventsJson
,
})
}
cb
(
null
,
''
)
})
.
catch
((
err
)
=>
{
console
.
log
(
err
)
cb
(
null
,
''
)
})
})
)
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