Skip to content

Commit

Permalink
Remove trace_target env var in favor of .next/trace (#31697)
Browse files Browse the repository at this point in the history
  • Loading branch information
timneutkens authored Nov 22, 2021
1 parent 7f58a2e commit f584722
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 252 deletions.
30 changes: 2 additions & 28 deletions packages/next/trace/report/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { TARGET, SpanId } from '../shared'
import reportToConsole from './to-console'
import reportToZipkin from './to-zipkin'
import reportToJaeger from './to-jaeger'
import { SpanId } from '../shared'
import reportToTelemetry from './to-telemetry'
import reportToJson from './to-json'

Expand Down Expand Up @@ -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
export const reporter = new MultiReporter([reportToJson, traceTargetReporter])
export const reporter = new MultiReporter([reportToJson, reportToTelemetry])
32 changes: 0 additions & 32 deletions packages/next/trace/report/to-console.ts

This file was deleted.

83 changes: 0 additions & 83 deletions packages/next/trace/report/to-jaeger.ts

This file was deleted.

45 changes: 44 additions & 1 deletion packages/next/trace/report/to-json.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,53 @@
import { randomBytes } from 'crypto'
import { batcher } from './to-zipkin'
import { traceGlobals } from '../shared'
import fs from 'fs'
import path from 'path'
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 traceId: string
let batch: ReturnType<typeof batcher> | undefined
Expand Down
101 changes: 0 additions & 101 deletions packages/next/trace/report/to-zipkin.ts

This file was deleted.

7 changes: 0 additions & 7 deletions packages/next/trace/shared.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
// 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

Expand Down

0 comments on commit f584722

Please sign in to comment.