Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance the wavefront-sdk-go to support Kubernetes environments #47

Merged
merged 3 commits into from
May 26, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 43 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 (
"log"
"os"
"regexp"
"strings"
)

// Tags Encapsulates application details
type Tags struct {
Application string
Service string
Expand All @@ -23,15 +30,44 @@ func New(application, service string) Tags {
}

// Map with all values
func (a Tags) Map() map[string]string {
func (app *Tags) Map() map[string]string {
os.Getenv("pp")
laullon marked this conversation as resolved.
Show resolved Hide resolved
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) {
r, err := regexp.Compile(regx)
if err != nil {
log.Printf("Error creating custom tags: %v\n", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return an error here rather than just log it?

return
}

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
}
}
}
}

// AddCustomTagFromEnv Set a custom tag from the given environment variable.
func (app *Tags) AddCustomTagFromEnv(varName, tag string) {
v := os.Getenv(varName)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe return an error if the env var doesn't exist?

if len(v) > 0 {
app.CustomTags[tag] = v
}
}
25 changes: 25 additions & 0 deletions application/application_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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"])
}