Skip to content

Commit e7ae19d

Browse files
authored
[hostmetricsreceiver] add system uptime metric (#35954)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description Add new uptime scraper to emit the `system.uptime` metric <!-- Issue number (e.g. #1234) or full URL to issue, if applicable. --> #### Link to tracking issue Fixes #31627 <!--Describe what testing was performed and which tests were added.--> #### Testing Unit test added. Also tested locally (Linux VM) with following config: ```yaml receivers: hostmetrics: scrapers: uptime: exporters: debug: verbosity: detailed service: pipelines: metrics: receivers: - hostmetrics exporters: - debug ``` <details><summary>Details</summary> <p> ```console $ make genotelcontribcol [...] $ make RUN_CONFIG=config-uptime.yaml run 2024-10-23T13:24:48.195+0200 info [email protected]/service.go:135 Setting up own telemetry... 2024-10-23T13:24:48.195+0200 info telemetry/metrics.go:70 Serving metrics {"address": "localhost:8888", "metrics level": "Normal"} 2024-10-23T13:24:48.195+0200 info builders/builders.go:26 Development component. May change in the future. {"kind": "exporter", "data_type": "metrics", "name": "debug"} 2024-10-23T13:24:48.199+0200 info [email protected]/service.go:207 Starting otelcontribcol... {"Version": "0.112.0-dev", "NumCPU": 7} 2024-10-23T13:24:48.199+0200 info extensions/extensions.go:39 Starting extensions... 2024-10-23T13:24:48.199+0200 info [email protected]/service.go:230 Everything is ready. Begin running and processing data. 2024-10-23T13:24:49.202+0200 info Metrics {"kind": "exporter", "data_type": "metrics", "name": "debug", "resource metrics": 1, "metrics": 1, "data points": 1} 2024-10-23T13:24:49.202+0200 info ResourceMetrics #0 Resource SchemaURL: https://opentelemetry.io/schemas/1.9.0 ScopeMetrics #0 ScopeMetrics SchemaURL: InstrumentationScope github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/uptimescraper 0.112.0-dev Metric #0 Descriptor: -> Name: system.uptime -> Description: The time the system has been running -> Unit: s -> DataType: Sum -> IsMonotonic: false -> AggregationTemporality: Cumulative NumberDataPoints #0 StartTimestamp: 2024-10-22 18:23:22 +0000 UTC Timestamp: 2024-10-23 11:24:49.202512199 +0000 UTC Value: 61287.000000 {"kind": "exporter", "data_type": "metrics", "name": "debug"} 2024-10-23T13:25:49.206+0200 info Metrics {"kind": "exporter", "data_type": "metrics", "name": "debug", "resource metrics": 1, "metrics": 1, "data points": 1} 2024-10-23T13:25:49.206+0200 info ResourceMetrics #0 Resource SchemaURL: https://opentelemetry.io/schemas/1.9.0 ScopeMetrics #0 ScopeMetrics SchemaURL: InstrumentationScope github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/uptimescraper 0.112.0-dev Metric #0 Descriptor: -> Name: system.uptime -> Description: The time the system has been running -> Unit: s -> DataType: Sum -> IsMonotonic: false -> AggregationTemporality: Cumulative NumberDataPoints #0 StartTimestamp: 2024-10-22 18:23:22 +0000 UTC Timestamp: 2024-10-23 11:25:49.206657828 +0000 UTC Value: 61347.000000 {"kind": "exporter", "data_type": "metrics", "name": "debug"} 2024-10-23T13:26:49.205+0200 info Metrics {"kind": "exporter", "data_type": "metrics", "name": "debug", "resource metrics": 1, "metrics": 1, "data points": 1} 2024-10-23T13:26:49.205+0200 info ResourceMetrics #0 Resource SchemaURL: https://opentelemetry.io/schemas/1.9.0 ScopeMetrics #0 ScopeMetrics SchemaURL: InstrumentationScope github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/uptimescraper 0.112.0-dev Metric #0 Descriptor: -> Name: system.uptime -> Description: The time the system has been running -> Unit: s -> DataType: Sum -> IsMonotonic: false -> AggregationTemporality: Cumulative NumberDataPoints #0 StartTimestamp: 2024-10-22 18:23:22 +0000 UTC Timestamp: 2024-10-23 11:26:49.2051249 +0000 UTC Value: 61407.000000 {"kind": "exporter", "data_type": "metrics", "name": "debug"} 2024-10-23T13:27:49.210+0200 info Metrics {"kind": "exporter", "data_type": "metrics", "name": "debug", "resource metrics": 1, "metrics": 1, "data points": 1} 2024-10-23T13:27:49.210+0200 info ResourceMetrics #0 Resource SchemaURL: https://opentelemetry.io/schemas/1.9.0 ScopeMetrics #0 ScopeMetrics SchemaURL: InstrumentationScope github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/uptimescraper 0.112.0-dev Metric #0 Descriptor: -> Name: system.uptime -> Description: The time the system has been running -> Unit: s -> DataType: Sum -> IsMonotonic: false -> AggregationTemporality: Cumulative NumberDataPoints #0 StartTimestamp: 2024-10-22 18:23:22 +0000 UTC Timestamp: 2024-10-23 11:27:49.21007371 +0000 UTC Value: 61467.000000 {"kind": "exporter", "data_type": "metrics", "name": "debug"} ^C 2024-10-23T13:27:54.204+0200 info [email protected]/collector.go:328 Received signal from OS {"signal": "interrupt"} 2024-10-23T13:27:54.204+0200 info [email protected]/service.go:266 Starting shutdown... 2024-10-23T13:27:54.204+0200 info extensions/extensions.go:66 Stopping extensions... 2024-10-23T13:27:54.204+0200 info [email protected]/service.go:280 Shutdown complete. ``` </p> </details> <!--Describe the documentation added.--> #### Documentation Updated readme & generated scraper docs <!--Please delete paragraphs that you did not use before submitting.-->
1 parent 6323659 commit e7ae19d

20 files changed

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

receiver/hostmetricsreceiver/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ The available scrapers are:
4949
| [paging] | All | Paging/Swap space utilization and I/O metrics |
5050
| [processes] | Linux, Mac | Process count metrics |
5151
| [process] | Linux, Windows, Mac | Per process CPU, Memory, and Disk I/O metrics |
52+
| [uptime] | Linux, Windows, Mac | Uptime metric |
5253

5354
[cpu]: ./internal/scraper/cpuscraper/documentation.md
5455
[disk]: ./internal/scraper/diskscraper/documentation.md
@@ -59,6 +60,7 @@ The available scrapers are:
5960
[paging]: ./internal/scraper/pagingscraper/documentation.md
6061
[processes]: ./internal/scraper/processesscraper/documentation.md
6162
[process]: ./internal/scraper/processscraper/documentation.md
63+
[uptime]: ./internal/scraper/uptimescraper/documentation.md
6264

6365
### Notes
6466

receiver/hostmetricsreceiver/config_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/pagingscraper"
2828
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/processesscraper"
2929
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/processscraper"
30+
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/uptimescraper"
3031
)
3132

3233
func TestLoadConfig(t *testing.T) {
@@ -118,6 +119,11 @@ func TestLoadConfig(t *testing.T) {
118119
cfg.SetEnvMap(common.EnvMap{})
119120
return cfg
120121
})(),
122+
uptimescraper.TypeStr: (func() internal.Config {
123+
cfg := (&uptimescraper.Factory{}).CreateDefaultConfig()
124+
cfg.SetEnvMap(common.EnvMap{})
125+
return cfg
126+
})(),
121127
},
122128
}
123129

receiver/hostmetricsreceiver/example_config.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ receivers:
1414
network:
1515
paging:
1616
processes:
17+
uptime:
1718

1819
exporters:
1920
debug:

receiver/hostmetricsreceiver/factory.go

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/pagingscraper"
2828
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/processesscraper"
2929
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/processscraper"
30+
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/uptimescraper"
3031
)
3132

3233
const (
@@ -45,6 +46,7 @@ var (
4546
pagingscraper.TypeStr: &pagingscraper.Factory{},
4647
processesscraper.TypeStr: &processesscraper.Factory{},
4748
processscraper.TypeStr: &processscraper.Factory{},
49+
uptimescraper.TypeStr: &uptimescraper.Factory{},
4850
}
4951
)
5052

receiver/hostmetricsreceiver/hostmetrics_receiver_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/pagingscraper"
3535
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/processesscraper"
3636
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/processscraper"
37+
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/uptimescraper"
3738
)
3839

3940
var allMetrics = []string{
@@ -81,6 +82,7 @@ var factories = map[string]internal.ScraperFactory{
8182
pagingscraper.TypeStr: &pagingscraper.Factory{},
8283
processesscraper.TypeStr: &processesscraper.Factory{},
8384
processscraper.TypeStr: &processscraper.Factory{},
85+
uptimescraper.TypeStr: &uptimescraper.Factory{},
8486
}
8587

8688
type testEnv struct {
@@ -395,6 +397,19 @@ func Benchmark_ScrapeProcessMetrics(b *testing.B) {
395397
benchmarkScrapeMetrics(b, cfg)
396398
}
397399

400+
func Benchmark_ScrapeUptimeMetrics(b *testing.B) {
401+
if runtime.GOOS != "linux" && runtime.GOOS != "windows" {
402+
b.Skip("skipping test on non linux/windows")
403+
}
404+
405+
cfg := &Config{
406+
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
407+
Scrapers: map[string]internal.Config{uptimescraper.TypeStr: (&uptimescraper.Factory{}).CreateDefaultConfig()},
408+
}
409+
410+
benchmarkScrapeMetrics(b, cfg)
411+
}
412+
398413
func Benchmark_ScrapeSystemMetrics(b *testing.B) {
399414
cfg := &Config{
400415
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package uptimescraper // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/uptimescraper"
5+
6+
import (
7+
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal"
8+
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/uptimescraper/internal/metadata"
9+
)
10+
11+
// Config relating to Uptime Metric Scraper.
12+
type Config struct {
13+
// MetricsBuilderConfig allows to customize scraped metrics/attributes representation.
14+
metadata.MetricsBuilderConfig `mapstructure:",squash"`
15+
internal.ScraperConfig
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
//go:generate mdatagen metadata.yaml
5+
6+
package uptimescraper // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/uptimescraper"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[comment]: <> (Code generated by mdatagen. DO NOT EDIT.)
2+
3+
# hostmetricsreceiver/uptime
4+
5+
**Parent Component:** hostmetrics
6+
7+
## Default Metrics
8+
9+
The following metrics are emitted by default. Each of them can be disabled by applying the following configuration:
10+
11+
```yaml
12+
metrics:
13+
<metric_name>:
14+
enabled: false
15+
```
16+
17+
### system.uptime
18+
19+
The time the system has been running
20+
21+
| Unit | Metric Type | Value Type |
22+
| ---- | ----------- | ---------- |
23+
| s | Gauge | Double |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package uptimescraper // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/uptimescraper"
5+
6+
import (
7+
"context"
8+
"errors"
9+
"runtime"
10+
11+
"go.opentelemetry.io/collector/receiver"
12+
"go.opentelemetry.io/collector/receiver/scraperhelper"
13+
14+
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal"
15+
hostmeta "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/metadata"
16+
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/uptimescraper/internal/metadata"
17+
)
18+
19+
// This file implements Factory for Uptime scraper.
20+
21+
const (
22+
// TypeStr the value of "type" key in configuration.
23+
TypeStr = "uptime"
24+
)
25+
26+
// Factory is the Factory for scraper.
27+
type Factory struct {
28+
}
29+
30+
// CreateDefaultConfig creates the default configuration for the Scraper.
31+
func (f *Factory) CreateDefaultConfig() internal.Config {
32+
return &Config{
33+
MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
34+
}
35+
}
36+
37+
// CreateMetricsScraper creates a resource scraper based on provided config.
38+
func (f *Factory) CreateMetricsScraper(
39+
ctx context.Context,
40+
settings receiver.Settings,
41+
cfg internal.Config,
42+
) (scraperhelper.Scraper, error) {
43+
if runtime.GOOS != "linux" && runtime.GOOS != "windows" && runtime.GOOS != "darwin" {
44+
return nil, errors.New("uptime scraper only available on Linux, Windows, or MacOS")
45+
}
46+
47+
uptimeScraper := newUptimeScraper(ctx, settings, cfg.(*Config))
48+
49+
return scraperhelper.NewScraper(
50+
hostmeta.Type,
51+
uptimeScraper.scrape,
52+
scraperhelper.WithStart(uptimeScraper.start),
53+
)
54+
}

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

+50
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

+59
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)