Skip to content

Commit

Permalink
Merge pull request #477 from metrico/bool_env
Browse files Browse the repository at this point in the history
fix: boolean environment
  • Loading branch information
akvlad authored Mar 19, 2024
2 parents ae965fb + 3e32e05 commit fdc3153
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
26 changes: 22 additions & 4 deletions common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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'

Expand All @@ -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 {
Expand All @@ -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
4 changes: 2 additions & 2 deletions lib/db/clickhouse.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions lib/db/clickhouse_options.js
Original file line number Diff line number Diff line change
@@ -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' }
}

Expand Down
7 changes: 4 additions & 3 deletions qryn_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit fdc3153

Please sign in to comment.