diff --git a/src/base.ts b/src/base.ts index 8f22187a8b7..23dcb7e9e66 100644 --- a/src/base.ts +++ b/src/base.ts @@ -1,6 +1,7 @@ import { Command } from '@oclif/core'; import { MetadataFromDocument, MetricMetadata, NewRelicSink, Recorder, Sink, StdOutSink } from '@smoya/asyncapi-adoption-metrics'; import { Parser } from '@asyncapi/parser'; +import { Specification } from 'models/SpecificationFile'; class DiscardSink implements Sink { async send() { @@ -11,6 +12,14 @@ class DiscardSink implements Sink { export default abstract class extends Command { recorder = this.recorderFromEnv('asyncapi_adoption'); parser = new Parser(); + metricsMetadata: MetricMetadata = {}; + specFile: Specification | undefined; + + async init(): Promise { + await super.init(); + const commandName : string = this.id || ''; + await this.recordActionInvoked(commandName); + } async catch(err: Error & { exitCode?: number; }): Promise { try { @@ -28,7 +37,7 @@ export default abstract class extends Command { try { const {document} = await this.parser.parse(rawDocument); if (document !== undefined) { - metadata = MetadataFromDocument(document, metadata); + this.metricsMetadata = MetadataFromDocument(document, metadata); } } catch (e: any) { if (e instanceof Error) { @@ -63,10 +72,10 @@ export default abstract class extends Command { } } - async init(): Promise { - await super.init(); - const commandName : string = this.id || ''; - await this.recordActionInvoked(commandName); + async finally(error: Error | undefined): Promise { + await super.finally(error); + this.metricsMetadata['success'] = error === undefined; + await this.recordActionExecuted(this.id as string, this.metricsMetadata, this.specFile?.text()); } recorderFromEnv(prefix: string): Recorder { diff --git a/src/commands/validate.ts b/src/commands/validate.ts index 5757cbaffa3..e2527f55c07 100644 --- a/src/commands/validate.ts +++ b/src/commands/validate.ts @@ -29,9 +29,8 @@ export default class Validate extends Command { specWatcher({ spec: specFile, handler: this, handlerName: 'validate' }); } - const result = await validate(this, specFile, flags); - // Metrics recording. - await this.recordActionExecuted('validate', {success: true, validation_result: result}, specFile.text()); + const result = await validate(this, specFile, flags); + this.metricsMetadata = {success: true, validation_result: result}; } }