-
Notifications
You must be signed in to change notification settings - Fork 21
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
+73
−7
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -23,15 +30,43 @@ 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) { | ||
r, err := regexp.Compile(regx) | ||
if err != nil { | ||
log.Printf("Error creating custom tags: %v\n", err) | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?