Skip to content

Commit

Permalink
Use pointer for processRootevent (#153)
Browse files Browse the repository at this point in the history
## What does this PR do?

This fixes a small bug introduced in #150, where we weren't setting the
`ProcState` object while creating the root event.

This is needed for elastic/beats#39620

## Why is it important?

The previous PR adds fields that weren't in events before, this fixes
that.

## Checklist

- [x] My code follows the style guidelines of this project
- [ ] I have commented my code, particularly in hard-to-understand areas
- [x] I have added tests that prove my fix is effective or that my
feature works
- [ ] I have added an entry in `CHANGELOG.md`
  • Loading branch information
fearful-symmetry authored May 17, 2024
1 parent 8889faa commit e221708
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
4 changes: 2 additions & 2 deletions metric/system/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (procStats *Stats) Get() ([]mapstr.M, []mapstr.M, error) {
// Add the RSS pct memory first
process.Memory.Rss.Pct = GetProcMemPercentage(process, totalPhyMem)
// Create the root event
rootMap := processRootEvent(process)
rootMap := processRootEvent(&process)

proc, err := procStats.getProcessEvent(&process)
if err != nil {
Expand Down Expand Up @@ -163,7 +163,7 @@ func (procStats *Stats) GetOneRootEvent(pid int) (mapstr.M, mapstr.M, error) {
return nil, nil, fmt.Errorf("error formatting process %d: %w", pid, err)
}

rootMap := processRootEvent(pidStat)
rootMap := processRootEvent(&pidStat)

return procMap, rootMap, err
}
Expand Down
2 changes: 1 addition & 1 deletion metric/system/process/process_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func (procStats *Stats) Init() error {
}

// processRootEvent formats the process state event for the ECS root fields used by the system/process metricsets
func processRootEvent(process ProcState) mapstr.M {
func processRootEvent(process *ProcState) mapstr.M {
// Create the root event
root := process.FormatForRoot()
rootMap := mapstr.M{}
Expand Down
16 changes: 16 additions & 0 deletions metric/system/process/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ import (
"github.com/elastic/elastic-agent-system-metrics/metric/system/resolve"
)

func TestProcessEvent(t *testing.T) {
proc := ProcState{Args: []string{"-b", "-c"},
Name: "test",
Username: "user",
Memory: ProcMemInfo{Rss: MemBytePct{Pct: opt.FloatWith(4.5)}},
}

root := processRootEvent(&proc)

require.Empty(t, proc.Name)
require.Empty(t, proc.Username)
require.Empty(t, proc.Args)

require.NotNil(t, root["process"].(map[string]interface{})["memory"])
}

// BenchmarkGetProcess runs a benchmark of the GetProcess method with caching
// of the command line and environment variables.
func BenchmarkGetProcess(b *testing.B) {
Expand Down

0 comments on commit e221708

Please sign in to comment.