Skip to content

Commit

Permalink
Split helpers package into prometheus_helper and config functions
Browse files Browse the repository at this point in the history
  • Loading branch information
kuskoman committed Mar 15, 2023
1 parent c503f6e commit c6ff1cc
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 129 deletions.
3 changes: 1 addition & 2 deletions cmd/exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
10 changes: 5 additions & 5 deletions collectors/nodeinfo/nodeinfo_collector.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions collectors/nodeinfo/nodeinfo_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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)
}
Expand Down
7 changes: 4 additions & 3 deletions collectors/nodestats/nodestats_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions collectors/nodestats/nodestats_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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)
}
Expand Down
7 changes: 4 additions & 3 deletions collectors/nodestats/pipeline_subcollector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"),
Expand Down
19 changes: 19 additions & 0 deletions config/env_helper.go
Original file line number Diff line number Diff line change
@@ -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
}
26 changes: 26 additions & 0 deletions config/env_helper_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
4 changes: 1 addition & 3 deletions config/logstash_config.go
Original file line number Diff line number Diff line change
@@ -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")
)
6 changes: 2 additions & 4 deletions config/server_config.go
Original file line number Diff line number Diff line change
@@ -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", "")
)
32 changes: 0 additions & 32 deletions helpers/env_helper.go

This file was deleted.

69 changes: 0 additions & 69 deletions helpers/env_helper_test.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package helpers
package prometheus_helper

import (
"errors"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package helpers
package prometheus_helper

import (
"fmt"
Expand Down

0 comments on commit c6ff1cc

Please sign in to comment.