Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[receiver/hostmetrics] Add process.start resource attribute #14479

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Expand Up @@ -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

Expand Down

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
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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")
}
}

Expand Down
6 changes: 6 additions & 0 deletions receiver/hostmetricsreceiver/internal/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down