Skip to content

Commit

Permalink
Log Turbosnap metrics to New Relic
Browse files Browse the repository at this point in the history
  • Loading branch information
codykaup committed Jan 14, 2025
1 parent 1ecc76c commit bc2f641
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
31 changes: 31 additions & 0 deletions node-src/lib/newRelic.ts
Original file line number Diff line number Diff line change
@@ -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
}

Check warning on line 30 in node-src/lib/newRelic.ts

View check run for this annotation

Codecov / codecov/patch

node-src/lib/newRelic.ts#L30

Added line #L30 was not covered by tests
}
20 changes: 20 additions & 0 deletions node-src/tasks/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -132,18 +133,31 @@ 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;

try {
// 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';

Check warning on line 159 in node-src/tasks/upload.ts

View check run for this annotation

Codecov / codecov/patch

node-src/tasks/upload.ts#L159

Added line #L159 was not covered by tests

ctx.git.changedDependencyNames = changedDependencyNames;
if (!ctx.options.interactive) {
const list =
Expand All @@ -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';
}

Check warning on line 172 in node-src/tasks/upload.ts

View check run for this annotation

Codecov / codecov/patch

node-src/tasks/upload.ts#L171-L172

Added lines #L171 - L172 were not covered by tests

ctx.log.warn(`Could not retrieve dependency changes from lockfiles; checking package.json`);

const changedPackageFiles = await findChangedPackageFiles(packageMetadataChanges);
Expand Down Expand Up @@ -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' });
}
};

Expand Down

0 comments on commit bc2f641

Please sign in to comment.