Skip to content

Commit

Permalink
[chore][receiver/hostmetrics] create metadata (#21827)
Browse files Browse the repository at this point in the history
Created Metadata file and added stability + type changes.
Link to tracking Issue: #21213
  • Loading branch information
BominRahmani authored May 30, 2023
1 parent 66ef316 commit f59b854
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 30 deletions.
24 changes: 13 additions & 11 deletions receiver/hostmetricsreceiver/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
# Host Metrics Receiver

| Status | |
| ------------------------ |-------------------------------------------------|
| Stability | [beta] |
| Supported pipeline types | metrics |
| Distributions | [core], [contrib], [observiq], [splunk], [sumo] |
<!-- status autogenerated section -->
| Status | |
| ------------- |-----------|
| Stability | [beta]: metrics |
| Distributions | [core], [contrib], [observiq], [splunk], [sumo] |

[beta]: https://github.com/open-telemetry/opentelemetry-collector#beta
[core]: https://github.com/open-telemetry/opentelemetry-collector-releases/tree/main/distributions/otelcol
[contrib]: https://github.com/open-telemetry/opentelemetry-collector-releases/tree/main/distributions/otelcol-contrib
[observiq]: https://github.com/observIQ/observiq-otel-collector
[splunk]: https://github.com/signalfx/splunk-otel-collector
[sumo]: https://github.com/SumoLogic/sumologic-otel-collector
<!-- end autogenerated section -->

The Host Metrics receiver generates metrics about the host system scraped
from various sources. This is intended to be used when the collector is
Expand Down Expand Up @@ -179,9 +187,3 @@ Currently, the hostmetrics receiver does not set any Resource attributes on the
export OTEL_RESOURCE_ATTRIBUTES="service.name=<the name of your service>,service.namespace=<the namespace of your service>,service.instance.id=<uuid of the instance>"
```
[beta]: https://github.com/open-telemetry/opentelemetry-collector#beta
[contrib]: https://github.com/open-telemetry/opentelemetry-collector-releases/tree/main/distributions/otelcol-contrib
[core]: https://github.com/open-telemetry/opentelemetry-collector-releases/tree/main/distributions/otelcol
[splunk]: https://github.com/signalfx/splunk-otel-collector
[observiq]: https://github.com/observIQ/observiq-otel-collector
[sumo]: https://github.com/SumoLogic/sumologic-otel-collector
11 changes: 6 additions & 5 deletions receiver/hostmetricsreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterset"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/metadata"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/cpuscraper"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/diskscraper"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper"
Expand All @@ -32,23 +33,23 @@ func TestLoadConfig(t *testing.T) {
require.NoError(t, err)

factory := NewFactory()
factories.Receivers[typeStr] = factory
factories.Receivers[metadata.Type] = factory
cfg, err := otelcoltest.LoadConfigAndValidate(filepath.Join("testdata", "config.yaml"), factories)

require.NoError(t, err)
require.NotNil(t, cfg)

assert.Equal(t, len(cfg.Receivers), 2)

r0 := cfg.Receivers[component.NewID(typeStr)]
r0 := cfg.Receivers[component.NewID(metadata.Type)]
defaultConfigCPUScraper := factory.CreateDefaultConfig()
defaultConfigCPUScraper.(*Config).Scrapers = map[string]internal.Config{
cpuscraper.TypeStr: (&cpuscraper.Factory{}).CreateDefaultConfig(),
}

assert.Equal(t, defaultConfigCPUScraper, r0)

r1 := cfg.Receivers[component.NewIDWithName(typeStr, "customname")].(*Config)
r1 := cfg.Receivers[component.NewIDWithName(metadata.Type, "customname")].(*Config)
expectedConfig := &Config{
ScraperControllerSettings: scraperhelper.ScraperControllerSettings{
CollectionInterval: 30 * time.Second,
Expand Down Expand Up @@ -92,7 +93,7 @@ func TestLoadInvalidConfig_NoScrapers(t *testing.T) {
require.NoError(t, err)

factory := NewFactory()
factories.Receivers[typeStr] = factory
factories.Receivers[metadata.Type] = factory
_, err = otelcoltest.LoadConfigAndValidate(filepath.Join("testdata", "config-noscrapers.yaml"), factories)

require.Contains(t, err.Error(), "must specify at least one scraper when using hostmetrics receiver")
Expand All @@ -103,7 +104,7 @@ func TestLoadInvalidConfig_InvalidScraperKey(t *testing.T) {
require.NoError(t, err)

factory := NewFactory()
factories.Receivers[typeStr] = factory
factories.Receivers[metadata.Type] = factory
_, err = otelcoltest.LoadConfigAndValidate(filepath.Join("testdata", "config-invalidscraperkey.yaml"), factories)

require.Contains(t, err.Error(), "error reading configuration for \"hostmetrics\": invalid scraper key: invalidscraperkey")
Expand Down
2 changes: 2 additions & 0 deletions receiver/hostmetricsreceiver/doc.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

//go:generate mdatagen metadata.yaml

// Package hostmetricsreceiver reads metrics like CPU usage, disk usage, and network usage from the host.
package hostmetricsreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver"
15 changes: 4 additions & 11 deletions receiver/hostmetricsreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"go.opentelemetry.io/collector/receiver/scraperhelper"

"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/metadata"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/cpuscraper"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/diskscraper"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper"
Expand All @@ -26,14 +27,6 @@ import (
)

// This file implements Factory for HostMetrics receiver.

const (
// The value of "type" key in configuration.
typeStr = "hostmetrics"
// The stability level of the host metrics receiver.
stability = component.StabilityLevelBeta
)

var (
scraperFactories = map[string]internal.ScraperFactory{
cpuscraper.TypeStr: &cpuscraper.Factory{},
Expand All @@ -51,9 +44,9 @@ var (
// NewFactory creates a new factory for host metrics receiver.
func NewFactory() receiver.Factory {
return receiver.NewFactory(
typeStr,
metadata.Type,
createDefaultConfig,
receiver.WithMetrics(createMetricsReceiver, stability))
receiver.WithMetrics(createMetricsReceiver, metadata.MetricsStability))
}

func getScraperFactory(key string) (internal.ScraperFactory, bool) {
Expand All @@ -66,7 +59,7 @@ func getScraperFactory(key string) (internal.ScraperFactory, bool) {

// createDefaultConfig creates the default configuration for receiver.
func createDefaultConfig() component.Config {
return &Config{ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(typeStr)}
return &Config{ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type)}
}

// createMetricsReceiver creates a metrics receiver based on provided config.
Expand Down
7 changes: 4 additions & 3 deletions receiver/hostmetricsreceiver/hostmetrics_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"go.opentelemetry.io/collector/otelcol/otelcoltest"

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

Expand All @@ -36,12 +37,12 @@ func TestLoadConfigRootPath(t *testing.T) {
t.Setenv("HOST_PROC", "testdata")
factories, _ := otelcoltest.NopFactories()
factory := NewFactory()
factories.Receivers[typeStr] = factory
factories.Receivers[metadata.Type] = factory
cfg, err := otelcoltest.LoadConfigAndValidate(filepath.Join("testdata", "config-root-path.yaml"), factories)
require.NoError(t, err)
globalRootPath = ""

r := cfg.Receivers[component.NewID(typeStr)].(*Config)
r := cfg.Receivers[component.NewID(metadata.Type)].(*Config)
expectedConfig := factory.CreateDefaultConfig().(*Config)
expectedConfig.RootPath = "testdata"
cpuScraperCfg := (&cpuscraper.Factory{}).CreateDefaultConfig()
Expand All @@ -54,7 +55,7 @@ func TestLoadConfigRootPath(t *testing.T) {
func TestLoadInvalidConfig_RootPathNotExist(t *testing.T) {
factories, _ := otelcoltest.NopFactories()
factory := NewFactory()
factories.Receivers[typeStr] = factory
factories.Receivers[metadata.Type] = factory
_, err := otelcoltest.LoadConfigAndValidate(filepath.Join("testdata", "config-bad-root-path.yaml"), factories)
assert.ErrorContains(t, err, "invalid root_path:")
globalRootPath = ""
Expand Down
12 changes: 12 additions & 0 deletions receiver/hostmetricsreceiver/internal/metadata/generated_status.go

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

8 changes: 8 additions & 0 deletions receiver/hostmetricsreceiver/metadata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
type: hostmetrics

status:
class: receiver
stability:
beta: [metrics]
distributions: [core, contrib, observiq, splunk, sumo]

0 comments on commit f59b854

Please sign in to comment.