Skip to content

Commit

Permalink
Add reading http timeout from yaml and test it
Browse files Browse the repository at this point in the history
  • Loading branch information
satk0 committed Jan 16, 2024
1 parent 1dfa258 commit 4cd4020
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions config.example.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
logstash:
servers:
- url: "http://localhost:9600"
timeout: 3s
server:
host: "127.0.0.1"
port: 9200
Expand Down
10 changes: 9 additions & 1 deletion config/exporter_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package config
import (
"log/slog"
"os"

"time"

"gopkg.in/yaml.v2"
)

Expand All @@ -12,6 +13,7 @@ const (
defaultPort = 9198
defaultLogLevel = "info"
defaultLogstashURL = "http://localhost:9600"
defaultHttpTimeout = time.Second * 2
)

var (
Expand All @@ -26,6 +28,7 @@ type LogstashServer struct {
// LogstashConfig holds the configuration for all Logstash servers
type LogstashConfig struct {
Servers []*LogstashServer `yaml:"servers"`
HttpTimeout time.Duration `yaml:"httpTimeout"`
}

// ServerConfig represents the server configuration
Expand Down Expand Up @@ -92,6 +95,11 @@ func mergeWithDefault(config *Config) *Config {
})
}

if config.Logstash.HttpTimeout == 0 {
slog.Debug("using default http timeout", "httpTimeout", defaultHttpTimeout)
config.Logstash.HttpTimeout = defaultHttpTimeout
}

return config
}

Expand Down
11 changes: 11 additions & 0 deletions config/exporter_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"testing"
"time"
)

func TestLoadConfig(t *testing.T) {
Expand Down Expand Up @@ -71,6 +72,9 @@ func TestMergeWithDefault(t *testing.T) {
if mergedConfig.Logstash.Servers[0].Host != defaultLogstashURL {
t.Errorf("expected URL to be %v, got %v", defaultLogstashURL, mergedConfig.Logstash.Servers[0].Host)
}
if mergedConfig.Logstash.HttpTimeout != defaultHttpTimeout{
t.Errorf("expected http timeout to be %v, got %v", defaultHttpTimeout, mergedConfig.Logstash.HttpTimeout)
}
})

t.Run("merge with nil config", func(t *testing.T) {
Expand All @@ -87,6 +91,9 @@ func TestMergeWithDefault(t *testing.T) {
if mergedConfig.Logstash.Servers[0].Host != defaultLogstashURL {
t.Errorf("expected URL to be %v, got %v", defaultLogstashURL, mergedConfig.Logstash.Servers[0].Host)
}
if mergedConfig.Logstash.HttpTimeout != defaultHttpTimeout{
t.Errorf("expected http timeout to be %v, got %v", defaultHttpTimeout, mergedConfig.Logstash.HttpTimeout)
}
})

t.Run("merge with non-empty config", func(t *testing.T) {
Expand All @@ -104,6 +111,7 @@ func TestMergeWithDefault(t *testing.T) {
{Host: "http://localhost:9601"},
{Host: "http://localhost:9602"},
},
HttpTimeout: 3 * time.Second,
},
}

Expand All @@ -124,6 +132,9 @@ func TestMergeWithDefault(t *testing.T) {
if mergedConfig.Logstash.Servers[1].Host != "http://localhost:9602" {
t.Errorf("expected URL to be %v, got %v", "http://localhost:9602", mergedConfig.Logstash.Servers[1].Host)
}
if mergedConfig.Logstash.HttpTimeout != 3 * time.Second{
t.Errorf("expected http timeout to be %v, got %v", 3 * time.Second, mergedConfig.Logstash.HttpTimeout)
}
})
}

Expand Down
1 change: 1 addition & 0 deletions config/http_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ package config

import "time"


// HttpTimeout is the timeout for http requests utilized by multiple contexts
const HttpTimeout = time.Second * 2
1 change: 1 addition & 0 deletions fixtures/valid_config.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
logstash:
servers:
- url: "http://localhost:9601"
httpTimeout: 3s
server:
host: "127.0.0.1"
port: 9200
Expand Down

0 comments on commit 4cd4020

Please sign in to comment.