From bc2f641b9c24381583f2f631e84baab84a48f3b1 Mon Sep 17 00:00:00 2001 From: Cody Kaup Date: Mon, 13 Jan 2025 17:12:16 -0600 Subject: [PATCH] Log Turbosnap metrics to New Relic --- node-src/lib/newRelic.ts | 31 +++++++++++++++++++++++++++++++ node-src/tasks/upload.ts | 20 ++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 node-src/lib/newRelic.ts diff --git a/node-src/lib/newRelic.ts b/node-src/lib/newRelic.ts new file mode 100644 index 000000000..0110f999b --- /dev/null +++ b/node-src/lib/newRelic.ts @@ -0,0 +1,31 @@ +import fetch from 'node-fetch'; + +// This is a temp (and limited) API key. This should be removed when we no longer need our TurboSnap +// metrics. +const NEW_RELIC_KEY = 'f887ede1f80741a1cd368cac8c8aa11fFFFFNRAL'; +const NEW_RELIC_ENDPOINT = 'https://log-api.newrelic.com/log/v1'; + +/** + * Writes a log line to New Relic + * + * @param data The object to write + */ +export async function writeLog(data: object) { + const body = JSON.stringify({ + name: 'cli', + service: 'cli', + ...data, + }); + + try { + await fetch(NEW_RELIC_ENDPOINT, { + method: 'POST', + headers: { + 'Api-Key': NEW_RELIC_KEY, + }, + body, + }); + } catch { + // Purposefully left blank + } +} diff --git a/node-src/tasks/upload.ts b/node-src/tasks/upload.ts index dd85af329..15c48687d 100644 --- a/node-src/tasks/upload.ts +++ b/node-src/tasks/upload.ts @@ -8,6 +8,7 @@ import { findChangedDependencies } from '../lib/findChangedDependencies'; import { findChangedPackageFiles } from '../lib/findChangedPackageFiles'; import { getDependentStoryFiles } from '../lib/getDependentStoryFiles'; import { getFileHashes } from '../lib/getFileHashes'; +import { writeLog } from '../lib/newRelic'; import { createTask, transitionTo } from '../lib/tasks'; import { uploadBuild } from '../lib/upload'; import { rewriteErrorMessage, throttle } from '../lib/utils'; @@ -132,6 +133,13 @@ export const traceChangedFiles = async (ctx: Context, task: Task) => { transitionTo(tracing)(ctx, task); + const turbosnapMetrics: { + dependencyChanges: boolean; + lockFileParseResult?: 'success' | 'error' | 'did not throw but no dependency changes found'; + error?: string; + } = { + dependencyChanges: false, + }; const { statsPath } = ctx.fileInfo; const { changedFiles, packageMetadataChanges } = ctx.git; @@ -139,11 +147,17 @@ export const traceChangedFiles = async (ctx: Context, task: Task) => { // eslint-disable-next-line @typescript-eslint/no-invalid-void-type let changedDependencyNames: void | string[] = []; if (packageMetadataChanges?.length) { + turbosnapMetrics.dependencyChanges = true; + changedDependencyNames = await findChangedDependencies(ctx).catch((err) => { const { name, message, stack, code } = err; ctx.log.debug({ name, message, stack, code }); + turbosnapMetrics.lockFileParseResult = 'error'; + turbosnapMetrics.error = message; }); if (changedDependencyNames) { + turbosnapMetrics.lockFileParseResult = 'success'; + ctx.git.changedDependencyNames = changedDependencyNames; if (!ctx.options.interactive) { const list = @@ -153,6 +167,10 @@ export const traceChangedFiles = async (ctx: Context, task: Task) => { ctx.log.info(`Found ${changedDependencyNames.length} changed dependencies${list}`); } } else { + if (!turbosnapMetrics.lockFileParseResult) { + turbosnapMetrics.lockFileParseResult = 'did not throw but no dependency changes found'; + } + ctx.log.warn(`Could not retrieve dependency changes from lockfiles; checking package.json`); const changedPackageFiles = await findChangedPackageFiles(packageMetadataChanges); @@ -206,6 +224,8 @@ export const traceChangedFiles = async (ctx: Context, task: Task) => { ctx.log.info('Failed to retrieve dependent story files', { statsPath, changedFiles, err }); } throw rewriteErrorMessage(err, `Could not retrieve dependent story files.\n${err.message}`); + } finally { + await writeLog({ ...turbosnapMetrics, message: 'Turbosnap lock file parsing metrics' }); } };