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

feat: Add config option to set additional attributes on all events #259

Merged
merged 7 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
4 changes: 4 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ type Config struct {

// The name of the pod the agent is running on.
AgentPodName string

// Additional attributes to add to all events.
AgentAttributes map[string]string
}

// NewConfig returns a new Config struct.
Expand Down Expand Up @@ -135,6 +138,7 @@ func NewConfig() Config {
AgentServiceAccount: utils.LookupEnvOrString("AGENT_SERVICE_ACCOUNT_NAME", ""),
AgentPodIP: utils.LookupEnvOrString("AGENT_POD_IP", ""),
AgentPodName: utils.LookupEnvOrString("AGENT_POD_NAME", ""),
AgentAttributes: utils.LookupEnvAsStringMap("AGENT_ATTRIBUTES"),
}
}

Expand Down
2 changes: 2 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func TestEnvVars(t *testing.T) {
t.Setenv("AGENT_SERVICE_ACCOUNT_NAME", "service_account_name")
t.Setenv("AGENT_POD_IP", "pod_ip")
t.Setenv("AGENT_POD_NAME", "pod_name")
t.Setenv("AGENT_ATTRIBUTES", "key1=value1,key2=value2")

config := config.NewConfig()
assert.Equal(t, "1234567890123456789012", config.APIKey)
Expand All @@ -74,4 +75,5 @@ func TestEnvVars(t *testing.T) {
assert.Equal(t, "service_account_name", config.AgentServiceAccount)
assert.Equal(t, "pod_ip", config.AgentPodIP)
assert.Equal(t, "pod_name", config.AgentPodName)
assert.Equal(t, map[string]string{"key1": "value1", "key2": "value2"}, config.AgentAttributes)
}
3 changes: 3 additions & 0 deletions handlers/libhoney_event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ func initLibhoney(config config.Config, version string) func() {
if config.AgentPodName != "" {
libhoney.AddField("meta.agent.pod.name", config.AgentPodName)
}
for k, v := range config.AgentAttributes {
libhoney.AddField(k, v)
}

return libhoney.Close
}
Expand Down
3 changes: 3 additions & 0 deletions smoke-tests/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ spec:
## uncomment this to enable profiling
# - name: DEBUG
# value: "$DEBUG"
## uncomment this to add extra attributes to all events
# - name: AGENT_ATTRIBUTES
# value: "key1=value1,key2=value2"
securityContext:
capabilities:
add:
Expand Down
17 changes: 17 additions & 0 deletions utils/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"os"
"strconv"
"strings"
)

// LookupEnvOrBool returns a bool parsed from the environment variable with the given key
Expand All @@ -24,3 +25,19 @@ func LookupEnvOrString(key string, def string) string {
}
return def
}

// LookupEnvAsStringMap returns a map of strings from the environment variable with the given key
// attributes are comma separated, key/value pairs are separated by an equals sign
// Example: key1=value1,key2=value2
func LookupEnvAsStringMap(key string) map[string]string {
values := make(map[string]string)
if env := os.Getenv(key); env != "" {
for _, value := range strings.Split(env, ",") {
parts := strings.Split(value, "=")
if len(parts) == 2 {
values[parts[0]] = parts[1]
}
}
}
return values
}