Skip to content

Commit

Permalink
[hostmetricsreceiver] add system uptime metric (open-telemetry#35954)
Browse files Browse the repository at this point in the history
<!--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. open-telemetry#1234) or full URL to issue, if applicable. -->
#### Link to tracking issue
Fixes open-telemetry#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.-->
  • Loading branch information
kevinnoel-be authored and jpbarto committed Oct 29, 2024
1 parent 3095e81 commit 595e438
Show file tree
Hide file tree
Showing 20 changed files with 710 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .chloggen/add-hostmetric-system-uptime.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 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 an uptime scraper in the hostmetrics receiver

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

# (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 receiver/hostmetricsreceiver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ The available scrapers are:
| [paging] | All | Paging/Swap space utilization and I/O metrics |
| [processes] | Linux, Mac | Process count metrics |
| [process] | Linux, Windows, Mac | Per process CPU, Memory, and Disk I/O metrics |
| [uptime] | Linux, Windows, Mac | Uptime metric |

[cpu]: ./internal/scraper/cpuscraper/documentation.md
[disk]: ./internal/scraper/diskscraper/documentation.md
Expand All @@ -59,6 +60,7 @@ The available scrapers are:
[paging]: ./internal/scraper/pagingscraper/documentation.md
[processes]: ./internal/scraper/processesscraper/documentation.md
[process]: ./internal/scraper/processscraper/documentation.md
[uptime]: ./internal/scraper/uptimescraper/documentation.md

### Notes

Expand Down
6 changes: 6 additions & 0 deletions receiver/hostmetricsreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/pagingscraper"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/processesscraper"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/processscraper"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/uptimescraper"
)

func TestLoadConfig(t *testing.T) {
Expand Down Expand Up @@ -118,6 +119,11 @@ func TestLoadConfig(t *testing.T) {
cfg.SetEnvMap(common.EnvMap{})
return cfg
})(),
uptimescraper.TypeStr: (func() internal.Config {
cfg := (&uptimescraper.Factory{}).CreateDefaultConfig()
cfg.SetEnvMap(common.EnvMap{})
return cfg
})(),
},
}

Expand Down
1 change: 1 addition & 0 deletions receiver/hostmetricsreceiver/example_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ receivers:
network:
paging:
processes:
uptime:

exporters:
debug:
Expand Down
2 changes: 2 additions & 0 deletions receiver/hostmetricsreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/pagingscraper"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/processesscraper"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/processscraper"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/uptimescraper"
)

const (
Expand All @@ -45,6 +46,7 @@ var (
pagingscraper.TypeStr: &pagingscraper.Factory{},
processesscraper.TypeStr: &processesscraper.Factory{},
processscraper.TypeStr: &processscraper.Factory{},
uptimescraper.TypeStr: &uptimescraper.Factory{},
}
)

Expand Down
15 changes: 15 additions & 0 deletions receiver/hostmetricsreceiver/hostmetrics_receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/pagingscraper"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/processesscraper"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/processscraper"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/uptimescraper"
)

var allMetrics = []string{
Expand Down Expand Up @@ -81,6 +82,7 @@ var factories = map[string]internal.ScraperFactory{
pagingscraper.TypeStr: &pagingscraper.Factory{},
processesscraper.TypeStr: &processesscraper.Factory{},
processscraper.TypeStr: &processscraper.Factory{},
uptimescraper.TypeStr: &uptimescraper.Factory{},
}

type testEnv struct {
Expand Down Expand Up @@ -395,6 +397,19 @@ func Benchmark_ScrapeProcessMetrics(b *testing.B) {
benchmarkScrapeMetrics(b, cfg)
}

func Benchmark_ScrapeUptimeMetrics(b *testing.B) {
if runtime.GOOS != "linux" && runtime.GOOS != "windows" {
b.Skip("skipping test on non linux/windows")
}

cfg := &Config{
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
Scrapers: map[string]internal.Config{uptimescraper.TypeStr: (&uptimescraper.Factory{}).CreateDefaultConfig()},
}

benchmarkScrapeMetrics(b, cfg)
}

func Benchmark_ScrapeSystemMetrics(b *testing.B) {
cfg := &Config{
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package uptimescraper // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/uptimescraper"

import (
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/uptimescraper/internal/metadata"
)

// Config relating to Uptime Metric Scraper.
type Config struct {
// MetricsBuilderConfig allows to customize scraped metrics/attributes representation.
metadata.MetricsBuilderConfig `mapstructure:",squash"`
internal.ScraperConfig
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

//go:generate mdatagen metadata.yaml

package uptimescraper // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/uptimescraper"
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[comment]: <> (Code generated by mdatagen. DO NOT EDIT.)

# hostmetricsreceiver/uptime

**Parent Component:** hostmetrics

## Default Metrics

The following metrics are emitted by default. Each of them can be disabled by applying the following configuration:

```yaml
metrics:
<metric_name>:
enabled: false
```
### system.uptime
The time the system has been running
| Unit | Metric Type | Value Type |
| ---- | ----------- | ---------- |
| s | Gauge | Double |
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package uptimescraper // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/uptimescraper"

import (
"context"
"errors"
"runtime"

"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/receiver/scraperhelper"

"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal"
hostmeta "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/metadata"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/uptimescraper/internal/metadata"
)

// This file implements Factory for Uptime scraper.

const (
// TypeStr the value of "type" key in configuration.
TypeStr = "uptime"
)

// Factory is the Factory for scraper.
type Factory struct {
}

// CreateDefaultConfig creates the default configuration for the Scraper.
func (f *Factory) CreateDefaultConfig() internal.Config {
return &Config{
MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
}
}

// CreateMetricsScraper creates a resource scraper based on provided config.
func (f *Factory) CreateMetricsScraper(
ctx context.Context,
settings receiver.Settings,
cfg internal.Config,
) (scraperhelper.Scraper, error) {
if runtime.GOOS != "linux" && runtime.GOOS != "windows" && runtime.GOOS != "darwin" {
return nil, errors.New("uptime scraper only available on Linux, Windows, or MacOS")
}

uptimeScraper := newUptimeScraper(ctx, settings, cfg.(*Config))

return scraperhelper.NewScraper(
hostmeta.Type,
uptimeScraper.scrape,
scraperhelper.WithStart(uptimeScraper.start),
)
}

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.

Loading

0 comments on commit 595e438

Please sign in to comment.