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

Log Turbosnap metrics to New Relic #1141

Merged
Show file tree
Hide file tree
Changes from all commits
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
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 { 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 @@

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 @@
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 @@
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
Loading