Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Telemetry-compatible tracing #22713

Merged
merged 16 commits into from
Mar 10, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Replace OpenTelemetry with custom tracing.
divmain committed Mar 10, 2021
commit b8ef91eda86b5e9d97bc0b8cc51d9ab10dd5adb8
65 changes: 65 additions & 0 deletions packages/next/telemetry/trace/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { randomBytes } from 'crypto'
import reportToConsole from './to-console'
import reportToLocalHost from './to-localhost'
import reportToTelemetry from './to-telemetry'

let idCounter = 0
const idUniqToProcess = randomBytes(16).toString('base64').slice(0, 22)
const getId = () => `${idUniqToProcess}:${idCounter++}`

const noop = (
_spanName: string,
_duration: number,
_id: string | null,
_parentId: string | null,
_attrs: Object
) => {}

enum TARGET {
CONSOLE = 'CONSOLE',
LOCALHOST = 'LOCALHOST',
TELEMETRY = 'TELEMETRY',
}

const target =
process.env.TRACE_TARGET && process.env.TRACE_TARGET in TARGET
? TARGET[process.env.TRACE_TARGET as TARGET]
: TARGET.TELEMETRY
const traceLevel = process.env.TRACE_LEVEL
? Number.parseInt(process.env.TRACE_LEVEL)
: 1

let report = noop
if (target === TARGET.CONSOLE) {
report = reportToConsole
} else if (target === TARGET.LOCALHOST) {
report = reportToLocalHost
} else {
report = reportToTelemetry
}

// This function reports durations in microseconds. This gives 1000x
// the precision of something like Date.now(), which reports in
// milliseconds. Additionally, ~285 years can be safely represented
// as microseconds as a float64 in both JSON and JavaScript.
const trace = (spanName: string, parentId: string | null, attrs: Object) => {
const endSpan = () => {
const id = getId()
const end: bigint = process.hrtime.bigint()
const duration = end - start
if (duration > Number.MAX_SAFE_INTEGER) {
throw new Error(`Duration is too long to express as float64: ${duration}`)
}
report(spanName, Number(duration), id, parentId, attrs)
}

const start: bigint = process.hrtime.bigint()
return endSpan
}

module.exports = {
TARGET,
primary: traceLevel >= 1 ? trace : noop,
secondary: traceLevel >= 2 ? trace : noop,
sensitive: traceLevel >= 3 ? trace : noop,
}
9 changes: 9 additions & 0 deletions packages/next/telemetry/trace/to-console.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const reportToConsole = (
_spanName: string,
_duration: number,
_id: string | null,
_parentId: string | null,
_attrs: Object
) => {}

export default reportToConsole
9 changes: 9 additions & 0 deletions packages/next/telemetry/trace/to-localhost.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const reportToLocalHost = (
_spanName: string,
_duration: number,
_id: string | null,
_parentId: string | null,
_attrs: Object
) => {}

export default reportToLocalHost
9 changes: 9 additions & 0 deletions packages/next/telemetry/trace/to-telemetry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const reportToTelemetry = (
_spanName: string,
_duration: number,
_id: string | null,
_parentId: string | null,
_attrs: Object
) => {}

export default reportToTelemetry