forked from meirim-org/meirim
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmetrics.js
More file actions
86 lines (73 loc) · 2.19 KB
/
Copy pathmetrics.js
File metadata and controls
86 lines (73 loc) · 2.19 KB
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
const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-grpc');
const { MeterProvider, PeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics');
const { Resource } = require('@opentelemetry/resources');
const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions');
const { Metadata } = require('@grpc/grpc-js');
const Log = require('./api/lib/log');
const Config = require('config');
const env = process.env.NODE_ENV;
const apikey = Config.get('coralogix.apikey');
const url = Config.get('coralogix.metricsEndpoint');
const serviceName = Config.get('coralogix.serviceName');
const metadata = new Metadata();
metadata.add('Authorization', 'Bearer ' + apikey);
const collectorOptions = {
url,
metadata: metadata
};
const metricExporter = new OTLPMetricExporter(collectorOptions);
const meterProvider = new MeterProvider({
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: serviceName,
}),
});
meterProvider.addMetricReader(new PeriodicExportingMetricReader({
exporter: metricExporter,
exportIntervalMillis: 100,
}));
['SIGINT', 'SIGTERM'].forEach(signal => {
process.on(signal, () => meterProvider.shutdown().catch(console.error));
});
const meter = meterProvider.getMeter('meirim');
const report = async ({ metricName, value = 1, attributes = {} }) => {
try {
const h = meter.createHistogram(metricName);
h.record(value, {
...attributes,
env,
});
} catch (err) {
console.error('failed to report metric', err);
}
};
async function runAndReport({ name, func, timeout = 60 * 60 * 1000 }) {
try {
await Promise.race(
[func(),
new Promise((resolve, reject) => {
setTimeout(() => {
reject(new Error('timeout from runAndReport'));
}, timeout);
})]
);
report({
metricName: 'job',
attributes: { result: 'success', 'job-id': name }
});
await new Promise((r) => setTimeout(r, 3000));
process.exit();
} catch (e) {
report({
metricName: 'job',
attributes: { result: 'failed', 'job-id': name }
});
await new Promise((r) => setTimeout(r, 3000));
Log.error('failed to run - ', name, e);
Log.error(e.stack);
process.exit(1);
}
}
module.exports = {
report,
runAndReport
};