Skip to content

Commit

Permalink
feat: Add config option to set additional attributes on all events (#259
Browse files Browse the repository at this point in the history
)

## Which problem is this PR solving?
When deploying the agent in multiple clusters, telemetry from each
cluster get mixed together and there is no easy way to separate it.
There are other use cases of setting per-cluster values too, eg SHA,
cloud provider, etc.

This PR adds a configuration option to set a collection of key-value
pairs that will be set on all events. For example; cluster-name=dev
would add the field cluster-name with a value of dev.

- Closes #54 

## Short description of the changes
- Add new look up env func that returns a map[string]string created from
a env var that holds a string of comma separated key value pairs
- Adds AgentAttributes to config and populate from
`ADDITIONAL_ATTRIBUTES` env var using new env look up func
- Add the map of attributes to libhoney fields during libhoney init
- Update config tests to verify expected behaviour

## How to verify that this has the expected result
When additional attributes are provided, they are added to all events.
  • Loading branch information
MikeGoldsmith authored Oct 4, 2023
1 parent d455fd5 commit e9e82a9
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 0 deletions.
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.
AdditionalAttributes 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", ""),
AdditionalAttributes: utils.LookupEnvAsStringMap("ADDITIONAL_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("ADDITIONAL_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.AdditionalAttributes)
}
3 changes: 3 additions & 0 deletions handlers/libhoney_event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,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.AdditionalAttributes {
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 @@ -124,6 +124,9 @@ spec:
## uncomment this to enable profiling
# - name: DEBUG
# value: "$DEBUG"
## uncomment this to add extra attributes to all events
# - name: ADDITIONAL_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
}

0 comments on commit e9e82a9

Please sign in to comment.