From 58a90783f57329dfb87ef53ec016fe06c3415ab0 Mon Sep 17 00:00:00 2001 From: Alexey Kuzyashin <33540273+Kuzyashin@users.noreply.github.com> Date: Thu, 3 Jun 2021 06:22:15 +0300 Subject: [PATCH] Add telegraf url env var (#8987) --- config/config.go | 10 ++++++++++ config/config_test.go | 16 ++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/config/config.go b/config/config.go index 0391a3c1a..88d6eedce 100644 --- a/config/config.go +++ b/config/config.go @@ -712,6 +712,10 @@ func getDefaultConfigPath() (string, error) { etcfile = programFiles + `\Telegraf\telegraf.conf` } for _, path := range []string{envfile, homefile, etcfile} { + if isURL(path) { + log.Printf("I! Using config url: %s", path) + return path, nil + } if _, err := os.Stat(path); err == nil { log.Printf("I! Using config file: %s", path) return path, nil @@ -723,6 +727,12 @@ func getDefaultConfigPath() (string, error) { " in $TELEGRAF_CONFIG_PATH, %s, or %s", homefile, etcfile) } +// isURL checks if string is valid url +func isURL(str string) bool { + u, err := url.Parse(str) + return err == nil && u.Scheme != "" && u.Host != "" +} + // LoadConfig loads the given config file and applies it to c func (c *Config) LoadConfig(path string) error { var err error diff --git a/config/config_test.go b/config/config_test.go index 91d0a81e8..940b84ada 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -324,6 +324,22 @@ func TestConfig_URLRetries3FailsThenPasses(t *testing.T) { require.Equal(t, 4, responseCounter) } +func TestConfig_getDefaultConfigPathFromEnvURL(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + })) + defer ts.Close() + + c := NewConfig() + err := os.Setenv("TELEGRAF_CONFIG_PATH", ts.URL) + require.NoError(t, err) + configPath, err := getDefaultConfigPath() + require.NoError(t, err) + require.Equal(t, ts.URL, configPath) + err = c.LoadConfig("") + require.NoError(t, err) +} + func TestConfig_URLLikeFileName(t *testing.T) { c := NewConfig() err := c.LoadConfig("http:##www.example.com.conf")