Skip to content

Commit

Permalink
Unify logic for metrics collection when command executed
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-rr committed Dec 20, 2023
1 parent e6f4917 commit 75370fc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
19 changes: 14 additions & 5 deletions src/base.ts
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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<void> {
await super.init();
const commandName : string = this.id || '';
await this.recordActionInvoked(commandName);
}

async catch(err: Error & { exitCode?: number; }): Promise<any> {
try {
Expand All @@ -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) {
Expand Down Expand Up @@ -63,10 +72,10 @@ export default abstract class extends Command {
}
}

async init(): Promise<void> {
await super.init();
const commandName : string = this.id || '';
await this.recordActionInvoked(commandName);
async finally(error: Error | undefined): Promise<any> {
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 {
Expand Down
5 changes: 2 additions & 3 deletions src/commands/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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};
}
}

0 comments on commit 75370fc

Please sign in to comment.