Skip to content

Commit

Permalink
[receiver/hostmetrics]: add process.start resource attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
andrzej-stencel committed Oct 24, 2022

Verified

This commit was signed with the committer’s verified signature. The key has expired.
andrzej-stencel Andrzej Stencel
1 parent 66dd4e5 commit c8aab87
Showing 7 changed files with 40 additions and 4 deletions.
16 changes: 16 additions & 0 deletions .chloggen/add-process-start-attribute.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: hostmetricsreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add a new resource attribute `process.start` to metrics created by the `process` scraper

# One or more tracking issues related to the change
issues: [14084]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
Original file line number Diff line number Diff line change
@@ -35,6 +35,7 @@ metrics:
| process.owner | The username of the user that owns the process. | Str |
| process.parent_pid | Parent Process identifier (PPID). | Int |
| process.pid | Process identifier (PID). | Int |
| process.start | The time the process started, e.g. '2016-05-23T08:05:34.853Z'. | Str |
## Metric attributes

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -37,6 +37,9 @@ resource_attributes:
process.owner:
description: The username of the user that owns the process.
type: string
process.start:
description: The time the process started, e.g. '2016-05-23T08:05:34.853Z'.
type: string

attributes:
direction:
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@ package processscraper // import "github.com/open-telemetry/opentelemetry-collec

import (
"strings"
"time"

"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/process"
@@ -55,6 +56,7 @@ func (m *processMetadata) resourceOptions() []metadata.ResourceMetricsOption {
metadata.WithProcessParentPid(int64(m.parentPid)),
metadata.WithProcessExecutableName(m.executable.name),
metadata.WithProcessExecutablePath(m.executable.path),
metadata.WithProcessStart(time.UnixMilli(m.createTime).UTC().Format("2006-01-02T15:04:05.999Z")),
)
if m.command != nil {
opts = append(opts, metadata.WithProcessCommand(m.command.command))
Original file line number Diff line number Diff line change
@@ -66,8 +66,8 @@ func TestScrape(t *testing.T) {
},
}

const createTime = 100
const expectedStartTime = 100 * 1e6
const createTime = 1664198907123 // 2022-09-26T13:28:27.123Z
const expectedStartTime = 1664198907123 * 1e6

for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
@@ -105,7 +105,7 @@ func TestScrape(t *testing.T) {
}

require.Greater(t, md.ResourceMetrics().Len(), 1)
assertProcessResourceAttributesExist(t, md.ResourceMetrics())
assertProcessResourceAttributesValid(t, md.ResourceMetrics())
assertCPUTimeMetricValid(t, md.ResourceMetrics(), expectedStartTime)
assertMemoryUsageMetricValid(t, md.ResourceMetrics(), expectedStartTime)
assertOldDiskIOMetricValid(t, md.ResourceMetrics(), expectedStartTime)
@@ -122,7 +122,7 @@ func TestScrape(t *testing.T) {
}
}

func assertProcessResourceAttributesExist(t *testing.T, resourceMetrics pmetric.ResourceMetricsSlice) {
func assertProcessResourceAttributesValid(t *testing.T, resourceMetrics pmetric.ResourceMetricsSlice) {
for i := 0; i < resourceMetrics.Len(); i++ {
attr := resourceMetrics.At(0).Resource().Attributes()
internal.AssertContainsAttribute(t, attr, conventions.AttributeProcessPID)
@@ -132,6 +132,7 @@ func assertProcessResourceAttributesExist(t *testing.T, resourceMetrics pmetric.
internal.AssertContainsAttribute(t, attr, conventions.AttributeProcessCommandLine)
internal.AssertContainsAttribute(t, attr, conventions.AttributeProcessOwner)
internal.AssertContainsAttribute(t, attr, "process.parent_pid")
internal.AssertStringAttributeValue(t, attr, "process.start", "2022-09-26T13:28:27.123Z")
}
}

6 changes: 6 additions & 0 deletions receiver/hostmetricsreceiver/internal/testutils.go
Original file line number Diff line number Diff line change
@@ -28,6 +28,12 @@ func AssertContainsAttribute(t *testing.T, attr pcommon.Map, key string) {
assert.True(t, ok)
}

func AssertStringAttributeValue(t *testing.T, attr pcommon.Map, key string, value string) {
attribute, ok := attr.Get(key)
assert.True(t, ok)
assert.Equal(t, value, attribute.Str())
}

func AssertDescriptorEqual(t *testing.T, expected pmetric.Metric, actual pmetric.Metric) {
assert.Equal(t, expected.Name(), actual.Name())
assert.Equal(t, expected.Description(), actual.Description())

0 comments on commit c8aab87

Please sign in to comment.