Skip to content

Commit

Permalink
hostmetricsreceiver doc updates and logging (#2038)
Browse files Browse the repository at this point in the history
CPU and disk metrics are not available in gopsutils on Mac when cgo is disabled
(https://github.com/shirou/gopsutil/blob/master/cpu/cpu_darwin_nocgo.go)

Errors were not logged which lead to confusion about why things weren't
working. @james-bebbington was this intentional or unintentional? It does
spam the logs a bit (every collection interval) but I don't think we can fully
rely on users checking traces for errors.
  • Loading branch information
jrcamp authored Nov 18, 2020
1 parent 8b9b7d5 commit 8b67a09
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 22 deletions.
26 changes: 15 additions & 11 deletions receiver/hostmetricsreceiver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,21 @@ hostmetrics:

The available scrapers are:

Scraper | Supported OSs | Description
-----------|--------------------|-------------
cpu | All | CPU utilization metrics
disk | All | Disk I/O metrics
load | All | CPU load metrics
filesystem | All | File System utilization metrics
memory | All | Memory utilization metrics
network | All | Network interface I/O metrics & TCP connection metrics
processes | Linux | Process count metrics
swap | All | Swap space utilization and I/O metrics
process | Linux & Windows | Per process CPU, Memory, and Disk I/O metrics
| Scraper | Supported OSs | Description |
|------------|------------------------------|--------------------------------------------------------|
| cpu | All except Mac<sup>[1]</sup> | CPU utilization metrics |
| disk | All except Mac<sup>[1]</sup> | Disk I/O metrics |
| load | All | CPU load metrics |
| filesystem | All | File System utilization metrics |
| memory | All | Memory utilization metrics |
| network | All | Network interface I/O metrics & TCP connection metrics |
| processes | Linux | Process count metrics |
| swap | All | Swap space utilization and I/O metrics |
| process | Linux & Windows | Per process CPU, Memory, and Disk I/O metrics |

### Notes

<sup>[1]</sup> Not supported on Mac when compiled without cgo which is the default.

Several scrapers support additional configuration:

Expand Down
1 change: 1 addition & 0 deletions receiver/hostmetricsreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ func createMetricsReceiver(

return receiverhelper.NewScraperControllerReceiver(
&oCfg.ScraperControllerSettings,
params.Logger,
consumer,
addScraperOptions...,
)
Expand Down
11 changes: 5 additions & 6 deletions receiver/hostmetricsreceiver/hostmetrics_receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/stretchr/testify/require"
"go.uber.org/zap"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/consumer/pdata"
Expand Down Expand Up @@ -120,7 +119,7 @@ func TestGatherMetrics_EndToEnd(t *testing.T) {
config.Scrapers[processscraper.TypeStr] = &processscraper.Config{}
}

receiver, err := NewFactory().CreateMetricsReceiver(context.Background(), component.ReceiverCreateParams{}, config, sink)
receiver, err := NewFactory().CreateMetricsReceiver(context.Background(), creationParams, config, sink)

require.NoError(t, err, "Failed to create metrics receiver: %v", err)

Expand Down Expand Up @@ -242,7 +241,7 @@ func TestGatherMetrics_ScraperKeyConfigError(t *testing.T) {

sink := new(consumertest.MetricsSink)
config := &Config{Scrapers: map[string]internal.Config{"error": &mockConfig{}}}
_, err := NewFactory().CreateMetricsReceiver(context.Background(), component.ReceiverCreateParams{}, config, sink)
_, err := NewFactory().CreateMetricsReceiver(context.Background(), creationParams, config, sink)
require.Error(t, err)
}

Expand All @@ -254,7 +253,7 @@ func TestGatherMetrics_CreateMetricsScraperError(t *testing.T) {

sink := new(consumertest.MetricsSink)
config := &Config{Scrapers: map[string]internal.Config{mockTypeStr: &mockConfig{}}}
_, err := NewFactory().CreateMetricsReceiver(context.Background(), component.ReceiverCreateParams{}, config, sink)
_, err := NewFactory().CreateMetricsReceiver(context.Background(), creationParams, config, sink)
require.Error(t, err)
}

Expand All @@ -266,7 +265,7 @@ func TestGatherMetrics_CreateMetricsResourceScraperError(t *testing.T) {

sink := new(consumertest.MetricsSink)
config := &Config{Scrapers: map[string]internal.Config{mockResourceTypeStr: &mockConfig{}}}
_, err := NewFactory().CreateMetricsReceiver(context.Background(), component.ReceiverCreateParams{}, config, sink)
_, err := NewFactory().CreateMetricsReceiver(context.Background(), creationParams, config, sink)
require.Error(t, err)
}

Expand Down Expand Up @@ -297,7 +296,7 @@ func benchmarkScrapeMetrics(b *testing.B, cfg *Config) {
require.NoError(b, err)
options = append(options, receiverhelper.WithTickerChannel(tickerCh))

receiver, err := receiverhelper.NewScraperControllerReceiver(&cfg.ScraperControllerSettings, sink, options...)
receiver, err := receiverhelper.NewScraperControllerReceiver(&cfg.ScraperControllerSettings, zap.NewNop(), sink, options...)
require.NoError(b, err)

require.NoError(b, receiver.Start(context.Background(), componenttest.NewNopHost()))
Expand Down
20 changes: 16 additions & 4 deletions receiver/receiverhelper/scrapercontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"errors"
"time"

"go.uber.org/zap"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenterror"
"go.opentelemetry.io/collector/config/configmodels"
Expand Down Expand Up @@ -85,6 +87,7 @@ func WithTickerChannel(tickerCh <-chan time.Time) ScraperControllerOption {

type scraperController struct {
name string
logger *zap.Logger
collectionInterval time.Duration
nextConsumer consumer.MetricsConsumer

Expand All @@ -99,7 +102,12 @@ type scraperController struct {
}

// NewScraperControllerReceiver creates a Receiver with the configured options, that can control multiple scrapers.
func NewScraperControllerReceiver(cfg *ScraperControllerSettings, nextConsumer consumer.MetricsConsumer, options ...ScraperControllerOption) (component.Receiver, error) {
func NewScraperControllerReceiver(
cfg *ScraperControllerSettings,
logger *zap.Logger,
nextConsumer consumer.MetricsConsumer,
options ...ScraperControllerOption,
) (component.Receiver, error) {
if nextConsumer == nil {
return nil, componenterror.ErrNilNextConsumer
}
Expand All @@ -110,6 +118,7 @@ func NewScraperControllerReceiver(cfg *ScraperControllerSettings, nextConsumer c

sc := &scraperController{
name: cfg.Name(),
logger: logger,
collectionInterval: cfg.CollectionInterval,
nextConsumer: nextConsumer,
metricsScrapers: &multiMetricScraper{},
Expand Down Expand Up @@ -201,10 +210,13 @@ func (sc *scraperController) scrapeMetricsAndReport(ctx context.Context) {

for _, rms := range sc.resourceMetricScrapers {
resourceMetrics, err := rms.Scrape(ctx, sc.name)
if err != nil && !consumererror.IsPartialScrapeError(err) {
continue
}
if err != nil {
sc.logger.Error("Error scraping metrics", zap.Error(err))

if !consumererror.IsPartialScrapeError(err) {
continue
}
}
resourceMetrics.MoveAndAppendTo(metrics.ResourceMetrics())
}

Expand Down
4 changes: 3 additions & 1 deletion receiver/receiverhelper/scrapercontroller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opencensus.io/trace"
"go.uber.org/zap"

"go.opentelemetry.io/collector/component/componenterror"
"go.opentelemetry.io/collector/component/componenttest"
Expand Down Expand Up @@ -211,7 +212,7 @@ func TestScrapeController(t *testing.T) {
cfg.NameVal = "receiver"
}

mr, err := NewScraperControllerReceiver(cfg, nextConsumer, options...)
mr, err := NewScraperControllerReceiver(cfg, zap.NewNop(), nextConsumer, options...)
if test.expectedNewErr != "" {
assert.EqualError(t, err, test.expectedNewErr)
return
Expand Down Expand Up @@ -431,6 +432,7 @@ func TestSingleScrapePerTick(t *testing.T) {

receiver, err := NewScraperControllerReceiver(
cfg,
zap.NewNop(),
new(consumertest.MetricsSink),
AddMetricsScraper(NewMetricsScraper("", tsm.scrape)),
AddResourceMetricsScraper(NewResourceMetricsScraper("", tsrm.scrape)),
Expand Down

0 comments on commit 8b67a09

Please sign in to comment.