Skip to content

Commit 96609c8

Browse files
MovieStoreGuychengchuanpeng
authored andcommitted
hostmetrics: Adding process.uptime metric (open-telemetry#37492)
#### 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
1 parent 183008a commit 96609c8

File tree

16 files changed

+196
-10
lines changed

16 files changed

+196
-10
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: hostmetrics/process
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Added support for tracking process.uptime
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [36667]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: [user]

connector/datadogconnector/go.sum

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exporter/datadogexporter/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ require (
334334
github.com/spf13/pflag v1.0.5 // indirect
335335
github.com/stormcat24/protodep v0.1.8 // indirect
336336
github.com/stretchr/objx v0.5.2 // indirect
337+
github.com/tilinna/clock v1.1.0 // indirect
337338
github.com/tinylib/msgp v1.2.5 // indirect
338339
github.com/tklauser/go-sysconf v0.3.14 // indirect
339340
github.com/tklauser/numcpus v0.8.0 // indirect

exporter/datadogexporter/go.sum

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exporter/datadogexporter/integrationtest/go.sum

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

receiver/hostmetricsreceiver/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ require (
1212
github.com/prometheus/procfs v0.15.1
1313
github.com/shirou/gopsutil/v4 v4.24.12
1414
github.com/stretchr/testify v1.10.0
15+
github.com/tilinna/clock v1.1.0
1516
github.com/yusufpapurcu/wmi v1.2.4
1617
go.opentelemetry.io/collector/component v0.118.1-0.20250123125445-24f88da7b583
1718
go.opentelemetry.io/collector/component/componenttest v0.118.1-0.20250123125445-24f88da7b583

receiver/hostmetricsreceiver/go.sum

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

receiver/hostmetricsreceiver/internal/scraper/processscraper/documentation.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,14 @@ Process threads count.
172172
| ---- | ----------- | ---------- | ----------------------- | --------- |
173173
| {threads} | Sum | Int | Cumulative | false |
174174
175+
### process.uptime
176+
177+
The time the process has been running.
178+
179+
| Unit | Metric Type | Value Type |
180+
| ---- | ----------- | ---------- |
181+
| s | Gauge | Double |
182+
175183
## Resource Attributes
176184
177185
| Name | Description | Values | Enabled |

receiver/hostmetricsreceiver/internal/scraper/processscraper/internal/metadata/generated_config.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

receiver/hostmetricsreceiver/internal/scraper/processscraper/internal/metadata/generated_config_test.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

receiver/hostmetricsreceiver/internal/scraper/processscraper/internal/metadata/generated_metrics.go

Lines changed: 57 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

receiver/hostmetricsreceiver/internal/scraper/processscraper/internal/metadata/generated_metrics_test.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

receiver/hostmetricsreceiver/internal/scraper/processscraper/internal/metadata/testdata/config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ all_set:
2727
enabled: true
2828
process.threads:
2929
enabled: true
30+
process.uptime:
31+
enabled: true
3032
resource_attributes:
3133
process.cgroup:
3234
enabled: true
@@ -72,6 +74,8 @@ none_set:
7274
enabled: false
7375
process.threads:
7476
enabled: false
77+
process.uptime:
78+
enabled: false
7579
resource_attributes:
7680
process.cgroup:
7781
enabled: false

receiver/hostmetricsreceiver/internal/scraper/processscraper/metadata.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,10 @@ metrics:
207207
aggregation_temporality: cumulative
208208
monotonic: true
209209
attributes: [direction]
210+
211+
process.uptime:
212+
enabled: false
213+
description: The time the process has been running.
214+
unit: s
215+
gauge:
216+
value_type: double

receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/shirou/gopsutil/v4/cpu"
1414
"github.com/shirou/gopsutil/v4/host"
1515
"github.com/shirou/gopsutil/v4/process"
16+
"github.com/tilinna/clock"
1617
"go.opentelemetry.io/collector/component"
1718
"go.opentelemetry.io/collector/pdata/pcommon"
1819
"go.opentelemetry.io/collector/pdata/pmetric"
@@ -36,8 +37,9 @@ const (
3637
fileDescriptorMetricsLen = 1
3738
handleMetricsLen = 1
3839
signalMetricsLen = 1
40+
uptimeMetricsLen = 1
3941

40-
metricsLen = cpuMetricsLen + memoryMetricsLen + diskMetricsLen + memoryUtilizationMetricsLen + pagingMetricsLen + threadMetricsLen + contextSwitchMetricsLen + fileDescriptorMetricsLen + signalMetricsLen
42+
metricsLen = cpuMetricsLen + memoryMetricsLen + diskMetricsLen + memoryUtilizationMetricsLen + pagingMetricsLen + threadMetricsLen + contextSwitchMetricsLen + fileDescriptorMetricsLen + signalMetricsLen + uptimeMetricsLen
4143
)
4244

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

135-
now := pcommon.NewTimestampFromTime(time.Now())
137+
now := pcommon.NewTimestampFromTime(clock.Now(ctx))
136138

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

179+
if err = s.scrapeAndAppendUptimeMetric(ctx, now, md.handle); err != nil {
180+
errs.AddPartial(uptimeMetricsLen, fmt.Errorf("error calculating uptime for process %q (pid %v): %w", md.executable.name, md.pid, err))
181+
}
182+
177183
s.mb.EmitForResource(metadata.WithResource(md.buildResource(s.mb.NewResourceBuilder())),
178184
metadata.WithStartTimeOverride(pcommon.Timestamp(md.createTime*1e6)))
179185
}
@@ -465,3 +471,20 @@ func (s *processScraper) scrapeAndAppendSignalsPendingMetric(ctx context.Context
465471

466472
return nil
467473
}
474+
475+
func (s *processScraper) scrapeAndAppendUptimeMetric(ctx context.Context, now pcommon.Timestamp, handle processHandle) error {
476+
if !s.config.MetricsBuilderConfig.Metrics.ProcessUptime.Enabled {
477+
return nil
478+
}
479+
480+
ts, err := s.getProcessCreateTime(handle, ctx)
481+
if err != nil {
482+
return err
483+
}
484+
// Since create time is in milliseconds, it needs to be multiplied
485+
// by the constant value so that it can be used as part of the time.Unix function.
486+
uptime := now.AsTime().Sub(time.Unix(0, ts*int64(time.Millisecond)))
487+
s.mb.RecordProcessUptimeDataPoint(now, uptime.Seconds())
488+
489+
return nil
490+
}

0 commit comments

Comments
 (0)