forked from asyncapi/cli
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.ts
86 lines (75 loc) · 2.56 KB
/
base.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { Command } from '@oclif/core';
import { MetadataFromDocument, MetricMetadata, NewRelicSink, Recorder, Sink, StdOutSink } from '@smoya/asyncapi-adoption-metrics';
import { Parser } from '@asyncapi/parser';
class DiscardSink implements Sink {
async send() {
// noop
}
}
export default abstract class extends Command {
recorder = recorderFromEnv('asyncapi_adoption');
parser = new Parser();
async catch(err: Error & { exitCode?: number; }): Promise<any> {
try {
return await super.catch(err);
} catch (e: any) {
if (e instanceof Error) {
this.logToStderr(`${e.name}: ${e.message}`);
process.exitCode = 1;
}
}
}
async recordActionExecuted(action: string, metadata: MetricMetadata = {}, rawDocument?: string) {
if (rawDocument !== undefined) {
try {
const {document} = await this.parser.parse(rawDocument);
if (document !== undefined) {
metadata = MetadataFromDocument(document, metadata);
}
} catch (e: any) {
if (e instanceof Error) {
this.log(`Skipping submitting anonymous metrics due to the following error: ${e.name}: ${e.message}`);
}
}
}
const callable = async function(recorder: Recorder) {
await recorder.recordActionExecuted(action, metadata);
};
await this.recordActionMetric(callable);
}
async recordActionInvoked(action: string, metadata?: MetricMetadata) {
const callable = async function(recorder: Recorder) {
await recorder.recordActionInvoked(action, metadata);
};
await this.recordActionMetric(callable);
}
async recordActionMetric(recordFunc: (recorder: Recorder) => void) {
try {
await recordFunc(this.recorder);
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}`);
}
}
}
}
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);
}