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

feat: send metrics to NR only when installed as NPM binary #2

Merged
merged 1 commit into from
Nov 23, 2023
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
2 changes: 2 additions & 0 deletions bin/run
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env node

process.env.NODE_ENV = 'development';

const oclif = require('@oclif/core');

oclif.run()
Expand Down
14 changes: 14 additions & 0 deletions bin/run_bin
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env node

// Only the binary installed through NPM is considered production environment. See "bin" in package.json.
process.env.NODE_ENV = 'production';

const oclif = require('@oclif/core');

oclif.run()
.then(require('@oclif/core/flush'))
.catch((err) => {
const oclifHandler = require('@oclif/core/handle');
return oclifHandler(err.message);
});

3 changes: 3 additions & 0 deletions bin/run_bin.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@echo off

node "%~dp0\run_bin" %*
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"version": "1.1.1",
"author": "@asyncapi",
"bin": {
"asyncapi": "./bin/run"
"asyncapi": "./bin/run_bin"
},
"bugs": "https://github.com/asyncapi/cli/issues",
"dependencies": {
Expand All @@ -23,7 +23,7 @@
"@oclif/core": "^1.26.2",
"@oclif/errors": "^1.3.6",
"@oclif/plugin-not-found": "^2.3.22",
"@smoya/asyncapi-adoption-metrics": "^1.1.1",
"@smoya/asyncapi-adoption-metrics": "^2.0.0",
"@smoya/multi-parser": "^4.0.0",
"@stoplight/spectral-cli": "6.9.0",
"ajv": "^8.12.0",
Expand Down
31 changes: 29 additions & 2 deletions src/base.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { Command } from '@oclif/core';
import { Recorder, StdOutSink } from '@smoya/asyncapi-adoption-metrics';
import { NewRelicSink, Recorder, Sink, StdOutSink } from '@smoya/asyncapi-adoption-metrics';

class DiscardSink implements Sink {
async send() {
// noop
}
}

export default abstract class extends Command {
recorder = new Recorder('asyncapi-adoption', new StdOutSink());
recorder = recorderFromEnv('asyncapi_adoption');

async catch(err: Error & { exitCode?: number; }): Promise<any> {
try {
Expand All @@ -15,3 +21,24 @@ export default abstract class extends Command {
}
}
}

function recorderFromEnv(prefix: string): Recorder {
let sink: Sink = new DiscardSink();
if (process.env.ASYNCAPI_METRICS !== 'false') {
switch (process.env.NODE_ENV) {
case 'development':
// NODE_ENV set to `development` in bin/run
if (!process.env.TEST) {
// Do not pollute stdout when running tests
sink = new StdOutSink();
}
break;
case 'production':
// NODE_ENV set to `production` in bin/run_bin, which is specified in 'bin' package.json section
sink = new NewRelicSink('eu01xx73a8521047150dd9414f6aedd2FFFFNRAL');
break;
}
}

return new Recorder(prefix, sink);
}
2 changes: 1 addition & 1 deletion src/commands/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

parser = new Parser();

async run() {

Check warning on line 33 in src/commands/bundle.ts

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

Refactor this function to reduce its Cognitive Complexity from 17 to the 15 allowed
const { argv, flags } = await this.parse(Bundle);
const output = flags.output;
let baseFile;
Expand Down Expand Up @@ -87,7 +87,7 @@
const metadata = MetadataFromDocument(document);
metadata['success'] = true;
metadata['files'] = AsyncAPIFiles.length;
await this.recorder.recordActionExecution('bundle', metadata);
await this.recorder.recordActionExecuted('bundle', metadata);
await this.recorder.flush();
}
} catch (e: any) {
Expand Down
3 changes: 1 addition & 2 deletions src/commands/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

parser = new Parser();

async run() {

Check warning on line 34 in src/commands/convert.ts

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

Refactor this function to reduce its Cognitive Complexity from 17 to the 15 allowed
const { args, flags } = await this.parse(Convert);
const filePath = args['spec-file'];
let specFile;
Expand Down Expand Up @@ -82,8 +82,7 @@
metadata['success'] = true;
metadata['from_version'] = document.version();
metadata['to_version'] = flags['target-version'];
console.log(metadata);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests were failing due to this console.log printing things to stdout. Some of our integration tests are based on the stdout made by the cli and this was polluting it.

await this.recorder.recordActionExecution('convert', metadata);
await this.recorder.recordActionExecuted('convert', metadata);
await this.recorder.flush();
}
} catch (e: any) {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/generate/fromTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export default class Template extends Command {
const metadata = MetadataFromDocument(document);
metadata['success'] = true;
metadata['template'] = template;
await this.recorder.recordActionExecution('generate_fromTemplate', metadata);
await this.recorder.recordActionExecuted('generate_fromTemplate', metadata);
await this.recorder.flush();
}
} catch (e: any) {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/optimize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@

parser = new Parser();

async run() {

Check warning on line 52 in src/commands/optimize.ts

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

Refactor this function to reduce its Cognitive Complexity from 20 to the 15 allowed
const { args, flags } = await this.parse(Optimize); //NOSONAR
const filePath = args['spec-file'];
let specFile: Specification;
Expand Down Expand Up @@ -104,7 +104,7 @@

const specPath = specFile.getFilePath();
let newPath = '';

if (specPath) {
const pos = specPath.lastIndexOf('.');
newPath = `${specPath.substring(0,pos) }_optimized.${ specPath.substring(pos+1)}`;
Expand Down Expand Up @@ -137,7 +137,7 @@
const metadata = MetadataFromDocument(document);
metadata['success'] = true;
metadata['optimizations'] = this.optimizations;
await this.recorder.recordActionExecution('optimize', metadata);
await this.recorder.recordActionExecuted('optimize', metadata);
await this.recorder.flush();
}
} catch (e: any) {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default class Validate extends Command {
const metadata = MetadataFromDocument(document);
metadata['success'] = true;
metadata['validation_result'] = result;
await this.recorder.recordActionExecution('validate', metadata);
await this.recorder.recordActionExecuted('validate', metadata);
await this.recorder.flush();
}
} catch (e: any) {
Expand Down
Loading