Skip to content

Commit

Permalink
hostmetrics: Adding process.uptime metric (open-telemetry#37492)
Browse files Browse the repository at this point in the history
#### Description
Adding new metric into hostmetric process scraper to track process
uptime

#### Link to tracking issue
Fixes
open-telemetry#36667

#### Testing

Added tests to validate coverage.

#### Documentation

Added as part of mdatagen
  • Loading branch information
MovieStoreGuy authored and chengchuanpeng committed Feb 8, 2025
1 parent 183008a commit 96609c8
Show file tree
Hide file tree
Showing 16 changed files with 196 additions and 10 deletions.
27 changes: 27 additions & 0 deletions .chloggen/msg_process-uptime-addition.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# 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: hostmetrics/process

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Added support for tracking process.uptime

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [36667]

# (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:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
2 changes: 2 additions & 0 deletions connector/datadogconnector/go.sum

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

1 change: 1 addition & 0 deletions exporter/datadogexporter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/stormcat24/protodep v0.1.8 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/tilinna/clock v1.1.0 // indirect
github.com/tinylib/msgp v1.2.5 // indirect
github.com/tklauser/go-sysconf v0.3.14 // indirect
github.com/tklauser/numcpus v0.8.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions exporter/datadogexporter/go.sum

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

2 changes: 2 additions & 0 deletions exporter/datadogexporter/integrationtest/go.sum

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

1 change: 1 addition & 0 deletions receiver/hostmetricsreceiver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/prometheus/procfs v0.15.1
github.com/shirou/gopsutil/v4 v4.24.12
github.com/stretchr/testify v1.10.0
github.com/tilinna/clock v1.1.0
github.com/yusufpapurcu/wmi v1.2.4
go.opentelemetry.io/collector/component v0.118.1-0.20250123125445-24f88da7b583
go.opentelemetry.io/collector/component/componenttest v0.118.1-0.20250123125445-24f88da7b583
Expand Down
2 changes: 2 additions & 0 deletions receiver/hostmetricsreceiver/go.sum

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 @@ -172,6 +172,14 @@ Process threads count.
| ---- | ----------- | ---------- | ----------------------- | --------- |
| {threads} | Sum | Int | Cumulative | false |
### process.uptime
The time the process has been running.
| Unit | Metric Type | Value Type |
| ---- | ----------- | ---------- |
| s | Gauge | Double |
## Resource Attributes
| Name | Description | Values | Enabled |
Expand Down

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

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

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

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 @@ -27,6 +27,8 @@ all_set:
enabled: true
process.threads:
enabled: true
process.uptime:
enabled: true
resource_attributes:
process.cgroup:
enabled: true
Expand Down Expand Up @@ -72,6 +74,8 @@ none_set:
enabled: false
process.threads:
enabled: false
process.uptime:
enabled: false
resource_attributes:
process.cgroup:
enabled: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,10 @@ metrics:
aggregation_temporality: cumulative
monotonic: true
attributes: [direction]

process.uptime:
enabled: false
description: The time the process has been running.
unit: s
gauge:
value_type: double
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/shirou/gopsutil/v4/cpu"
"github.com/shirou/gopsutil/v4/host"
"github.com/shirou/gopsutil/v4/process"
"github.com/tilinna/clock"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
Expand All @@ -36,8 +37,9 @@ const (
fileDescriptorMetricsLen = 1
handleMetricsLen = 1
signalMetricsLen = 1
uptimeMetricsLen = 1

metricsLen = cpuMetricsLen + memoryMetricsLen + diskMetricsLen + memoryUtilizationMetricsLen + pagingMetricsLen + threadMetricsLen + contextSwitchMetricsLen + fileDescriptorMetricsLen + signalMetricsLen
metricsLen = cpuMetricsLen + memoryMetricsLen + diskMetricsLen + memoryUtilizationMetricsLen + pagingMetricsLen + threadMetricsLen + contextSwitchMetricsLen + fileDescriptorMetricsLen + signalMetricsLen + uptimeMetricsLen
)

// scraper for Process Metrics
Expand Down Expand Up @@ -132,7 +134,7 @@ func (s *processScraper) scrape(ctx context.Context) (pmetric.Metrics, error) {
for _, md := range data {
presentPIDs[md.pid] = struct{}{}

now := pcommon.NewTimestampFromTime(time.Now())
now := pcommon.NewTimestampFromTime(clock.Now(ctx))

if err = s.scrapeAndAppendCPUTimeMetric(ctx, now, md.handle, md.pid); err != nil {
errs.AddPartial(cpuMetricsLen, fmt.Errorf("error reading cpu times for process %q (pid %v): %w", md.executable.name, md.pid, err))
Expand Down Expand Up @@ -174,6 +176,10 @@ func (s *processScraper) scrape(ctx context.Context) (pmetric.Metrics, error) {
errs.AddPartial(signalMetricsLen, fmt.Errorf("error reading pending signals for process %q (pid %v): %w", md.executable.name, md.pid, err))
}

if err = s.scrapeAndAppendUptimeMetric(ctx, now, md.handle); err != nil {
errs.AddPartial(uptimeMetricsLen, fmt.Errorf("error calculating uptime for process %q (pid %v): %w", md.executable.name, md.pid, err))
}

s.mb.EmitForResource(metadata.WithResource(md.buildResource(s.mb.NewResourceBuilder())),
metadata.WithStartTimeOverride(pcommon.Timestamp(md.createTime*1e6)))
}
Expand Down Expand Up @@ -465,3 +471,20 @@ func (s *processScraper) scrapeAndAppendSignalsPendingMetric(ctx context.Context

return nil
}

func (s *processScraper) scrapeAndAppendUptimeMetric(ctx context.Context, now pcommon.Timestamp, handle processHandle) error {
if !s.config.MetricsBuilderConfig.Metrics.ProcessUptime.Enabled {
return nil
}

ts, err := s.getProcessCreateTime(handle, ctx)
if err != nil {
return err
}
// Since create time is in milliseconds, it needs to be multiplied
// by the constant value so that it can be used as part of the time.Unix function.
uptime := now.AsTime().Sub(time.Unix(0, ts*int64(time.Millisecond)))
s.mb.RecordProcessUptimeDataPoint(now, uptime.Seconds())

return nil
}
Loading

0 comments on commit 96609c8

Please sign in to comment.