Skip to content

Commit

Permalink
Fix tag names
Browse files Browse the repository at this point in the history
  • Loading branch information
hmahmood committed Nov 28, 2023
1 parent 2895633 commit 9ca8815
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 25 deletions.
32 changes: 22 additions & 10 deletions pkg/network/events/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 != "" {
Expand Down
2 changes: 1 addition & 1 deletion pkg/network/tracer/process_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
20 changes: 10 additions & 10 deletions pkg/network/tracer/process_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -196,29 +196,29 @@ 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)
assert.True(t, ok)
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)
assert.True(t, ok)
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"))
})
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/network/tracer/tracer_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 9ca8815

Please sign in to comment.