From 3e32e053ae703b6f14a563e2dad739117eae8b0f Mon Sep 17 00:00:00 2001 From: akvlad Date: Tue, 19 Mar 2024 13:29:28 +0200 Subject: [PATCH] fix: boolean environment --- common.js | 26 ++++++++++++++++++++++---- lib/db/clickhouse.js | 4 ++-- lib/db/clickhouse_options.js | 4 ++-- qryn_node.js | 7 ++++--- 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/common.js b/common.js index e16812bf..e8748973 100644 --- a/common.js +++ b/common.js @@ -33,6 +33,24 @@ module.exports.hashLabels = (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} @@ -101,7 +119,7 @@ module.exports.asyncLogError = async (err, logger) => { } } -module.exports.isOmitTablesCreation = () => process.env.OMIT_CREATE_TABLES === '1' +module.exports.isOmitTablesCreation = () => boolEnv('OMIT_CREATE_TABLES') module.exports.LineFmtOption = () => process.env.LINE_FMT || 'handlebars' @@ -126,7 +144,7 @@ module.exports.CORS = process.env.CORS_ALLOW_ORIGIN || '*' module.exports.clusterName = process.env.CLUSTER_NAME -module.exports.readonly = process.env.READONLY || false +module.exports.readonly = boolEnv('READONLY') module.exports.bun = () => { try { @@ -136,8 +154,8 @@ module.exports.bun = () => { } } -module.exports.logType = process.env.DISTINGUISH_LOGS_METRICS ? 1 : 0 +module.exports.logType = boolEnv('DISTINGUISH_LOGS_METRICS') ? 1 : 0 -module.exports.metricType = process.env.DISTINGUISH_LOGS_METRICS ? 2 : 0 +module.exports.metricType = boolEnv('DISTINGUISH_LOGS_METRICS') ? 2 : 0 module.exports.bothType = 0 diff --git a/lib/db/clickhouse.js b/lib/db/clickhouse.js index 008b70e4..f8426c64 100644 --- a/lib/db/clickhouse.js +++ b/lib/db/clickhouse.js @@ -25,7 +25,7 @@ const axios = require('axios') const { samplesTableName, samplesReadTableName } = UTILS const path = require('path') const { Transform } = require('stream') -const { CORS, bun, readonly } = require('../../common') +const { CORS, bun, readonly, boolEnv } = require('../../common') const clickhouseOptions = require('./clickhouse_options').databaseOptions const { getClickhouseUrl } = require('./clickhouse_options') @@ -1050,7 +1050,7 @@ const scanClickhouse = function (settings, client, params) { template += ' GROUP BY t, ' + settings.tag + ' ORDER BY t, ' + settings.tag + ')' template += ' GROUP BY ' + settings.tag + ' ORDER BY ' + settings.tag // Read-Only: Initiate a new driver connection - if (process.env.READONLY) { + if (boolEnv('READONLY')) { const tmp = { ...clickhouseOptions, queryOptions: { database: settings.db } } ch = new ClickHouse(tmp) } diff --git a/lib/db/clickhouse_options.js b/lib/db/clickhouse_options.js index 2db4510b..76a15bc4 100644 --- a/lib/db/clickhouse_options.js +++ b/lib/db/clickhouse_options.js @@ -1,12 +1,12 @@ const UTILS = require('../utils') const { samplesTableName, samplesReadTableName } = UTILS - +const { boolEnv } = require('../../common') const clickhouseOptions = { host: process.env.CLICKHOUSE_SERVER || 'localhost', port: process.env.CLICKHOUSE_PORT || 8123, auth: process.env.CLICKHOUSE_AUTH || 'default:', protocol: process.env.CLICKHOUSE_PROTO ? process.env.CLICKHOUSE_PROTO + ':' : 'http:', - readonly: !!process.env.READONLY, + readonly: boolEnv('READONLY'), queryOptions: { database: process.env.CLICKHOUSE_DB || 'cloki' } } diff --git a/qryn_node.js b/qryn_node.js index 3f80063f..62f08dae 100755 --- a/qryn_node.js +++ b/qryn_node.js @@ -4,8 +4,9 @@ * qryn: polyglot observability API * (C) 2018-2024 QXIP BV */ +const { boolEnv } = require('./common') -this.readonly = process.env.READONLY || false +this.readonly = boolEnv('READONLY') this.http_user = process.env.QRYN_LOGIN || process.env.CLOKI_LOGIN || undefined this.http_password = process.env.QRYN_PASSWORD || process.env.CLOKI_PASSWORD || undefined @@ -117,7 +118,7 @@ let fastify = require('fastify')({ await fastify.register(require('@fastify/websocket')) /* Fastify local metrics exporter */ - if (process.env.FASTIFY_METRICS) { + if (boolEnv('FASTIFY_METRICS')) { const metricsPlugin = require('fastify-metrics') fastify.register(metricsPlugin, { endpoint: '/metrics' }) } else { @@ -239,7 +240,7 @@ let fastify = require('fastify')({ }) /* Tempo Write Handler */ - this.tempo_tagtrace = process.env.TEMPO_TAGTRACE || false + this.tempo_tagtrace = boolEnv('TEMPO_TAGTRACE') const handlerTempoPush = require('./lib/handlers/tempo_push.js').bind(this) fastify.post('/tempo/api/push', handlerTempoPush, { 'application/json': tempoPushParser,