Skip to content

Commit

Permalink
Enhance the wavefront-sdk-go to support Kubernetes environments (#47)
Browse files Browse the repository at this point in the history
* Enhance the wavefront-sdk-go to support Kubernetes environments

* Update application.go

* review comments
  • Loading branch information
laullon authored May 26, 2020
1 parent 0723997 commit 913ff76
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 7 deletions.
52 changes: 45 additions & 7 deletions application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
28 changes: 28 additions & 0 deletions application/application_test.go
Original file line number Diff line number Diff line change
@@ -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"))
}

0 comments on commit 913ff76

Please sign in to comment.