Add telegraf url env var (#8987)

This commit is contained in:
Alexey Kuzyashin 2021-06-03 06:22:15 +03:00 committed by GitHub
parent aa427ed812
commit 58a90783f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 0 deletions

View File

@ -712,6 +712,10 @@ func getDefaultConfigPath() (string, error) {
etcfile = programFiles + `\Telegraf\telegraf.conf` etcfile = programFiles + `\Telegraf\telegraf.conf`
} }
for _, path := range []string{envfile, homefile, etcfile} { 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 { if _, err := os.Stat(path); err == nil {
log.Printf("I! Using config file: %s", path) log.Printf("I! Using config file: %s", path)
return path, nil return path, nil
@ -723,6 +727,12 @@ func getDefaultConfigPath() (string, error) {
" in $TELEGRAF_CONFIG_PATH, %s, or %s", homefile, etcfile) " 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 // LoadConfig loads the given config file and applies it to c
func (c *Config) LoadConfig(path string) error { func (c *Config) LoadConfig(path string) error {
var err error var err error

View File

@ -324,6 +324,22 @@ func TestConfig_URLRetries3FailsThenPasses(t *testing.T) {
require.Equal(t, 4, responseCounter) 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) { func TestConfig_URLLikeFileName(t *testing.T) {
c := NewConfig() c := NewConfig()
err := c.LoadConfig("http:##www.example.com.conf") err := c.LoadConfig("http:##www.example.com.conf")