-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
common.js
166 lines (148 loc) · 3.9 KB
/
common.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
*
* @param labels {Object | string[] | string}
* @returns {Object}
*/
module.exports.parseLabels = (labels) => {
if (Array.isArray(labels)) {
return labels.reduce((sum, l) => {
sum[l[0]] = l[1]
return sum
}, {})
}
if (typeof labels === 'object') {
return labels
}
return JSON.parse(labels)
}
/**
*
* @param labels {Object | string[] | string}
* @returns {string}
*/
module.exports.hashLabels = (labels) => {
if (Array.isArray(labels)) {
return JSON.stringify(labels)
}
if (typeof labels === 'object' && labels !== null) {
const res = [...Object.entries(labels)]
res.sort()
return JSON.stringify(labels)
}
return labels
}
/**
*
* @param name {string}
* @returns {boolean}
*/
function boolEnv (name) {
const boolVal = process.env[name]
if (typeof boolVal === 'undefined' || ['no', 'n', 'false', '0'].indexOf(`${boolVal}`.toLowerCase()) !== -1) {
return false
}
if (['yes', 'y', 'true', '1'].indexOf(`${boolVal}`.toLowerCase()) !== -1) {
return true
}
throw new Error(`${name} value must be one of [no, n, false, 0, yes, y, true, 1]`)
}
module.exports.boolEnv = boolEnv
/**
*
* @param durationStr {string}
* @returns {number}
*/
module.exports.durationToMs = (durationStr) => {
const durations = {
ns: 1 / 1000000,
us: 1 / 1000,
ms: 1,
s: 1000,
m: 60000,
h: 60000 * 60,
d: 60000 * 60 * 24,
w: 60000 * 60 * 24 * 7
}
for (const k of Object.keys(durations)) {
const m = durationStr.match(new RegExp(`^([0-9][.0-9]*)${k}$`))
if (m) {
return parseInt(m[1]) * durations[k]
}
}
throw new Error('Unsupported duration')
}
/**
*
* @param durationStr {string}
* @returns {number}
*/
module.exports.durationToNs = (durationStr) => {
const durations = {
ns: 1,
us: 1000,
ms: 1000000,
s: 1000000000,
m: 60000000000,
h: 60000000000 * 60,
d: 60000000000 * 60 * 24,
w: 60000000000 * 60 * 24 * 7
}
for (const k of Object.keys(durations)) {
const m = durationStr.match(new RegExp(`^([0-9][.0-9]*)${k}$`))
if (m) {
return parseInt(m[1]) * durations[k]
}
}
throw new Error('Unsupported duration')
}
module.exports.asyncLogError = async (err, logger) => {
try {
const resp = err.response || err.err.response
if (resp) {
if (typeof resp.data === 'object') {
err.responseData = ''
err.response.data.on('data', data => { err.responseData += data })
await new Promise((resolve) => err.response.data.once('end', resolve))
} else {
err.responseData = err.response.data
}
logger.error(err)
}
} catch (e) {
logger.error(err)
}
}
module.exports.isOmitTablesCreation = () => boolEnv('OMIT_CREATE_TABLES')
module.exports.LineFmtOption = () => process.env.LINE_FMT || 'handlebars'
module.exports.errors = require('./lib/handlers/errors')
/**
* @returns {string}
*/
module.exports.samplesOrderingRule = () => {
return process.env.ADVANCED_SAMPLES_ORDERING
? process.env.ADVANCED_SAMPLES_ORDERING
: 'timestamp_ns'
}
/**
* @returns {boolean}
*/
module.exports.isCustomSamplesOrderingRule = () => {
return process.env.ADVANCED_SAMPLES_ORDERING && process.env.ADVANCED_SAMPLES_ORDERING !== 'timestamp_ns'
}
module.exports.CORS = process.env.CORS_ALLOW_ORIGIN || '*'
module.exports.clusterName = process.env.CLUSTER_NAME
module.exports.readonly = boolEnv('READONLY')
module.exports.bun = () => {
try {
return Bun
} catch (err) {
return false
}
}
module.exports.logType = boolEnv('DISTINGUISH_LOGS_METRICS') ? 1 : 0
module.exports.metricType = boolEnv('DISTINGUISH_LOGS_METRICS') ? 2 : 0
module.exports.bothType = 0
module.exports.writerMode = (process.env.MODE === 'writer' || !process.env.MODE || process.env.MODE === 'all') &&
!boolEnv('READONLY')
module.exports.readerMode = process.env.MODE === 'reader' || process.env.MODE === 'all' || boolEnv('READONLY') ||
!process.env.MODE