-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstrumentation.js
119 lines (92 loc) · 3.08 KB
/
instrumentation.js
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
function exec(command, options) {
return new Promise((res, rej) => {
let output = "";
const write = (data) => {
output += data;
};
const cmd = require("child_process").exec(command, options);
cmd.stderr.on("data", write);
cmd.stdout.on("data", write);
cmd.on("error", write);
cmd.once("exit", (code) => {
cmd.stderr.off("data", write);
cmd.stdout.off("data", write);
cmd.off("error", write);
if (code !== 0) rej(new Error(`Command failed: ${command}\n${output}`));
res(output);
});
});
}
function tagsCheck(tags,DEFAULT_TAGS){
if (!(tags instanceof Array))
tags = Object.keys(tags).map(tg=> `${tg}:${tags[tg]}` );
return DEFAULT_TAGS.concat(tags);
}
(async () => {
const INSTANCE = process.env.PRIME_FLAVORED_CLIENT || process.env.NODE_ENV !== "production" ? "main" : "beta";
process.env.DD_VERSION= (await exec("git rev-parse --short HEAD")).trim();
process.env.DD_ENV= process.env.NODE_ENV;
process.env.DD_SERVICE="Pollux-" + INSTANCE;
process.env.DD_LOGS_INJECTION=true;
global.INSTR = {};
const StatsD = require('hot-shots');
const dogstatD = new StatsD();
/*
const tracer = require('dd-trace').init({
logInjection: true,
analytics: true,
tags: {
cluster: process.env.PRIME_FLAVORED_CLIENT ? "-1: dev" : clusterNames[parseInt(process.env.CLUSTER_ID) || 0]
}
});
tracer.use('bluebird', {service: 'bluebird'});
tracer.use('mongoose', {service: 'mongoose'});
*/
const DEFAULT_TAGS = ['client:'+ INSTANCE || "unknown", 'cluster:'+ PLX.cluster.name, "build:"+process.env.DD_VERSION]
global.INSTR.inc = (metric,tags=[],rate=1) => {
tags = tagsCheck(tags,DEFAULT_TAGS);
return dogstatD.increment("plx."+metric,rate, tags )
}
global.INSTR.dec = (metric,tags=[],rate=1) => {
tags = tagsCheck(tags,DEFAULT_TAGS);
return dogstatD.decrement("plx."+metric,rate, tags )
}
global.INSTR.gauge = (metric,value,tags=[]) => {
tags = tagsCheck(tags,DEFAULT_TAGS);
return dogstatD.gauge( "plx."+metric , value, tags )
}
global.INSTR.top_inc = (metric,tags=[],rate) => {
tags = tagsCheck(tags,DEFAULT_TAGS);
return dogstatD.increment(metric, DEFAULT_TAGS.concat(tags) )
}
global.INSTR.event = (title, message, eventData, tags) => {
if (PLX.logInstr) console.log({title,eventData});
tags ??= eventData.tags
tags = tagsCheck(tags,DEFAULT_TAGS);
eventData.timestamp ??= Date.now();
eventData.tags ??= DEFAULT_TAGS.concat(tags);
//eventData.aggregation_key
//eventData.priority
//eventData.source_type
//eventData.alert_type // error, warning, success, or info
return dogstatD.event( title, message, eventData );
}
global.INSTR.error = (t,m,ed,tg) => {
ed.alert_type = "error";
return global.INSTR.event(t,m,ed,tg)
}
global.INSTR.info = (t,m,ed,tg) => {
ed.alert_type = "info";
ed.priority ??= "low";
return global.INSTR.event(t,m,ed,tg)
}
global.INSTR.warn = (t,m,ed,tg) => {
ed.alert_type = "warning";
return global.INSTR.event(t,m,ed,tg)
}
global.INSTR.success = (t,m,ed,tg) => {
ed.alert_type = "success";
ed.priority ??= "low";
return global.INSTR.event(t,m,ed,tg)
}
})()