From c6ff1ccf4778b237ec042f3a5a0c76b2e02963aa Mon Sep 17 00:00:00 2001 From: Jakub Date: Wed, 15 Mar 2023 19:08:33 +0100 Subject: [PATCH] Split helpers package into prometheus_helper and config functions --- cmd/exporter/main.go | 3 +- collectors/nodeinfo/nodeinfo_collector.go | 10 +-- .../nodeinfo/nodeinfo_collector_test.go | 7 +- collectors/nodestats/nodestats_collector.go | 7 +- .../nodestats/nodestats_collector_test.go | 7 +- collectors/nodestats/pipeline_subcollector.go | 7 +- config/env_helper.go | 19 +++++ config/env_helper_test.go | 26 +++++++ config/logstash_config.go | 4 +- config/server_config.go | 6 +- helpers/env_helper.go | 32 --------- helpers/env_helper_test.go | 69 ------------------- .../prometheus_helper.go | 2 +- .../prometheus_helper_test.go | 2 +- 14 files changed, 72 insertions(+), 129 deletions(-) create mode 100644 config/env_helper.go create mode 100644 config/env_helper_test.go delete mode 100644 helpers/env_helper.go delete mode 100644 helpers/env_helper_test.go rename {helpers => prometheus_helper}/prometheus_helper.go (97%) rename {helpers => prometheus_helper}/prometheus_helper_test.go (98%) diff --git a/cmd/exporter/main.go b/cmd/exporter/main.go index 65c8f60f..d710315d 100644 --- a/cmd/exporter/main.go +++ b/cmd/exporter/main.go @@ -5,13 +5,12 @@ import ( "github.com/kuskoman/logstash-exporter/collectors" "github.com/kuskoman/logstash-exporter/config" - "github.com/kuskoman/logstash-exporter/helpers" "github.com/kuskoman/logstash-exporter/server" "github.com/prometheus/client_golang/prometheus" ) func main() { - warn := helpers.InitializeEnv() + warn := config.InitializeEnv() if warn != nil { log.Println(warn) } diff --git a/collectors/nodeinfo/nodeinfo_collector.go b/collectors/nodeinfo/nodeinfo_collector.go index f56bca39..240d7ea2 100644 --- a/collectors/nodeinfo/nodeinfo_collector.go +++ b/collectors/nodeinfo/nodeinfo_collector.go @@ -1,15 +1,15 @@ package nodeinfo import ( + "log" "strconv" + "github.com/prometheus/client_golang/prometheus" + "github.com/kuskoman/logstash-exporter/config" logstashclient "github.com/kuskoman/logstash-exporter/fetcher/logstash_client" "github.com/kuskoman/logstash-exporter/fetcher/responses" - "github.com/kuskoman/logstash-exporter/helpers" - "github.com/prometheus/client_golang/prometheus" - - "log" + "github.com/kuskoman/logstash-exporter/prometheus_helper" ) type NodestatsCollector struct { @@ -30,7 +30,7 @@ type NodestatsCollector struct { func NewNodestatsCollector(client logstashclient.Client) *NodestatsCollector { const subsystem = "info" namespace := config.PrometheusNamespace - descHelper := helpers.SimpleDescHelper{Namespace: namespace, Subsystem: subsystem} + descHelper := prometheus_helper.SimpleDescHelper{Namespace: namespace, Subsystem: subsystem} return &NodestatsCollector{ client: client, diff --git a/collectors/nodeinfo/nodeinfo_collector_test.go b/collectors/nodeinfo/nodeinfo_collector_test.go index 8fd5db08..79b20ed8 100644 --- a/collectors/nodeinfo/nodeinfo_collector_test.go +++ b/collectors/nodeinfo/nodeinfo_collector_test.go @@ -5,9 +5,10 @@ import ( "os" "testing" - "github.com/kuskoman/logstash-exporter/fetcher/responses" - "github.com/kuskoman/logstash-exporter/helpers" "github.com/prometheus/client_golang/prometheus" + + "github.com/kuskoman/logstash-exporter/fetcher/responses" + "github.com/kuskoman/logstash-exporter/prometheus_helper" ) type mockClient struct{} @@ -60,7 +61,7 @@ func TestCollectNotNil(t *testing.T) { } foundMetricDesc := metric.Desc().String() - foundMetricFqName, err := helpers.ExtractFqName(foundMetricDesc) + foundMetricFqName, err := prometheus_helper.ExtractFqName(foundMetricDesc) if err != nil { t.Errorf("failed to extract fqName from metric %s", foundMetricDesc) } diff --git a/collectors/nodestats/nodestats_collector.go b/collectors/nodestats/nodestats_collector.go index b0aa403a..26a1fe5a 100644 --- a/collectors/nodestats/nodestats_collector.go +++ b/collectors/nodestats/nodestats_collector.go @@ -4,10 +4,11 @@ import ( "fmt" "log" + "github.com/prometheus/client_golang/prometheus" + "github.com/kuskoman/logstash-exporter/config" logstashclient "github.com/kuskoman/logstash-exporter/fetcher/logstash_client" - "github.com/kuskoman/logstash-exporter/helpers" - "github.com/prometheus/client_golang/prometheus" + "github.com/kuskoman/logstash-exporter/prometheus_helper" ) const subsystem = "stats" @@ -50,7 +51,7 @@ type NodestatsCollector struct { } func NewNodestatsCollector(client logstashclient.Client) *NodestatsCollector { - descHelper := helpers.SimpleDescHelper{Namespace: namespace, Subsystem: subsystem} + descHelper := prometheus_helper.SimpleDescHelper{Namespace: namespace, Subsystem: subsystem} return &NodestatsCollector{ client: client, diff --git a/collectors/nodestats/nodestats_collector_test.go b/collectors/nodestats/nodestats_collector_test.go index e8fa3624..6cbf9bd1 100644 --- a/collectors/nodestats/nodestats_collector_test.go +++ b/collectors/nodestats/nodestats_collector_test.go @@ -5,9 +5,10 @@ import ( "os" "testing" - "github.com/kuskoman/logstash-exporter/fetcher/responses" - "github.com/kuskoman/logstash-exporter/helpers" "github.com/prometheus/client_golang/prometheus" + + "github.com/kuskoman/logstash-exporter/fetcher/responses" + "github.com/kuskoman/logstash-exporter/prometheus_helper" ) type mockClient struct{} @@ -84,7 +85,7 @@ func TestCollectNotNil(t *testing.T) { } foundMetricDesc := metric.Desc().String() - foundMetricFqName, err := helpers.ExtractFqName(foundMetricDesc) + foundMetricFqName, err := prometheus_helper.ExtractFqName(foundMetricDesc) if err != nil { t.Errorf("failed to extract fqName from metric %s", foundMetricDesc) } diff --git a/collectors/nodestats/pipeline_subcollector.go b/collectors/nodestats/pipeline_subcollector.go index bd7fff7b..d990fa67 100644 --- a/collectors/nodestats/pipeline_subcollector.go +++ b/collectors/nodestats/pipeline_subcollector.go @@ -5,9 +5,10 @@ import ( "log" "time" - "github.com/kuskoman/logstash-exporter/fetcher/responses" - "github.com/kuskoman/logstash-exporter/helpers" "github.com/prometheus/client_golang/prometheus" + + "github.com/kuskoman/logstash-exporter/fetcher/responses" + "github.com/kuskoman/logstash-exporter/prometheus_helper" ) type PipelineSubcollector struct { @@ -28,7 +29,7 @@ type PipelineSubcollector struct { } func NewPipelineSubcollector() *PipelineSubcollector { - descHelper := helpers.SimpleDescHelper{Namespace: namespace, Subsystem: fmt.Sprintf("%s_pipeline", subsystem)} + descHelper := prometheus_helper.SimpleDescHelper{Namespace: namespace, Subsystem: fmt.Sprintf("%s_pipeline", subsystem)} return &PipelineSubcollector{ EventsOut: descHelper.NewDescWithHelpAndLabel("events_out", "Number of events that have been processed by this pipeline.", "pipeline_id"), EventsFiltered: descHelper.NewDescWithHelpAndLabel("events_filtered", "Number of events that have been filtered out by this pipeline.", "pipeline_id"), diff --git a/config/env_helper.go b/config/env_helper.go new file mode 100644 index 00000000..aaa76b4f --- /dev/null +++ b/config/env_helper.go @@ -0,0 +1,19 @@ +package config + +import ( + "os" + + "github.com/joho/godotenv" +) + +func InitializeEnv() error { + return godotenv.Load() +} + +func getEnvWithDefault(key string, defaultValue string) string { + value := os.Getenv(key) + if value == "" { + return defaultValue + } + return value +} diff --git a/config/env_helper_test.go b/config/env_helper_test.go new file mode 100644 index 00000000..90f289b8 --- /dev/null +++ b/config/env_helper_test.go @@ -0,0 +1,26 @@ +package config + +import ( + "os" + "testing" +) + +func TestGetEnvWithDefault(t *testing.T) { + t.Run("should return value for set environment variable", func(t *testing.T) { + key := "TEST3" + expected := "value" + os.Setenv(key, expected) + actual := getEnvWithDefault(key, "default") + if actual != expected { + t.Errorf("expected %s but got %s", expected, actual) + } + }) + + t.Run("should return default value for unset environment variable", func(t *testing.T) { + expected := "default" + actual := getEnvWithDefault("TEST_UNSET3", expected) + if actual != expected { + t.Errorf("expected %s but got %s", expected, actual) + } + }) +} diff --git a/config/logstash_config.go b/config/logstash_config.go index 2f03c1d8..c1f8f9c3 100644 --- a/config/logstash_config.go +++ b/config/logstash_config.go @@ -1,7 +1,5 @@ package config -import "github.com/kuskoman/logstash-exporter/helpers" - var ( - LogstashUrl = helpers.GetEnvWithDefault("LOGSTASH_URL", "http://localhost:9600") + LogstashUrl = getEnvWithDefault("LOGSTASH_URL", "http://localhost:9600") ) diff --git a/config/server_config.go b/config/server_config.go index 1b23d563..a80f978a 100644 --- a/config/server_config.go +++ b/config/server_config.go @@ -1,8 +1,6 @@ package config -import "github.com/kuskoman/logstash-exporter/helpers" - var ( - Port = helpers.GetEnvWithDefault("PORT", "9198") - Host = helpers.GetEnvWithDefault("HOST", "") + Port = getEnvWithDefault("PORT", "9198") + Host = getEnvWithDefault("HOST", "") ) diff --git a/helpers/env_helper.go b/helpers/env_helper.go deleted file mode 100644 index c639d356..00000000 --- a/helpers/env_helper.go +++ /dev/null @@ -1,32 +0,0 @@ -package helpers - -import ( - "errors" - "os" - - "github.com/joho/godotenv" -) - -func InitializeEnv() error { - return godotenv.Load() -} - -func GetEnv(key string) string { - return os.Getenv(key) -} - -func GetRequiredEnv(key string) (string, error) { - value := GetEnv(key) - if value == "" { - return "", errors.New("required environment variable " + key + " is not set") - } - return value, nil -} - -func GetEnvWithDefault(key string, defaultValue string) string { - value := GetEnv(key) - if value == "" { - return defaultValue - } - return value -} diff --git a/helpers/env_helper_test.go b/helpers/env_helper_test.go deleted file mode 100644 index f82f7dd3..00000000 --- a/helpers/env_helper_test.go +++ /dev/null @@ -1,69 +0,0 @@ -package helpers - -import ( - "os" - "testing" -) - -func TestGetEnv(t *testing.T) { - t.Run("should return value for set environment variable", func(t *testing.T) { - expected := "value" - key := "TEST" - os.Setenv(key, expected) - actual := GetEnv(key) - if actual != expected { - t.Errorf("expected %s but got %s", expected, actual) - } - }) - - t.Run("should return empty string for unset environment variable", func(t *testing.T) { - expected := "" - actual := GetEnv("TEST_UNSET") - if actual != expected { - t.Errorf("expected %s but got %s", expected, actual) - } - }) -} - -func TestGetRequiredEnv(t *testing.T) { - t.Run("should return value for set environment variable", func(t *testing.T) { - expected := "value" - key := "TEST2" - os.Setenv(key, expected) - actual, err := GetRequiredEnv(key) - if err != nil { - t.Errorf("expected no error but got %s", err) - } - if actual != expected { - t.Errorf("expected %s but got %s", expected, actual) - } - }) - - t.Run("should return error for unset environment variable", func(t *testing.T) { - key := "TEST_UNSET2" - _, err := GetRequiredEnv(key) - if err == nil { - t.Errorf("expected error but got none") - } - }) -} - -func TestGetEnvWithDefault(t *testing.T) { - t.Run("should return value for set environment variable", func(t *testing.T) { - key := "TEST3" - expected := "value" - os.Setenv(key, expected) - actual := GetEnvWithDefault(key, "default") - if actual != expected { - t.Errorf("expected %s but got %s", expected, actual) - } - }) - - t.Run("should return default value for unset environment variable", func(t *testing.T) { - expected := "default" - actual := GetEnvWithDefault("TEST_UNSET3", expected) - if actual != expected { - t.Errorf("expected %s but got %s", expected, actual) - } - }) -} diff --git a/helpers/prometheus_helper.go b/prometheus_helper/prometheus_helper.go similarity index 97% rename from helpers/prometheus_helper.go rename to prometheus_helper/prometheus_helper.go index 174ebb4e..720322e6 100644 --- a/helpers/prometheus_helper.go +++ b/prometheus_helper/prometheus_helper.go @@ -1,4 +1,4 @@ -package helpers +package prometheus_helper import ( "errors" diff --git a/helpers/prometheus_helper_test.go b/prometheus_helper/prometheus_helper_test.go similarity index 98% rename from helpers/prometheus_helper_test.go rename to prometheus_helper/prometheus_helper_test.go index 6a5c038d..f9312cce 100644 --- a/helpers/prometheus_helper_test.go +++ b/prometheus_helper/prometheus_helper_test.go @@ -1,4 +1,4 @@ -package helpers +package prometheus_helper import ( "fmt"