Skip to content

Commit

Permalink
feat: add asyncapi file birth timestamp in metrics collection. (async…
Browse files Browse the repository at this point in the history
…api#1304)

Co-authored-by: souvik <souvikde.ns@gmail.com>
  • Loading branch information
KhudaDad414 and Souvikns authored May 14, 2024
1 parent 5c99587 commit 254c721
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ spec-examples.zip
# Coverage for testing

coverage
.asyncapi-analytics

# Analytics

Expand Down
13 changes: 12 additions & 1 deletion src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { promises as fPromises } from 'fs';
import { v4 as uuidv4 } from 'uuid';
import { homedir } from 'os';

const { readFile, writeFile } = fPromises;
const { readFile, writeFile, stat } = fPromises;

class DiscardSink implements Sink {
async send() {
Expand Down Expand Up @@ -70,6 +70,7 @@ export default abstract class extends Command {

async recordActionMetric(recordFunc: (recorder: Recorder) => Promise<void>) {
try {
await this.setSource();
await recordFunc(await this.recorder);
await (await this.recorder).flush();
} catch (e: any) {
Expand All @@ -79,6 +80,16 @@ export default abstract class extends Command {
}
}

async setSource() {
const specFilePath = this.specFile?.getFilePath();
if (!specFilePath) { return; }
try {
const stats = await stat(specFilePath);
this.metricsMetadata['file_creation_timestamp'] = stats.birthtimeMs;
} catch (e: any) {
// If there's an error with the file, we don't handle it here because it's expected to be handled and reported in the 'finally' method of the command.
}
}
async finally(error: Error | undefined): Promise<any> {
await super.finally(error);
this.metricsMetadata['success'] = error === undefined;
Expand Down

0 comments on commit 254c721

Please sign in to comment.