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

fix: change prefix format and pass New Relic sink #1

Closed
23 changes: 16 additions & 7 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
"@asyncapi/modelina": "^1.9.1",
"@asyncapi/openapi-schema-parser": "^3.0.5",
"@asyncapi/optimizer": "^0.2.1",
"@asyncapi/parser": "^3.0.0-next-major-spec.2",
"@asyncapi/parser": "^3.0.0-next-major-spec.3",
"@asyncapi/protobuf-schema-parser": "3.0.0",
"@asyncapi/raml-dt-schema-parser": "^4.0.3",
"@asyncapi/studio": "^0.17.3",
"@oclif/core": "^1.26.2",
"@oclif/errors": "^1.3.6",
"@oclif/plugin-not-found": "^2.3.22",
"@smoya/asyncapi-adoption-metrics": "^1.3.2",
"@smoya/multi-parser": "^4.0.0",
"@stoplight/spectral-cli": "6.9.0",
"ajv": "^8.12.0",
Expand Down
3 changes: 3 additions & 0 deletions src/base.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Command } from '@oclif/core';
import { Recorder, NewRelicSink } from '@smoya/asyncapi-adoption-metrics';

export default abstract class extends Command {
recorder = new Recorder('asyncapi_adoption', new NewRelicSink('API key'));

async catch(err: Error & { exitCode?: number; }): Promise<any> {
try {
return await super.catch(err);
Expand Down
22 changes: 22 additions & 0 deletions src/commands/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import bundle from '@asyncapi/bundler';
import { promises } from 'fs';
import path from 'path';
import { Specification, load } from '../models/SpecificationFile';
import { MetadataFromDocument } from '@smoya/asyncapi-adoption-metrics';
import { Parser } from '@asyncapi/parser';

const { writeFile } = promises;

Expand All @@ -26,6 +28,8 @@ export default class Bundle extends Command {
base: Flags.string({ char: 'b', description: 'Path to the file which will act as a base. This is required when some properties are to needed to be overwritten.' }),
};

parser = new Parser();

async run() {
const { argv, flags } = await this.parse(Bundle);
const output = flags.output;
Expand Down Expand Up @@ -73,6 +77,24 @@ export default class Bundle extends Command {
}
this.log(`Check out your shiny new bundled files at ${output}`);
}

const result = await load(output);

try {
// Metrics recording.
const {document} = await this.parser.parse(result.text());
if (document !== undefined) {
const metadata = MetadataFromDocument(document);
metadata['success'] = true;
metadata['files'] = AsyncAPIFiles.length;
await this.recorder.recordActionExecution('bundle', metadata);
await this.recorder.flush();
}
} catch (e: any) {
if (e instanceof Error) {
this.log(`Skipping submitting anonymous metrics due to the following error: ${e.name}: ${e.message}`);
}
}
}

async loadFiles(filepaths: string[]): Promise<Specification[]> {
Expand Down
22 changes: 22 additions & 0 deletions src/commands/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { ValidationError } from '../errors/validation-error';
import { load } from '../models/SpecificationFile';
import { SpecificationFileNotFound } from '../errors/specification-file';
import { convert } from '@asyncapi/converter';
import { MetadataFromDocument } from '@smoya/asyncapi-adoption-metrics';
import { Parser } from '@asyncapi/parser';

import type { ConvertVersion } from '@asyncapi/converter';

Expand All @@ -27,6 +29,8 @@ export default class Convert extends Command {
{ name: 'spec-file', description: 'spec path, url, or context-name', required: false },
];

parser = new Parser();

async run() {
const { args, flags } = await this.parse(Convert);
const filePath = args['spec-file'];
Expand Down Expand Up @@ -69,5 +73,23 @@ export default class Convert extends Command {
this.error(err as Error);
}
}

try {
// Metrics recording.
const {document} = await this.parser.parse(specFile.text());
if (document !== undefined && convertedFileFormatted) {
const metadata = MetadataFromDocument(document);
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.flush();
}
} catch (e: any) {
if (e instanceof Error) {
this.log(`Skipping submitting anonymous metrics due to the following error: ${e.name}: ${e.message}`);
}
}
}
}
22 changes: 21 additions & 1 deletion src/commands/generate/fromTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { watchFlag } from '../../flags';
import { isLocalTemplate, Watcher } from '../../utils/generator';
import { ValidationError } from '../../errors/validation-error';
import { GeneratorError } from '../../errors/generator-error';
import { MetadataFromDocument } from '@smoya/asyncapi-adoption-metrics';
import { Parser } from '@asyncapi/parser';

import type { Example } from '@oclif/core/lib/interfaces';

Expand Down Expand Up @@ -107,6 +109,8 @@ export default class Template extends Command {
{ name: 'template', description: '- Name of the generator template like for example @asyncapi/html-template or https://github.com/asyncapi/html-template', required: true }
];

parser = new Parser();

async run() {
const { args, flags } = await this.parse(Template); // NOSONAR

Expand Down Expand Up @@ -137,11 +141,27 @@ export default class Template extends Command {
this.error(`${template} template does not support AsyncAPI v3 documents, please checkout ${v3IssueLink}`);
}
}
await this.generate(asyncapi, template, output, options, genOption);
const result = await this.generate(asyncapi, template, output, options, genOption);
if (watchTemplate) {
const watcherHandler = this.watcherHandler(asyncapi, template, output, options, genOption);
await this.runWatchMode(asyncapi, template, output, watcherHandler);
}

try {
// Metrics recording.
const {document} = await this.parser.parse(asyncapiInput.text());
if (document !== undefined && result) {
const metadata = MetadataFromDocument(document);
metadata['success'] = true;
metadata['template'] = template;
await this.recorder.recordActionExecution('generate_fromTemplate', metadata);
await this.recorder.flush();
}
} catch (e: any) {
if (e instanceof Error) {
this.log(`Skipping submitting anonymous metrics due to the following error: ${e.name}: ${e.message}`);
}
}
}

private parseFlags(disableHooks?: string[], params?: string[], mapBaseUrl?: string): ParsedFlags {
Expand Down
23 changes: 23 additions & 0 deletions src/commands/optimize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import * as inquirer from 'inquirer';
import chalk from 'chalk';
import { promises } from 'fs';
import { Example } from '@oclif/core/lib/interfaces';
import { MetadataFromDocument } from '@smoya/asyncapi-adoption-metrics';
import { Parser } from '@asyncapi/parser';

const { writeFile } = promises;

export enum Optimizations {
Expand Down Expand Up @@ -44,6 +47,8 @@ export default class Optimize extends Command {
{ name: 'spec-file', description: 'spec path, url, or context-name', required: false },
];

parser = new Parser();

async run() {
const { args, flags } = await this.parse(Optimize); //NOSONAR
const filePath = args['spec-file'];
Expand Down Expand Up @@ -123,7 +128,25 @@ export default class Optimize extends Command {
err: error
});
}

try {
// Metrics recording.
const {document} = await this.parser.parse(specFile.text());
const optimizedDoc = optimizer.getOptimizedDocument();
if (document !== undefined && optimizedDoc) {
const metadata = MetadataFromDocument(document);
metadata['success'] = true;
metadata['optimizations'] = this.optimizations;
await this.recorder.recordActionExecution('optimize', metadata);
await this.recorder.flush();
}
} catch (e: any) {
if (e instanceof Error) {
this.log(`Skipping submitting anonymous metrics due to the following error: ${e.name}: ${e.message}`);
}
}
}

private showOptimizations(elements: ReportElement[] | undefined) {
if (!elements) {
return;
Expand Down
22 changes: 21 additions & 1 deletion src/commands/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { validate, validationFlags } from '../parser';
import { load } from '../models/SpecificationFile';
import { specWatcher } from '../globals';
import { watchFlag } from '../flags';
import { MetadataFromDocument } from '@smoya/asyncapi-adoption-metrics';
import { Parser } from '@asyncapi/parser';

export default class Validate extends Command {
static description = 'validate asyncapi file';
Expand All @@ -19,6 +21,8 @@ export default class Validate extends Command {
{ name: 'spec-file', description: 'spec path, url, or context-name', required: false },
];

parser = new Parser();

async run() {
const { args, flags } = await this.parse(Validate); //NOSONAR
const filePath = args['spec-file'];
Expand All @@ -29,6 +33,22 @@ export default class Validate extends Command {
specWatcher({ spec: specFile, handler: this, handlerName: 'validate' });
}

await validate(this, specFile, flags);
const result = await validate(this, specFile, flags);

try {
// Metrics recording.
const {document} = await this.parser.parse(specFile.text());
if (document !== undefined) {
const metadata = MetadataFromDocument(document);
metadata['success'] = true;
metadata['validation_result'] = result;
await this.recorder.recordActionExecution('validate', metadata);
await this.recorder.flush();
}
} catch (e: any) {
if (e instanceof Error) {
this.log(`Skipping submitting anonymous metrics due to the following error: ${e.name}: ${e.message}`);
}
}
}
}
Loading