Skip to content

Commit fdc3153

Browse files
authored
Merge pull request #477 from metrico/bool_env
fix: boolean environment
2 parents ae965fb + 3e32e05 commit fdc3153

File tree

4 files changed

+30
-11
lines changed

4 files changed

+30
-11
lines changed

common.js

+22-4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,24 @@ module.exports.hashLabels = (labels) => {
3333
return labels
3434
}
3535

36+
/**
37+
*
38+
* @param name {string}
39+
* @returns {boolean}
40+
*/
41+
function boolEnv (name) {
42+
const boolVal = process.env[name]
43+
if (typeof boolVal === 'undefined' || ['no', 'n', 'false', '0'].indexOf(`${boolVal}`.toLowerCase()) !== -1) {
44+
return false
45+
}
46+
if (['yes', 'y', 'true', '1'].indexOf(`${boolVal}`.toLowerCase()) !== -1) {
47+
return true
48+
}
49+
throw new Error(`${name} value must be one of [no, n, false, 0, yes, y, true, 1]`)
50+
}
51+
52+
module.exports.boolEnv = boolEnv
53+
3654
/**
3755
*
3856
* @param durationStr {string}
@@ -101,7 +119,7 @@ module.exports.asyncLogError = async (err, logger) => {
101119
}
102120
}
103121

104-
module.exports.isOmitTablesCreation = () => process.env.OMIT_CREATE_TABLES === '1'
122+
module.exports.isOmitTablesCreation = () => boolEnv('OMIT_CREATE_TABLES')
105123

106124
module.exports.LineFmtOption = () => process.env.LINE_FMT || 'handlebars'
107125

@@ -126,7 +144,7 @@ module.exports.CORS = process.env.CORS_ALLOW_ORIGIN || '*'
126144

127145
module.exports.clusterName = process.env.CLUSTER_NAME
128146

129-
module.exports.readonly = process.env.READONLY || false
147+
module.exports.readonly = boolEnv('READONLY')
130148

131149
module.exports.bun = () => {
132150
try {
@@ -136,8 +154,8 @@ module.exports.bun = () => {
136154
}
137155
}
138156

139-
module.exports.logType = process.env.DISTINGUISH_LOGS_METRICS ? 1 : 0
157+
module.exports.logType = boolEnv('DISTINGUISH_LOGS_METRICS') ? 1 : 0
140158

141-
module.exports.metricType = process.env.DISTINGUISH_LOGS_METRICS ? 2 : 0
159+
module.exports.metricType = boolEnv('DISTINGUISH_LOGS_METRICS') ? 2 : 0
142160

143161
module.exports.bothType = 0

lib/db/clickhouse.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const axios = require('axios')
2525
const { samplesTableName, samplesReadTableName } = UTILS
2626
const path = require('path')
2727
const { Transform } = require('stream')
28-
const { CORS, bun, readonly } = require('../../common')
28+
const { CORS, bun, readonly, boolEnv } = require('../../common')
2929
const clickhouseOptions = require('./clickhouse_options').databaseOptions
3030
const { getClickhouseUrl } = require('./clickhouse_options')
3131

@@ -1050,7 +1050,7 @@ const scanClickhouse = function (settings, client, params) {
10501050
template += ' GROUP BY t, ' + settings.tag + ' ORDER BY t, ' + settings.tag + ')'
10511051
template += ' GROUP BY ' + settings.tag + ' ORDER BY ' + settings.tag
10521052
// Read-Only: Initiate a new driver connection
1053-
if (process.env.READONLY) {
1053+
if (boolEnv('READONLY')) {
10541054
const tmp = { ...clickhouseOptions, queryOptions: { database: settings.db } }
10551055
ch = new ClickHouse(tmp)
10561056
}

lib/db/clickhouse_options.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
const UTILS = require('../utils')
22
const { samplesTableName, samplesReadTableName } = UTILS
3-
3+
const { boolEnv } = require('../../common')
44
const clickhouseOptions = {
55
host: process.env.CLICKHOUSE_SERVER || 'localhost',
66
port: process.env.CLICKHOUSE_PORT || 8123,
77
auth: process.env.CLICKHOUSE_AUTH || 'default:',
88
protocol: process.env.CLICKHOUSE_PROTO ? process.env.CLICKHOUSE_PROTO + ':' : 'http:',
9-
readonly: !!process.env.READONLY,
9+
readonly: boolEnv('READONLY'),
1010
queryOptions: { database: process.env.CLICKHOUSE_DB || 'cloki' }
1111
}
1212

qryn_node.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
* qryn: polyglot observability API
55
* (C) 2018-2024 QXIP BV
66
*/
7+
const { boolEnv } = require('./common')
78

8-
this.readonly = process.env.READONLY || false
9+
this.readonly = boolEnv('READONLY')
910
this.http_user = process.env.QRYN_LOGIN || process.env.CLOKI_LOGIN || undefined
1011
this.http_password = process.env.QRYN_PASSWORD || process.env.CLOKI_PASSWORD || undefined
1112

@@ -117,7 +118,7 @@ let fastify = require('fastify')({
117118
await fastify.register(require('@fastify/websocket'))
118119

119120
/* Fastify local metrics exporter */
120-
if (process.env.FASTIFY_METRICS) {
121+
if (boolEnv('FASTIFY_METRICS')) {
121122
const metricsPlugin = require('fastify-metrics')
122123
fastify.register(metricsPlugin, { endpoint: '/metrics' })
123124
} else {
@@ -239,7 +240,7 @@ let fastify = require('fastify')({
239240
})
240241

241242
/* Tempo Write Handler */
242-
this.tempo_tagtrace = process.env.TEMPO_TAGTRACE || false
243+
this.tempo_tagtrace = boolEnv('TEMPO_TAGTRACE')
243244
const handlerTempoPush = require('./lib/handlers/tempo_push.js').bind(this)
244245
fastify.post('/tempo/api/push', handlerTempoPush, {
245246
'application/json': tempoPushParser,

0 commit comments

Comments
 (0)