From 7e84db980369d13b00b3cfac816493c6f3c1b669 Mon Sep 17 00:00:00 2001 From: Daniel Kanchev Date: Fri, 27 Jul 2018 10:38:37 +0300 Subject: [PATCH 1/4] Added getEnvBool helper function to parse env variables of type boolean. Now all config parameters can be set as env variables. --- main.go | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/main.go b/main.go index 6b9960c9..c2c8d127 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "net/http" "os" "runtime" + "strconv" "github.com/oliver006/redis_exporter/exporter" "github.com/prometheus/client_golang/prometheus" @@ -18,18 +19,18 @@ var ( redisFile = flag.String("redis.file", getEnv("REDIS_FILE", ""), "Path to file containing one or more redis nodes, separated by newline. NOTE: mutually exclusive with redis.addr") redisPassword = flag.String("redis.password", getEnv("REDIS_PASSWORD", ""), "Password for one or more redis nodes, separated by separator") redisAlias = flag.String("redis.alias", getEnv("REDIS_ALIAS", ""), "Redis instance alias for one or more redis nodes, separated by separator") - namespace = flag.String("namespace", "redis", "Namespace for metrics") - checkKeys = flag.String("check-keys", "", "Comma separated list of key-patterns to export value and length/size, searched for with SCAN") - checkSingleKeys = flag.String("check-single-keys", "", "Comma separated list of single keys to export value and length/size") - scriptPath = flag.String("script", "", "Path to Lua Redis script for collecting extra metrics") - separator = flag.String("separator", ",", "separator used to split redis.addr, redis.password and redis.alias into several elements.") - listenAddress = flag.String("web.listen-address", ":9121", "Address to listen on for web interface and telemetry.") - metricPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.") - isDebug = flag.Bool("debug", false, "Output verbose debug information") - logFormat = flag.String("log-format", "txt", "Log format, valid options are txt and json") - showVersion = flag.Bool("version", false, "Show version information and exit") - useCfBindings = flag.Bool("use-cf-bindings", false, "Use Cloud Foundry service bindings") - redisMetricsOnly = flag.Bool("redis-only-metrics", false, "Whether to export go runtime metrics also") + namespace = flag.String("namespace", getEnv("NAMESPACE", "redis"), "Namespace for metrics") + checkKeys = flag.String("check-keys", getEnv("CHECK_KEYS", ""), "Comma separated list of key-patterns to export value and length/size, searched for with SCAN") + checkSingleKeys = flag.String("check-single-keys", getEnv("CHECK_SINGLE_KEYS", ""), "Comma separated list of single keys to export value and length/size") + scriptPath = flag.String("script", getEnv("SCRIPT", ""), "Path to Lua Redis script for collecting extra metrics") + separator = flag.String("separator", getEnv("SEPARATOR", ","), "separator used to split redis.addr, redis.password and redis.alias into several elements.") + listenAddress = flag.String("web.listen-address", getEnv("WEB_LISTEN_ADDRESS", ":9121"), "Address to listen on for web interface and telemetry.") + metricPath = flag.String("web.telemetry-path", getEnv("WEB_TELEMETRY_PATH", "/metrics"), "Path under which to expose metrics.") + isDebug = flag.Bool("debug", getEnvBool("DEBUG"), "Output verbose debug information") + logFormat = flag.String("log-format", getEnv("LOG_FORMAT", "txt"), "Log format, valid options are txt and json") + showVersion = flag.Bool("version", getEnvBool("VERSION"), "Show version information and exit") + useCfBindings = flag.Bool("use-cf-bindings", getEnvBool("USE-CF-BINDINGS"), "Use Cloud Foundry service bindings") + redisMetricsOnly = flag.Bool("redis-only-metrics", getEnvBool("REDIS_ONLY_METRICS"), "Whether to export go runtime metrics also") // VERSION, BUILD_DATE, GIT_COMMIT are filled in by the build script VERSION = "<<< filled in by build >>>" @@ -44,6 +45,16 @@ func getEnv(key string, defaultVal string) string { return defaultVal } +func getEnvBool(key string) bool { + if envVal, ok := os.LookupEnv(key); ok { + if envValBool, err := strconv.ParseBool(envVal); err ==nil { + return envValBool + } + return false + } + return false +} + func main() { flag.Parse() From 5cc9a53488922520350c89b82a9b274675556acd Mon Sep 17 00:00:00 2001 From: Daniel Kanchev Date: Thu, 16 Aug 2018 10:58:30 +0300 Subject: [PATCH 2/4] Refactored getEnvBool helper function. Added prefix to all env variables. --- main.go | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/main.go b/main.go index 6b9960c9..9bd3a44b 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "net/http" "os" "runtime" + "strconv" "github.com/oliver006/redis_exporter/exporter" "github.com/prometheus/client_golang/prometheus" @@ -18,18 +19,18 @@ var ( redisFile = flag.String("redis.file", getEnv("REDIS_FILE", ""), "Path to file containing one or more redis nodes, separated by newline. NOTE: mutually exclusive with redis.addr") redisPassword = flag.String("redis.password", getEnv("REDIS_PASSWORD", ""), "Password for one or more redis nodes, separated by separator") redisAlias = flag.String("redis.alias", getEnv("REDIS_ALIAS", ""), "Redis instance alias for one or more redis nodes, separated by separator") - namespace = flag.String("namespace", "redis", "Namespace for metrics") - checkKeys = flag.String("check-keys", "", "Comma separated list of key-patterns to export value and length/size, searched for with SCAN") - checkSingleKeys = flag.String("check-single-keys", "", "Comma separated list of single keys to export value and length/size") - scriptPath = flag.String("script", "", "Path to Lua Redis script for collecting extra metrics") - separator = flag.String("separator", ",", "separator used to split redis.addr, redis.password and redis.alias into several elements.") - listenAddress = flag.String("web.listen-address", ":9121", "Address to listen on for web interface and telemetry.") - metricPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.") - isDebug = flag.Bool("debug", false, "Output verbose debug information") - logFormat = flag.String("log-format", "txt", "Log format, valid options are txt and json") - showVersion = flag.Bool("version", false, "Show version information and exit") - useCfBindings = flag.Bool("use-cf-bindings", false, "Use Cloud Foundry service bindings") - redisMetricsOnly = flag.Bool("redis-only-metrics", false, "Whether to export go runtime metrics also") + namespace = flag.String("namespace", getEnv("REDIS_EXPORTER_NAMESPACE", "redis"), "Namespace for metrics") + checkKeys = flag.String("check-keys", getEnv("REDIS_EXPORTER_CHECK_KEYS", ""), "Comma separated list of key-patterns to export value and length/size, searched for with SCAN") + checkSingleKeys = flag.String("check-single-keys", getEnv("REDIS_EXPORTER_CHECK_SINGLE_KEYS", ""), "Comma separated list of single keys to export value and length/size") + scriptPath = flag.String("script", getEnv("REDIS_EXPORTER_SCRIPT", ""), "Path to Lua Redis script for collecting extra metrics") + separator = flag.String("separator", getEnv("REDIS_EXPORTER_SEPARATOR", ","), "separator used to split redis.addr, redis.password and redis.alias into several elements.") + listenAddress = flag.String("web.listen-address", getEnv("REDIS_EXPORTER_WEB_LISTEN_ADDRESS", ":9121"), "Address to listen on for web interface and telemetry.") + metricPath = flag.String("web.telemetry-path", getEnv("REDIS_EXPORTER_WEB_TELEMETRY_PATH", "/metrics"), "Path under which to expose metrics.") + isDebug = flag.Bool("debug", getEnvBool("REDIS_EXPORTER_DEBUG"), "Output verbose debug information") + logFormat = flag.String("log-format", getEnv("REDIS_EXPORTER_LOG_FORMAT", "txt"), "Log format, valid options are txt and json") + showVersion = flag.Bool("version", getEnvBool("REDIS_EXPORTER_VERSION"), "Show version information and exit") + useCfBindings = flag.Bool("use-cf-bindings", getEnvBool("REDIS_EXPORTER_USE-CF-BINDINGS"), "Use Cloud Foundry service bindings") + redisMetricsOnly = flag.Bool("redis-only-metrics", getEnvBool("REDIS_EXPORTER_REDIS_ONLY_METRICS"), "Whether to export go runtime metrics also") // VERSION, BUILD_DATE, GIT_COMMIT are filled in by the build script VERSION = "<<< filled in by build >>>" @@ -44,6 +45,13 @@ func getEnv(key string, defaultVal string) string { return defaultVal } +func getEnvBool(key string) (envValBool bool) { + if envVal, ok := os.LookupEnv(key); ok { + envValBool, _ = strconv.ParseBool(envVal) + } + return +} + func main() { flag.Parse() From 718ab8a1883d88af7501b00242f9afbc0c477544 Mon Sep 17 00:00:00 2001 From: Daniel Kanchev Date: Fri, 17 Aug 2018 10:48:43 +0300 Subject: [PATCH 3/4] Removed code for getting env variables which do not make sense. --- main.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index 9bd3a44b..928a5a3a 100644 --- a/main.go +++ b/main.go @@ -19,16 +19,16 @@ var ( redisFile = flag.String("redis.file", getEnv("REDIS_FILE", ""), "Path to file containing one or more redis nodes, separated by newline. NOTE: mutually exclusive with redis.addr") redisPassword = flag.String("redis.password", getEnv("REDIS_PASSWORD", ""), "Password for one or more redis nodes, separated by separator") redisAlias = flag.String("redis.alias", getEnv("REDIS_ALIAS", ""), "Redis instance alias for one or more redis nodes, separated by separator") - namespace = flag.String("namespace", getEnv("REDIS_EXPORTER_NAMESPACE", "redis"), "Namespace for metrics") - checkKeys = flag.String("check-keys", getEnv("REDIS_EXPORTER_CHECK_KEYS", ""), "Comma separated list of key-patterns to export value and length/size, searched for with SCAN") - checkSingleKeys = flag.String("check-single-keys", getEnv("REDIS_EXPORTER_CHECK_SINGLE_KEYS", ""), "Comma separated list of single keys to export value and length/size") - scriptPath = flag.String("script", getEnv("REDIS_EXPORTER_SCRIPT", ""), "Path to Lua Redis script for collecting extra metrics") - separator = flag.String("separator", getEnv("REDIS_EXPORTER_SEPARATOR", ","), "separator used to split redis.addr, redis.password and redis.alias into several elements.") - listenAddress = flag.String("web.listen-address", getEnv("REDIS_EXPORTER_WEB_LISTEN_ADDRESS", ":9121"), "Address to listen on for web interface and telemetry.") - metricPath = flag.String("web.telemetry-path", getEnv("REDIS_EXPORTER_WEB_TELEMETRY_PATH", "/metrics"), "Path under which to expose metrics.") - isDebug = flag.Bool("debug", getEnvBool("REDIS_EXPORTER_DEBUG"), "Output verbose debug information") - logFormat = flag.String("log-format", getEnv("REDIS_EXPORTER_LOG_FORMAT", "txt"), "Log format, valid options are txt and json") - showVersion = flag.Bool("version", getEnvBool("REDIS_EXPORTER_VERSION"), "Show version information and exit") + namespace = flag.String("namespace", "redis", "Namespace for metrics") + checkKeys = flag.String("check-keys", "", "Comma separated list of key-patterns to export value and length/size, searched for with SCAN") + checkSingleKeys = flag.String("check-single-keys", "", "Comma separated list of single keys to export value and length/size") + scriptPath = flag.String("script", "", "Path to Lua Redis script for collecting extra metrics") + separator = flag.String("separator", ",", "separator used to split redis.addr, redis.password and redis.alias into several elements.") + listenAddress = flag.String("web.listen-address", ":9121", "Address to listen on for web interface and telemetry.") + metricPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.") + isDebug = flag.Bool("debug", false, "Output verbose debug information") + logFormat = flag.String("log-format", "txt", "Log format, valid options are txt and json") + showVersion = flag.Bool("version", false, "Show version information and exit") useCfBindings = flag.Bool("use-cf-bindings", getEnvBool("REDIS_EXPORTER_USE-CF-BINDINGS"), "Use Cloud Foundry service bindings") redisMetricsOnly = flag.Bool("redis-only-metrics", getEnvBool("REDIS_EXPORTER_REDIS_ONLY_METRICS"), "Whether to export go runtime metrics also") From 14570a78a056888552e2671fb73975176e66ca48 Mon Sep 17 00:00:00 2001 From: Daniel Kanchev Date: Mon, 20 Aug 2018 15:01:01 +0300 Subject: [PATCH 4/4] Removed only the env variable for the exporter version --- main.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index 928a5a3a..6ba4f6c5 100644 --- a/main.go +++ b/main.go @@ -19,15 +19,15 @@ var ( redisFile = flag.String("redis.file", getEnv("REDIS_FILE", ""), "Path to file containing one or more redis nodes, separated by newline. NOTE: mutually exclusive with redis.addr") redisPassword = flag.String("redis.password", getEnv("REDIS_PASSWORD", ""), "Password for one or more redis nodes, separated by separator") redisAlias = flag.String("redis.alias", getEnv("REDIS_ALIAS", ""), "Redis instance alias for one or more redis nodes, separated by separator") - namespace = flag.String("namespace", "redis", "Namespace for metrics") - checkKeys = flag.String("check-keys", "", "Comma separated list of key-patterns to export value and length/size, searched for with SCAN") - checkSingleKeys = flag.String("check-single-keys", "", "Comma separated list of single keys to export value and length/size") - scriptPath = flag.String("script", "", "Path to Lua Redis script for collecting extra metrics") - separator = flag.String("separator", ",", "separator used to split redis.addr, redis.password and redis.alias into several elements.") - listenAddress = flag.String("web.listen-address", ":9121", "Address to listen on for web interface and telemetry.") - metricPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.") - isDebug = flag.Bool("debug", false, "Output verbose debug information") - logFormat = flag.String("log-format", "txt", "Log format, valid options are txt and json") + namespace = flag.String("namespace", getEnv("REDIS_EXPORTER_NAMESPACE", "redis"), "Namespace for metrics") + checkKeys = flag.String("check-keys", getEnv("REDIS_EXPORTER_CHECK_KEYS", ""), "Comma separated list of key-patterns to export value and length/size, searched for with SCAN") + checkSingleKeys = flag.String("check-single-keys", getEnv("REDIS_EXPORTER_CHECK_SINGLE_KEYS", ""), "Comma separated list of single keys to export value and length/size") + scriptPath = flag.String("script", getEnv("REDIS_EXPORTER_SCRIPT", ""), "Path to Lua Redis script for collecting extra metrics") + separator = flag.String("separator", getEnv("REDIS_EXPORTER_SEPARATOR", ","), "separator used to split redis.addr, redis.password and redis.alias into several elements.") + listenAddress = flag.String("web.listen-address", getEnv("REDIS_EXPORTER_WEB_LISTEN_ADDRESS", ":9121"), "Address to listen on for web interface and telemetry.") + metricPath = flag.String("web.telemetry-path", getEnv("REDIS_EXPORTER_WEB_TELEMETRY_PATH", "/metrics"), "Path under which to expose metrics.") + isDebug = flag.Bool("debug", getEnvBool("REDIS_EXPORTER_DEBUG"), "Output verbose debug information") + logFormat = flag.String("log-format", getEnv("REDIS_EXPORTER_LOG_FORMAT", "txt"), "Log format, valid options are txt and json") showVersion = flag.Bool("version", false, "Show version information and exit") useCfBindings = flag.Bool("use-cf-bindings", getEnvBool("REDIS_EXPORTER_USE-CF-BINDINGS"), "Use Cloud Foundry service bindings") redisMetricsOnly = flag.Bool("redis-only-metrics", getEnvBool("REDIS_EXPORTER_REDIS_ONLY_METRICS"), "Whether to export go runtime metrics also")