| import { createReadStream } from 'fs' |
| import { createInterface } from 'readline' |
| import path from 'path' |
| import Tracer from 'dd-trace' |
| import flat from 'flat' |
|
|
| const cleanFilename = (filename) => { |
| if (filename.includes('&absolutePagePath=')) { |
| filename = |
| 'page ' + |
| decodeURIComponent( |
| filename.replace(/.+&absolutePagePath=/, '').slice(0, -1) |
| ) |
| } |
| filename = filename.replace(/.+!(?!$)/, '') |
| return filename |
| } |
|
|
| const getPackageName = (filename) => { |
| const match = /.+[\\/]node_modules[\\/]((?:@[^\\/]+[\\/])?[^\\/]+)/.exec( |
| cleanFilename(filename) |
| ) |
| return match && match[1] |
| } |
|
|
| |
| |
| |
| const reportSpanRecursively = (tracer, trace, parentSpan) => { |
| |
| const isBuildModule = trace.name.startsWith('build-module-') |
| if (isBuildModule) { |
| trace.packageName = getPackageName(trace.tags.name) |
| |
| trace.tags.name = trace.packageName |
| if (trace.children) { |
| const queue = [...trace.children] |
| trace.children = [] |
| for (const e of queue) { |
| if (e.name.startsWith('build-module-')) { |
| const pkgName = getPackageName(e.tags.name) |
| if (!trace.packageName || pkgName !== trace.packageName) { |
| trace.children.push(e) |
| } else { |
| if (e.children) queue.push(...e.children) |
| } |
| } |
| } |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| let span = tracer.startSpan(trace.name, { |
| startTime: trace.startTime, |
| childOf: parentSpan, |
| tags: Object.keys(trace?.tags).length > 0 ? trace?.tags : undefined, |
| }) |
|
|
| |
| trace.children?.sort((a, b) => a.startTime - b.startTime) |
| trace.children?.forEach((childTrace) => |
| reportSpanRecursively(tracer, childTrace, span) |
| ) |
|
|
| span.finish(trace.startTime + trace.duration / 1000) |
| return span |
| } |
|
|
| |
| |
| |
| const collectTraces = async (filePath, metadata) => { |
| const tracer = Tracer.init({ |
| tags: metadata, |
| |
| logLevel: 'error', |
| |
| |
| flushInterval: 20, |
| flushMinSpans: 10, |
| }) |
|
|
| const readLineInterface = createInterface({ |
| input: createReadStream(filePath), |
| crlfDelay: Infinity, |
| }) |
|
|
| const traces = new Map() |
| const rootTraces = [] |
|
|
| |
| |
| |
| |
| |
| for await (const line of readLineInterface) { |
| JSON.parse(line).forEach((trace) => traces.set(trace.id, trace)) |
| } |
|
|
| |
| for (const event of traces.values()) { |
| if (event.parentId) { |
| event.parent = traces.get(event.parentId) |
| if (event.parent) { |
| if (!event.parent.children) event.parent.children = [] |
| event.parent.children.push(event) |
| } |
| } |
|
|
| if (!event.parent) { |
| rootTraces.push(event) |
| } |
| } |
|
|
| for (const trace of rootTraces) { |
| reportSpanRecursively(tracer, trace) |
| } |
| } |
|
|
| |
| |
| |
| const validateArgs = async () => { |
| const { DD_ENV, DD_SERVICE, DATA_DOG_API_KEY } = process.env |
|
|
| if (!DATA_DOG_API_KEY) { |
| console.log( |
| "Skipping trace collection, api key is not available. Ensure 'DATA_DOG_API_KEY' env variable is set." |
| ) |
| return |
| } |
|
|
| if (!DD_ENV || !DD_SERVICE) { |
| throw new Error( |
| `Could not find proper environment variables. Ensure to set DD_ENV / DD_SERVICE` |
| ) |
| } |
|
|
| |
| |
| |
| |
| |
| const [, , traceFilePath, command, commit, configFilePath] = process.argv |
| const config = configFilePath |
| ? (await import(path.resolve(process.cwd(), configFilePath))).default |
| : {} |
|
|
| if (!traceFilePath || !command || !commit) { |
| throw new Error( |
| `Cannot collect traces without necessary metadata. |
| Try to run script with below args: |
| |
| node trace-dd.mjs tracefilepath command commit [configfilepath]` |
| ) |
| } |
|
|
| const metadata = { |
| command, |
| commit, |
| |
| nextjs_config: flat.flatten(config), |
| } |
|
|
| return [traceFilePath, metadata] |
| } |
|
|
| validateArgs() |
| .then(([traceFilePath, metadata]) => collectTraces(traceFilePath, metadata)) |
| .catch((e) => { |
| console.error(`Failed to collect traces`) |
| console.error(e) |
| }) |
|
|