Skip to content

Commit

Permalink
[receiver/hostmetrics] add EntityState entity event as log record for…
Browse files Browse the repository at this point in the history
… host (open-telemetry#33928)

**Description:** 
Add EntityState event for host, implementation is similiar to
k8sclusterreceiver. This change sends empty event, which can be used
together with resourcedetection processor to add required attributes:
```
    receivers:
      hostmetrics:
        enabled: true
        metadata_collection_interval: "5m"

    processors:
      resourcedetection/env:
        detectors: ["system", "env"]
        timeout: 2s
        override: false
        system:
          resource_attributes:
            host.id:
              enabled: true
      resourcedetection/region:
        detectors: ["gcp", "ec2", "azure"]
        timeout: 2s
        override: true
      transform/entity-event:
        log_statements:
          - context: log
            statements:
              - set(attributes["otel.entity.attributes"]["otel.entity.id"]["host.id"], resource.attributes["host.id"])
              - set(attributes["otel.entity.attributes"]["host.name"], resource.attributes["host.name"])
              - set(attributes["otel.entity.attributes"]["host.type"], resource.attributes["host.type"])
              - set(attributes["otel.entity.attributes"]["host.image.id"], resource.attributes["host.image.id"])
              - set(attributes["otel.entity.attributes"]["host.image.name"], resource.attributes["host.image.name"])
          - context: resource
            statements:
              - keep_keys(attributes, [""])
    service:
      pipelines:
        logs/entity:
          exporters:
            - otlp
          processors:
            - k8sattributes
            - resourcedetection/env
            - resourcedetection/region
            - transform/entity-event
          receivers:
            - hostmetrics
```

**Link to tracking Issue:**
open-telemetry#33927

**Testing:**
- kind cluster
- unit tests

**Documentation:** <Describe the documentation added.>
- added docs

---------

Co-authored-by: Tigran Najaryan <[email protected]>
Co-authored-by: Dmitrii Anoshin <[email protected]>
  • Loading branch information
3 people authored Jul 22, 2024
1 parent 3b6d872 commit f11bad9
Show file tree
Hide file tree
Showing 17 changed files with 201 additions and 89 deletions.
27 changes: 27 additions & 0 deletions .chloggen/entity-event.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: hostmetricsreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "allow configuring log pipeline to send host EntityState event"

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

# (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.mod
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,5 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/processor/tran
replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/sampling => ../../pkg/sampling

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/pdatautil => ../../internal/pdatautil

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/experimentalmetricmetadata => ../../pkg/experimentalmetricmetadata
3 changes: 3 additions & 0 deletions exporter/datadogexporter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ require (
github.com/open-telemetry/opentelemetry-collector-contrib/internal/docker v0.105.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter v0.105.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/pdatautil v0.105.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/experimentalmetricmetadata v0.0.0-00010101000000-000000000000 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl v0.105.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil v0.105.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/sampling v0.105.0 // indirect
Expand Down Expand Up @@ -445,3 +446,5 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/processor/tran
replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/sampling => ../../pkg/sampling

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/pdatautil => ../../internal/pdatautil

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/experimentalmetricmetadata => ../../pkg/experimentalmetricmetadata
2 changes: 2 additions & 0 deletions exporter/datadogexporter/integrationtest/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,5 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/processor/tran
replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/sampling => ../../../pkg/sampling

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/pdatautil => ../../../internal/pdatautil

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/experimentalmetricmetadata => ../../../pkg/experimentalmetricmetadata
11 changes: 8 additions & 3 deletions receiver/hostmetricsreceiver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@
<!-- status autogenerated section -->
| Status | |
| ------------- |-----------|
| Stability | [beta]: metrics |
| Stability | [development]: logs |
| | [beta]: metrics |
| Distributions | [core], [contrib] |
| Issues | [![Open issues](https://img.shields.io/github/issues-search/open-telemetry/opentelemetry-collector-contrib?query=is%3Aissue%20is%3Aopen%20label%3Areceiver%2Fhostmetrics%20&label=open&color=orange&logo=opentelemetry)](https://github.com/open-telemetry/opentelemetry-collector-contrib/issues?q=is%3Aopen+is%3Aissue+label%3Areceiver%2Fhostmetrics) [![Closed issues](https://img.shields.io/github/issues-search/open-telemetry/opentelemetry-collector-contrib?query=is%3Aissue%20is%3Aclosed%20label%3Areceiver%2Fhostmetrics%20&label=closed&color=blue&logo=opentelemetry)](https://github.com/open-telemetry/opentelemetry-collector-contrib/issues?q=is%3Aclosed+is%3Aissue+label%3Areceiver%2Fhostmetrics) |
| [Code Owners](https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/CONTRIBUTING.md#becoming-a-code-owner) | [@dmitryax](https://www.github.com/dmitryax), [@braydonk](https://www.github.com/braydonk) |

[development]: https://github.com/open-telemetry/opentelemetry-collector#development
[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
<!-- 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
deployed as an agent.
from various sources and host entity event as log. This is intended to be
used when the collector is deployed as an agent.

## Getting Started

Expand Down Expand Up @@ -188,6 +190,9 @@ 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>"
```
## Entity Events
**Entity Events as logs are experimental** and might eventually be replaced by the result of [the OTEP](https://github.com/open-telemetry/oteps/blob/main/text/entities/0256-entities-data-model.md#entity-events). For now, the hostmetrics receiver can send the host entity event as a log records. By default, the hostmetrics receiver sends periodic EntityState events every 5 minutes. You can change that by setting `metadata_collection_interval`. Entity Events as logs are experimental. The result of the OTEP might eventually replace that.
## Feature Gates
Expand Down
8 changes: 8 additions & 0 deletions receiver/hostmetricsreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package hostmetricsreceiver // import "github.com/open-telemetry/opentelemetry-c
import (
"errors"
"fmt"
"time"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/confmap"
Expand All @@ -25,6 +26,13 @@ type Config struct {
Scrapers map[string]internal.Config `mapstructure:"-"`
// RootPath is the host's root directory (linux only).
RootPath string `mapstructure:"root_path"`

// Collection interval for metadata.
// Metadata of the particular entity is collected when the entity changes.
// In addition metadata of all entities is collected periodically even if no changes happen.
// Setting the duration to 0 will disable periodic collection (however will not impact
// metadata collection on changes).
MetadataCollectionInterval time.Duration `mapstructure:"metadata_collection_interval"`
}

var _ component.Config = (*Config)(nil)
Expand Down
1 change: 1 addition & 0 deletions receiver/hostmetricsreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func TestLoadConfig(t *testing.T) {

r1 := cfg.Receivers[component.NewIDWithName(metadata.Type, "customname")].(*Config)
expectedConfig := &Config{
MetadataCollectionInterval: 5 * time.Minute,
ControllerConfig: scraperhelper.ControllerConfig{
CollectionInterval: 30 * time.Second,
InitialDelay: time.Second,
Expand Down
23 changes: 21 additions & 2 deletions receiver/hostmetricsreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"fmt"
"os"
"time"

"github.com/shirou/gopsutil/v4/host"
"github.com/shirou/gopsutil/v4/process"
Expand All @@ -28,6 +29,10 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/processscraper"
)

const (
defaultMetadataCollectionInterval = 5 * time.Minute
)

// This file implements Factory for HostMetrics receiver.
var (
scraperFactories = map[string]internal.ScraperFactory{
Expand All @@ -48,7 +53,8 @@ func NewFactory() receiver.Factory {
return receiver.NewFactory(
metadata.Type,
createDefaultConfig,
receiver.WithMetrics(createMetricsReceiver, metadata.MetricsStability))
receiver.WithMetrics(createMetricsReceiver, metadata.MetricsStability),
receiver.WithLogs(createLogsReceiver, metadata.LogsStability))
}

func getScraperFactory(key string) (internal.ScraperFactory, bool) {
Expand All @@ -61,7 +67,10 @@ func getScraperFactory(key string) (internal.ScraperFactory, bool) {

// createDefaultConfig creates the default configuration for receiver.
func createDefaultConfig() component.Config {
return &Config{ControllerConfig: scraperhelper.NewDefaultControllerConfig()}
return &Config{
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
MetadataCollectionInterval: defaultMetadataCollectionInterval,
}
}

// createMetricsReceiver creates a metrics receiver based on provided config.
Expand Down Expand Up @@ -89,6 +98,16 @@ func createMetricsReceiver(
)
}

func createLogsReceiver(
_ context.Context, set receiver.Settings, cfg component.Config, consumer consumer.Logs,
) (receiver.Logs, error) {
return &hostEntitiesReceiver{
cfg: cfg.(*Config),
nextLogs: consumer,
settings: &set,
}, nil
}

func createAddScraperOptions(
ctx context.Context,
set receiver.Settings,
Expand Down
4 changes: 2 additions & 2 deletions receiver/hostmetricsreceiver/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func TestCreateReceiver(t *testing.T) {
assert.NotNil(t, mReceiver)

tLogs, err := factory.CreateLogsReceiver(context.Background(), creationSet, cfg, consumertest.NewNop())
assert.Equal(t, err, component.ErrDataTypeIsNotSupported)
assert.Nil(t, tLogs)
assert.NoError(t, err)
assert.NotNil(t, tLogs)
}

func TestCreateReceiver_ScraperKeyConfigError(t *testing.T) {
Expand Down
7 changes: 7 additions & 0 deletions receiver/hostmetricsreceiver/generated_component_test.go

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

7 changes: 5 additions & 2 deletions receiver/hostmetricsreceiver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/google/go-cmp v0.6.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.105.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter v0.105.0
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/experimentalmetricmetadata v0.0.0-00010101000000-000000000000
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest v0.105.0
github.com/prometheus-community/windows_exporter v0.25.1
github.com/prometheus/procfs v0.15.1
Expand All @@ -21,8 +22,6 @@ require (
go.opentelemetry.io/collector/pdata v1.12.1-0.20240716231837-5753a58f712b
go.opentelemetry.io/collector/receiver v0.105.1-0.20240717163034-43ed6184f9fe
go.opentelemetry.io/collector/semconv v0.105.1-0.20240717163034-43ed6184f9fe
go.opentelemetry.io/otel/metric v1.28.0
go.opentelemetry.io/otel/trace v1.28.0
go.uber.org/goleak v1.3.0
go.uber.org/multierr v1.11.0
go.uber.org/zap v1.27.0
Expand Down Expand Up @@ -128,9 +127,11 @@ require (
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.28.0 // indirect
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.28.0 // indirect
go.opentelemetry.io/otel/log v0.4.0 // indirect
go.opentelemetry.io/otel/metric v1.28.0 // indirect
go.opentelemetry.io/otel/sdk v1.28.0 // indirect
go.opentelemetry.io/otel/sdk/log v0.4.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.28.0 // indirect
go.opentelemetry.io/otel/trace v1.28.0 // indirect
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 // indirect
Expand Down Expand Up @@ -164,3 +165,5 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil
replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest => ../../pkg/pdatatest

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden => ../../pkg/golden

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/experimentalmetricmetadata => ../../pkg/experimentalmetricmetadata

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

This file was deleted.

This file was deleted.

1 change: 1 addition & 0 deletions receiver/hostmetricsreceiver/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ status:
class: receiver
stability:
beta: [metrics]
development: [logs]
distributions: [core, contrib]
codeowners:
active: [dmitryax, braydonk]
Expand Down
Loading

0 comments on commit f11bad9

Please sign in to comment.