Unverified Commit f584722f authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Remove trace_target env var in favor of .next/trace (#31697)

parent 7f58a2e4
import { TARGET, SpanId } from '../shared' import { SpanId } from '../shared'
import reportToConsole from './to-console'
import reportToZipkin from './to-zipkin'
import reportToJaeger from './to-jaeger'
import reportToTelemetry from './to-telemetry' import reportToTelemetry from './to-telemetry'
import reportToJson from './to-json' import reportToJson from './to-json'
...@@ -42,28 +39,5 @@ class MultiReporter implements Reporter { ...@@ -42,28 +39,5 @@ class MultiReporter implements Reporter {
} }
} }
const target =
process.env.TRACE_TARGET && process.env.TRACE_TARGET in TARGET
? TARGET[process.env.TRACE_TARGET as TARGET]
: TARGET.TELEMETRY
if (process.env.TRACE_TARGET && !target) {
console.info(
'For TRACE_TARGET, please specify one of: CONSOLE, ZIPKIN, TELEMETRY'
)
}
let traceTargetReporter: Reporter
if (target === TARGET.CONSOLE) {
traceTargetReporter = reportToConsole
} else if (target === TARGET.ZIPKIN) {
traceTargetReporter = reportToZipkin
} else if (target === TARGET.JAEGER) {
traceTargetReporter = reportToJaeger
} else {
traceTargetReporter = reportToTelemetry
}
// JSON is always reported to allow for diagnostics // JSON is always reported to allow for diagnostics
export const reporter = new MultiReporter([reportToJson, traceTargetReporter]) export const reporter = new MultiReporter([reportToJson, reportToTelemetry])
import * as Log from '../../build/output/log'
const idToName = new Map<string, string>()
const reportToConsole = (
spanName: string,
duration: number,
_timestamp: number,
id: string,
parentId?: string,
attrs?: Object
) => {
idToName.set(id, spanName)
const parentStr =
parentId && idToName.has(parentId)
? `, parent: ${idToName.get(parentId)}`
: ''
const attrsStr = attrs
? `, ${Object.entries(attrs)
.map(([key, val]) => `${key}: ${val}`)
.join(', ')}`
: ''
Log.trace(
`${spanName} took ${Number(duration) / 1000} ms${parentStr}${attrsStr}`
)
}
export default {
flushAll: () => {},
report: reportToConsole,
}
import retry from 'next/dist/compiled/async-retry'
import { randomBytes } from 'crypto'
import fetch from 'node-fetch'
import * as Log from '../../build/output/log'
// Jaeger uses Zipkin's reporting
import { batcher } from './to-zipkin'
let traceId: string
let batch: ReturnType<typeof batcher> | undefined
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`
function logWebUrl() {
Log.info(
`Jaeger trace will be available on ${jaegerWebUiUrl}/trace/${traceId}`
)
}
const reportToLocalHost = (
name: string,
duration: number,
timestamp: number,
id: string,
parentId?: string,
attrs?: Object
) => {
if (!traceId) {
traceId = process.env.TRACE_ID || randomBytes(8).toString('hex')
logWebUrl()
}
if (!batch) {
batch = batcher((events) => {
const eventsJson = JSON.stringify(events)
// Ensure ECONNRESET error is retried 3 times before erroring out
return retry(
() =>
// Send events to jaeger's zipkin endpoint
fetch(zipkinAPI, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: eventsJson,
}),
{ minTimeout: 500, retries: 3, factor: 1 }
)
.then(async (res: any) => {
if (res.status !== 202) {
console.log({
status: res.status,
body: await res.text(),
events: eventsJson,
})
}
})
.catch(console.log)
})
}
batch.report({
traceId,
parentId,
name,
id,
timestamp,
duration,
localEndpoint,
tags: attrs,
})
}
export default {
flushAll: () =>
batch ? batch.flushAll().then(() => logWebUrl()) : undefined,
report: reportToLocalHost,
}
import { randomBytes } from 'crypto' import { randomBytes } from 'crypto'
import { batcher } from './to-zipkin'
import { traceGlobals } from '../shared' import { traceGlobals } from '../shared'
import fs from 'fs' import fs from 'fs'
import path from 'path' import path from 'path'
import { PHASE_DEVELOPMENT_SERVER } from '../../shared/lib/constants' import { PHASE_DEVELOPMENT_SERVER } from '../../shared/lib/constants'
const localEndpoint = {
serviceName: 'nextjs',
ipv4: '127.0.0.1',
port: 9411,
}
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
export function batcher(reportEvents: (evts: Event[]) => Promise<void>) {
const events: Event[] = []
// Promise queue to ensure events are always sent on flushAll
const queue = new Set()
return {
flushAll: async () => {
await Promise.all(queue)
if (events.length > 0) {
await reportEvents(events)
events.length = 0
}
},
report: (event: Event) => {
events.push(event)
if (events.length > 100) {
const evts = events.slice()
events.length = 0
const report = reportEvents(evts)
queue.add(report)
report.then(() => queue.delete(report))
}
},
}
}
let writeStream: RotatingWriteStream let writeStream: RotatingWriteStream
let traceId: string let traceId: string
let batch: ReturnType<typeof batcher> | undefined let batch: ReturnType<typeof batcher> | undefined
......
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: string
let batch: ReturnType<typeof batcher> | undefined
const localEndpoint = {
serviceName: 'nextjs',
ipv4: '127.0.0.1',
port: 9411,
}
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
export function batcher(reportEvents: (evts: Event[]) => Promise<void>) {
const events: Event[] = []
// Promise queue to ensure events are always sent on flushAll
const queue = new Set()
return {
flushAll: async () => {
await Promise.all(queue)
if (events.length > 0) {
await reportEvents(events)
events.length = 0
}
},
report: (event: Event) => {
events.push(event)
if (events.length > 100) {
const evts = events.slice()
events.length = 0
const report = reportEvents(evts)
queue.add(report)
report.then(() => queue.delete(report))
}
},
}
}
const reportToLocalHost = (
name: string,
duration: number,
timestamp: number,
id: string,
parentId?: string,
attrs?: Object
) => {
if (!traceId) {
traceId = process.env.TRACE_ID || randomBytes(8).toString('hex')
Log.info(
`Zipkin trace will be available on ${zipkinUrl}/zipkin/traces/${traceId}`
)
}
if (!batch) {
batch = batcher((events) => {
// Ensure ECONNRESET error is retried 3 times before erroring out
return 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.report({
traceId,
parentId,
name,
id,
timestamp,
duration,
localEndpoint,
tags: attrs,
})
}
export default {
flushAll: () => (batch ? batch.flushAll() : undefined),
report: reportToLocalHost,
}
// eslint typescript has a bug with TS enums // eslint typescript has a bug with TS enums
/* eslint-disable no-shadow */
export enum TARGET {
CONSOLE = 'CONSOLE',
ZIPKIN = 'ZIPKIN',
JAEGER = 'JAEGER',
TELEMETRY = 'TELEMETRY',
}
export type SpanId = string export type SpanId = string
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment