Skip to content

Commit

Permalink
feat: send metrics to NR only when installed as NPM binary
Browse files Browse the repository at this point in the history
  • Loading branch information
smoya committed Nov 23, 2023
1 parent e9e79f6 commit 82960a1
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 16 deletions.
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 @@ -87,7 +87,7 @@ export default class Bundle extends Command {
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 @@ -82,8 +82,7 @@ export default class Convert extends Command {
metadata['success'] = true;
metadata['from_version'] = document.version();
metadata['to_version'] = flags['target-version'];
console.log(metadata);
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 @@ -104,7 +104,7 @@ export default class Optimize extends Command {

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 @@ export default class Optimize extends Command {
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

0 comments on commit 82960a1

Please sign in to comment.