diff --git a/application/application.go b/application/application.go index ea7c64d..f4318b1 100644 --- a/application/application.go +++ b/application/application.go @@ -2,7 +2,14 @@ // The application details can be reported to Wavefront in the form of tags. package application -// Encapsulates application details +import ( + "fmt" + "os" + "regexp" + "strings" +) + +// Tags Encapsulates application details type Tags struct { Application string Service string @@ -23,15 +30,46 @@ func New(application, service string) Tags { } // Map with all values -func (a Tags) Map() map[string]string { +func (app *Tags) Map() map[string]string { allTags := make(map[string]string) - allTags["application"] = a.Application - allTags["service"] = a.Service - allTags["cluster"] = a.Cluster - allTags["shard"] = a.Shard + allTags["application"] = app.Application + allTags["service"] = app.Service + allTags["cluster"] = app.Cluster + allTags["shard"] = app.Shard - for k, v := range a.CustomTags { + for k, v := range app.CustomTags { allTags[k] = v } return allTags } + +// AddCustomTagsFromEnv set additional custom tags from environment variables that match the given regex. +func (app *Tags) AddCustomTagsFromEnv(regx string) error { + r, err := regexp.Compile(regx) + if err != nil { + return fmt.Errorf("error creating custom tags, %v", err) + } + + env := os.Environ() + for _, envVar := range env { + k := strings.Split(envVar, "=")[0] + if r.Match([]byte(k)) { + v := os.Getenv(k) + if len(v) > 0 { + app.CustomTags[k] = v + } + } + } + return nil +} + +// AddCustomTagFromEnv Set a custom tag from the given environment variable. +func (app *Tags) AddCustomTagFromEnv(varName, tag string) error { + v := os.Getenv(varName) + if len(v) > 0 { + app.CustomTags[tag] = v + } else { + return fmt.Errorf("error creating custom tag, env var '%s' not found", varName) + } + return nil +} diff --git a/application/application_test.go b/application/application_test.go new file mode 100644 index 0000000..8ca0651 --- /dev/null +++ b/application/application_test.go @@ -0,0 +1,28 @@ +package application_test + +import ( + "os" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/wavefronthq/wavefront-sdk-go/application" +) + +func TestAppTagsEnv(t *testing.T) { + os.Setenv("app_label_1", "value_1") + os.Setenv("app_label_2", "value_2") + os.Setenv("label_3", "value_3") + + appTags := application.New("app", "srv") + + appTags.AddCustomTagsFromEnv("app_.*") + appTags.AddCustomTagFromEnv("label_3", "app_3") + + tags := appTags.Map() + assert.Equal(t, "value_1", tags["app_label_1"]) + assert.Empty(t, tags["label_3"]) + assert.Equal(t, "value_3", tags["app_3"]) + + assert.NotNil(t, appTags.AddCustomTagsFromEnv("ap\\p_.*")) + assert.NotNil(t, appTags.AddCustomTagFromEnv("label_x", "app_3")) +}