diff --git a/pkg/network/events/monitor.go b/pkg/network/events/monitor.go index 5e2bc913ccadbf..39c3f974d51fb6 100644 --- a/pkg/network/events/monitor.go +++ b/pkg/network/events/monitor.go @@ -23,9 +23,21 @@ import ( "github.com/DataDog/datadog-agent/pkg/util/log" ) -var theMonitor atomic.Value -var once sync.Once -var initErr error +var ( + theMonitor atomic.Value + once sync.Once + initErr error + envFilter = map[string]bool{ + "DD_SERVICE": true, + "DD_VERSION": true, + "DD_ENV": true, + } + envTagNames = map[string]string{ + "DD_SERVICE": "service", + "DD_VERSION": "version", + "DD_ENV": "env", + } +) // Process is a process type Process struct { @@ -107,14 +119,14 @@ func (h *eventHandlerWrapper) Copy(ev *model.Event) any { StartTime: processStartTime.UnixNano(), } - envs := model.FilterEnvs(ev.GetProcessEnvp(), map[string]bool{ - "DD_SERVICE": true, - "DD_VERSION": true, - "DD_ENV": true, - }) - + envs := model.FilterEnvs(ev.GetProcessEnvp(), envFilter) for _, env := range envs { - p.Tags = append(p.Tags, intern.GetByString(strings.Replace(env, "=", ":", 1))) + k, v, _ := strings.Cut(env, "=") + if len(v) > 0 { + if t := envTagNames[k]; t != "" { + p.Tags = append(p.Tags, intern.GetByString(k+":"+v)) + } + } } if cid := ev.GetContainerId(); cid != "" { diff --git a/pkg/network/tracer/process_cache.go b/pkg/network/tracer/process_cache.go index 68ab3b9a9bf906..89d71145c83e6c 100644 --- a/pkg/network/tracer/process_cache.go +++ b/pkg/network/tracer/process_cache.go @@ -122,7 +122,7 @@ func (pc *processCache) HandleProcessEvent(entry *events.Process) { } func (pc *processCache) processEvent(entry *events.Process) *events.Process { - if len(entry.Envs) == 0 && entry.ContainerID == nil { + if len(entry.Tags) == 0 && entry.ContainerID == nil { return nil } diff --git a/pkg/network/tracer/process_cache_test.go b/pkg/network/tracer/process_cache_test.go index 08b39161d98d17..94b02d95d5feeb 100644 --- a/pkg/network/tracer/process_cache_test.go +++ b/pkg/network/tracer/process_cache_test.go @@ -25,7 +25,7 @@ func TestProcessCacheProcessEvent(t *testing.T) { t.Cleanup(pc.Stop) p := pc.processEvent(entry) - if entry.ContainerID == nil && len(entry.Envs) == 0 { + if entry.ContainerID == nil && len(entry.Tags) == 0 { assert.Nil(t, p) } else { assert.Equal(t, entry, p) @@ -47,17 +47,17 @@ func TestProcessCacheProcessEvent(t *testing.T) { testFunc(t, t.Name(), &entry) }) - t.Run("without container id, with envs", func(t *testing.T) { - entry := events.Process{Pid: 1234, Envs: []string{"foo", "bar"}} + t.Run("without container id, with tags", func(t *testing.T) { + entry := events.Process{Pid: 1234, Tags: []*intern.Value{intern.GetByString("foo"), intern.GetByString("bar")}} testFunc(t, t.Name(), &entry) }) - t.Run("with container id, with envs", func(t *testing.T) { + t.Run("with container id, with tags", func(t *testing.T) { entry := events.Process{ Pid: 1234, ContainerID: intern.GetByString("container"), - Envs: []string{"foo", "bar"}, + Tags: []*intern.Value{intern.GetByString("foo"), intern.GetByString("bar")}, } testFunc(t, t.Name(), &entry) @@ -196,7 +196,7 @@ func TestProcessCacheAdd(t *testing.T) { pc.add(&events.Process{ Pid: 1234, StartTime: 1, - Envs: []string{"foo=bar"}, + Tags: []*intern.Value{intern.GetByString("foo:bar")}, }) p, ok := pc.Get(1234, 1) @@ -204,12 +204,12 @@ func TestProcessCacheAdd(t *testing.T) { require.NotNil(t, p) assert.Equal(t, uint32(1234), p.Pid) assert.Equal(t, int64(1), p.StartTime) - assert.Equal(t, p.Env("foo"), "bar") + assert.Contains(t, p.Tags, intern.GetByString("foo:bar")) pc.add(&events.Process{ Pid: 1234, StartTime: 1, - Envs: []string{"bar=foo"}, + Tags: []*intern.Value{intern.GetByString("bar:foo")}, }) p, ok = pc.Get(1234, 1) @@ -217,8 +217,8 @@ func TestProcessCacheAdd(t *testing.T) { require.NotNil(t, p) assert.Equal(t, uint32(1234), p.Pid) assert.Equal(t, int64(1), p.StartTime) - assert.Equal(t, p.Env("bar"), "foo") - assert.NotContains(t, p.Envs, "foo") + assert.Contains(t, p.Tags, intern.GetByString("bar:foo")) + assert.NotContains(t, p.Tags, intern.GetByString("foo:bar")) }) } diff --git a/pkg/network/tracer/tracer_linux_test.go b/pkg/network/tracer/tracer_linux_test.go index 7687b651c15211..015c5644f43ca7 100644 --- a/pkg/network/tracer/tracer_linux_test.go +++ b/pkg/network/tracer/tracer_linux_test.go @@ -2139,10 +2139,10 @@ func BenchmarkAddProcessInfo(b *testing.B) { c.Pid = 1 tr.processCache.add(&events.Process{ Pid: 1, - Envs: []string{ - "DD_ENV=env", - "DD_VERSION=version", - "DD_SERVICE=service", + Tags: []*intern.Value{ + intern.GetByString("env:env"), + intern.GetByString("version:version"), + intern.GetByString("service:service"), }, ContainerID: intern.GetByString("container"), StartTime: time.Now().Unix(),