From 14ed05086c59670855832c99b0648f34367fde53 Mon Sep 17 00:00:00 2001 From: Mrod1598 Date: Wed, 9 Mar 2022 10:16:09 -0500 Subject: [PATCH 01/24] Add ability to define metrics --- .../windowsperfcountersreceiver/config.go | 58 ++++++++-- .../config_test.go | 75 ++++++++++++- .../factory_others_test.go | 17 ++- .../factory_test.go | 53 ++++++++- .../factory_windows_test.go | 18 +++- .../testdata/config-emptyinstance.yaml | 9 +- .../config-negative-collection-interval.yaml | 9 +- .../testdata/config-nocounters.yaml | 6 ++ .../testdata/config-noobjectname.yaml | 9 +- .../testdata/config-noperfcounters.yaml | 7 +- .../testdata/config.yaml | 33 ++++-- .../windowsperfcounters_scraper.go | 102 +++++++++++++----- .../windowsperfcounters_scraper_test.go | 33 ++++-- 13 files changed, 364 insertions(+), 65 deletions(-) diff --git a/receiver/windowsperfcountersreceiver/config.go b/receiver/windowsperfcountersreceiver/config.go index 396acb242cc3..38f9f9ecde42 100644 --- a/receiver/windowsperfcountersreceiver/config.go +++ b/receiver/windowsperfcountersreceiver/config.go @@ -25,14 +25,40 @@ import ( type Config struct { scraperhelper.ScraperControllerSettings `mapstructure:",squash"` - PerfCounters []PerfCounterConfig `mapstructure:"perfcounters"` + MetricMetaData []MetricConfig `mapstructure:"metric_metadata"` + PerfCounters []PerfCounterConfig `mapstructure:"perfcounters"` } // PerfCounterConfig defines configuration for a perf counter object. type PerfCounterConfig struct { - Object string `mapstructure:"object"` - Instances []string `mapstructure:"instances"` - Counters []string `mapstructure:"counters"` + Object string `mapstructure:"object"` + Instances []string `mapstructure:"instances"` + Counters []CounterConfig `mapstructure:"counters"` +} + +// MetricsConfig defines the configuration for a metric to be created. +type MetricConfig struct { + MetricName string `mapstructure:"metric_name"` + Unit string `mapstructure:"unit"` + Description string `mapstructure:"description"` + Gauge GaugeMetric `mapstructure:"gauge"` + Sum SumMetric `mapstructure:"sum"` +} + +type GaugeMetric struct { + ValueType string `mapstructure:"value_type"` +} + +type SumMetric struct { + ValueType string `mapstructure:"value_type"` + Aggregation string `mapstructure:"aggregation"` + Monotonic bool `mapstructure:"monotonic"` +} + +type CounterConfig struct { + MetricName string `mapstructure:"metric_name"` + CounterName string `mapstructure:"counter_name"` + Attributes map[string]string `mapstructure:"attributes"` } func (c *Config) Validate() error { @@ -46,6 +72,10 @@ func (c *Config) Validate() error { errs = multierr.Append(errs, fmt.Errorf("must specify at least one perf counter")) } + if len(c.MetricMetaData) == 0 { + errs = multierr.Append(errs, fmt.Errorf("must specify at least one metric")) + } + var perfCounterMissingObjectName bool for _, pc := range c.PerfCounters { if pc.Object == "" { @@ -53,16 +83,28 @@ func (c *Config) Validate() error { continue } + if len(pc.Counters) == 0 { + errs = multierr.Append(errs, fmt.Errorf("perf counter for object %q does not specify any counters", pc.Object)) + } + + for _, counter := range pc.Counters { + foundMatchingMetric := false + for _, metric := range c.MetricMetaData { + if counter.MetricName == metric.MetricName { + foundMatchingMetric = true + } + } + if !foundMatchingMetric { + errs = multierr.Append(errs, fmt.Errorf("perf counter for object %q includes an undefined metric", pc.Object)) + } + } + for _, instance := range pc.Instances { if instance == "" { errs = multierr.Append(errs, fmt.Errorf("perf counter for object %q includes an empty instance", pc.Object)) break } } - - if len(pc.Counters) == 0 { - errs = multierr.Append(errs, fmt.Errorf("perf counter for object %q does not specify any counters", pc.Object)) - } } if perfCounterMissingObjectName { diff --git a/receiver/windowsperfcountersreceiver/config_test.go b/receiver/windowsperfcountersreceiver/config_test.go index 2f88f381e668..d1e1c4df76f9 100644 --- a/receiver/windowsperfcountersreceiver/config_test.go +++ b/receiver/windowsperfcountersreceiver/config_test.go @@ -43,10 +43,30 @@ func TestLoadConfig(t *testing.T) { r0 := cfg.Receivers[config.NewComponentID(typeStr)] defaultConfigSingleObject := factory.CreateDefaultConfig() - defaultConfigSingleObject.(*Config).PerfCounters = []PerfCounterConfig{{Object: "object", Counters: []string{"counter"}}} + + counterConfig := CounterConfig{ + CounterName: "counter1", + MetricName: "metric", + } + defaultConfigSingleObject.(*Config).PerfCounters = []PerfCounterConfig{{Object: "object", Counters: []CounterConfig{counterConfig}}} + defaultConfigSingleObject.(*Config).MetricMetaData = []MetricConfig{ + { + MetricName: "metric", + Description: "desc", + Unit: "1", + Gauge: GaugeMetric{ + ValueType: "double", + }, + }, + } assert.Equal(t, defaultConfigSingleObject, r0) + counterConfig2 := CounterConfig{ + CounterName: "counter2", + MetricName: "metric2", + } + r1 := cfg.Receivers[config.NewComponentIDWithName(typeStr, "customname")].(*Config) expectedConfig := &Config{ ScraperControllerSettings: scraperhelper.ScraperControllerSettings{ @@ -56,11 +76,29 @@ func TestLoadConfig(t *testing.T) { PerfCounters: []PerfCounterConfig{ { Object: "object1", - Counters: []string{"counter1"}, + Counters: []CounterConfig{counterConfig}, }, { Object: "object2", - Counters: []string{"counter1", "counter2"}, + Counters: []CounterConfig{counterConfig, counterConfig2}, + }, + }, + MetricMetaData: []MetricConfig{ + { + MetricName: "metric", + Description: "desc", + Unit: "1", + Gauge: GaugeMetric{ + ValueType: "double", + }, + }, + { + MetricName: "metric2", + Description: "desc", + Unit: "1", + Gauge: GaugeMetric{ + ValueType: "double", + }, }, }, } @@ -82,6 +120,12 @@ func TestLoadConfig_Error(t *testing.T) { noObjectNameErr = "must specify object name for all perf counters" noCountersErr = `perf counter for object "%s" does not specify any counters` emptyInstanceErr = `perf counter for object "%s" includes an empty instance` + undefinedMetricErr = `perf counter for object "%s" includes an undefined metric` + missingMetricName = `a metric does not include a name` + missingMetricDesc = `metric "%s" does not include a description"` + missingMetricUnit = `metric "%s" does not include a unit"` + missingMetricMetricType = `metric "%s" does not include a metric type"` + missingMetrics = `must specify at least one metric` ) testCases := []testCase{ @@ -110,15 +154,36 @@ func TestLoadConfig_Error(t *testing.T) { cfgFile: "config-emptyinstance.yaml", expectedErr: fmt.Sprintf("%s: %s", errorPrefix, fmt.Sprintf(emptyInstanceErr, "object")), }, + { + name: "EmptyMetricDescription", + cfgFile: "config-emptymetricdesc.yaml", + expectedErr: fmt.Sprintf("%s: %s", errorPrefix, fmt.Sprintf(missingMetricDesc, "metric")), + }, + { + name: "EmptyMetricUnit", + cfgFile: "config-emptymetricunit.yaml", + expectedErr: fmt.Sprintf("%s: %s", errorPrefix, fmt.Sprintf(missingMetricUnit, "metric")), + }, + { + name: "EmptyMetricMetricType", + cfgFile: "config-emptymetricdesc.yaml", + expectedErr: fmt.Sprintf("%s: %s", errorPrefix, fmt.Sprintf(missingMetricMetricType, "metric")), + }, + { + name: "EmptyMetricName", + cfgFile: "config-emptymetricdesc.yaml", + expectedErr: fmt.Sprintf("%s: %s", errorPrefix, missingMetricName), + }, { name: "AllErrors", cfgFile: "config-allerrors.yaml", expectedErr: fmt.Sprintf( - "%s: %s; %s; %s; %s", + "%s: %s; %s; %s; %s; %s", errorPrefix, negativeCollectionIntervalErr, - fmt.Sprintf(emptyInstanceErr, "object"), + missingMetrics, fmt.Sprintf(noCountersErr, "object"), + fmt.Sprintf(emptyInstanceErr, "object"), noObjectNameErr, ), }, diff --git a/receiver/windowsperfcountersreceiver/factory_others_test.go b/receiver/windowsperfcountersreceiver/factory_others_test.go index babbf2069dfd..a3341cd9fe46 100644 --- a/receiver/windowsperfcountersreceiver/factory_others_test.go +++ b/receiver/windowsperfcountersreceiver/factory_others_test.go @@ -28,8 +28,23 @@ import ( func TestCreateMetricsReceiver(t *testing.T) { factory := NewFactory() cfg := factory.CreateDefaultConfig() - cfg.(*Config).PerfCounters = []PerfCounterConfig{{Object: "object", Counters: []string{"counter"}}} + cfg.(*Config).PerfCounters = []PerfCounterConfig{ + { + Object: "object", + Counters: []CounterConfig{{CounterName: "counter", MetricName: "metric"}}, + }, + } + cfg.(*Config).MetricMetaData = []MetricConfig{ + { + MetricName: "metric", + Description: "desc", + Unit: "1", + Gauge: GaugeMetric{ + ValueType: "double", + }, + }, + } mReceiver, err := factory.CreateMetricsReceiver(context.Background(), creationParams, cfg, consumertest.NewNop()) assert.EqualError(t, err, "the windows perf counters receiver is only supported on Windows") diff --git a/receiver/windowsperfcountersreceiver/factory_test.go b/receiver/windowsperfcountersreceiver/factory_test.go index 23e9b2d3ad88..21b7b85d2a58 100644 --- a/receiver/windowsperfcountersreceiver/factory_test.go +++ b/receiver/windowsperfcountersreceiver/factory_test.go @@ -34,7 +34,23 @@ func TestCreateDefaultConfig(t *testing.T) { assert.NotNil(t, cfg, "failed to create default config") assert.NoError(t, configtest.CheckConfigStruct(cfg)) - cfg.(*Config).PerfCounters = []PerfCounterConfig{{Object: "object", Counters: []string{"counter"}}} + cfg.(*Config).PerfCounters = []PerfCounterConfig{ + { + Object: "object", + Counters: []CounterConfig{{CounterName: "counter", MetricName: "metric"}}, + }, + } + + cfg.(*Config).MetricMetaData = []MetricConfig{ + { + MetricName: "metric", + Description: "desc", + Unit: "1", + Gauge: GaugeMetric{ + ValueType: "double", + }, + }, + } assert.NoError(t, cfg.Validate()) } @@ -42,8 +58,23 @@ func TestCreateDefaultConfig(t *testing.T) { func TestCreateTracesReceiver(t *testing.T) { factory := NewFactory() cfg := factory.CreateDefaultConfig() - cfg.(*Config).PerfCounters = []PerfCounterConfig{{Object: "object", Counters: []string{"counter"}}} + cfg.(*Config).PerfCounters = []PerfCounterConfig{ + { + Object: "object", + Counters: []CounterConfig{{CounterName: "counter", MetricName: "metric"}}, + }, + } + cfg.(*Config).MetricMetaData = []MetricConfig{ + { + MetricName: "metric", + Description: "desc", + Unit: "1", + Gauge: GaugeMetric{ + ValueType: "double", + }, + }, + } tReceiver, err := factory.CreateTracesReceiver(context.Background(), creationParams, cfg, consumertest.NewNop()) assert.ErrorIs(t, err, componenterror.ErrDataTypeIsNotSupported) @@ -53,7 +84,23 @@ func TestCreateTracesReceiver(t *testing.T) { func TestCreateLogsReceiver(t *testing.T) { factory := NewFactory() cfg := factory.CreateDefaultConfig() - cfg.(*Config).PerfCounters = []PerfCounterConfig{{Object: "object", Counters: []string{"counter"}}} + cfg.(*Config).PerfCounters = []PerfCounterConfig{ + { + Object: "object", + Counters: []CounterConfig{{CounterName: "counter", MetricName: "metric"}}, + }, + } + + cfg.(*Config).MetricMetaData = []MetricConfig{ + { + MetricName: "metric", + Description: "desc", + Unit: "1", + Gauge: GaugeMetric{ + ValueType: "double", + }, + }, + } tReceiver, err := factory.CreateLogsReceiver(context.Background(), creationParams, cfg, consumertest.NewNop()) diff --git a/receiver/windowsperfcountersreceiver/factory_windows_test.go b/receiver/windowsperfcountersreceiver/factory_windows_test.go index 00c824d21685..39c7b8c4550b 100644 --- a/receiver/windowsperfcountersreceiver/factory_windows_test.go +++ b/receiver/windowsperfcountersreceiver/factory_windows_test.go @@ -28,7 +28,23 @@ import ( func TestCreateMetricsReceiver(t *testing.T) { factory := NewFactory() cfg := factory.CreateDefaultConfig() - cfg.(*Config).PerfCounters = []PerfCounterConfig{{Object: "object", Counters: []string{"counter"}}} + cfg.(*Config).PerfCounters = []PerfCounterConfig{ + { + Object: "object", + Counters: []CounterConfig{{CounterName: "counter", MetricName: "metric"}}, + }, + } + + cfg.(*Config).MetricMetaData = []MetricConfig{ + { + MetricName: "metric", + Description: "desc", + Unit: "1", + Gauge: GaugeMetric{ + ValueType: "double", + }, + }, + } mReceiver, err := factory.CreateMetricsReceiver(context.Background(), creationParams, cfg, consumertest.NewNop()) diff --git a/receiver/windowsperfcountersreceiver/testdata/config-emptyinstance.yaml b/receiver/windowsperfcountersreceiver/testdata/config-emptyinstance.yaml index c39318a307dc..b995b99786dc 100644 --- a/receiver/windowsperfcountersreceiver/testdata/config-emptyinstance.yaml +++ b/receiver/windowsperfcountersreceiver/testdata/config-emptyinstance.yaml @@ -1,10 +1,17 @@ receivers: windowsperfcounters: + metrics: + - metric_name: metric + description: desc + unit: "1" + metric_data_type: gauge + metric_value_type: double perfcounters: - object: "object" instances: [""] counters: - - "counter" + - counter_name: counter + metric_name: metric processors: nop: diff --git a/receiver/windowsperfcountersreceiver/testdata/config-negative-collection-interval.yaml b/receiver/windowsperfcountersreceiver/testdata/config-negative-collection-interval.yaml index e682561b55bc..8ee2d3d2be2f 100644 --- a/receiver/windowsperfcountersreceiver/testdata/config-negative-collection-interval.yaml +++ b/receiver/windowsperfcountersreceiver/testdata/config-negative-collection-interval.yaml @@ -1,10 +1,17 @@ receivers: windowsperfcounters: + metrics: + - metric_name: metric + description: desc + unit: "1" + metric_data_type: gauge + metric_value_type: double collection_interval: -1m perfcounters: - object: "object" counters: - - "counter" + - counter_name: counter1 + metric_name: metric processors: nop: diff --git a/receiver/windowsperfcountersreceiver/testdata/config-nocounters.yaml b/receiver/windowsperfcountersreceiver/testdata/config-nocounters.yaml index b93aa0e23eee..d739301defd8 100644 --- a/receiver/windowsperfcountersreceiver/testdata/config-nocounters.yaml +++ b/receiver/windowsperfcountersreceiver/testdata/config-nocounters.yaml @@ -1,5 +1,11 @@ receivers: windowsperfcounters: + metrics: + - metric_name: metric + description: desc + unit: "1" + metric_data_type: gauge + metric_value_type: double perfcounters: - object: "object" diff --git a/receiver/windowsperfcountersreceiver/testdata/config-noobjectname.yaml b/receiver/windowsperfcountersreceiver/testdata/config-noobjectname.yaml index 61719356b62f..20420efb5531 100644 --- a/receiver/windowsperfcountersreceiver/testdata/config-noobjectname.yaml +++ b/receiver/windowsperfcountersreceiver/testdata/config-noobjectname.yaml @@ -1,8 +1,15 @@ receivers: windowsperfcounters: + metrics: + - metric_name: metric + description: desc + unit: "1" + metric_data_type: gauge + metric_value_type: double perfcounters: - counters: - - "counter" + + processors: nop: diff --git a/receiver/windowsperfcountersreceiver/testdata/config-noperfcounters.yaml b/receiver/windowsperfcountersreceiver/testdata/config-noperfcounters.yaml index ff272819dd27..71560c586e58 100644 --- a/receiver/windowsperfcountersreceiver/testdata/config-noperfcounters.yaml +++ b/receiver/windowsperfcountersreceiver/testdata/config-noperfcounters.yaml @@ -1,6 +1,11 @@ receivers: windowsperfcounters: - + metrics: + - metric_name: metric + description: desc + unit: "1" + metric_data_type: gauge + metric_value_type: double processors: nop: diff --git a/receiver/windowsperfcountersreceiver/testdata/config.yaml b/receiver/windowsperfcountersreceiver/testdata/config.yaml index ab2b288e6325..b5a9234b58ac 100644 --- a/receiver/windowsperfcountersreceiver/testdata/config.yaml +++ b/receiver/windowsperfcountersreceiver/testdata/config.yaml @@ -1,19 +1,40 @@ receivers: windowsperfcounters: + metric_metadata: + - metric_name: metric + description: desc + unit: "1" + gauge: + value_type: double perfcounters: - object: "object" counters: - - "counter" + - counter_name: counter1 + metric_name: metric windowsperfcounters/customname: + metric_metadata: + - metric_name: metric + description: desc + unit: "1" + gauge: + value_type: double + - metric_name: metric2 + description: desc + unit: "1" + gauge: + value_type: double collection_interval: 30s perfcounters: - - object: "object1" + - object: object1 counters: - - "counter1" - - object: "object2" + - counter_name: counter1 + metric_name: metric + - object: object2 counters: - - "counter1" - - "counter2" + - counter_name: counter1 + metric_name: metric + - counter_name: counter2 + metric_name: metric2 processors: nop: diff --git a/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper.go b/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper.go index ce450f28c488..cebdf3c71953 100644 --- a/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper.go +++ b/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper.go @@ -46,7 +46,19 @@ type PerfCounterScraper interface { type scraper struct { cfg *Config logger *zap.Logger - counters []PerfCounterScraper + counters []PerfCounterMetrics +} + +type PerfCounterMetrics struct { + CounterScraper PerfCounterScraper + Attributes map[string]string + Metric string +} + +type PerfCounterScrapedMetrics struct { + CounterScraper PerfCounterScraper + Attributes map[string]string + Metric string } func newScraper(cfg *Config, logger *zap.Logger) (*scraper, error) { @@ -63,14 +75,14 @@ func (s *scraper) start(context.Context, component.Host) error { for _, perfCounterCfg := range s.cfg.PerfCounters { for _, instance := range perfCounterCfg.instances() { - for _, counterName := range perfCounterCfg.Counters { - counterPath := counterPath(perfCounterCfg.Object, instance, counterName) + for _, counterCfg := range perfCounterCfg.Counters { + counterPath := counterPath(perfCounterCfg.Object, instance, counterCfg.CounterName) c, err := pdh.NewPerfCounter(counterPath, true) if err != nil { errs = multierr.Append(errs, fmt.Errorf("counter %v: %w", counterPath, err)) } else { - s.counters = append(s.counters, c) + s.counters = append(s.counters, PerfCounterMetrics{CounterScraper: c, Metric: counterCfg.MetricName, Attributes: counterCfg.Attributes}) } } } @@ -96,7 +108,7 @@ func (s *scraper) shutdown(context.Context) error { var errs error for _, counter := range s.counters { - errs = multierr.Append(errs, counter.Close()) + errs = multierr.Append(errs, counter.CounterScraper.Close()) } return errs @@ -106,40 +118,76 @@ func (s *scraper) scrape(context.Context) (pdata.Metrics, error) { md := pdata.NewMetrics() metrics := md.ResourceMetrics().AppendEmpty().InstrumentationLibraryMetrics().AppendEmpty().Metrics() now := pdata.NewTimestampFromTime(time.Now()) - var errs error metrics.EnsureCapacity(len(s.counters)) - for _, counter := range s.counters { - counterValues, err := counter.ScrapeData() - if err != nil { - errs = multierr.Append(errs, err) - continue + + for _, metricCfg := range s.cfg.MetricMetaData { + builtMetric := metrics.AppendEmpty() + + builtMetric.SetName(metricCfg.MetricName) + builtMetric.SetDescription(metricCfg.Description) + builtMetric.SetUnit(metricCfg.Unit) + + if (metricCfg.Gauge != GaugeMetric{}) { + builtMetric.SetDataType(pdata.MetricDataTypeGauge) + } else if (metricCfg.Sum != SumMetric{}) { + builtMetric.SetDataType(pdata.MetricDataTypeSum) + builtMetric.Sum().SetIsMonotonic(metricCfg.Sum.Monotonic) + + switch metricCfg.Sum.Aggregation { + case "cumulative": + builtMetric.Sum().SetAggregationTemporality(pdata.MetricAggregationTemporalityCumulative) + case "delta": + builtMetric.Sum().SetAggregationTemporality(pdata.MetricAggregationTemporalityDelta) + default: + builtMetric.Sum().SetAggregationTemporality(pdata.MetricAggregationTemporalityCumulative) + } } - initializeDoubleGaugeMetric(metrics.AppendEmpty(), now, counter.Path(), counterValues) + for _, counter := range s.counters { + if counter.Metric == builtMetric.Name() { + counterValues, err := counter.CounterScraper.ScrapeData() + if err != nil { + errs = multierr.Append(errs, err) + continue + } + initializeMetricDps(metricCfg, builtMetric, now, counterValues, counter.Attributes) + } + } } return md, errs } -func initializeDoubleGaugeMetric(metric pdata.Metric, now pdata.Timestamp, name string, counterValues []win_perf_counters.CounterValue) { - metric.SetName(name) - metric.SetDataType(pdata.MetricDataTypeGauge) +func initializeMetricDps(metricCfg MetricConfig, metric pdata.Metric, now pdata.Timestamp, counterValues []win_perf_counters.CounterValue, attributes map[string]string) { + var dps pdata.NumberDataPointSlice + var valueType string - dg := metric.Gauge() - ddps := dg.DataPoints() - ddps.EnsureCapacity(len(counterValues)) - for _, counterValue := range counterValues { - initializeNumberDataPointAsDouble(ddps.AppendEmpty(), now, counterValue.InstanceName, counterValue.Value) + if metric.DataType() == pdata.MetricDataTypeGauge { + dps = metric.Gauge().DataPoints() + valueType = metricCfg.Gauge.ValueType + } else { + dps = metric.Sum().DataPoints() + valueType = metricCfg.Sum.ValueType } -} -func initializeNumberDataPointAsDouble(dataPoint pdata.NumberDataPoint, now pdata.Timestamp, instanceLabel string, value float64) { - if instanceLabel != "" { - dataPoint.Attributes().InsertString(instanceLabelName, instanceLabel) - } + dps.EnsureCapacity(len(counterValues)) + for _, counterValue := range counterValues { + dp := dps.AppendEmpty() - dataPoint.SetTimestamp(now) - dataPoint.SetDoubleVal(value) + for attKey, attVal := range attributes { + dp.Attributes().InsertString(attKey, attVal) + } + if counterValue.InstanceName != "" { + dp.Attributes().InsertString(instanceLabelName, counterValue.InstanceName) + } + + dp.SetTimestamp(now) + if valueType == "int" { + dp.SetIntVal(int64(counterValue.Value)) + } else if valueType == "double" { + dp.SetDoubleVal(counterValue.Value) + } + } } diff --git a/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper_test.go b/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper_test.go index 504eaae3341f..5624e8f96bdd 100644 --- a/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper_test.go +++ b/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper_test.go @@ -81,7 +81,20 @@ func Test_WindowsPerfCounterScraper(t *testing.T) { defaultConfig := &Config{ PerfCounters: []PerfCounterConfig{ - {Object: "Memory", Counters: []string{"Committed Bytes"}}, + { + Object: "object", + Counters: []CounterConfig{{CounterName: "Committed Bytes", MetricName: "commited.bytes"}}, + }, + }, + MetricMetaData: []MetricConfig{ + { + MetricName: "metric", + Description: "desc", + Unit: "1", + Gauge: GaugeMetric{ + ValueType: "double", + }, + }, }, ScraperControllerSettings: scraperhelper.ScraperControllerSettings{CollectionInterval: time.Minute}, } @@ -91,17 +104,17 @@ func Test_WindowsPerfCounterScraper(t *testing.T) { name: "Standard", cfg: &Config{ PerfCounters: []PerfCounterConfig{ - {Object: "Memory", Counters: []string{"Committed Bytes"}}, - {Object: "Processor", Instances: []string{"*"}, Counters: []string{"% Processor Time"}}, - {Object: "Processor", Instances: []string{"1", "2"}, Counters: []string{"% Idle Time"}}, + {Object: "Memory", Counters: []CounterConfig{{CounterName: "Committed Bytes", MetricName: "Committed Bytes"}}}, + {Object: "Processor", Instances: []string{"*"}, Counters: []CounterConfig{{CounterName: "% Idle Time", MetricName: "cpu.idle"}}}, + {Object: "Processor", Instances: []string{"1", "2"}, Counters: []CounterConfig{{CounterName: "% Processor Time", MetricName: "bytes.commited"}}}, }, ScraperControllerSettings: scraperhelper.ScraperControllerSettings{CollectionInterval: time.Minute}, }, expectedMetrics: []expectedMetric{ {name: `\Memory\Committed Bytes`}, - {name: `\Processor(*)\% Processor Time`, instanceLabelValues: []string{"*"}}, - {name: `\Processor(1)\% Idle Time`, instanceLabelValues: []string{"1"}}, - {name: `\Processor(2)\% Idle Time`, instanceLabelValues: []string{"2"}}, + {name: `cpu.idle`, instanceLabelValues: []string{"*"}}, + {name: `bytes.commited"`, instanceLabelValues: []string{"1"}}, + {name: `bytes.commited"`, instanceLabelValues: []string{"2"}}, }, }, { @@ -110,11 +123,11 @@ func Test_WindowsPerfCounterScraper(t *testing.T) { PerfCounters: []PerfCounterConfig{ { Object: "Memory", - Counters: []string{"Committed Bytes"}, + Counters: []CounterConfig{{CounterName: "Committed Bytes", MetricName: "Committed Bytes"}}, }, { Object: "Invalid Object", - Counters: []string{"Invalid Counter"}, + Counters: []CounterConfig{{CounterName: "Invalid Counter", MetricName: "invalid"}}, }, }, ScraperControllerSettings: scraperhelper.ScraperControllerSettings{CollectionInterval: time.Minute}, @@ -163,7 +176,7 @@ func Test_WindowsPerfCounterScraper(t *testing.T) { if test.mockCounterPath != "" || test.scrapeErr != nil || test.shutdownErr != nil { for i := range scraper.counters { - scraper.counters[i] = newMockPerfCounter(test.mockCounterPath, test.scrapeErr, test.shutdownErr) + scraper.counters[i].CounterScraper = newMockPerfCounter(test.mockCounterPath, test.scrapeErr, test.shutdownErr) } } From 30e280dbc93ffe26f0d438269575ede49346c594 Mon Sep 17 00:00:00 2001 From: Miguel Rodriguez Date: Wed, 9 Mar 2022 20:34:50 -0500 Subject: [PATCH 02/24] Update tests --- .../windowsperfcountersreceiver/config.go | 28 +++ .../config_test.go | 29 ++- .../factory_windows.go | 5 +- receiver/windowsperfcountersreceiver/go.mod | 1 + receiver/windowsperfcountersreceiver/go.sum | 2 + .../testdata/config-emptyinstance.yaml | 12 +- .../testdata/config-missingdatatype.yaml | 24 +++ .../config-missinggaugevaluetype.yaml | 26 +++ .../config-missingmetricdescription.yaml | 25 +++ .../testdata/config-missingmetricname.yaml | 25 +++ .../testdata/config-missingmetricunit.yaml | 25 +++ .../config-missingsumaggregation.yaml | 26 +++ .../testdata/config-missingsumvaluetype.yaml | 26 +++ .../config-negative-collection-interval.yaml | 6 +- .../testdata/config-nocounters.yaml | 12 +- .../testdata/config-noobjectname.yaml | 12 +- .../testdata/config-noperfcounters.yaml | 6 +- .../testdata/scraper/standard.json | 165 ++++++++++++++++ .../testdata/scraper/sum_metric.json | 28 +++ .../windowsperfcounters_scraper.go | 14 +- .../windowsperfcounters_scraper_test.go | 176 +++++++++--------- 21 files changed, 535 insertions(+), 138 deletions(-) create mode 100644 receiver/windowsperfcountersreceiver/testdata/config-missingdatatype.yaml create mode 100644 receiver/windowsperfcountersreceiver/testdata/config-missinggaugevaluetype.yaml create mode 100644 receiver/windowsperfcountersreceiver/testdata/config-missingmetricdescription.yaml create mode 100644 receiver/windowsperfcountersreceiver/testdata/config-missingmetricname.yaml create mode 100644 receiver/windowsperfcountersreceiver/testdata/config-missingmetricunit.yaml create mode 100644 receiver/windowsperfcountersreceiver/testdata/config-missingsumaggregation.yaml create mode 100644 receiver/windowsperfcountersreceiver/testdata/config-missingsumvaluetype.yaml create mode 100644 receiver/windowsperfcountersreceiver/testdata/scraper/standard.json create mode 100644 receiver/windowsperfcountersreceiver/testdata/scraper/sum_metric.json diff --git a/receiver/windowsperfcountersreceiver/config.go b/receiver/windowsperfcountersreceiver/config.go index 38f9f9ecde42..a715c6608b51 100644 --- a/receiver/windowsperfcountersreceiver/config.go +++ b/receiver/windowsperfcountersreceiver/config.go @@ -76,6 +76,34 @@ func (c *Config) Validate() error { errs = multierr.Append(errs, fmt.Errorf("must specify at least one metric")) } + for _, metric := range c.MetricMetaData { + if metric.MetricName == "" { + errs = multierr.Append(errs, fmt.Errorf("a metric does not include a name")) + continue + } + if metric.Description == "" { + errs = multierr.Append(errs, fmt.Errorf("metric %q does not include a description", metric.MetricName)) + } + if metric.Unit == "" { + errs = multierr.Append(errs, fmt.Errorf("metric %q does not include a unit", metric.MetricName)) + } + + if (metric.Gauge == GaugeMetric{}) && (metric.Sum == SumMetric{}) { + errs = multierr.Append(errs, fmt.Errorf("metric %q does not include a metric definition", metric.MetricName)) + } else if (metric.Gauge != GaugeMetric{}) { + if metric.Gauge.ValueType == "" { + errs = multierr.Append(errs, fmt.Errorf("gauge metric %q does not include a value type", metric.MetricName)) + } + } else if (metric.Sum != SumMetric{}) { + if metric.Sum.ValueType == "" { + errs = multierr.Append(errs, fmt.Errorf("sum metric %q does not include a value type", metric.MetricName)) + } + if metric.Sum.Aggregation == "" { + errs = multierr.Append(errs, fmt.Errorf("sum metric %q does not include an aggregation", metric.MetricName)) + } + } + } + var perfCounterMissingObjectName bool for _, pc := range c.PerfCounters { if pc.Object == "" { diff --git a/receiver/windowsperfcountersreceiver/config_test.go b/receiver/windowsperfcountersreceiver/config_test.go index d1e1c4df76f9..310b3d82c5eb 100644 --- a/receiver/windowsperfcountersreceiver/config_test.go +++ b/receiver/windowsperfcountersreceiver/config_test.go @@ -122,9 +122,12 @@ func TestLoadConfig_Error(t *testing.T) { emptyInstanceErr = `perf counter for object "%s" includes an empty instance` undefinedMetricErr = `perf counter for object "%s" includes an undefined metric` missingMetricName = `a metric does not include a name` - missingMetricDesc = `metric "%s" does not include a description"` - missingMetricUnit = `metric "%s" does not include a unit"` - missingMetricMetricType = `metric "%s" does not include a metric type"` + missingMetricDesc = `metric "%s" does not include a description` + missingMetricUnit = `metric "%s" does not include a unit` + missingMetricMetricType = `metric "%s" does not include a metric definition` + missingGaugeValueType = `gauge metric "%s" does not include a value type` + missingSumValueType = `sum metric "%s" does not include a value type` + missingSumAggregation = `sum metric "%s" does not include an aggregation` missingMetrics = `must specify at least one metric` ) @@ -156,23 +159,33 @@ func TestLoadConfig_Error(t *testing.T) { }, { name: "EmptyMetricDescription", - cfgFile: "config-emptymetricdesc.yaml", + cfgFile: "config-missingmetricdescription.yaml", expectedErr: fmt.Sprintf("%s: %s", errorPrefix, fmt.Sprintf(missingMetricDesc, "metric")), }, { name: "EmptyMetricUnit", - cfgFile: "config-emptymetricunit.yaml", + cfgFile: "config-missingmetricunit.yaml", expectedErr: fmt.Sprintf("%s: %s", errorPrefix, fmt.Sprintf(missingMetricUnit, "metric")), }, { name: "EmptyMetricMetricType", - cfgFile: "config-emptymetricdesc.yaml", + cfgFile: "config-missingdatatype.yaml", expectedErr: fmt.Sprintf("%s: %s", errorPrefix, fmt.Sprintf(missingMetricMetricType, "metric")), }, { name: "EmptyMetricName", - cfgFile: "config-emptymetricdesc.yaml", - expectedErr: fmt.Sprintf("%s: %s", errorPrefix, missingMetricName), + cfgFile: "config-missingmetricname.yaml", + expectedErr: fmt.Sprintf("%s: %s; %s", errorPrefix, missingMetricName, fmt.Sprintf(undefinedMetricErr, "object")), + }, + { + name: "EmptySumValueType", + cfgFile: "config-missingsumvaluetype.yaml", + expectedErr: fmt.Sprintf("%s: %s", errorPrefix, fmt.Sprintf(missingSumValueType, "metric")), + }, + { + name: "EmptySumAggregation", + cfgFile: "config-missingsumaggregation.yaml", + expectedErr: fmt.Sprintf("%s: %s", errorPrefix, fmt.Sprintf(missingSumAggregation, "metric")), }, { name: "AllErrors", diff --git a/receiver/windowsperfcountersreceiver/factory_windows.go b/receiver/windowsperfcountersreceiver/factory_windows.go index fca7bd0e19f9..a7c932b835fb 100644 --- a/receiver/windowsperfcountersreceiver/factory_windows.go +++ b/receiver/windowsperfcountersreceiver/factory_windows.go @@ -34,10 +34,7 @@ func createMetricsReceiver( consumer consumer.Metrics, ) (component.MetricsReceiver, error) { oCfg := cfg.(*Config) - scraper, err := newScraper(oCfg, params.Logger) - if err != nil { - return nil, err - } + scraper := newScraper(oCfg, params.TelemetrySettings) scrp, err := scraperhelper.NewScraper( cfg.ID().String(), diff --git a/receiver/windowsperfcountersreceiver/go.mod b/receiver/windowsperfcountersreceiver/go.mod index 17e2bcef9117..7c669c961073 100644 --- a/receiver/windowsperfcountersreceiver/go.mod +++ b/receiver/windowsperfcountersreceiver/go.mod @@ -21,6 +21,7 @@ require ( github.com/mitchellh/copystructure v1.2.0 // indirect github.com/mitchellh/mapstructure v1.4.3 // indirect github.com/mitchellh/reflectwalk v1.0.2 // indirect + github.com/open-telemetry/opentelemetry-collector-contrib/internal/scrapertest v0.46.0 github.com/pelletier/go-toml v1.9.4 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect diff --git a/receiver/windowsperfcountersreceiver/go.sum b/receiver/windowsperfcountersreceiver/go.sum index 2e717f43e5c2..fb6bec78a2b7 100644 --- a/receiver/windowsperfcountersreceiver/go.sum +++ b/receiver/windowsperfcountersreceiver/go.sum @@ -131,6 +131,8 @@ github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx github.com/mostynb/go-grpc-compression v1.1.16 h1:D9tGUINmcII049pxOj9dl32Fzhp26TrDVQXECoKJqQg= github.com/npillmayer/nestext v0.1.3/go.mod h1:h2lrijH8jpicr25dFY+oAJLyzlya6jhnuG+zWp9L0Uk= github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA= +github.com/open-telemetry/opentelemetry-collector-contrib/internal/scrapertest v0.46.0 h1:UfROTTu9qppkAX92iRdVXpre5/b9mr98nebVFAo2Tag= +github.com/open-telemetry/opentelemetry-collector-contrib/internal/scrapertest v0.46.0/go.mod h1:I9hcw9At1o33hIfcmkzo4AOPY7vEK92YDxEeU5DbOew= github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= github.com/pelletier/go-toml v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE= github.com/pelletier/go-toml v1.9.4 h1:tjENF6MfZAg8e4ZmZTeWaWiT2vXtsoO6+iuOjFhECwM= diff --git a/receiver/windowsperfcountersreceiver/testdata/config-emptyinstance.yaml b/receiver/windowsperfcountersreceiver/testdata/config-emptyinstance.yaml index b995b99786dc..b2c7ecbdc856 100644 --- a/receiver/windowsperfcountersreceiver/testdata/config-emptyinstance.yaml +++ b/receiver/windowsperfcountersreceiver/testdata/config-emptyinstance.yaml @@ -1,11 +1,11 @@ receivers: windowsperfcounters: - metrics: - - metric_name: metric - description: desc - unit: "1" - metric_data_type: gauge - metric_value_type: double + metric_metadata: + - metric_name: metric + description: desc + unit: "1" + gauge: + value_type: double perfcounters: - object: "object" instances: [""] diff --git a/receiver/windowsperfcountersreceiver/testdata/config-missingdatatype.yaml b/receiver/windowsperfcountersreceiver/testdata/config-missingdatatype.yaml new file mode 100644 index 000000000000..aca7bbfbaff9 --- /dev/null +++ b/receiver/windowsperfcountersreceiver/testdata/config-missingdatatype.yaml @@ -0,0 +1,24 @@ +receivers: + windowsperfcounters: + metric_metadata: + - metric_name: metric + description: desc + unit: "1" + perfcounters: + - object: "object" + counters: + - counter_name: counter1 + metric_name: metric + +processors: + nop: + +exporters: + nop: + +service: + pipelines: + metrics: + receivers: [windowsperfcounters] + processors: [nop] + exporters: [nop] diff --git a/receiver/windowsperfcountersreceiver/testdata/config-missinggaugevaluetype.yaml b/receiver/windowsperfcountersreceiver/testdata/config-missinggaugevaluetype.yaml new file mode 100644 index 000000000000..20a8ff8b0aee --- /dev/null +++ b/receiver/windowsperfcountersreceiver/testdata/config-missinggaugevaluetype.yaml @@ -0,0 +1,26 @@ +receivers: + windowsperfcounters: + metric_metadata: + - metric_name: metric + description: desc + unit: "1" + gauge: + value_type: "" + perfcounters: + - object: "object" + counters: + - counter_name: counter1 + metric_name: metric + +processors: + nop: + +exporters: + nop: + +service: + pipelines: + metrics: + receivers: [windowsperfcounters] + processors: [nop] + exporters: [nop] diff --git a/receiver/windowsperfcountersreceiver/testdata/config-missingmetricdescription.yaml b/receiver/windowsperfcountersreceiver/testdata/config-missingmetricdescription.yaml new file mode 100644 index 000000000000..ccbc234da7c5 --- /dev/null +++ b/receiver/windowsperfcountersreceiver/testdata/config-missingmetricdescription.yaml @@ -0,0 +1,25 @@ +receivers: + windowsperfcounters: + metric_metadata: + - metric_name: metric + unit: "1" + gauge: + value_type: double + perfcounters: + - object: "object" + counters: + - counter_name: counter1 + metric_name: metric + +processors: + nop: + +exporters: + nop: + +service: + pipelines: + metrics: + receivers: [windowsperfcounters] + processors: [nop] + exporters: [nop] diff --git a/receiver/windowsperfcountersreceiver/testdata/config-missingmetricname.yaml b/receiver/windowsperfcountersreceiver/testdata/config-missingmetricname.yaml new file mode 100644 index 000000000000..873a32054f11 --- /dev/null +++ b/receiver/windowsperfcountersreceiver/testdata/config-missingmetricname.yaml @@ -0,0 +1,25 @@ +receivers: + windowsperfcounters: + metric_metadata: + - description: desc + unit: "1" + gauge: + value_type: double + perfcounters: + - object: "object" + counters: + - counter_name: counter1 + metric_name: metric + +processors: + nop: + +exporters: + nop: + +service: + pipelines: + metrics: + receivers: [windowsperfcounters] + processors: [nop] + exporters: [nop] diff --git a/receiver/windowsperfcountersreceiver/testdata/config-missingmetricunit.yaml b/receiver/windowsperfcountersreceiver/testdata/config-missingmetricunit.yaml new file mode 100644 index 000000000000..3fb9b3025127 --- /dev/null +++ b/receiver/windowsperfcountersreceiver/testdata/config-missingmetricunit.yaml @@ -0,0 +1,25 @@ +receivers: + windowsperfcounters: + metric_metadata: + - metric_name: metric + description: desc + gauge: + value_type: double + perfcounters: + - object: "object" + counters: + - counter_name: counter1 + metric_name: metric + +processors: + nop: + +exporters: + nop: + +service: + pipelines: + metrics: + receivers: [windowsperfcounters] + processors: [nop] + exporters: [nop] diff --git a/receiver/windowsperfcountersreceiver/testdata/config-missingsumaggregation.yaml b/receiver/windowsperfcountersreceiver/testdata/config-missingsumaggregation.yaml new file mode 100644 index 000000000000..74faea98f411 --- /dev/null +++ b/receiver/windowsperfcountersreceiver/testdata/config-missingsumaggregation.yaml @@ -0,0 +1,26 @@ +receivers: + windowsperfcounters: + metric_metadata: + - metric_name: metric + description: desc + unit: "1" + sum: + value_type: "double" + perfcounters: + - object: "object" + counters: + - counter_name: counter1 + metric_name: metric + +processors: + nop: + +exporters: + nop: + +service: + pipelines: + metrics: + receivers: [windowsperfcounters] + processors: [nop] + exporters: [nop] diff --git a/receiver/windowsperfcountersreceiver/testdata/config-missingsumvaluetype.yaml b/receiver/windowsperfcountersreceiver/testdata/config-missingsumvaluetype.yaml new file mode 100644 index 000000000000..7419e4ebd42a --- /dev/null +++ b/receiver/windowsperfcountersreceiver/testdata/config-missingsumvaluetype.yaml @@ -0,0 +1,26 @@ +receivers: + windowsperfcounters: + metric_metadata: + - metric_name: metric + description: desc + unit: "1" + sum: + aggregation: "cumulative" + perfcounters: + - object: "object" + counters: + - counter_name: counter1 + metric_name: metric + +processors: + nop: + +exporters: + nop: + +service: + pipelines: + metrics: + receivers: [windowsperfcounters] + processors: [nop] + exporters: [nop] diff --git a/receiver/windowsperfcountersreceiver/testdata/config-negative-collection-interval.yaml b/receiver/windowsperfcountersreceiver/testdata/config-negative-collection-interval.yaml index 8ee2d3d2be2f..71d5424b067d 100644 --- a/receiver/windowsperfcountersreceiver/testdata/config-negative-collection-interval.yaml +++ b/receiver/windowsperfcountersreceiver/testdata/config-negative-collection-interval.yaml @@ -1,11 +1,11 @@ receivers: windowsperfcounters: - metrics: + metric_metadata: - metric_name: metric description: desc unit: "1" - metric_data_type: gauge - metric_value_type: double + gauge: + value_type: double collection_interval: -1m perfcounters: - object: "object" diff --git a/receiver/windowsperfcountersreceiver/testdata/config-nocounters.yaml b/receiver/windowsperfcountersreceiver/testdata/config-nocounters.yaml index d739301defd8..d56cb88a2b54 100644 --- a/receiver/windowsperfcountersreceiver/testdata/config-nocounters.yaml +++ b/receiver/windowsperfcountersreceiver/testdata/config-nocounters.yaml @@ -1,11 +1,11 @@ receivers: windowsperfcounters: - metrics: - - metric_name: metric - description: desc - unit: "1" - metric_data_type: gauge - metric_value_type: double + metric_metadata: + - metric_name: metric + description: desc + unit: "1" + gauge: + value_type: double perfcounters: - object: "object" diff --git a/receiver/windowsperfcountersreceiver/testdata/config-noobjectname.yaml b/receiver/windowsperfcountersreceiver/testdata/config-noobjectname.yaml index 20420efb5531..76e5cb957ee1 100644 --- a/receiver/windowsperfcountersreceiver/testdata/config-noobjectname.yaml +++ b/receiver/windowsperfcountersreceiver/testdata/config-noobjectname.yaml @@ -1,11 +1,11 @@ receivers: windowsperfcounters: - metrics: - - metric_name: metric - description: desc - unit: "1" - metric_data_type: gauge - metric_value_type: double + metric_metadata: + - metric_name: metric + description: desc + unit: "1" + gauge: + value_type: double perfcounters: - counters: diff --git a/receiver/windowsperfcountersreceiver/testdata/config-noperfcounters.yaml b/receiver/windowsperfcountersreceiver/testdata/config-noperfcounters.yaml index 71560c586e58..cdc6ea115a2d 100644 --- a/receiver/windowsperfcountersreceiver/testdata/config-noperfcounters.yaml +++ b/receiver/windowsperfcountersreceiver/testdata/config-noperfcounters.yaml @@ -1,11 +1,11 @@ receivers: windowsperfcounters: - metrics: + metric_metadata: - metric_name: metric description: desc unit: "1" - metric_data_type: gauge - metric_value_type: double + gauge: + value_type: double processors: nop: diff --git a/receiver/windowsperfcountersreceiver/testdata/scraper/standard.json b/receiver/windowsperfcountersreceiver/testdata/scraper/standard.json new file mode 100644 index 000000000000..3b05c056e2e7 --- /dev/null +++ b/receiver/windowsperfcountersreceiver/testdata/scraper/standard.json @@ -0,0 +1,165 @@ +{ + "resourceMetrics": [ + { + "instrumentationLibraryMetrics": [ + { + "instrumentationLibrary": {}, + "metrics": [ + { + "description": "percentage of time CPU is idle.", + "gauge": { + "dataPoints": [ + { + "asDouble": 0, + "attributes": [ + { + "key": "instance", + "value": { + "stringValue": "0" + } + } + ], + "timeUnixNano": "1646857199239674900" + }, + { + "asDouble": 0, + "attributes": [ + { + "key": "instance", + "value": { + "stringValue": "1" + } + } + ], + "timeUnixNano": "1646857199239674900" + }, + { + "asDouble": 0, + "attributes": [ + { + "key": "instance", + "value": { + "stringValue": "2" + } + } + ], + "timeUnixNano": "1646857199239674900" + }, + { + "asDouble": 0, + "attributes": [ + { + "key": "instance", + "value": { + "stringValue": "3" + } + } + ], + "timeUnixNano": "1646857199239674900" + }, + { + "asDouble": 0, + "attributes": [ + { + "key": "instance", + "value": { + "stringValue": "4" + } + } + ], + "timeUnixNano": "1646857199239674900" + }, + { + "asDouble": 0, + "attributes": [ + { + "key": "instance", + "value": { + "stringValue": "5" + } + } + ], + "timeUnixNano": "1646857199239674900" + }, + { + "asDouble": 0, + "attributes": [ + { + "key": "instance", + "value": { + "stringValue": "6" + } + } + ], + "timeUnixNano": "1646857199239674900" + }, + { + "asDouble": 0, + "attributes": [ + { + "key": "instance", + "value": { + "stringValue": "7" + } + } + ], + "timeUnixNano": "1646857199239674900" + } + ] + }, + "name": "cpu.idle", + "unit": "%" + }, + { + "description": "number of bytes commited to memory", + "gauge": { + "dataPoints": [ + { + "asDouble": 19516747776, + "timeUnixNano": "1646857199239674900" + } + ] + }, + "name": "bytes.commited", + "unit": "By" + }, + { + "description": "amount of time processor is busy", + "gauge": { + "dataPoints": [ + { + "asDouble": 0, + "attributes": [ + { + "key": "instance", + "value": { + "stringValue": "1" + } + } + ], + "timeUnixNano": "1646857199239674900" + }, + { + "asDouble": 0, + "attributes": [ + { + "key": "instance", + "value": { + "stringValue": "2" + } + } + ], + "timeUnixNano": "1646857199239674900" + } + ] + }, + "name": "processor.time", + "unit": "%" + } + ] + } + ], + "resource": {} + } + ] +} diff --git a/receiver/windowsperfcountersreceiver/testdata/scraper/sum_metric.json b/receiver/windowsperfcountersreceiver/testdata/scraper/sum_metric.json new file mode 100644 index 000000000000..cad1d52249a1 --- /dev/null +++ b/receiver/windowsperfcountersreceiver/testdata/scraper/sum_metric.json @@ -0,0 +1,28 @@ +{ + "resourceMetrics": [ + { + "instrumentationLibraryMetrics": [ + { + "instrumentationLibrary": {}, + "metrics": [ + { + "description": "number of bytes commited to memory", + "name": "bytes.commited", + "sum": { + "aggregationTemporality": "AGGREGATION_TEMPORALITY_CUMULATIVE", + "dataPoints": [ + { + "asInt": "19446169600", + "timeUnixNano": "1646862225775600200" + } + ] + }, + "unit": "By" + } + ] + } + ], + "resource": {} + } + ] +} diff --git a/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper.go b/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper.go index cebdf3c71953..3a65befbee98 100644 --- a/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper.go +++ b/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper.go @@ -45,7 +45,7 @@ type PerfCounterScraper interface { // scraper is the type that scrapes various host metrics. type scraper struct { cfg *Config - logger *zap.Logger + settings component.TelemetrySettings counters []PerfCounterMetrics } @@ -61,13 +61,9 @@ type PerfCounterScrapedMetrics struct { Metric string } -func newScraper(cfg *Config, logger *zap.Logger) (*scraper, error) { - if err := cfg.Validate(); err != nil { - return nil, err - } - - s := &scraper{cfg: cfg, logger: logger} - return s, nil +func newScraper(cfg *Config, settings component.TelemetrySettings) *scraper { + s := &scraper{cfg: cfg, settings: settings} + return s } func (s *scraper) start(context.Context, component.Host) error { @@ -90,7 +86,7 @@ func (s *scraper) start(context.Context, component.Host) error { // log a warning if some counters cannot be loaded, but do not crash the app if errs != nil { - s.logger.Warn("some performance counters could not be initialized", zap.Error(errs)) + s.settings.Logger.Warn("some performance counters could not be initialized", zap.Error(errs)) } return nil diff --git a/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper_test.go b/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper_test.go index 5624e8f96bdd..5cc1341006c8 100644 --- a/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper_test.go +++ b/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper_test.go @@ -20,6 +20,7 @@ package windowsperfcountersreceiver import ( "context" "errors" + "path/filepath" "testing" "time" @@ -31,6 +32,8 @@ import ( "go.uber.org/zap/zapcore" "go.uber.org/zap/zaptest/observer" + "github.com/open-telemetry/opentelemetry-collector-contrib/internal/scrapertest" + "github.com/open-telemetry/opentelemetry-collector-contrib/internal/scrapertest/golden" "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/windowsperfcountersreceiver/internal/third_party/telegraf/win_perf_counters" ) @@ -60,62 +63,88 @@ func (mpc *mockPerfCounter) Close() error { } func Test_WindowsPerfCounterScraper(t *testing.T) { - type expectedMetric struct { - name string - instanceLabelValues []string - } - type testCase struct { name string cfg *Config - newErr string mockCounterPath string startMessage string startErr string scrapeErr error shutdownErr error - expectedMetrics []expectedMetric + expectedMetricPath string } - defaultConfig := &Config{ - PerfCounters: []PerfCounterConfig{ - { - Object: "object", - Counters: []CounterConfig{{CounterName: "Committed Bytes", MetricName: "commited.bytes"}}, - }, - }, - MetricMetaData: []MetricConfig{ - { - MetricName: "metric", - Description: "desc", - Unit: "1", - Gauge: GaugeMetric{ - ValueType: "double", - }, - }, + defaultConfig := createDefaultConfig().(*Config) + + defaultMetric := MetricConfig{ + MetricName: "metric", + Description: "desc", + Unit: "1", + Gauge: GaugeMetric{ + ValueType: "double", }, - ScraperControllerSettings: scraperhelper.ScraperControllerSettings{CollectionInterval: time.Minute}, } testCases := []testCase{ { name: "Standard", cfg: &Config{ + MetricMetaData: []MetricConfig{ + { + MetricName: "cpu.idle", + Description: "percentage of time CPU is idle.", + Unit: "%", + Gauge: GaugeMetric{ + ValueType: "double", + }, + }, + { + MetricName: "bytes.commited", + Description: "number of bytes commited to memory", + Unit: "By", + Gauge: GaugeMetric{ + ValueType: "double", + }, + }, + { + MetricName: "processor.time", + Description: "amount of time processor is busy", + Unit: "%", + Gauge: GaugeMetric{ + ValueType: "double", + }, + }, + }, PerfCounters: []PerfCounterConfig{ - {Object: "Memory", Counters: []CounterConfig{{CounterName: "Committed Bytes", MetricName: "Committed Bytes"}}}, + {Object: "Memory", Counters: []CounterConfig{{CounterName: "Committed Bytes", MetricName: "bytes.commited"}}}, {Object: "Processor", Instances: []string{"*"}, Counters: []CounterConfig{{CounterName: "% Idle Time", MetricName: "cpu.idle"}}}, - {Object: "Processor", Instances: []string{"1", "2"}, Counters: []CounterConfig{{CounterName: "% Processor Time", MetricName: "bytes.commited"}}}, + {Object: "Processor", Instances: []string{"1", "2"}, Counters: []CounterConfig{{CounterName: "% Processor Time", MetricName: "processor.time"}}}, }, ScraperControllerSettings: scraperhelper.ScraperControllerSettings{CollectionInterval: time.Minute}, }, - expectedMetrics: []expectedMetric{ - {name: `\Memory\Committed Bytes`}, - {name: `cpu.idle`, instanceLabelValues: []string{"*"}}, - {name: `bytes.commited"`, instanceLabelValues: []string{"1"}}, - {name: `bytes.commited"`, instanceLabelValues: []string{"2"}}, + expectedMetricPath: filepath.Join("testdata", "scraper", "standard.json"), + }, + { + name: "SumMetric", + cfg: &Config{ + MetricMetaData: []MetricConfig{ + { + MetricName: "bytes.commited", + Description: "number of bytes commited to memory", + Unit: "By", + Sum: SumMetric{ + ValueType: "int", + }, + }, + }, + PerfCounters: []PerfCounterConfig{ + {Object: "Memory", Counters: []CounterConfig{{CounterName: "Committed Bytes", MetricName: "bytes.commited"}}}, + }, + ScraperControllerSettings: scraperhelper.ScraperControllerSettings{CollectionInterval: time.Minute}, }, + expectedMetricPath: filepath.Join("testdata", "scraper", "sum_metric.json"), }, { name: "InvalidCounter", @@ -137,12 +166,11 @@ func Test_WindowsPerfCounterScraper(t *testing.T) { }, { name: "ScrapeError", - scrapeErr: errors.New("err1"), + scrapeErr: errors.New("err2"), }, { - name: "CloseError", - expectedMetrics: []expectedMetric{{name: ""}}, - shutdownErr: errors.New("err1"), + name: "CloseError", + shutdownErr: errors.New("err1"), }, } @@ -155,14 +183,11 @@ func Test_WindowsPerfCounterScraper(t *testing.T) { core, obs := observer.New(zapcore.WarnLevel) logger := zap.New(core) - scraper, err := newScraper(cfg, logger) - if test.newErr != "" { - require.EqualError(t, err, test.newErr) - return - } + settings := componenttest.NewNopTelemetrySettings() + settings.Logger = logger + scraper := newScraper(cfg, settings) - err = scraper.start(context.Background(), componenttest.NewNopHost()) - require.NoError(t, err) + err := scraper.start(context.Background(), componenttest.NewNopHost()) if test.startErr != "" { require.Equal(t, 1, obs.Len()) log := obs.All()[0] @@ -175,66 +200,31 @@ func Test_WindowsPerfCounterScraper(t *testing.T) { require.NoError(t, err) if test.mockCounterPath != "" || test.scrapeErr != nil || test.shutdownErr != nil { - for i := range scraper.counters { - scraper.counters[i].CounterScraper = newMockPerfCounter(test.mockCounterPath, test.scrapeErr, test.shutdownErr) + scraper.cfg.MetricMetaData = []MetricConfig{defaultMetric} + scraper.counters = []PerfCounterMetrics{ + { + CounterScraper: newMockPerfCounter(test.mockCounterPath, test.scrapeErr, test.shutdownErr), + Metric: "metric", + }, } } - md, err := scraper.scrape(context.Background()) + actualMetrics, err := scraper.scrape(context.Background()) if test.scrapeErr != nil { - assert.Equal(t, err, test.scrapeErr) - } else { - require.NoError(t, err) - } - metrics := md.ResourceMetrics().At(0).InstrumentationLibraryMetrics().At(0).Metrics() - require.Equal(t, len(test.expectedMetrics), metrics.Len()) - for i, e := range test.expectedMetrics { - metric := metrics.At(i) - assert.Equal(t, e.name, metric.Name()) - - ddp := metric.Gauge().DataPoints() - - var allInstances bool - for _, v := range e.instanceLabelValues { - if v == "*" { - allInstances = true - break - } - } - - if allInstances { - require.GreaterOrEqual(t, ddp.Len(), 1) - } else { - expectedDataPoints := 1 - if len(e.instanceLabelValues) > 0 { - expectedDataPoints = len(e.instanceLabelValues) - } - - require.Equal(t, expectedDataPoints, ddp.Len()) - } - - if len(e.instanceLabelValues) > 0 { - instanceLabelValues := make([]string, 0, ddp.Len()) - for i := 0; i < ddp.Len(); i++ { - instanceLabelValue, ok := ddp.At(i).Attributes().Get(instanceLabelName) - require.Truef(t, ok, "data point was missing %q label", instanceLabelName) - instanceLabelValues = append(instanceLabelValues, instanceLabelValue.StringVal()) - } - - if !allInstances { - for _, v := range e.instanceLabelValues { - assert.Contains(t, instanceLabelValues, v) - } - } - } + require.Equal(t, test.scrapeErr, err) + return } + require.NoError(t, err) err = scraper.shutdown(context.Background()) if test.shutdownErr != nil { - assert.Equal(t, err, test.shutdownErr) - } else { - require.NoError(t, err) + assert.Equal(t, test.shutdownErr, err) + return } + require.NoError(t, err) + expectedMetrics, err := golden.ReadMetrics(test.expectedMetricPath) + scrapertest.CompareMetrics(expectedMetrics, actualMetrics) + require.NoError(t, err) }) } } From 1bbdeee97a0591063ec205569194942b0170b380 Mon Sep 17 00:00:00 2001 From: Miguel Rodriguez Date: Thu, 10 Mar 2022 08:30:27 -0500 Subject: [PATCH 03/24] Update Readme --- .../windowsperfcountersreceiver/README.md | 66 +++++++++++++------ .../windowsperfcountersreceiver/config.go | 6 ++ 2 files changed, 52 insertions(+), 20 deletions(-) diff --git a/receiver/windowsperfcountersreceiver/README.md b/receiver/windowsperfcountersreceiver/README.md index 9d7c4216ecb9..7b83d54c9340 100644 --- a/receiver/windowsperfcountersreceiver/README.md +++ b/receiver/windowsperfcountersreceiver/README.md @@ -6,9 +6,6 @@ interface](https://docs.microsoft.com/en-us/windows/win32/perfctrs/using-the-pdh It is based on the [Telegraf Windows Performance Counters Input Plugin](https://github.com/influxdata/telegraf/tree/master/plugins/inputs/win_perf_counters). -Metrics will be generated with names and labels that match the performance -counter path, i.e. - - `Memory\Committed Bytes` - `Processor\% Processor Time`, with a datapoint for each `Instance` label = (`_Total`, `1`, `2`, `3`, ... ) @@ -25,11 +22,25 @@ be configured: ```yaml windowsperfcounters: collection_interval: # default = "1m" + metric_metadata: + - metric_name: + description: + unit: + gauge: + value_type: + - metric_name: + description: + unit: + sum: + value_type: + aggregation: + monotonic: perfcounters: - object: instances: []* counters: - - + - counter_name: + metric_name: ``` *Note `instances` can have several special values depending on the type of @@ -53,23 +64,42 @@ you can configure multiple `windowsperfcounters` receivers with different ```yaml receivers: windowsperfcounters/memory: + metric_metadata: + - metric_name: bytes.commited + description: the number of bytes commited to memory + unit: By + gauge: + value_type: int collection_interval: 30s perfcounters: - object: Memory counters: - - Committed Bytes + - counter_name: Committed Bytes + metric_name: bytes.commited windowsperfcounters/processor: collection_interval: 1m + metric_metadata: + - metric_name: processor.time + description: active and idle time of the processor + unit: "%" + gauge: + value_type: double perfcounters: - object: "Processor" instances: "*" counters: - - "% Processor Time" + - counter_name: "% Processor Time" + metric_name: processor.time + attributes: + state: active - object: "Processor" instances: [1, 2] counters: - - "% Idle Time" + - counter_name: "% Idle Time" + metric_name: processor.time + attributes: + state: idle service: pipelines: @@ -77,36 +107,32 @@ service: receivers: [windowsperfcounters/memory, windowsperfcounters/processor] ``` -### Changing metric format +### Defining metric format -To report metrics in the desired output format, it's recommended you use this -receiver with the [metrics transform -processor](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/processor/metricstransformprocessor). +To report metrics in the desired output format, build a metric the metric and reference it in the given counter with any applicable attributes. e.g. To output the `Memory/Committed Bytes` counter as a metric with the name -`system.memory.usage`: +`bytes.commited`: ```yaml receivers: windowsperfcounters: + metric_metadata: + - metric_name: bytes.commited + description: the number of bytes commited to memory + unit: By + gauge: + value_type: int collection_interval: 30s perfcounters: - object: Memory counters: - Committed Bytes -processors: - metricstransformprocessor: - transforms: - - metric_name: "Memory/Committed Bytes" - action: update - new_name: system.memory.usage - service: pipelines: metrics: receivers: [windowsperfcounters] - processors: [metricstransformprocessor] ``` ## Recommended configuration for common applications diff --git a/receiver/windowsperfcountersreceiver/config.go b/receiver/windowsperfcountersreceiver/config.go index a715c6608b51..2a133455e0d3 100644 --- a/receiver/windowsperfcountersreceiver/config.go +++ b/receiver/windowsperfcountersreceiver/config.go @@ -93,13 +93,19 @@ func (c *Config) Validate() error { } else if (metric.Gauge != GaugeMetric{}) { if metric.Gauge.ValueType == "" { errs = multierr.Append(errs, fmt.Errorf("gauge metric %q does not include a value type", metric.MetricName)) + } else if metric.Gauge.ValueType != "int" && metric.Gauge.ValueType != "double" { + errs = multierr.Append(errs, fmt.Errorf("gauge metric %q includes an invalid value type", metric.MetricName)) } } else if (metric.Sum != SumMetric{}) { if metric.Sum.ValueType == "" { errs = multierr.Append(errs, fmt.Errorf("sum metric %q does not include a value type", metric.MetricName)) + } else if metric.Sum.ValueType != "int" && metric.Sum.ValueType != "double" { + errs = multierr.Append(errs, fmt.Errorf("sum metric %q includes an invalid value type", metric.MetricName)) } if metric.Sum.Aggregation == "" { errs = multierr.Append(errs, fmt.Errorf("sum metric %q does not include an aggregation", metric.MetricName)) + } else if metric.Sum.Aggregation != "cumulative" && metric.Sum.Aggregation != "delta" { + errs = multierr.Append(errs, fmt.Errorf("sum metric %q includes an invalid aggregation", metric.MetricName)) } } } From e1850b3165f842e5d8116130608823dfc4edc957 Mon Sep 17 00:00:00 2001 From: Miguel Rodriguez Date: Thu, 10 Mar 2022 08:39:08 -0500 Subject: [PATCH 04/24] Add attributes to readme --- receiver/windowsperfcountersreceiver/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/receiver/windowsperfcountersreceiver/README.md b/receiver/windowsperfcountersreceiver/README.md index 7b83d54c9340..b2744362592e 100644 --- a/receiver/windowsperfcountersreceiver/README.md +++ b/receiver/windowsperfcountersreceiver/README.md @@ -41,6 +41,8 @@ windowsperfcounters: counters: - counter_name: metric_name: + attributes: + : ``` *Note `instances` can have several special values depending on the type of From 9c193567389df811f9504563886437d5cc57f42a Mon Sep 17 00:00:00 2001 From: Miguel Rodriguez Date: Thu, 10 Mar 2022 08:40:46 -0500 Subject: [PATCH 05/24] Update values for metric config in readme --- receiver/windowsperfcountersreceiver/README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/receiver/windowsperfcountersreceiver/README.md b/receiver/windowsperfcountersreceiver/README.md index b2744362592e..66e5177f6a0c 100644 --- a/receiver/windowsperfcountersreceiver/README.md +++ b/receiver/windowsperfcountersreceiver/README.md @@ -27,13 +27,13 @@ windowsperfcounters: description: unit: gauge: - value_type: + value_type: - metric_name: description: unit: - sum: - value_type: - aggregation: + sum: + value_type: + aggregation: monotonic: perfcounters: - object: From 048308c386e4373bc21f06b8df83a7d1d1a881cf Mon Sep 17 00:00:00 2001 From: Mrod1598 Date: Thu, 10 Mar 2022 09:17:46 -0500 Subject: [PATCH 06/24] Update changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 45d1501b1b2d..68055ab562cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### 🛑 Breaking changes 🛑 + +- `windowsperfcountersreceiver`: Added metrics configuration (#8376) + ## v0.46.0 ### 💡 Enhancements 💡 From a416cfe8613ee25c5cc77b1574c84bfa36013ac7 Mon Sep 17 00:00:00 2001 From: Mrod1598 Date: Thu, 10 Mar 2022 09:19:34 -0500 Subject: [PATCH 07/24] Fix lint --- receiver/windowsperfcountersreceiver/README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/receiver/windowsperfcountersreceiver/README.md b/receiver/windowsperfcountersreceiver/README.md index 66e5177f6a0c..3593e7821836 100644 --- a/receiver/windowsperfcountersreceiver/README.md +++ b/receiver/windowsperfcountersreceiver/README.md @@ -67,8 +67,8 @@ you can configure multiple `windowsperfcounters` receivers with different receivers: windowsperfcounters/memory: metric_metadata: - - metric_name: bytes.commited - description: the number of bytes commited to memory + - metric_name: bytes.committed + description: the number of bytes committed to memory unit: By gauge: value_type: int @@ -77,7 +77,7 @@ receivers: - object: Memory counters: - counter_name: Committed Bytes - metric_name: bytes.commited + metric_name: bytes.committed windowsperfcounters/processor: collection_interval: 1m @@ -114,14 +114,14 @@ service: To report metrics in the desired output format, build a metric the metric and reference it in the given counter with any applicable attributes. e.g. To output the `Memory/Committed Bytes` counter as a metric with the name -`bytes.commited`: +`bytes.committed`: ```yaml receivers: windowsperfcounters: metric_metadata: - - metric_name: bytes.commited - description: the number of bytes commited to memory + - metric_name: bytes.committed + description: the number of bytes committed to memory unit: By gauge: value_type: int From 24def51f465d499b7a888de7b8afc9b8881bf103 Mon Sep 17 00:00:00 2001 From: Mrod1598 Date: Thu, 10 Mar 2022 09:52:03 -0500 Subject: [PATCH 08/24] Fix misspelling --- .../testdata/scraper/standard.json | 4 ++-- .../testdata/scraper/sum_metric.json | 4 ++-- .../windowsperfcounters_scraper_test.go | 12 ++++++------ 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/receiver/windowsperfcountersreceiver/testdata/scraper/standard.json b/receiver/windowsperfcountersreceiver/testdata/scraper/standard.json index 3b05c056e2e7..f117a87e29cf 100644 --- a/receiver/windowsperfcountersreceiver/testdata/scraper/standard.json +++ b/receiver/windowsperfcountersreceiver/testdata/scraper/standard.json @@ -111,7 +111,7 @@ "unit": "%" }, { - "description": "number of bytes commited to memory", + "description": "number of bytes committed to memory", "gauge": { "dataPoints": [ { @@ -120,7 +120,7 @@ } ] }, - "name": "bytes.commited", + "name": "bytes.committed", "unit": "By" }, { diff --git a/receiver/windowsperfcountersreceiver/testdata/scraper/sum_metric.json b/receiver/windowsperfcountersreceiver/testdata/scraper/sum_metric.json index cad1d52249a1..49e50334da1e 100644 --- a/receiver/windowsperfcountersreceiver/testdata/scraper/sum_metric.json +++ b/receiver/windowsperfcountersreceiver/testdata/scraper/sum_metric.json @@ -6,8 +6,8 @@ "instrumentationLibrary": {}, "metrics": [ { - "description": "number of bytes commited to memory", - "name": "bytes.commited", + "description": "number of bytes committed to memory", + "name": "bytes.committed", "sum": { "aggregationTemporality": "AGGREGATION_TEMPORALITY_CUMULATIVE", "dataPoints": [ diff --git a/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper_test.go b/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper_test.go index 5cc1341006c8..eb617adccc46 100644 --- a/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper_test.go +++ b/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper_test.go @@ -101,8 +101,8 @@ func Test_WindowsPerfCounterScraper(t *testing.T) { }, }, { - MetricName: "bytes.commited", - Description: "number of bytes commited to memory", + MetricName: "bytes.committed", + Description: "number of bytes committed to memory", Unit: "By", Gauge: GaugeMetric{ ValueType: "double", @@ -118,7 +118,7 @@ func Test_WindowsPerfCounterScraper(t *testing.T) { }, }, PerfCounters: []PerfCounterConfig{ - {Object: "Memory", Counters: []CounterConfig{{CounterName: "Committed Bytes", MetricName: "bytes.commited"}}}, + {Object: "Memory", Counters: []CounterConfig{{CounterName: "Committed Bytes", MetricName: "bytes.committed"}}}, {Object: "Processor", Instances: []string{"*"}, Counters: []CounterConfig{{CounterName: "% Idle Time", MetricName: "cpu.idle"}}}, {Object: "Processor", Instances: []string{"1", "2"}, Counters: []CounterConfig{{CounterName: "% Processor Time", MetricName: "processor.time"}}}, }, @@ -131,8 +131,8 @@ func Test_WindowsPerfCounterScraper(t *testing.T) { cfg: &Config{ MetricMetaData: []MetricConfig{ { - MetricName: "bytes.commited", - Description: "number of bytes commited to memory", + MetricName: "bytes.committed", + Description: "number of bytes committed to memory", Unit: "By", Sum: SumMetric{ ValueType: "int", @@ -140,7 +140,7 @@ func Test_WindowsPerfCounterScraper(t *testing.T) { }, }, PerfCounters: []PerfCounterConfig{ - {Object: "Memory", Counters: []CounterConfig{{CounterName: "Committed Bytes", MetricName: "bytes.commited"}}}, + {Object: "Memory", Counters: []CounterConfig{{CounterName: "Committed Bytes", MetricName: "bytes.committed"}}}, }, ScraperControllerSettings: scraperhelper.ScraperControllerSettings{CollectionInterval: time.Minute}, }, From 3d770414b3fce1b26c425123feb0a2ec0ad26cbe Mon Sep 17 00:00:00 2001 From: Mrod1598 Date: Thu, 10 Mar 2022 10:03:40 -0500 Subject: [PATCH 09/24] Force rerun --- receiver/windowsperfcountersreceiver/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/receiver/windowsperfcountersreceiver/README.md b/receiver/windowsperfcountersreceiver/README.md index 3593e7821836..429a222931b1 100644 --- a/receiver/windowsperfcountersreceiver/README.md +++ b/receiver/windowsperfcountersreceiver/README.md @@ -63,7 +63,7 @@ If you would like to scrape some counters at a different frequency than others, you can configure multiple `windowsperfcounters` receivers with different `collection_interval` values. For example: -```yaml +```yaml receivers: windowsperfcounters/memory: metric_metadata: From 9c7ecf5a9ae5df2edd23745990bead1fa5e39128 Mon Sep 17 00:00:00 2001 From: Mrod1598 Date: Mon, 14 Mar 2022 17:11:05 -0400 Subject: [PATCH 10/24] Rename keys in Counter config --- receiver/windowsperfcountersreceiver/README.md | 8 ++++---- receiver/windowsperfcountersreceiver/config.go | 10 ++++++---- .../windowsperfcountersreceiver/config_test.go | 14 ++++++++++---- .../factory_others_test.go | 2 +- .../windowsperfcountersreceiver/factory_test.go | 6 +++--- .../factory_windows_test.go | 2 +- .../testdata/config-emptyinstance.yaml | 4 ++-- .../testdata/config-missingdatatype.yaml | 4 ++-- .../testdata/config-missinggaugevaluetype.yaml | 4 ++-- .../config-missingmetricdescription.yaml | 4 ++-- .../testdata/config-missingmetricname.yaml | 4 ++-- .../testdata/config-missingmetricunit.yaml | 4 ++-- .../testdata/config-missingsumaggregation.yaml | 4 ++-- .../testdata/config-missingsumvaluetype.yaml | 4 ++-- .../config-negative-collection-interval.yaml | 4 ++-- .../testdata/config.yaml | 17 +++++++++-------- .../windowsperfcounters_scraper.go | 13 ++----------- .../windowsperfcounters_scraper_test.go | 12 ++++++------ 18 files changed, 60 insertions(+), 60 deletions(-) diff --git a/receiver/windowsperfcountersreceiver/README.md b/receiver/windowsperfcountersreceiver/README.md index 429a222931b1..9a235af68746 100644 --- a/receiver/windowsperfcountersreceiver/README.md +++ b/receiver/windowsperfcountersreceiver/README.md @@ -39,7 +39,7 @@ windowsperfcounters: - object: instances: []* counters: - - counter_name: + - name: metric_name: attributes: : @@ -76,7 +76,7 @@ receivers: perfcounters: - object: Memory counters: - - counter_name: Committed Bytes + - name: Committed Bytes metric_name: bytes.committed windowsperfcounters/processor: @@ -91,14 +91,14 @@ receivers: - object: "Processor" instances: "*" counters: - - counter_name: "% Processor Time" + - name: "% Processor Time" metric_name: processor.time attributes: state: active - object: "Processor" instances: [1, 2] counters: - - counter_name: "% Idle Time" + - name: "% Idle Time" metric_name: processor.time attributes: state: idle diff --git a/receiver/windowsperfcountersreceiver/config.go b/receiver/windowsperfcountersreceiver/config.go index 2a133455e0d3..75b488db1072 100644 --- a/receiver/windowsperfcountersreceiver/config.go +++ b/receiver/windowsperfcountersreceiver/config.go @@ -56,9 +56,9 @@ type SumMetric struct { } type CounterConfig struct { - MetricName string `mapstructure:"metric_name"` - CounterName string `mapstructure:"counter_name"` - Attributes map[string]string `mapstructure:"attributes"` + Metric string `mapstructure:"metric"` + Name string `mapstructure:"name"` + Attributes map[string]string `mapstructure:"attributes"` } func (c *Config) Validate() error { @@ -107,6 +107,8 @@ func (c *Config) Validate() error { } else if metric.Sum.Aggregation != "cumulative" && metric.Sum.Aggregation != "delta" { errs = multierr.Append(errs, fmt.Errorf("sum metric %q includes an invalid aggregation", metric.MetricName)) } + } else if (metric.Sum != SumMetric{}) && (metric.Gauge != GaugeMetric{}) { + errs = multierr.Append(errs, fmt.Errorf("metric %q provides both a sum config and a gauge config", metric.MetricName)) } } @@ -124,7 +126,7 @@ func (c *Config) Validate() error { for _, counter := range pc.Counters { foundMatchingMetric := false for _, metric := range c.MetricMetaData { - if counter.MetricName == metric.MetricName { + if counter.Metric == metric.MetricName { foundMatchingMetric = true } } diff --git a/receiver/windowsperfcountersreceiver/config_test.go b/receiver/windowsperfcountersreceiver/config_test.go index 310b3d82c5eb..5f8ff50d0bf9 100644 --- a/receiver/windowsperfcountersreceiver/config_test.go +++ b/receiver/windowsperfcountersreceiver/config_test.go @@ -45,8 +45,8 @@ func TestLoadConfig(t *testing.T) { defaultConfigSingleObject := factory.CreateDefaultConfig() counterConfig := CounterConfig{ - CounterName: "counter1", - MetricName: "metric", + Name: "counter1", + Metric: "metric", } defaultConfigSingleObject.(*Config).PerfCounters = []PerfCounterConfig{{Object: "object", Counters: []CounterConfig{counterConfig}}} defaultConfigSingleObject.(*Config).MetricMetaData = []MetricConfig{ @@ -63,8 +63,8 @@ func TestLoadConfig(t *testing.T) { assert.Equal(t, defaultConfigSingleObject, r0) counterConfig2 := CounterConfig{ - CounterName: "counter2", - MetricName: "metric2", + Name: "counter2", + Metric: "metric2", } r1 := cfg.Receivers[config.NewComponentIDWithName(typeStr, "customname")].(*Config) @@ -129,6 +129,7 @@ func TestLoadConfig_Error(t *testing.T) { missingSumValueType = `sum metric "%s" does not include a value type` missingSumAggregation = `sum metric "%s" does not include an aggregation` missingMetrics = `must specify at least one metric` + gaugeAndSum = `metric "%s" provides both a sum config and a gauge config` ) testCases := []testCase{ @@ -187,6 +188,11 @@ func TestLoadConfig_Error(t *testing.T) { cfgFile: "config-missingsumaggregation.yaml", expectedErr: fmt.Sprintf("%s: %s", errorPrefix, fmt.Sprintf(missingSumAggregation, "metric")), }, + { + name: "GaugeAndSum", + cfgFile: "config-gaugeandsum.yaml", + expectedErr: fmt.Sprintf("%s: %s", errorPrefix, fmt.Sprintf(missingSumAggregation, "metric")), + }, { name: "AllErrors", cfgFile: "config-allerrors.yaml", diff --git a/receiver/windowsperfcountersreceiver/factory_others_test.go b/receiver/windowsperfcountersreceiver/factory_others_test.go index a3341cd9fe46..d11903e9df70 100644 --- a/receiver/windowsperfcountersreceiver/factory_others_test.go +++ b/receiver/windowsperfcountersreceiver/factory_others_test.go @@ -31,7 +31,7 @@ func TestCreateMetricsReceiver(t *testing.T) { cfg.(*Config).PerfCounters = []PerfCounterConfig{ { Object: "object", - Counters: []CounterConfig{{CounterName: "counter", MetricName: "metric"}}, + Counters: []CounterConfig{{Name: "counter", Metric: "metric"}}, }, } diff --git a/receiver/windowsperfcountersreceiver/factory_test.go b/receiver/windowsperfcountersreceiver/factory_test.go index 21b7b85d2a58..401d0b1610f7 100644 --- a/receiver/windowsperfcountersreceiver/factory_test.go +++ b/receiver/windowsperfcountersreceiver/factory_test.go @@ -37,7 +37,7 @@ func TestCreateDefaultConfig(t *testing.T) { cfg.(*Config).PerfCounters = []PerfCounterConfig{ { Object: "object", - Counters: []CounterConfig{{CounterName: "counter", MetricName: "metric"}}, + Counters: []CounterConfig{{Name: "counter", Metric: "metric"}}, }, } @@ -61,7 +61,7 @@ func TestCreateTracesReceiver(t *testing.T) { cfg.(*Config).PerfCounters = []PerfCounterConfig{ { Object: "object", - Counters: []CounterConfig{{CounterName: "counter", MetricName: "metric"}}, + Counters: []CounterConfig{{Name: "counter", Metric: "metric"}}, }, } @@ -87,7 +87,7 @@ func TestCreateLogsReceiver(t *testing.T) { cfg.(*Config).PerfCounters = []PerfCounterConfig{ { Object: "object", - Counters: []CounterConfig{{CounterName: "counter", MetricName: "metric"}}, + Counters: []CounterConfig{{Name: "counter", Metric: "metric"}}, }, } diff --git a/receiver/windowsperfcountersreceiver/factory_windows_test.go b/receiver/windowsperfcountersreceiver/factory_windows_test.go index 39c7b8c4550b..13c3b0af5630 100644 --- a/receiver/windowsperfcountersreceiver/factory_windows_test.go +++ b/receiver/windowsperfcountersreceiver/factory_windows_test.go @@ -31,7 +31,7 @@ func TestCreateMetricsReceiver(t *testing.T) { cfg.(*Config).PerfCounters = []PerfCounterConfig{ { Object: "object", - Counters: []CounterConfig{{CounterName: "counter", MetricName: "metric"}}, + Counters: []CounterConfig{{Name: "counter", Metric: "metric"}}, }, } diff --git a/receiver/windowsperfcountersreceiver/testdata/config-emptyinstance.yaml b/receiver/windowsperfcountersreceiver/testdata/config-emptyinstance.yaml index b2c7ecbdc856..25db94c7005d 100644 --- a/receiver/windowsperfcountersreceiver/testdata/config-emptyinstance.yaml +++ b/receiver/windowsperfcountersreceiver/testdata/config-emptyinstance.yaml @@ -10,8 +10,8 @@ receivers: - object: "object" instances: [""] counters: - - counter_name: counter - metric_name: metric + - name: counter + metric: metric processors: nop: diff --git a/receiver/windowsperfcountersreceiver/testdata/config-missingdatatype.yaml b/receiver/windowsperfcountersreceiver/testdata/config-missingdatatype.yaml index aca7bbfbaff9..599e6dfbe172 100644 --- a/receiver/windowsperfcountersreceiver/testdata/config-missingdatatype.yaml +++ b/receiver/windowsperfcountersreceiver/testdata/config-missingdatatype.yaml @@ -7,8 +7,8 @@ receivers: perfcounters: - object: "object" counters: - - counter_name: counter1 - metric_name: metric + - name: counter1 + metric: metric processors: nop: diff --git a/receiver/windowsperfcountersreceiver/testdata/config-missinggaugevaluetype.yaml b/receiver/windowsperfcountersreceiver/testdata/config-missinggaugevaluetype.yaml index 20a8ff8b0aee..dca22c005487 100644 --- a/receiver/windowsperfcountersreceiver/testdata/config-missinggaugevaluetype.yaml +++ b/receiver/windowsperfcountersreceiver/testdata/config-missinggaugevaluetype.yaml @@ -9,8 +9,8 @@ receivers: perfcounters: - object: "object" counters: - - counter_name: counter1 - metric_name: metric + - name: counter1 + metric: metric processors: nop: diff --git a/receiver/windowsperfcountersreceiver/testdata/config-missingmetricdescription.yaml b/receiver/windowsperfcountersreceiver/testdata/config-missingmetricdescription.yaml index ccbc234da7c5..67bba5231c56 100644 --- a/receiver/windowsperfcountersreceiver/testdata/config-missingmetricdescription.yaml +++ b/receiver/windowsperfcountersreceiver/testdata/config-missingmetricdescription.yaml @@ -8,8 +8,8 @@ receivers: perfcounters: - object: "object" counters: - - counter_name: counter1 - metric_name: metric + - name: counter1 + metric: metric processors: nop: diff --git a/receiver/windowsperfcountersreceiver/testdata/config-missingmetricname.yaml b/receiver/windowsperfcountersreceiver/testdata/config-missingmetricname.yaml index 873a32054f11..e30b70c70dfe 100644 --- a/receiver/windowsperfcountersreceiver/testdata/config-missingmetricname.yaml +++ b/receiver/windowsperfcountersreceiver/testdata/config-missingmetricname.yaml @@ -8,8 +8,8 @@ receivers: perfcounters: - object: "object" counters: - - counter_name: counter1 - metric_name: metric + - name: counter1 + metric: metric processors: nop: diff --git a/receiver/windowsperfcountersreceiver/testdata/config-missingmetricunit.yaml b/receiver/windowsperfcountersreceiver/testdata/config-missingmetricunit.yaml index 3fb9b3025127..d07c8d7502b2 100644 --- a/receiver/windowsperfcountersreceiver/testdata/config-missingmetricunit.yaml +++ b/receiver/windowsperfcountersreceiver/testdata/config-missingmetricunit.yaml @@ -8,8 +8,8 @@ receivers: perfcounters: - object: "object" counters: - - counter_name: counter1 - metric_name: metric + - name: counter1 + metric: metric processors: nop: diff --git a/receiver/windowsperfcountersreceiver/testdata/config-missingsumaggregation.yaml b/receiver/windowsperfcountersreceiver/testdata/config-missingsumaggregation.yaml index 74faea98f411..d755efc3a424 100644 --- a/receiver/windowsperfcountersreceiver/testdata/config-missingsumaggregation.yaml +++ b/receiver/windowsperfcountersreceiver/testdata/config-missingsumaggregation.yaml @@ -9,8 +9,8 @@ receivers: perfcounters: - object: "object" counters: - - counter_name: counter1 - metric_name: metric + - name: counter1 + metric: metric processors: nop: diff --git a/receiver/windowsperfcountersreceiver/testdata/config-missingsumvaluetype.yaml b/receiver/windowsperfcountersreceiver/testdata/config-missingsumvaluetype.yaml index 7419e4ebd42a..e85e174193b5 100644 --- a/receiver/windowsperfcountersreceiver/testdata/config-missingsumvaluetype.yaml +++ b/receiver/windowsperfcountersreceiver/testdata/config-missingsumvaluetype.yaml @@ -9,8 +9,8 @@ receivers: perfcounters: - object: "object" counters: - - counter_name: counter1 - metric_name: metric + - name: counter1 + metric: metric processors: nop: diff --git a/receiver/windowsperfcountersreceiver/testdata/config-negative-collection-interval.yaml b/receiver/windowsperfcountersreceiver/testdata/config-negative-collection-interval.yaml index 71d5424b067d..7f01643ff3b0 100644 --- a/receiver/windowsperfcountersreceiver/testdata/config-negative-collection-interval.yaml +++ b/receiver/windowsperfcountersreceiver/testdata/config-negative-collection-interval.yaml @@ -10,8 +10,8 @@ receivers: perfcounters: - object: "object" counters: - - counter_name: counter1 - metric_name: metric + - name: counter1 + metric: metric processors: nop: diff --git a/receiver/windowsperfcountersreceiver/testdata/config.yaml b/receiver/windowsperfcountersreceiver/testdata/config.yaml index b5a9234b58ac..ba429af40cd5 100644 --- a/receiver/windowsperfcountersreceiver/testdata/config.yaml +++ b/receiver/windowsperfcountersreceiver/testdata/config.yaml @@ -9,8 +9,8 @@ receivers: perfcounters: - object: "object" counters: - - counter_name: counter1 - metric_name: metric + - name: counter1 + metric: metric windowsperfcounters/customname: metric_metadata: - metric_name: metric @@ -27,14 +27,14 @@ receivers: perfcounters: - object: object1 counters: - - counter_name: counter1 - metric_name: metric + - name: counter1 + metric: metric - object: object2 counters: - - counter_name: counter1 - metric_name: metric - - counter_name: counter2 - metric_name: metric2 + - name: counter1 + metric: metric + - name: counter2 + metric: metric2 processors: nop: @@ -48,3 +48,4 @@ service: receivers: [windowsperfcounters] processors: [nop] exporters: [nop] + \ No newline at end of file diff --git a/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper.go b/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper.go index 3a65befbee98..1f2b0e25064b 100644 --- a/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper.go +++ b/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper.go @@ -55,12 +55,6 @@ type PerfCounterMetrics struct { Metric string } -type PerfCounterScrapedMetrics struct { - CounterScraper PerfCounterScraper - Attributes map[string]string - Metric string -} - func newScraper(cfg *Config, settings component.TelemetrySettings) *scraper { s := &scraper{cfg: cfg, settings: settings} return s @@ -72,13 +66,13 @@ func (s *scraper) start(context.Context, component.Host) error { for _, perfCounterCfg := range s.cfg.PerfCounters { for _, instance := range perfCounterCfg.instances() { for _, counterCfg := range perfCounterCfg.Counters { - counterPath := counterPath(perfCounterCfg.Object, instance, counterCfg.CounterName) + counterPath := counterPath(perfCounterCfg.Object, instance, counterCfg.Name) c, err := pdh.NewPerfCounter(counterPath, true) if err != nil { errs = multierr.Append(errs, fmt.Errorf("counter %v: %w", counterPath, err)) } else { - s.counters = append(s.counters, PerfCounterMetrics{CounterScraper: c, Metric: counterCfg.MetricName, Attributes: counterCfg.Attributes}) + s.counters = append(s.counters, PerfCounterMetrics{CounterScraper: c, Metric: counterCfg.Metric, Attributes: counterCfg.Attributes}) } } } @@ -136,8 +130,6 @@ func (s *scraper) scrape(context.Context) (pdata.Metrics, error) { builtMetric.Sum().SetAggregationTemporality(pdata.MetricAggregationTemporalityCumulative) case "delta": builtMetric.Sum().SetAggregationTemporality(pdata.MetricAggregationTemporalityDelta) - default: - builtMetric.Sum().SetAggregationTemporality(pdata.MetricAggregationTemporalityCumulative) } } @@ -171,7 +163,6 @@ func initializeMetricDps(metricCfg MetricConfig, metric pdata.Metric, now pdata. dps.EnsureCapacity(len(counterValues)) for _, counterValue := range counterValues { dp := dps.AppendEmpty() - for attKey, attVal := range attributes { dp.Attributes().InsertString(attKey, attVal) } diff --git a/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper_test.go b/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper_test.go index eb617adccc46..82ce762725c2 100644 --- a/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper_test.go +++ b/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper_test.go @@ -118,9 +118,9 @@ func Test_WindowsPerfCounterScraper(t *testing.T) { }, }, PerfCounters: []PerfCounterConfig{ - {Object: "Memory", Counters: []CounterConfig{{CounterName: "Committed Bytes", MetricName: "bytes.committed"}}}, - {Object: "Processor", Instances: []string{"*"}, Counters: []CounterConfig{{CounterName: "% Idle Time", MetricName: "cpu.idle"}}}, - {Object: "Processor", Instances: []string{"1", "2"}, Counters: []CounterConfig{{CounterName: "% Processor Time", MetricName: "processor.time"}}}, + {Object: "Memory", Counters: []CounterConfig{{Name: "Committed Bytes", Metric: "bytes.committed"}}}, + {Object: "Processor", Instances: []string{"*"}, Counters: []CounterConfig{{Name: "% Idle Time", Metric: "cpu.idle"}}}, + {Object: "Processor", Instances: []string{"1", "2"}, Counters: []CounterConfig{{Name: "% Processor Time", Metric: "processor.time"}}}, }, ScraperControllerSettings: scraperhelper.ScraperControllerSettings{CollectionInterval: time.Minute}, }, @@ -140,7 +140,7 @@ func Test_WindowsPerfCounterScraper(t *testing.T) { }, }, PerfCounters: []PerfCounterConfig{ - {Object: "Memory", Counters: []CounterConfig{{CounterName: "Committed Bytes", MetricName: "bytes.committed"}}}, + {Object: "Memory", Counters: []CounterConfig{{Name: "Committed Bytes", Metric: "bytes.committed"}}}, }, ScraperControllerSettings: scraperhelper.ScraperControllerSettings{CollectionInterval: time.Minute}, }, @@ -152,11 +152,11 @@ func Test_WindowsPerfCounterScraper(t *testing.T) { PerfCounters: []PerfCounterConfig{ { Object: "Memory", - Counters: []CounterConfig{{CounterName: "Committed Bytes", MetricName: "Committed Bytes"}}, + Counters: []CounterConfig{{Name: "Committed Bytes", Metric: "Committed Bytes"}}, }, { Object: "Invalid Object", - Counters: []CounterConfig{{CounterName: "Invalid Counter", MetricName: "invalid"}}, + Counters: []CounterConfig{{Name: "Invalid Counter", Metric: "invalid"}}, }, }, ScraperControllerSettings: scraperhelper.ScraperControllerSettings{CollectionInterval: time.Minute}, From 33a56b7e88d11bd3359626ebda9cefe7461edc90 Mon Sep 17 00:00:00 2001 From: Mrod1598 Date: Tue, 15 Mar 2022 09:11:27 -0400 Subject: [PATCH 11/24] Restructure metrics into map --- .../windowsperfcountersreceiver/config.go | 33 ++++++++-------- .../config_test.go | 38 +++---------------- .../factory_others_test.go | 5 +-- .../factory_test.go | 15 +++----- .../factory_windows_test.go | 5 +-- .../testdata/config-emptyinstance.yaml | 4 +- .../testdata/config-missingdatatype.yaml | 4 +- .../config-missinggaugevaluetype.yaml | 26 ------------- .../config-missingmetricdescription.yaml | 25 ------------ .../testdata/config-missingmetricname.yaml | 25 ------------ .../testdata/config-missingmetricunit.yaml | 25 ------------ .../config-missingsumaggregation.yaml | 4 +- .../testdata/config-missingsumvaluetype.yaml | 26 ------------- .../config-negative-collection-interval.yaml | 4 +- .../testdata/config-nocounters.yaml | 2 +- .../testdata/config-noobjectname.yaml | 2 +- .../testdata/config-noperfcounters.yaml | 4 +- .../testdata/config.yaml | 6 +-- .../windowsperfcounters_scraper.go | 4 +- .../windowsperfcounters_scraper_test.go | 35 ++++++++--------- 20 files changed, 63 insertions(+), 229 deletions(-) delete mode 100644 receiver/windowsperfcountersreceiver/testdata/config-missinggaugevaluetype.yaml delete mode 100644 receiver/windowsperfcountersreceiver/testdata/config-missingmetricdescription.yaml delete mode 100644 receiver/windowsperfcountersreceiver/testdata/config-missingmetricname.yaml delete mode 100644 receiver/windowsperfcountersreceiver/testdata/config-missingmetricunit.yaml delete mode 100644 receiver/windowsperfcountersreceiver/testdata/config-missingsumvaluetype.yaml diff --git a/receiver/windowsperfcountersreceiver/config.go b/receiver/windowsperfcountersreceiver/config.go index 75b488db1072..41ae75a6c4f6 100644 --- a/receiver/windowsperfcountersreceiver/config.go +++ b/receiver/windowsperfcountersreceiver/config.go @@ -25,8 +25,8 @@ import ( type Config struct { scraperhelper.ScraperControllerSettings `mapstructure:",squash"` - MetricMetaData []MetricConfig `mapstructure:"metric_metadata"` - PerfCounters []PerfCounterConfig `mapstructure:"perfcounters"` + MetricMetaData map[string]MetricConfig `mapstructure:"metrics"` + PerfCounters []PerfCounterConfig `mapstructure:"perfcounters"` } // PerfCounterConfig defines configuration for a perf counter object. @@ -38,7 +38,6 @@ type PerfCounterConfig struct { // MetricsConfig defines the configuration for a metric to be created. type MetricConfig struct { - MetricName string `mapstructure:"metric_name"` Unit string `mapstructure:"unit"` Description string `mapstructure:"description"` Gauge GaugeMetric `mapstructure:"gauge"` @@ -76,39 +75,39 @@ func (c *Config) Validate() error { errs = multierr.Append(errs, fmt.Errorf("must specify at least one metric")) } - for _, metric := range c.MetricMetaData { - if metric.MetricName == "" { + for name, metric := range c.MetricMetaData { + if name == "" { errs = multierr.Append(errs, fmt.Errorf("a metric does not include a name")) continue } if metric.Description == "" { - errs = multierr.Append(errs, fmt.Errorf("metric %q does not include a description", metric.MetricName)) + errs = multierr.Append(errs, fmt.Errorf("metric %q does not include a description", name)) } if metric.Unit == "" { - errs = multierr.Append(errs, fmt.Errorf("metric %q does not include a unit", metric.MetricName)) + errs = multierr.Append(errs, fmt.Errorf("metric %q does not include a unit", name)) } if (metric.Gauge == GaugeMetric{}) && (metric.Sum == SumMetric{}) { - errs = multierr.Append(errs, fmt.Errorf("metric %q does not include a metric definition", metric.MetricName)) + errs = multierr.Append(errs, fmt.Errorf("metric %q does not include a metric definition", name)) } else if (metric.Gauge != GaugeMetric{}) { if metric.Gauge.ValueType == "" { - errs = multierr.Append(errs, fmt.Errorf("gauge metric %q does not include a value type", metric.MetricName)) + errs = multierr.Append(errs, fmt.Errorf("gauge metric %q does not include a value type", name)) } else if metric.Gauge.ValueType != "int" && metric.Gauge.ValueType != "double" { - errs = multierr.Append(errs, fmt.Errorf("gauge metric %q includes an invalid value type", metric.MetricName)) + errs = multierr.Append(errs, fmt.Errorf("gauge metric %q includes an invalid value type", name)) } } else if (metric.Sum != SumMetric{}) { if metric.Sum.ValueType == "" { - errs = multierr.Append(errs, fmt.Errorf("sum metric %q does not include a value type", metric.MetricName)) + errs = multierr.Append(errs, fmt.Errorf("sum metric %q does not include a value type", name)) } else if metric.Sum.ValueType != "int" && metric.Sum.ValueType != "double" { - errs = multierr.Append(errs, fmt.Errorf("sum metric %q includes an invalid value type", metric.MetricName)) + errs = multierr.Append(errs, fmt.Errorf("sum metric %q includes an invalid value type", name)) } if metric.Sum.Aggregation == "" { - errs = multierr.Append(errs, fmt.Errorf("sum metric %q does not include an aggregation", metric.MetricName)) + errs = multierr.Append(errs, fmt.Errorf("sum metric %q does not include an aggregation", name)) } else if metric.Sum.Aggregation != "cumulative" && metric.Sum.Aggregation != "delta" { - errs = multierr.Append(errs, fmt.Errorf("sum metric %q includes an invalid aggregation", metric.MetricName)) + errs = multierr.Append(errs, fmt.Errorf("sum metric %q includes an invalid aggregation", name)) } } else if (metric.Sum != SumMetric{}) && (metric.Gauge != GaugeMetric{}) { - errs = multierr.Append(errs, fmt.Errorf("metric %q provides both a sum config and a gauge config", metric.MetricName)) + errs = multierr.Append(errs, fmt.Errorf("metric %q provides both a sum config and a gauge config", name)) } } @@ -125,8 +124,8 @@ func (c *Config) Validate() error { for _, counter := range pc.Counters { foundMatchingMetric := false - for _, metric := range c.MetricMetaData { - if counter.Metric == metric.MetricName { + for name := range c.MetricMetaData { + if counter.Metric == name { foundMatchingMetric = true } } diff --git a/receiver/windowsperfcountersreceiver/config_test.go b/receiver/windowsperfcountersreceiver/config_test.go index 5f8ff50d0bf9..1e202ce8f12e 100644 --- a/receiver/windowsperfcountersreceiver/config_test.go +++ b/receiver/windowsperfcountersreceiver/config_test.go @@ -49,9 +49,8 @@ func TestLoadConfig(t *testing.T) { Metric: "metric", } defaultConfigSingleObject.(*Config).PerfCounters = []PerfCounterConfig{{Object: "object", Counters: []CounterConfig{counterConfig}}} - defaultConfigSingleObject.(*Config).MetricMetaData = []MetricConfig{ - { - MetricName: "metric", + defaultConfigSingleObject.(*Config).MetricMetaData = map[string]MetricConfig{ + "metric": { Description: "desc", Unit: "1", Gauge: GaugeMetric{ @@ -83,17 +82,15 @@ func TestLoadConfig(t *testing.T) { Counters: []CounterConfig{counterConfig, counterConfig2}, }, }, - MetricMetaData: []MetricConfig{ - { - MetricName: "metric", + MetricMetaData: map[string]MetricConfig{ + "metric": { Description: "desc", Unit: "1", Gauge: GaugeMetric{ ValueType: "double", }, }, - { - MetricName: "metric2", + "metric2": { Description: "desc", Unit: "1", Gauge: GaugeMetric{ @@ -121,12 +118,7 @@ func TestLoadConfig_Error(t *testing.T) { noCountersErr = `perf counter for object "%s" does not specify any counters` emptyInstanceErr = `perf counter for object "%s" includes an empty instance` undefinedMetricErr = `perf counter for object "%s" includes an undefined metric` - missingMetricName = `a metric does not include a name` - missingMetricDesc = `metric "%s" does not include a description` - missingMetricUnit = `metric "%s" does not include a unit` missingMetricMetricType = `metric "%s" does not include a metric definition` - missingGaugeValueType = `gauge metric "%s" does not include a value type` - missingSumValueType = `sum metric "%s" does not include a value type` missingSumAggregation = `sum metric "%s" does not include an aggregation` missingMetrics = `must specify at least one metric` gaugeAndSum = `metric "%s" provides both a sum config and a gauge config` @@ -158,31 +150,11 @@ func TestLoadConfig_Error(t *testing.T) { cfgFile: "config-emptyinstance.yaml", expectedErr: fmt.Sprintf("%s: %s", errorPrefix, fmt.Sprintf(emptyInstanceErr, "object")), }, - { - name: "EmptyMetricDescription", - cfgFile: "config-missingmetricdescription.yaml", - expectedErr: fmt.Sprintf("%s: %s", errorPrefix, fmt.Sprintf(missingMetricDesc, "metric")), - }, - { - name: "EmptyMetricUnit", - cfgFile: "config-missingmetricunit.yaml", - expectedErr: fmt.Sprintf("%s: %s", errorPrefix, fmt.Sprintf(missingMetricUnit, "metric")), - }, { name: "EmptyMetricMetricType", cfgFile: "config-missingdatatype.yaml", expectedErr: fmt.Sprintf("%s: %s", errorPrefix, fmt.Sprintf(missingMetricMetricType, "metric")), }, - { - name: "EmptyMetricName", - cfgFile: "config-missingmetricname.yaml", - expectedErr: fmt.Sprintf("%s: %s; %s", errorPrefix, missingMetricName, fmt.Sprintf(undefinedMetricErr, "object")), - }, - { - name: "EmptySumValueType", - cfgFile: "config-missingsumvaluetype.yaml", - expectedErr: fmt.Sprintf("%s: %s", errorPrefix, fmt.Sprintf(missingSumValueType, "metric")), - }, { name: "EmptySumAggregation", cfgFile: "config-missingsumaggregation.yaml", diff --git a/receiver/windowsperfcountersreceiver/factory_others_test.go b/receiver/windowsperfcountersreceiver/factory_others_test.go index d11903e9df70..4bc09d4c9ee8 100644 --- a/receiver/windowsperfcountersreceiver/factory_others_test.go +++ b/receiver/windowsperfcountersreceiver/factory_others_test.go @@ -35,9 +35,8 @@ func TestCreateMetricsReceiver(t *testing.T) { }, } - cfg.(*Config).MetricMetaData = []MetricConfig{ - { - MetricName: "metric", + cfg.(*Config).MetricMetaData = map[string]MetricConfig{ + "metric": { Description: "desc", Unit: "1", Gauge: GaugeMetric{ diff --git a/receiver/windowsperfcountersreceiver/factory_test.go b/receiver/windowsperfcountersreceiver/factory_test.go index 401d0b1610f7..766863941989 100644 --- a/receiver/windowsperfcountersreceiver/factory_test.go +++ b/receiver/windowsperfcountersreceiver/factory_test.go @@ -41,9 +41,8 @@ func TestCreateDefaultConfig(t *testing.T) { }, } - cfg.(*Config).MetricMetaData = []MetricConfig{ - { - MetricName: "metric", + cfg.(*Config).MetricMetaData = map[string]MetricConfig{ + "metric": { Description: "desc", Unit: "1", Gauge: GaugeMetric{ @@ -65,9 +64,8 @@ func TestCreateTracesReceiver(t *testing.T) { }, } - cfg.(*Config).MetricMetaData = []MetricConfig{ - { - MetricName: "metric", + cfg.(*Config).MetricMetaData = map[string]MetricConfig{ + "metric": { Description: "desc", Unit: "1", Gauge: GaugeMetric{ @@ -91,9 +89,8 @@ func TestCreateLogsReceiver(t *testing.T) { }, } - cfg.(*Config).MetricMetaData = []MetricConfig{ - { - MetricName: "metric", + cfg.(*Config).MetricMetaData = map[string]MetricConfig{ + "metric": { Description: "desc", Unit: "1", Gauge: GaugeMetric{ diff --git a/receiver/windowsperfcountersreceiver/factory_windows_test.go b/receiver/windowsperfcountersreceiver/factory_windows_test.go index 13c3b0af5630..337ab0f0ee20 100644 --- a/receiver/windowsperfcountersreceiver/factory_windows_test.go +++ b/receiver/windowsperfcountersreceiver/factory_windows_test.go @@ -35,9 +35,8 @@ func TestCreateMetricsReceiver(t *testing.T) { }, } - cfg.(*Config).MetricMetaData = []MetricConfig{ - { - MetricName: "metric", + cfg.(*Config).MetricMetaData = map[string]MetricConfig{ + "metric": { Description: "desc", Unit: "1", Gauge: GaugeMetric{ diff --git a/receiver/windowsperfcountersreceiver/testdata/config-emptyinstance.yaml b/receiver/windowsperfcountersreceiver/testdata/config-emptyinstance.yaml index 25db94c7005d..7f5cb8e0bfb7 100644 --- a/receiver/windowsperfcountersreceiver/testdata/config-emptyinstance.yaml +++ b/receiver/windowsperfcountersreceiver/testdata/config-emptyinstance.yaml @@ -1,7 +1,7 @@ receivers: windowsperfcounters: - metric_metadata: - - metric_name: metric + metrics: + metric: description: desc unit: "1" gauge: diff --git a/receiver/windowsperfcountersreceiver/testdata/config-missingdatatype.yaml b/receiver/windowsperfcountersreceiver/testdata/config-missingdatatype.yaml index 599e6dfbe172..6ebba19e4094 100644 --- a/receiver/windowsperfcountersreceiver/testdata/config-missingdatatype.yaml +++ b/receiver/windowsperfcountersreceiver/testdata/config-missingdatatype.yaml @@ -1,7 +1,7 @@ receivers: windowsperfcounters: - metric_metadata: - - metric_name: metric + metrics: + metric: description: desc unit: "1" perfcounters: diff --git a/receiver/windowsperfcountersreceiver/testdata/config-missinggaugevaluetype.yaml b/receiver/windowsperfcountersreceiver/testdata/config-missinggaugevaluetype.yaml deleted file mode 100644 index dca22c005487..000000000000 --- a/receiver/windowsperfcountersreceiver/testdata/config-missinggaugevaluetype.yaml +++ /dev/null @@ -1,26 +0,0 @@ -receivers: - windowsperfcounters: - metric_metadata: - - metric_name: metric - description: desc - unit: "1" - gauge: - value_type: "" - perfcounters: - - object: "object" - counters: - - name: counter1 - metric: metric - -processors: - nop: - -exporters: - nop: - -service: - pipelines: - metrics: - receivers: [windowsperfcounters] - processors: [nop] - exporters: [nop] diff --git a/receiver/windowsperfcountersreceiver/testdata/config-missingmetricdescription.yaml b/receiver/windowsperfcountersreceiver/testdata/config-missingmetricdescription.yaml deleted file mode 100644 index 67bba5231c56..000000000000 --- a/receiver/windowsperfcountersreceiver/testdata/config-missingmetricdescription.yaml +++ /dev/null @@ -1,25 +0,0 @@ -receivers: - windowsperfcounters: - metric_metadata: - - metric_name: metric - unit: "1" - gauge: - value_type: double - perfcounters: - - object: "object" - counters: - - name: counter1 - metric: metric - -processors: - nop: - -exporters: - nop: - -service: - pipelines: - metrics: - receivers: [windowsperfcounters] - processors: [nop] - exporters: [nop] diff --git a/receiver/windowsperfcountersreceiver/testdata/config-missingmetricname.yaml b/receiver/windowsperfcountersreceiver/testdata/config-missingmetricname.yaml deleted file mode 100644 index e30b70c70dfe..000000000000 --- a/receiver/windowsperfcountersreceiver/testdata/config-missingmetricname.yaml +++ /dev/null @@ -1,25 +0,0 @@ -receivers: - windowsperfcounters: - metric_metadata: - - description: desc - unit: "1" - gauge: - value_type: double - perfcounters: - - object: "object" - counters: - - name: counter1 - metric: metric - -processors: - nop: - -exporters: - nop: - -service: - pipelines: - metrics: - receivers: [windowsperfcounters] - processors: [nop] - exporters: [nop] diff --git a/receiver/windowsperfcountersreceiver/testdata/config-missingmetricunit.yaml b/receiver/windowsperfcountersreceiver/testdata/config-missingmetricunit.yaml deleted file mode 100644 index d07c8d7502b2..000000000000 --- a/receiver/windowsperfcountersreceiver/testdata/config-missingmetricunit.yaml +++ /dev/null @@ -1,25 +0,0 @@ -receivers: - windowsperfcounters: - metric_metadata: - - metric_name: metric - description: desc - gauge: - value_type: double - perfcounters: - - object: "object" - counters: - - name: counter1 - metric: metric - -processors: - nop: - -exporters: - nop: - -service: - pipelines: - metrics: - receivers: [windowsperfcounters] - processors: [nop] - exporters: [nop] diff --git a/receiver/windowsperfcountersreceiver/testdata/config-missingsumaggregation.yaml b/receiver/windowsperfcountersreceiver/testdata/config-missingsumaggregation.yaml index d755efc3a424..b2fec01481cf 100644 --- a/receiver/windowsperfcountersreceiver/testdata/config-missingsumaggregation.yaml +++ b/receiver/windowsperfcountersreceiver/testdata/config-missingsumaggregation.yaml @@ -1,7 +1,7 @@ receivers: windowsperfcounters: - metric_metadata: - - metric_name: metric + metrics: + metric: description: desc unit: "1" sum: diff --git a/receiver/windowsperfcountersreceiver/testdata/config-missingsumvaluetype.yaml b/receiver/windowsperfcountersreceiver/testdata/config-missingsumvaluetype.yaml deleted file mode 100644 index e85e174193b5..000000000000 --- a/receiver/windowsperfcountersreceiver/testdata/config-missingsumvaluetype.yaml +++ /dev/null @@ -1,26 +0,0 @@ -receivers: - windowsperfcounters: - metric_metadata: - - metric_name: metric - description: desc - unit: "1" - sum: - aggregation: "cumulative" - perfcounters: - - object: "object" - counters: - - name: counter1 - metric: metric - -processors: - nop: - -exporters: - nop: - -service: - pipelines: - metrics: - receivers: [windowsperfcounters] - processors: [nop] - exporters: [nop] diff --git a/receiver/windowsperfcountersreceiver/testdata/config-negative-collection-interval.yaml b/receiver/windowsperfcountersreceiver/testdata/config-negative-collection-interval.yaml index 7f01643ff3b0..28b299781679 100644 --- a/receiver/windowsperfcountersreceiver/testdata/config-negative-collection-interval.yaml +++ b/receiver/windowsperfcountersreceiver/testdata/config-negative-collection-interval.yaml @@ -1,7 +1,7 @@ receivers: windowsperfcounters: - metric_metadata: - - metric_name: metric + metrics: + metric: description: desc unit: "1" gauge: diff --git a/receiver/windowsperfcountersreceiver/testdata/config-nocounters.yaml b/receiver/windowsperfcountersreceiver/testdata/config-nocounters.yaml index d56cb88a2b54..bf29812e3d36 100644 --- a/receiver/windowsperfcountersreceiver/testdata/config-nocounters.yaml +++ b/receiver/windowsperfcountersreceiver/testdata/config-nocounters.yaml @@ -1,7 +1,7 @@ receivers: windowsperfcounters: metric_metadata: - - metric_name: metric + metric: description: desc unit: "1" gauge: diff --git a/receiver/windowsperfcountersreceiver/testdata/config-noobjectname.yaml b/receiver/windowsperfcountersreceiver/testdata/config-noobjectname.yaml index 76e5cb957ee1..6d2071705a51 100644 --- a/receiver/windowsperfcountersreceiver/testdata/config-noobjectname.yaml +++ b/receiver/windowsperfcountersreceiver/testdata/config-noobjectname.yaml @@ -1,7 +1,7 @@ receivers: windowsperfcounters: metric_metadata: - - metric_name: metric + metric: description: desc unit: "1" gauge: diff --git a/receiver/windowsperfcountersreceiver/testdata/config-noperfcounters.yaml b/receiver/windowsperfcountersreceiver/testdata/config-noperfcounters.yaml index cdc6ea115a2d..5119fde9f6f9 100644 --- a/receiver/windowsperfcountersreceiver/testdata/config-noperfcounters.yaml +++ b/receiver/windowsperfcountersreceiver/testdata/config-noperfcounters.yaml @@ -1,7 +1,7 @@ receivers: windowsperfcounters: - metric_metadata: - - metric_name: metric + metrics: + metric: description: desc unit: "1" gauge: diff --git a/receiver/windowsperfcountersreceiver/testdata/config.yaml b/receiver/windowsperfcountersreceiver/testdata/config.yaml index ba429af40cd5..e5b336e14fb3 100644 --- a/receiver/windowsperfcountersreceiver/testdata/config.yaml +++ b/receiver/windowsperfcountersreceiver/testdata/config.yaml @@ -1,7 +1,7 @@ receivers: windowsperfcounters: metric_metadata: - - metric_name: metric + metric: description: desc unit: "1" gauge: @@ -13,12 +13,12 @@ receivers: metric: metric windowsperfcounters/customname: metric_metadata: - - metric_name: metric + metric: description: desc unit: "1" gauge: value_type: double - - metric_name: metric2 + metric2: description: desc unit: "1" gauge: diff --git a/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper.go b/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper.go index 1f2b0e25064b..005c55d64709 100644 --- a/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper.go +++ b/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper.go @@ -112,10 +112,10 @@ func (s *scraper) scrape(context.Context) (pdata.Metrics, error) { metrics.EnsureCapacity(len(s.counters)) - for _, metricCfg := range s.cfg.MetricMetaData { + for name, metricCfg := range s.cfg.MetricMetaData { builtMetric := metrics.AppendEmpty() - builtMetric.SetName(metricCfg.MetricName) + builtMetric.SetName(name) builtMetric.SetDescription(metricCfg.Description) builtMetric.SetUnit(metricCfg.Unit) diff --git a/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper_test.go b/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper_test.go index 82ce762725c2..51928c003b65 100644 --- a/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper_test.go +++ b/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper_test.go @@ -78,38 +78,26 @@ func Test_WindowsPerfCounterScraper(t *testing.T) { defaultConfig := createDefaultConfig().(*Config) - defaultMetric := MetricConfig{ - MetricName: "metric", - Description: "desc", - Unit: "1", - Gauge: GaugeMetric{ - ValueType: "double", - }, - } - testCases := []testCase{ { name: "Standard", cfg: &Config{ - MetricMetaData: []MetricConfig{ - { - MetricName: "cpu.idle", + MetricMetaData: map[string]MetricConfig{ + "cpu.idle": { Description: "percentage of time CPU is idle.", Unit: "%", Gauge: GaugeMetric{ ValueType: "double", }, }, - { - MetricName: "bytes.committed", + "bytes.committed": { Description: "number of bytes committed to memory", Unit: "By", Gauge: GaugeMetric{ ValueType: "double", }, }, - { - MetricName: "processor.time", + "processor.time": { Description: "amount of time processor is busy", Unit: "%", Gauge: GaugeMetric{ @@ -129,9 +117,8 @@ func Test_WindowsPerfCounterScraper(t *testing.T) { { name: "SumMetric", cfg: &Config{ - MetricMetaData: []MetricConfig{ - { - MetricName: "bytes.committed", + MetricMetaData: map[string]MetricConfig{ + "bytes.committed": { Description: "number of bytes committed to memory", Unit: "By", Sum: SumMetric{ @@ -200,7 +187,15 @@ func Test_WindowsPerfCounterScraper(t *testing.T) { require.NoError(t, err) if test.mockCounterPath != "" || test.scrapeErr != nil || test.shutdownErr != nil { - scraper.cfg.MetricMetaData = []MetricConfig{defaultMetric} + scraper.cfg.MetricMetaData = map[string]MetricConfig{ + "metric": { + Description: "desc", + Unit: "1", + Gauge: GaugeMetric{ + ValueType: "double", + }, + }, + } scraper.counters = []PerfCounterMetrics{ { CounterScraper: newMockPerfCounter(test.mockCounterPath, test.scrapeErr, test.shutdownErr), From f724e8baddf35e8007ff918e8650b215e890b1a8 Mon Sep 17 00:00:00 2001 From: Mrod1598 Date: Tue, 15 Mar 2022 10:51:04 -0400 Subject: [PATCH 12/24] Remove Value type & Add defaults --- receiver/simpleprometheusreceiver/receiver.go | 1 + .../windowsperfcountersreceiver/README.md | 4 +-- .../windowsperfcountersreceiver/config.go | 30 ++----------------- .../config_test.go | 18 ++--------- .../factory_others_test.go | 4 +-- .../factory_test.go | 12 ++------ .../factory_windows_test.go | 4 +-- .../testdata/config-emptyinstance.yaml | 1 - .../testdata/config-missingdatatype.yaml | 24 --------------- .../config-missingsumaggregation.yaml | 1 - .../config-negative-collection-interval.yaml | 1 - .../testdata/config-nocounters.yaml | 1 - .../testdata/config-noobjectname.yaml | 1 - .../testdata/config-noperfcounters.yaml | 1 - .../testdata/config.yaml | 3 -- .../windowsperfcounters_scraper.go | 15 +++------- .../windowsperfcounters_scraper_test.go | 20 ++++--------- 17 files changed, 22 insertions(+), 119 deletions(-) delete mode 100644 receiver/windowsperfcountersreceiver/testdata/config-missingdatatype.yaml diff --git a/receiver/simpleprometheusreceiver/receiver.go b/receiver/simpleprometheusreceiver/receiver.go index 6c923b8c22ca..d24e6101a938 100644 --- a/receiver/simpleprometheusreceiver/receiver.go +++ b/receiver/simpleprometheusreceiver/receiver.go @@ -58,6 +58,7 @@ func (prw *prometheusReceiverWrapper) Start(ctx context.Context, host component. prw.prometheusRecever = pr return prw.prometheusRecever.Start(ctx, host) + return prw.prometheusRecever.(ctx, host) } func getPrometheusConfig(cfg *Config) (*prometheusreceiver.Config, error) { diff --git a/receiver/windowsperfcountersreceiver/README.md b/receiver/windowsperfcountersreceiver/README.md index 9a235af68746..8f2c5194f7fe 100644 --- a/receiver/windowsperfcountersreceiver/README.md +++ b/receiver/windowsperfcountersreceiver/README.md @@ -27,12 +27,10 @@ windowsperfcounters: description: unit: gauge: - value_type: - metric_name: description: unit: - sum: - value_type: + sum: aggregation: monotonic: perfcounters: diff --git a/receiver/windowsperfcountersreceiver/config.go b/receiver/windowsperfcountersreceiver/config.go index 41ae75a6c4f6..b093f0b69fe3 100644 --- a/receiver/windowsperfcountersreceiver/config.go +++ b/receiver/windowsperfcountersreceiver/config.go @@ -45,11 +45,9 @@ type MetricConfig struct { } type GaugeMetric struct { - ValueType string `mapstructure:"value_type"` } type SumMetric struct { - ValueType string `mapstructure:"value_type"` Aggregation string `mapstructure:"aggregation"` Monotonic bool `mapstructure:"monotonic"` } @@ -76,34 +74,12 @@ func (c *Config) Validate() error { } for name, metric := range c.MetricMetaData { - if name == "" { - errs = multierr.Append(errs, fmt.Errorf("a metric does not include a name")) - continue - } - if metric.Description == "" { - errs = multierr.Append(errs, fmt.Errorf("metric %q does not include a description", name)) - } if metric.Unit == "" { - errs = multierr.Append(errs, fmt.Errorf("metric %q does not include a unit", name)) + metric.Unit = "1" } - if (metric.Gauge == GaugeMetric{}) && (metric.Sum == SumMetric{}) { - errs = multierr.Append(errs, fmt.Errorf("metric %q does not include a metric definition", name)) - } else if (metric.Gauge != GaugeMetric{}) { - if metric.Gauge.ValueType == "" { - errs = multierr.Append(errs, fmt.Errorf("gauge metric %q does not include a value type", name)) - } else if metric.Gauge.ValueType != "int" && metric.Gauge.ValueType != "double" { - errs = multierr.Append(errs, fmt.Errorf("gauge metric %q includes an invalid value type", name)) - } - } else if (metric.Sum != SumMetric{}) { - if metric.Sum.ValueType == "" { - errs = multierr.Append(errs, fmt.Errorf("sum metric %q does not include a value type", name)) - } else if metric.Sum.ValueType != "int" && metric.Sum.ValueType != "double" { - errs = multierr.Append(errs, fmt.Errorf("sum metric %q includes an invalid value type", name)) - } - if metric.Sum.Aggregation == "" { - errs = multierr.Append(errs, fmt.Errorf("sum metric %q does not include an aggregation", name)) - } else if metric.Sum.Aggregation != "cumulative" && metric.Sum.Aggregation != "delta" { + if (metric.Sum != SumMetric{}) { + if metric.Sum.Aggregation != "cumulative" && metric.Sum.Aggregation != "delta" { errs = multierr.Append(errs, fmt.Errorf("sum metric %q includes an invalid aggregation", name)) } } else if (metric.Sum != SumMetric{}) && (metric.Gauge != GaugeMetric{}) { diff --git a/receiver/windowsperfcountersreceiver/config_test.go b/receiver/windowsperfcountersreceiver/config_test.go index 1e202ce8f12e..77166ef261f7 100644 --- a/receiver/windowsperfcountersreceiver/config_test.go +++ b/receiver/windowsperfcountersreceiver/config_test.go @@ -53,9 +53,7 @@ func TestLoadConfig(t *testing.T) { "metric": { Description: "desc", Unit: "1", - Gauge: GaugeMetric{ - ValueType: "double", - }, + Gauge: GaugeMetric{}, }, } @@ -86,16 +84,12 @@ func TestLoadConfig(t *testing.T) { "metric": { Description: "desc", Unit: "1", - Gauge: GaugeMetric{ - ValueType: "double", - }, + Gauge: GaugeMetric{}, }, "metric2": { Description: "desc", Unit: "1", - Gauge: GaugeMetric{ - ValueType: "double", - }, + Gauge: GaugeMetric{}, }, }, } @@ -118,7 +112,6 @@ func TestLoadConfig_Error(t *testing.T) { noCountersErr = `perf counter for object "%s" does not specify any counters` emptyInstanceErr = `perf counter for object "%s" includes an empty instance` undefinedMetricErr = `perf counter for object "%s" includes an undefined metric` - missingMetricMetricType = `metric "%s" does not include a metric definition` missingSumAggregation = `sum metric "%s" does not include an aggregation` missingMetrics = `must specify at least one metric` gaugeAndSum = `metric "%s" provides both a sum config and a gauge config` @@ -150,11 +143,6 @@ func TestLoadConfig_Error(t *testing.T) { cfgFile: "config-emptyinstance.yaml", expectedErr: fmt.Sprintf("%s: %s", errorPrefix, fmt.Sprintf(emptyInstanceErr, "object")), }, - { - name: "EmptyMetricMetricType", - cfgFile: "config-missingdatatype.yaml", - expectedErr: fmt.Sprintf("%s: %s", errorPrefix, fmt.Sprintf(missingMetricMetricType, "metric")), - }, { name: "EmptySumAggregation", cfgFile: "config-missingsumaggregation.yaml", diff --git a/receiver/windowsperfcountersreceiver/factory_others_test.go b/receiver/windowsperfcountersreceiver/factory_others_test.go index 4bc09d4c9ee8..23254fb323b2 100644 --- a/receiver/windowsperfcountersreceiver/factory_others_test.go +++ b/receiver/windowsperfcountersreceiver/factory_others_test.go @@ -39,9 +39,7 @@ func TestCreateMetricsReceiver(t *testing.T) { "metric": { Description: "desc", Unit: "1", - Gauge: GaugeMetric{ - ValueType: "double", - }, + Gauge: GaugeMetric{}, }, } mReceiver, err := factory.CreateMetricsReceiver(context.Background(), creationParams, cfg, consumertest.NewNop()) diff --git a/receiver/windowsperfcountersreceiver/factory_test.go b/receiver/windowsperfcountersreceiver/factory_test.go index 766863941989..ebd39113903a 100644 --- a/receiver/windowsperfcountersreceiver/factory_test.go +++ b/receiver/windowsperfcountersreceiver/factory_test.go @@ -45,9 +45,7 @@ func TestCreateDefaultConfig(t *testing.T) { "metric": { Description: "desc", Unit: "1", - Gauge: GaugeMetric{ - ValueType: "double", - }, + Gauge: GaugeMetric{}, }, } @@ -68,9 +66,7 @@ func TestCreateTracesReceiver(t *testing.T) { "metric": { Description: "desc", Unit: "1", - Gauge: GaugeMetric{ - ValueType: "double", - }, + Gauge: GaugeMetric{}, }, } tReceiver, err := factory.CreateTracesReceiver(context.Background(), creationParams, cfg, consumertest.NewNop()) @@ -93,9 +89,7 @@ func TestCreateLogsReceiver(t *testing.T) { "metric": { Description: "desc", Unit: "1", - Gauge: GaugeMetric{ - ValueType: "double", - }, + Gauge: GaugeMetric{}, }, } diff --git a/receiver/windowsperfcountersreceiver/factory_windows_test.go b/receiver/windowsperfcountersreceiver/factory_windows_test.go index 337ab0f0ee20..d6e1a4bd0edc 100644 --- a/receiver/windowsperfcountersreceiver/factory_windows_test.go +++ b/receiver/windowsperfcountersreceiver/factory_windows_test.go @@ -39,9 +39,7 @@ func TestCreateMetricsReceiver(t *testing.T) { "metric": { Description: "desc", Unit: "1", - Gauge: GaugeMetric{ - ValueType: "double", - }, + Gauge: GaugeMetric{}, }, } diff --git a/receiver/windowsperfcountersreceiver/testdata/config-emptyinstance.yaml b/receiver/windowsperfcountersreceiver/testdata/config-emptyinstance.yaml index 7f5cb8e0bfb7..5a8b2091dc67 100644 --- a/receiver/windowsperfcountersreceiver/testdata/config-emptyinstance.yaml +++ b/receiver/windowsperfcountersreceiver/testdata/config-emptyinstance.yaml @@ -5,7 +5,6 @@ receivers: description: desc unit: "1" gauge: - value_type: double perfcounters: - object: "object" instances: [""] diff --git a/receiver/windowsperfcountersreceiver/testdata/config-missingdatatype.yaml b/receiver/windowsperfcountersreceiver/testdata/config-missingdatatype.yaml deleted file mode 100644 index 6ebba19e4094..000000000000 --- a/receiver/windowsperfcountersreceiver/testdata/config-missingdatatype.yaml +++ /dev/null @@ -1,24 +0,0 @@ -receivers: - windowsperfcounters: - metrics: - metric: - description: desc - unit: "1" - perfcounters: - - object: "object" - counters: - - name: counter1 - metric: metric - -processors: - nop: - -exporters: - nop: - -service: - pipelines: - metrics: - receivers: [windowsperfcounters] - processors: [nop] - exporters: [nop] diff --git a/receiver/windowsperfcountersreceiver/testdata/config-missingsumaggregation.yaml b/receiver/windowsperfcountersreceiver/testdata/config-missingsumaggregation.yaml index b2fec01481cf..c6febae547ea 100644 --- a/receiver/windowsperfcountersreceiver/testdata/config-missingsumaggregation.yaml +++ b/receiver/windowsperfcountersreceiver/testdata/config-missingsumaggregation.yaml @@ -5,7 +5,6 @@ receivers: description: desc unit: "1" sum: - value_type: "double" perfcounters: - object: "object" counters: diff --git a/receiver/windowsperfcountersreceiver/testdata/config-negative-collection-interval.yaml b/receiver/windowsperfcountersreceiver/testdata/config-negative-collection-interval.yaml index 28b299781679..83a5c9d25a64 100644 --- a/receiver/windowsperfcountersreceiver/testdata/config-negative-collection-interval.yaml +++ b/receiver/windowsperfcountersreceiver/testdata/config-negative-collection-interval.yaml @@ -5,7 +5,6 @@ receivers: description: desc unit: "1" gauge: - value_type: double collection_interval: -1m perfcounters: - object: "object" diff --git a/receiver/windowsperfcountersreceiver/testdata/config-nocounters.yaml b/receiver/windowsperfcountersreceiver/testdata/config-nocounters.yaml index bf29812e3d36..df786852fe4f 100644 --- a/receiver/windowsperfcountersreceiver/testdata/config-nocounters.yaml +++ b/receiver/windowsperfcountersreceiver/testdata/config-nocounters.yaml @@ -5,7 +5,6 @@ receivers: description: desc unit: "1" gauge: - value_type: double perfcounters: - object: "object" diff --git a/receiver/windowsperfcountersreceiver/testdata/config-noobjectname.yaml b/receiver/windowsperfcountersreceiver/testdata/config-noobjectname.yaml index 6d2071705a51..a30a782efdd8 100644 --- a/receiver/windowsperfcountersreceiver/testdata/config-noobjectname.yaml +++ b/receiver/windowsperfcountersreceiver/testdata/config-noobjectname.yaml @@ -5,7 +5,6 @@ receivers: description: desc unit: "1" gauge: - value_type: double perfcounters: - counters: diff --git a/receiver/windowsperfcountersreceiver/testdata/config-noperfcounters.yaml b/receiver/windowsperfcountersreceiver/testdata/config-noperfcounters.yaml index 5119fde9f6f9..03d5d4eb3382 100644 --- a/receiver/windowsperfcountersreceiver/testdata/config-noperfcounters.yaml +++ b/receiver/windowsperfcountersreceiver/testdata/config-noperfcounters.yaml @@ -5,7 +5,6 @@ receivers: description: desc unit: "1" gauge: - value_type: double processors: nop: diff --git a/receiver/windowsperfcountersreceiver/testdata/config.yaml b/receiver/windowsperfcountersreceiver/testdata/config.yaml index e5b336e14fb3..f2b01e4935e2 100644 --- a/receiver/windowsperfcountersreceiver/testdata/config.yaml +++ b/receiver/windowsperfcountersreceiver/testdata/config.yaml @@ -5,7 +5,6 @@ receivers: description: desc unit: "1" gauge: - value_type: double perfcounters: - object: "object" counters: @@ -17,12 +16,10 @@ receivers: description: desc unit: "1" gauge: - value_type: double metric2: description: desc unit: "1" gauge: - value_type: double collection_interval: 30s perfcounters: - object: object1 diff --git a/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper.go b/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper.go index 005c55d64709..0e0a66d04026 100644 --- a/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper.go +++ b/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper.go @@ -119,9 +119,7 @@ func (s *scraper) scrape(context.Context) (pdata.Metrics, error) { builtMetric.SetDescription(metricCfg.Description) builtMetric.SetUnit(metricCfg.Unit) - if (metricCfg.Gauge != GaugeMetric{}) { - builtMetric.SetDataType(pdata.MetricDataTypeGauge) - } else if (metricCfg.Sum != SumMetric{}) { + if (metricCfg.Sum != SumMetric{}) { builtMetric.SetDataType(pdata.MetricDataTypeSum) builtMetric.Sum().SetIsMonotonic(metricCfg.Sum.Monotonic) @@ -131,6 +129,8 @@ func (s *scraper) scrape(context.Context) (pdata.Metrics, error) { case "delta": builtMetric.Sum().SetAggregationTemporality(pdata.MetricAggregationTemporalityDelta) } + } else { + builtMetric.SetDataType(pdata.MetricDataTypeGauge) } for _, counter := range s.counters { @@ -150,14 +150,11 @@ func (s *scraper) scrape(context.Context) (pdata.Metrics, error) { func initializeMetricDps(metricCfg MetricConfig, metric pdata.Metric, now pdata.Timestamp, counterValues []win_perf_counters.CounterValue, attributes map[string]string) { var dps pdata.NumberDataPointSlice - var valueType string if metric.DataType() == pdata.MetricDataTypeGauge { dps = metric.Gauge().DataPoints() - valueType = metricCfg.Gauge.ValueType } else { dps = metric.Sum().DataPoints() - valueType = metricCfg.Sum.ValueType } dps.EnsureCapacity(len(counterValues)) @@ -171,10 +168,6 @@ func initializeMetricDps(metricCfg MetricConfig, metric pdata.Metric, now pdata. } dp.SetTimestamp(now) - if valueType == "int" { - dp.SetIntVal(int64(counterValue.Value)) - } else if valueType == "double" { - dp.SetDoubleVal(counterValue.Value) - } + dp.SetIntVal(int64(counterValue.Value)) } } diff --git a/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper_test.go b/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper_test.go index 51928c003b65..02848c6f0a1d 100644 --- a/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper_test.go +++ b/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper_test.go @@ -86,23 +86,17 @@ func Test_WindowsPerfCounterScraper(t *testing.T) { "cpu.idle": { Description: "percentage of time CPU is idle.", Unit: "%", - Gauge: GaugeMetric{ - ValueType: "double", - }, + Gauge: GaugeMetric{}, }, "bytes.committed": { Description: "number of bytes committed to memory", Unit: "By", - Gauge: GaugeMetric{ - ValueType: "double", - }, + Gauge: GaugeMetric{}, }, "processor.time": { Description: "amount of time processor is busy", Unit: "%", - Gauge: GaugeMetric{ - ValueType: "double", - }, + Gauge: GaugeMetric{}, }, }, PerfCounters: []PerfCounterConfig{ @@ -121,9 +115,7 @@ func Test_WindowsPerfCounterScraper(t *testing.T) { "bytes.committed": { Description: "number of bytes committed to memory", Unit: "By", - Sum: SumMetric{ - ValueType: "int", - }, + Sum: SumMetric{}, }, }, PerfCounters: []PerfCounterConfig{ @@ -191,9 +183,7 @@ func Test_WindowsPerfCounterScraper(t *testing.T) { "metric": { Description: "desc", Unit: "1", - Gauge: GaugeMetric{ - ValueType: "double", - }, + Gauge: GaugeMetric{}, }, } scraper.counters = []PerfCounterMetrics{ From 4e863f78ac455fd290710565b0aacb49e0d9cf20 Mon Sep 17 00:00:00 2001 From: Mrod1598 Date: Tue, 15 Mar 2022 10:52:53 -0400 Subject: [PATCH 13/24] Remove extra test case --- receiver/windowsperfcountersreceiver/config_test.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/receiver/windowsperfcountersreceiver/config_test.go b/receiver/windowsperfcountersreceiver/config_test.go index 77166ef261f7..ad46999e0148 100644 --- a/receiver/windowsperfcountersreceiver/config_test.go +++ b/receiver/windowsperfcountersreceiver/config_test.go @@ -148,11 +148,6 @@ func TestLoadConfig_Error(t *testing.T) { cfgFile: "config-missingsumaggregation.yaml", expectedErr: fmt.Sprintf("%s: %s", errorPrefix, fmt.Sprintf(missingSumAggregation, "metric")), }, - { - name: "GaugeAndSum", - cfgFile: "config-gaugeandsum.yaml", - expectedErr: fmt.Sprintf("%s: %s", errorPrefix, fmt.Sprintf(missingSumAggregation, "metric")), - }, { name: "AllErrors", cfgFile: "config-allerrors.yaml", From 7ecedb647c6a00acf7efc1dcc2dce51646fda922 Mon Sep 17 00:00:00 2001 From: Miguel Rodriguez Date: Tue, 15 Mar 2022 11:36:14 -0400 Subject: [PATCH 14/24] Fix tests --- receiver/simpleprometheusreceiver/receiver.go | 1 - .../config_test.go | 6 ----- .../config-missingsumaggregation.yaml | 25 ------------------- .../testdata/config-nocounters.yaml | 2 +- .../testdata/config-noobjectname.yaml | 2 +- .../testdata/config.yaml | 4 +-- 6 files changed, 4 insertions(+), 36 deletions(-) delete mode 100644 receiver/windowsperfcountersreceiver/testdata/config-missingsumaggregation.yaml diff --git a/receiver/simpleprometheusreceiver/receiver.go b/receiver/simpleprometheusreceiver/receiver.go index d24e6101a938..6c923b8c22ca 100644 --- a/receiver/simpleprometheusreceiver/receiver.go +++ b/receiver/simpleprometheusreceiver/receiver.go @@ -58,7 +58,6 @@ func (prw *prometheusReceiverWrapper) Start(ctx context.Context, host component. prw.prometheusRecever = pr return prw.prometheusRecever.Start(ctx, host) - return prw.prometheusRecever.(ctx, host) } func getPrometheusConfig(cfg *Config) (*prometheusreceiver.Config, error) { diff --git a/receiver/windowsperfcountersreceiver/config_test.go b/receiver/windowsperfcountersreceiver/config_test.go index ad46999e0148..6f505865393e 100644 --- a/receiver/windowsperfcountersreceiver/config_test.go +++ b/receiver/windowsperfcountersreceiver/config_test.go @@ -112,7 +112,6 @@ func TestLoadConfig_Error(t *testing.T) { noCountersErr = `perf counter for object "%s" does not specify any counters` emptyInstanceErr = `perf counter for object "%s" includes an empty instance` undefinedMetricErr = `perf counter for object "%s" includes an undefined metric` - missingSumAggregation = `sum metric "%s" does not include an aggregation` missingMetrics = `must specify at least one metric` gaugeAndSum = `metric "%s" provides both a sum config and a gauge config` ) @@ -143,11 +142,6 @@ func TestLoadConfig_Error(t *testing.T) { cfgFile: "config-emptyinstance.yaml", expectedErr: fmt.Sprintf("%s: %s", errorPrefix, fmt.Sprintf(emptyInstanceErr, "object")), }, - { - name: "EmptySumAggregation", - cfgFile: "config-missingsumaggregation.yaml", - expectedErr: fmt.Sprintf("%s: %s", errorPrefix, fmt.Sprintf(missingSumAggregation, "metric")), - }, { name: "AllErrors", cfgFile: "config-allerrors.yaml", diff --git a/receiver/windowsperfcountersreceiver/testdata/config-missingsumaggregation.yaml b/receiver/windowsperfcountersreceiver/testdata/config-missingsumaggregation.yaml deleted file mode 100644 index c6febae547ea..000000000000 --- a/receiver/windowsperfcountersreceiver/testdata/config-missingsumaggregation.yaml +++ /dev/null @@ -1,25 +0,0 @@ -receivers: - windowsperfcounters: - metrics: - metric: - description: desc - unit: "1" - sum: - perfcounters: - - object: "object" - counters: - - name: counter1 - metric: metric - -processors: - nop: - -exporters: - nop: - -service: - pipelines: - metrics: - receivers: [windowsperfcounters] - processors: [nop] - exporters: [nop] diff --git a/receiver/windowsperfcountersreceiver/testdata/config-nocounters.yaml b/receiver/windowsperfcountersreceiver/testdata/config-nocounters.yaml index df786852fe4f..7781bb162af0 100644 --- a/receiver/windowsperfcountersreceiver/testdata/config-nocounters.yaml +++ b/receiver/windowsperfcountersreceiver/testdata/config-nocounters.yaml @@ -1,6 +1,6 @@ receivers: windowsperfcounters: - metric_metadata: + metrics: metric: description: desc unit: "1" diff --git a/receiver/windowsperfcountersreceiver/testdata/config-noobjectname.yaml b/receiver/windowsperfcountersreceiver/testdata/config-noobjectname.yaml index a30a782efdd8..ad2d27ed10c7 100644 --- a/receiver/windowsperfcountersreceiver/testdata/config-noobjectname.yaml +++ b/receiver/windowsperfcountersreceiver/testdata/config-noobjectname.yaml @@ -1,6 +1,6 @@ receivers: windowsperfcounters: - metric_metadata: + metrics: metric: description: desc unit: "1" diff --git a/receiver/windowsperfcountersreceiver/testdata/config.yaml b/receiver/windowsperfcountersreceiver/testdata/config.yaml index f2b01e4935e2..069e7919365e 100644 --- a/receiver/windowsperfcountersreceiver/testdata/config.yaml +++ b/receiver/windowsperfcountersreceiver/testdata/config.yaml @@ -1,6 +1,6 @@ receivers: windowsperfcounters: - metric_metadata: + metrics: metric: description: desc unit: "1" @@ -11,7 +11,7 @@ receivers: - name: counter1 metric: metric windowsperfcounters/customname: - metric_metadata: + metrics: metric: description: desc unit: "1" From 473d0b006b426b2c46ef40c102bb7333cd085e4b Mon Sep 17 00:00:00 2001 From: Mrod1598 Date: Wed, 16 Mar 2022 15:12:58 -0400 Subject: [PATCH 15/24] Add handling for no metrics defined --- .../windowsperfcountersreceiver/README.md | 85 ++++++++++++------- .../windowsperfcountersreceiver/config.go | 8 +- .../config_test.go | 3 - .../windowsperfcounters_scraper.go | 43 ++++++++-- 4 files changed, 96 insertions(+), 43 deletions(-) diff --git a/receiver/windowsperfcountersreceiver/README.md b/receiver/windowsperfcountersreceiver/README.md index 8f2c5194f7fe..b15dc3b5d298 100644 --- a/receiver/windowsperfcountersreceiver/README.md +++ b/receiver/windowsperfcountersreceiver/README.md @@ -14,31 +14,37 @@ warning will be printed, but the application will not fail fast. It is expected that some performance counters may not exist on some systems due to different OS configuration. + + ## Configuration + + + + The collection interval and the list of performance counters to be scraped can be configured: ```yaml windowsperfcounters: collection_interval: # default = "1m" - metric_metadata: - - metric_name: - description: - unit: - gauge: - - metric_name: - description: - unit: - sum: - aggregation: - monotonic: + metrics: + : + description: + unit: + gauge: + : + description: + unit: + sum: + aggregation: + monotonic: perfcounters: - object: instances: []* counters: - name: - metric_name: + metric: attributes: : ``` @@ -64,40 +70,38 @@ you can configure multiple `windowsperfcounters` receivers with different ```yaml receivers: windowsperfcounters/memory: - metric_metadata: - - metric_name: bytes.committed + metrics: + bytes.committed: description: the number of bytes committed to memory unit: By gauge: - value_type: int collection_interval: 30s perfcounters: - object: Memory counters: - name: Committed Bytes - metric_name: bytes.committed + metric: bytes.committed windowsperfcounters/processor: collection_interval: 1m - metric_metadata: - - metric_name: processor.time + metrics: + processor.time: description: active and idle time of the processor unit: "%" gauge: - value_type: double perfcounters: - object: "Processor" instances: "*" counters: - name: "% Processor Time" - metric_name: processor.time + metric: processor.time attributes: state: active - object: "Processor" instances: [1, 2] counters: - name: "% Idle Time" - metric_name: processor.time + metric: processor.time attributes: state: idle @@ -109,7 +113,29 @@ service: ### Defining metric format -To report metrics in the desired output format, build a metric the metric and reference it in the given counter with any applicable attributes. +To report metrics in the desired output format, build a metric the metric and reference it in the given counter with any applicable attributes. Metrics will default to gauges if no other metric type is defined. + +| Field Name | Description | Value | Default | +| -- | -- | -- | -- | +| name | The key for the metric. | string | Counter Name | +| description | definition of what the metric measures. | string | | +| unit | what is being measured. | string | `1` | +| sum | representation of a sum metric. | Sum Config | | +| gauge | representation of a gauge metric. | Gauge Config | | + + +#### Sum Config + +| Field Name | Description | Value | Default | +| -- | -- | -- | -- | +| aggregation | The type of aggregation temporality for the metric. | [`cumulative` or `delta`] | | +| monotonic | whether or not the metric value can decrease. | false | | + +#### Gauge Config + +| Field Name | Description | Value | Default | +| -- | -- | -- | -- | +||||| e.g. To output the `Memory/Committed Bytes` counter as a metric with the name `bytes.committed`: @@ -117,17 +143,18 @@ e.g. To output the `Memory/Committed Bytes` counter as a metric with the name ```yaml receivers: windowsperfcounters: - metric_metadata: - - metric_name: bytes.committed - description: the number of bytes committed to memory - unit: By - gauge: - value_type: int + metrics: + bytes.committed: + description: the number of bytes committed to memory + unit: By + gauge: + value_type: int collection_interval: 30s perfcounters: - object: Memory counters: - - Committed Bytes + - name: Committed Bytes + metric: bytes.committed service: pipelines: diff --git a/receiver/windowsperfcountersreceiver/config.go b/receiver/windowsperfcountersreceiver/config.go index b093f0b69fe3..dce18c985e2d 100644 --- a/receiver/windowsperfcountersreceiver/config.go +++ b/receiver/windowsperfcountersreceiver/config.go @@ -69,10 +69,6 @@ func (c *Config) Validate() error { errs = multierr.Append(errs, fmt.Errorf("must specify at least one perf counter")) } - if len(c.MetricMetaData) == 0 { - errs = multierr.Append(errs, fmt.Errorf("must specify at least one metric")) - } - for name, metric := range c.MetricMetaData { if metric.Unit == "" { metric.Unit = "1" @@ -99,6 +95,10 @@ func (c *Config) Validate() error { } for _, counter := range pc.Counters { + if counter.Metric == "" { + continue + } + foundMatchingMetric := false for name := range c.MetricMetaData { if counter.Metric == name { diff --git a/receiver/windowsperfcountersreceiver/config_test.go b/receiver/windowsperfcountersreceiver/config_test.go index 6f505865393e..b72b1ba4febf 100644 --- a/receiver/windowsperfcountersreceiver/config_test.go +++ b/receiver/windowsperfcountersreceiver/config_test.go @@ -111,9 +111,6 @@ func TestLoadConfig_Error(t *testing.T) { noObjectNameErr = "must specify object name for all perf counters" noCountersErr = `perf counter for object "%s" does not specify any counters` emptyInstanceErr = `perf counter for object "%s" includes an empty instance` - undefinedMetricErr = `perf counter for object "%s" includes an undefined metric` - missingMetrics = `must specify at least one metric` - gaugeAndSum = `metric "%s" provides both a sum config and a gauge config` ) testCases := []testCase{ diff --git a/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper.go b/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper.go index 0e0a66d04026..7b9a5c3196db 100644 --- a/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper.go +++ b/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper.go @@ -44,9 +44,10 @@ type PerfCounterScraper interface { // scraper is the type that scrapes various host metrics. type scraper struct { - cfg *Config - settings component.TelemetrySettings - counters []PerfCounterMetrics + cfg *Config + settings component.TelemetrySettings + counters []PerfCounterMetrics + undefinedMetricCounters []PerfCounterScraper } type PerfCounterMetrics struct { @@ -72,6 +73,9 @@ func (s *scraper) start(context.Context, component.Host) error { if err != nil { errs = multierr.Append(errs, fmt.Errorf("counter %v: %w", counterPath, err)) } else { + if counterCfg.Metric == "" { + s.undefinedMetricCounters = append(s.undefinedMetricCounters, c) + } s.counters = append(s.counters, PerfCounterMetrics{CounterScraper: c, Metric: counterCfg.Metric, Attributes: counterCfg.Attributes}) } } @@ -140,15 +144,37 @@ func (s *scraper) scrape(context.Context) (pdata.Metrics, error) { errs = multierr.Append(errs, err) continue } - initializeMetricDps(metricCfg, builtMetric, now, counterValues, counter.Attributes) + initializeMetricDps(builtMetric, now, counterValues, counter.Attributes) } } } + for _, counter := range s.undefinedMetricCounters { + counterValues, err := counter.ScrapeData() + if err != nil { + errs = multierr.Append(errs, err) + continue + } + + builtMetric := metrics.AppendEmpty() + builtMetric.SetName(counter.Path()) + builtMetric.SetDataType(pdata.MetricDataTypeGauge) + initializeMetricDps(builtMetric, now, counterValues, nil) + } + return md, errs } -func initializeMetricDps(metricCfg MetricConfig, metric pdata.Metric, now pdata.Timestamp, counterValues []win_perf_counters.CounterValue, attributes map[string]string) { +func initializeNumberDataPointAsDouble(dataPoint pdata.NumberDataPoint, now pdata.Timestamp, instanceLabel string, value float64) { + if instanceLabel != "" { + dataPoint.Attributes().InsertString(instanceLabelName, instanceLabel) + } + + dataPoint.SetTimestamp(now) + dataPoint.SetDoubleVal(value) +} + +func initializeMetricDps(metric pdata.Metric, now pdata.Timestamp, counterValues []win_perf_counters.CounterValue, attributes map[string]string) { var dps pdata.NumberDataPointSlice if metric.DataType() == pdata.MetricDataTypeGauge { @@ -160,9 +186,12 @@ func initializeMetricDps(metricCfg MetricConfig, metric pdata.Metric, now pdata. dps.EnsureCapacity(len(counterValues)) for _, counterValue := range counterValues { dp := dps.AppendEmpty() - for attKey, attVal := range attributes { - dp.Attributes().InsertString(attKey, attVal) + if attributes != nil { + for attKey, attVal := range attributes { + dp.Attributes().InsertString(attKey, attVal) + } } + if counterValue.InstanceName != "" { dp.Attributes().InsertString(instanceLabelName, counterValue.InstanceName) } From 6a18fa11b1e9d6ef28afa73f5c2cfeea8f72d50e Mon Sep 17 00:00:00 2001 From: Miguel Rodriguez Date: Wed, 16 Mar 2022 15:32:25 -0400 Subject: [PATCH 16/24] Add no metric definition test --- .../config_test.go | 3 +-- .../testdata/scraper/no_metric_def.json | 25 +++++++++++++++++++ .../windowsperfcounters_scraper_test.go | 10 ++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 receiver/windowsperfcountersreceiver/testdata/scraper/no_metric_def.json diff --git a/receiver/windowsperfcountersreceiver/config_test.go b/receiver/windowsperfcountersreceiver/config_test.go index b72b1ba4febf..62c11c286e19 100644 --- a/receiver/windowsperfcountersreceiver/config_test.go +++ b/receiver/windowsperfcountersreceiver/config_test.go @@ -143,10 +143,9 @@ func TestLoadConfig_Error(t *testing.T) { name: "AllErrors", cfgFile: "config-allerrors.yaml", expectedErr: fmt.Sprintf( - "%s: %s; %s; %s; %s; %s", + "%s: %s; %s; %s; %s", errorPrefix, negativeCollectionIntervalErr, - missingMetrics, fmt.Sprintf(noCountersErr, "object"), fmt.Sprintf(emptyInstanceErr, "object"), noObjectNameErr, diff --git a/receiver/windowsperfcountersreceiver/testdata/scraper/no_metric_def.json b/receiver/windowsperfcountersreceiver/testdata/scraper/no_metric_def.json new file mode 100644 index 000000000000..f90b666ee4e3 --- /dev/null +++ b/receiver/windowsperfcountersreceiver/testdata/scraper/no_metric_def.json @@ -0,0 +1,25 @@ +{ + "resourceMetrics": [ + { + "instrumentationLibraryMetrics": [ + { + "instrumentationLibrary": {}, + "metrics": [ + { + "gauge": { + "dataPoints": [ + { + "asInt": "25089622016", + "timeUnixNano": "1647459021285009300" + } + ] + }, + "name": "\\Memory\\Committed Bytes" + } + ] + } + ], + "resource": {} + } + ] +} diff --git a/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper_test.go b/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper_test.go index 02848c6f0a1d..07ca3e522b7a 100644 --- a/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper_test.go +++ b/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper_test.go @@ -125,6 +125,16 @@ func Test_WindowsPerfCounterScraper(t *testing.T) { }, expectedMetricPath: filepath.Join("testdata", "scraper", "sum_metric.json"), }, + { + name: "NoMetricDefinition", + cfg: &Config{ + PerfCounters: []PerfCounterConfig{ + {Object: "Memory", Counters: []CounterConfig{{Name: "Committed Bytes"}}}, + }, + ScraperControllerSettings: scraperhelper.ScraperControllerSettings{CollectionInterval: time.Minute}, + }, + expectedMetricPath: filepath.Join("testdata", "scraper", "no_metric_def.json"), + }, { name: "InvalidCounter", cfg: &Config{ From 4e61f8ef1dccb2d19977ebb57a4640032ede1ed0 Mon Sep 17 00:00:00 2001 From: Mrod1598 Date: Thu, 17 Mar 2022 10:34:53 -0400 Subject: [PATCH 17/24] Rework Readme wording --- receiver/windowsperfcountersreceiver/README.md | 12 ++---------- receiver/windowsperfcountersreceiver/config.go | 6 ++++-- .../windowsperfcountersreceiver/testdata/config.yaml | 1 - .../windowsperfcounters_scraper.go | 4 ++-- 4 files changed, 8 insertions(+), 15 deletions(-) diff --git a/receiver/windowsperfcountersreceiver/README.md b/receiver/windowsperfcountersreceiver/README.md index b15dc3b5d298..ab194a9ac67d 100644 --- a/receiver/windowsperfcountersreceiver/README.md +++ b/receiver/windowsperfcountersreceiver/README.md @@ -14,14 +14,8 @@ warning will be printed, but the application will not fail fast. It is expected that some performance counters may not exist on some systems due to different OS configuration. - - ## Configuration - - - - The collection interval and the list of performance counters to be scraped can be configured: @@ -113,7 +107,7 @@ service: ### Defining metric format -To report metrics in the desired output format, build a metric the metric and reference it in the given counter with any applicable attributes. Metrics will default to gauges if no other metric type is defined. +To report metrics in the desired output format, define a metric and reference it in the corresponding counter, along with any applicable attributes. The metric's data type can either be `gauge` (default) or `sum`. | Field Name | Description | Value | Default | | -- | -- | -- | -- | @@ -133,9 +127,7 @@ To report metrics in the desired output format, build a metric the metric and re #### Gauge Config -| Field Name | Description | Value | Default | -| -- | -- | -- | -- | -||||| +A `gauge` config currently accepts no settings. It is specified as an object for forwards compatibility. e.g. To output the `Memory/Committed Bytes` counter as a metric with the name `bytes.committed`: diff --git a/receiver/windowsperfcountersreceiver/config.go b/receiver/windowsperfcountersreceiver/config.go index dce18c985e2d..f73b356c4c6e 100644 --- a/receiver/windowsperfcountersreceiver/config.go +++ b/receiver/windowsperfcountersreceiver/config.go @@ -75,11 +75,13 @@ func (c *Config) Validate() error { } if (metric.Sum != SumMetric{}) { + if (metric.Gauge != GaugeMetric{}) { + errs = multierr.Append(errs, fmt.Errorf("metric %q provides both a sum config and a gauge config", name)) + } + if metric.Sum.Aggregation != "cumulative" && metric.Sum.Aggregation != "delta" { errs = multierr.Append(errs, fmt.Errorf("sum metric %q includes an invalid aggregation", name)) } - } else if (metric.Sum != SumMetric{}) && (metric.Gauge != GaugeMetric{}) { - errs = multierr.Append(errs, fmt.Errorf("metric %q provides both a sum config and a gauge config", name)) } } diff --git a/receiver/windowsperfcountersreceiver/testdata/config.yaml b/receiver/windowsperfcountersreceiver/testdata/config.yaml index 069e7919365e..41660cebd152 100644 --- a/receiver/windowsperfcountersreceiver/testdata/config.yaml +++ b/receiver/windowsperfcountersreceiver/testdata/config.yaml @@ -45,4 +45,3 @@ service: receivers: [windowsperfcounters] processors: [nop] exporters: [nop] - \ No newline at end of file diff --git a/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper.go b/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper.go index 7b9a5c3196db..8f44ce1693c8 100644 --- a/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper.go +++ b/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper.go @@ -57,8 +57,7 @@ type PerfCounterMetrics struct { } func newScraper(cfg *Config, settings component.TelemetrySettings) *scraper { - s := &scraper{cfg: cfg, settings: settings} - return s + return &scraper{cfg: cfg, settings: settings} } func (s *scraper) start(context.Context, component.Host) error { @@ -75,6 +74,7 @@ func (s *scraper) start(context.Context, component.Host) error { } else { if counterCfg.Metric == "" { s.undefinedMetricCounters = append(s.undefinedMetricCounters, c) + continue } s.counters = append(s.counters, PerfCounterMetrics{CounterScraper: c, Metric: counterCfg.Metric, Attributes: counterCfg.Attributes}) } From 5b249396edfceeb101144138d064a333269a3451 Mon Sep 17 00:00:00 2001 From: Mrod1598 Date: Thu, 17 Mar 2022 14:05:54 -0400 Subject: [PATCH 18/24] Add no metrics factory test --- .../windowsperfcountersreceiver/factory_test.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/receiver/windowsperfcountersreceiver/factory_test.go b/receiver/windowsperfcountersreceiver/factory_test.go index ebd39113903a..5d3101772e0a 100644 --- a/receiver/windowsperfcountersreceiver/factory_test.go +++ b/receiver/windowsperfcountersreceiver/factory_test.go @@ -75,6 +75,21 @@ func TestCreateTracesReceiver(t *testing.T) { assert.Nil(t, tReceiver) } +func TestCreateTracesReceiverNoMetrics(t *testing.T) { + factory := NewFactory() + cfg := factory.CreateDefaultConfig() + cfg.(*Config).PerfCounters = []PerfCounterConfig{ + { + Object: "object", + Counters: []CounterConfig{{Name: "counter"}}, + }, + } + tReceiver, err := factory.CreateTracesReceiver(context.Background(), creationParams, cfg, consumertest.NewNop()) + + assert.ErrorIs(t, err, componenterror.ErrDataTypeIsNotSupported) + assert.Nil(t, tReceiver) +} + func TestCreateLogsReceiver(t *testing.T) { factory := NewFactory() cfg := factory.CreateDefaultConfig() From 566c5abbfdab7539e35c75954fbb1e06a5606620 Mon Sep 17 00:00:00 2001 From: Mrod1598 Date: Thu, 17 Mar 2022 14:29:54 -0400 Subject: [PATCH 19/24] Add no Metrics config test --- .../config_test.go | 19 +++++++++++++++++++ .../testdata/config-nometrics.yaml | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 receiver/windowsperfcountersreceiver/testdata/config-nometrics.yaml diff --git a/receiver/windowsperfcountersreceiver/config_test.go b/receiver/windowsperfcountersreceiver/config_test.go index 62c11c286e19..6820422a7e2b 100644 --- a/receiver/windowsperfcountersreceiver/config_test.go +++ b/receiver/windowsperfcountersreceiver/config_test.go @@ -97,6 +97,25 @@ func TestLoadConfig(t *testing.T) { assert.Equal(t, expectedConfig, r1) } +func TestLoadConfigNoMetrics(t *testing.T) { + factories, err := componenttest.NopFactories() + require.NoError(t, err) + + factory := NewFactory() + factories.Receivers[typeStr] = factory + cfg, err := servicetest.LoadConfigAndValidate(filepath.Join("testdata", "config-nometrics.yaml"), factories) + require.NoError(t, err) + require.NotNil(t, cfg) + + assert.Equal(t, len(cfg.Receivers), 1) + + r0 := cfg.Receivers[config.NewComponentID(typeStr)] + defaultConfigSingleObject := factory.CreateDefaultConfig() + + defaultConfigSingleObject.(*Config).PerfCounters = []PerfCounterConfig{{Object: "object", Counters: []CounterConfig{{Name: "counter1"}}}} + assert.Equal(t, defaultConfigSingleObject, r0) +} + func TestLoadConfig_Error(t *testing.T) { type testCase struct { name string diff --git a/receiver/windowsperfcountersreceiver/testdata/config-nometrics.yaml b/receiver/windowsperfcountersreceiver/testdata/config-nometrics.yaml new file mode 100644 index 000000000000..efa3251595b5 --- /dev/null +++ b/receiver/windowsperfcountersreceiver/testdata/config-nometrics.yaml @@ -0,0 +1,19 @@ +receivers: + windowsperfcounters: + perfcounters: + - object: "object" + counters: + - name: counter1 + +processors: + nop: + +exporters: + nop: + +service: + pipelines: + metrics: + receivers: [windowsperfcounters] + processors: [nop] + exporters: [nop] From 412c4310faa971fdc9ab2ca35576a1c1f3258be4 Mon Sep 17 00:00:00 2001 From: Mrod1598 Date: Thu, 17 Mar 2022 15:15:53 -0400 Subject: [PATCH 20/24] Add no metric specified test --- .../config_test.go | 67 +++++++++++++++---- .../testdata/config-nometricspecified.yaml | 24 +++++++ 2 files changed, 79 insertions(+), 12 deletions(-) create mode 100644 receiver/windowsperfcountersreceiver/testdata/config-nometricspecified.yaml diff --git a/receiver/windowsperfcountersreceiver/config_test.go b/receiver/windowsperfcountersreceiver/config_test.go index 6820422a7e2b..be1f38cc4547 100644 --- a/receiver/windowsperfcountersreceiver/config_test.go +++ b/receiver/windowsperfcountersreceiver/config_test.go @@ -98,22 +98,65 @@ func TestLoadConfig(t *testing.T) { } func TestLoadConfigNoMetrics(t *testing.T) { - factories, err := componenttest.NopFactories() - require.NoError(t, err) + testCases := []struct { + TestName string + TestPath string + Expected Config + }{ + { + TestName: "NoMetricsDefined", + TestPath: filepath.Join("testdata", "config-nometrics.yaml"), + Expected: Config{ + PerfCounters: []PerfCounterConfig{ + { + Object: "object1", + Counters: []CounterConfig{{Name: "counter1"}}, + }, + }, + }, + }, + { + TestName: "NoMetricSpecified", + TestPath: filepath.Join("testdata", "config-nometricspecified.yaml"), + Expected: Config{ + PerfCounters: []PerfCounterConfig{ + { + Object: "object1", + Counters: []CounterConfig{{Name: "counter1"}}, + }, + }, + MetricMetaData: map[string]MetricConfig{ + "metric": { + Description: "desc", + Unit: "1", + Gauge: GaugeMetric{}, + }, + }, + }, + }, + } + for _, test := range testCases { + t.Run(test.TestName, func(t *testing.T) { + factories, err := componenttest.NopFactories() + require.NoError(t, err) - factory := NewFactory() - factories.Receivers[typeStr] = factory - cfg, err := servicetest.LoadConfigAndValidate(filepath.Join("testdata", "config-nometrics.yaml"), factories) - require.NoError(t, err) - require.NotNil(t, cfg) + factory := NewFactory() + factories.Receivers[typeStr] = factory + cfg, err := servicetest.LoadConfigAndValidate(test.TestPath, factories) + + require.NoError(t, err) + require.NotNil(t, cfg) - assert.Equal(t, len(cfg.Receivers), 1) + assert.Equal(t, len(cfg.Receivers), 2) - r0 := cfg.Receivers[config.NewComponentID(typeStr)] - defaultConfigSingleObject := factory.CreateDefaultConfig() + actualReceiver := cfg.Receivers[config.NewComponentID(typeStr)] + expectedReceiver := factory.CreateDefaultConfig() + expectedReceiver.(*Config).PerfCounters = test.Expected.PerfCounters + expectedReceiver.(*Config).MetricMetaData = test.Expected.MetricMetaData - defaultConfigSingleObject.(*Config).PerfCounters = []PerfCounterConfig{{Object: "object", Counters: []CounterConfig{{Name: "counter1"}}}} - assert.Equal(t, defaultConfigSingleObject, r0) + assert.Equal(t, expectedReceiver, actualReceiver) + }) + } } func TestLoadConfig_Error(t *testing.T) { diff --git a/receiver/windowsperfcountersreceiver/testdata/config-nometricspecified.yaml b/receiver/windowsperfcountersreceiver/testdata/config-nometricspecified.yaml new file mode 100644 index 000000000000..8277abe96d46 --- /dev/null +++ b/receiver/windowsperfcountersreceiver/testdata/config-nometricspecified.yaml @@ -0,0 +1,24 @@ +receivers: + windowsperfcounters: + metrics: + metric: + description: desc + unit: "1" + gauge: + perfcounters: + - object: "object" + counters: + - name: counter1 + +processors: + nop: + +exporters: + nop: + +service: + pipelines: + metrics: + receivers: [windowsperfcounters] + processors: [nop] + exporters: [nop] From 0b086a67b0d706d66bf3e712d4affe3ee98d75e9 Mon Sep 17 00:00:00 2001 From: Miguel Rodriguez Date: Thu, 17 Mar 2022 15:41:39 -0400 Subject: [PATCH 21/24] Fix test --- receiver/windowsperfcountersreceiver/config_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/receiver/windowsperfcountersreceiver/config_test.go b/receiver/windowsperfcountersreceiver/config_test.go index be1f38cc4547..7fcb65461345 100644 --- a/receiver/windowsperfcountersreceiver/config_test.go +++ b/receiver/windowsperfcountersreceiver/config_test.go @@ -109,7 +109,7 @@ func TestLoadConfigNoMetrics(t *testing.T) { Expected: Config{ PerfCounters: []PerfCounterConfig{ { - Object: "object1", + Object: "object", Counters: []CounterConfig{{Name: "counter1"}}, }, }, @@ -121,7 +121,7 @@ func TestLoadConfigNoMetrics(t *testing.T) { Expected: Config{ PerfCounters: []PerfCounterConfig{ { - Object: "object1", + Object: "object", Counters: []CounterConfig{{Name: "counter1"}}, }, }, @@ -147,7 +147,7 @@ func TestLoadConfigNoMetrics(t *testing.T) { require.NoError(t, err) require.NotNil(t, cfg) - assert.Equal(t, len(cfg.Receivers), 2) + assert.Equal(t, len(cfg.Receivers), 1) actualReceiver := cfg.Receivers[config.NewComponentID(typeStr)] expectedReceiver := factory.CreateDefaultConfig() From c3e44a1fe5646fb24acfb6700c13891a94d89eed Mon Sep 17 00:00:00 2001 From: Mrod1598 Date: Fri, 18 Mar 2022 13:05:24 -0400 Subject: [PATCH 22/24] Add More Tests --- .../windowsperfcountersreceiver/README.md | 3 +- .../config_test.go | 43 ++++++++++++++++++- receiver/windowsperfcountersreceiver/go.mod | 4 +- receiver/windowsperfcountersreceiver/go.sum | 5 +++ .../testdata/config-summetric.yaml | 26 +++++++++++ .../config-unspecifiedmetrictype.yaml | 24 +++++++++++ 6 files changed, 100 insertions(+), 5 deletions(-) create mode 100644 receiver/windowsperfcountersreceiver/testdata/config-summetric.yaml create mode 100644 receiver/windowsperfcountersreceiver/testdata/config-unspecifiedmetrictype.yaml diff --git a/receiver/windowsperfcountersreceiver/README.md b/receiver/windowsperfcountersreceiver/README.md index ab194a9ac67d..77e95b4d62a4 100644 --- a/receiver/windowsperfcountersreceiver/README.md +++ b/receiver/windowsperfcountersreceiver/README.md @@ -61,7 +61,7 @@ If you would like to scrape some counters at a different frequency than others, you can configure multiple `windowsperfcounters` receivers with different `collection_interval` values. For example: -```yaml +```yaml receivers: windowsperfcounters/memory: metrics: @@ -140,7 +140,6 @@ receivers: description: the number of bytes committed to memory unit: By gauge: - value_type: int collection_interval: 30s perfcounters: - object: Memory diff --git a/receiver/windowsperfcountersreceiver/config_test.go b/receiver/windowsperfcountersreceiver/config_test.go index 7fcb65461345..7c7c7a7213d7 100644 --- a/receiver/windowsperfcountersreceiver/config_test.go +++ b/receiver/windowsperfcountersreceiver/config_test.go @@ -97,7 +97,7 @@ func TestLoadConfig(t *testing.T) { assert.Equal(t, expectedConfig, r1) } -func TestLoadConfigNoMetrics(t *testing.T) { +func TestLoadConfigMetrics(t *testing.T) { testCases := []struct { TestName string TestPath string @@ -134,6 +134,47 @@ func TestLoadConfigNoMetrics(t *testing.T) { }, }, }, + { + TestName: "SumMetric", + TestPath: filepath.Join("testdata", "config-summetric.yaml"), + Expected: Config{ + PerfCounters: []PerfCounterConfig{ + { + Object: "object", + Counters: []CounterConfig{{Name: "counter1", Metric: "metric"}}, + }, + }, + MetricMetaData: map[string]MetricConfig{ + "metric": { + Description: "desc", + Unit: "1", + Sum: SumMetric{ + Aggregation: "cumulative", + Monotonic: false, + }, + }, + }, + }, + }, + { + TestName: "MetricUnspecifiedType", + TestPath: filepath.Join("testdata", "config-unspecifiedmetrictype.yaml"), + Expected: Config{ + PerfCounters: []PerfCounterConfig{ + { + Object: "object", + Counters: []CounterConfig{{Name: "counter1", Metric: "metric"}}, + }, + }, + MetricMetaData: map[string]MetricConfig{ + "metric": { + Description: "desc", + Unit: "1", + Gauge: GaugeMetric{}, + }, + }, + }, + }, } for _, test := range testCases { t.Run(test.TestName, func(t *testing.T) { diff --git a/receiver/windowsperfcountersreceiver/go.mod b/receiver/windowsperfcountersreceiver/go.mod index 6112d480077a..72ed16537ea5 100644 --- a/receiver/windowsperfcountersreceiver/go.mod +++ b/receiver/windowsperfcountersreceiver/go.mod @@ -5,7 +5,7 @@ go 1.17 require ( github.com/stretchr/testify v1.7.0 go.opentelemetry.io/collector v0.46.1-0.20220307173244-f980c9ef25b1 - go.opentelemetry.io/collector/model v0.46.1-0.20220307173244-f980c9ef25b1 + go.opentelemetry.io/collector/model v0.47.0 go.uber.org/multierr v1.8.0 go.uber.org/zap v1.21.0 golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 @@ -21,7 +21,7 @@ require ( github.com/mitchellh/copystructure v1.2.0 // indirect github.com/mitchellh/mapstructure v1.4.3 // indirect github.com/mitchellh/reflectwalk v1.0.2 // indirect - github.com/open-telemetry/opentelemetry-collector-contrib/internal/scrapertest v0.46.0 + github.com/open-telemetry/opentelemetry-collector-contrib/internal/scrapertest v0.47.0 github.com/pelletier/go-toml v1.9.4 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect diff --git a/receiver/windowsperfcountersreceiver/go.sum b/receiver/windowsperfcountersreceiver/go.sum index bd509b44246c..f314e46566cb 100644 --- a/receiver/windowsperfcountersreceiver/go.sum +++ b/receiver/windowsperfcountersreceiver/go.sum @@ -133,6 +133,8 @@ github.com/npillmayer/nestext v0.1.3/go.mod h1:h2lrijH8jpicr25dFY+oAJLyzlya6jhnu github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA= github.com/open-telemetry/opentelemetry-collector-contrib/internal/scrapertest v0.46.0 h1:UfROTTu9qppkAX92iRdVXpre5/b9mr98nebVFAo2Tag= github.com/open-telemetry/opentelemetry-collector-contrib/internal/scrapertest v0.46.0/go.mod h1:I9hcw9At1o33hIfcmkzo4AOPY7vEK92YDxEeU5DbOew= +github.com/open-telemetry/opentelemetry-collector-contrib/internal/scrapertest v0.47.0 h1:61MPuKne5R2/w54YZjI/S1Iw+X7N7rsL3CZ/EDEWdDk= +github.com/open-telemetry/opentelemetry-collector-contrib/internal/scrapertest v0.47.0/go.mod h1:lW6MaaOr/PMf2Tno2WcwHJBkEJYsmhcosVqib/JFVI8= github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= github.com/pelletier/go-toml v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE= github.com/pelletier/go-toml v1.9.4 h1:tjENF6MfZAg8e4ZmZTeWaWiT2vXtsoO6+iuOjFhECwM= @@ -170,6 +172,8 @@ go.opentelemetry.io/collector v0.46.1-0.20220307173244-f980c9ef25b1 h1:XA3Rg5Abr go.opentelemetry.io/collector v0.46.1-0.20220307173244-f980c9ef25b1/go.mod h1:3G6HUzm11xa5ZHxf8QWMYYUwtSmPkTZT9DiTuo3fodQ= go.opentelemetry.io/collector/model v0.46.1-0.20220307173244-f980c9ef25b1 h1:pqCaE5Ue6DCSlIU7EZjyC1QJ9iZBywMXZ+BKihwX6UU= go.opentelemetry.io/collector/model v0.46.1-0.20220307173244-f980c9ef25b1/go.mod h1:uyiyyq8lV45zrJ94MnLip26sorfNLP6J9XmOvaEmy7w= +go.opentelemetry.io/collector/model v0.47.0 h1:qfIc/rhua50MTowB5BFveEVaYyZWL3Yld3pMmhUDU5w= +go.opentelemetry.io/collector/model v0.47.0/go.mod h1:tyZ1XdPtljZ9I09pJGcz5ktV9G1AAZ/HDmf6YOMHebc= go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.29.0 h1:n9b7AAdbQtQ0k9dm0Dm2/KUcUqtG8i2O15KzNaDze8c= go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.29.0 h1:SLme4Porm+UwX0DdHMxlwRt7FzPSE0sys81bet2o0pU= go.opentelemetry.io/otel v1.4.0/go.mod h1:jeAqMFKy2uLIxCtKxoFj0FAL5zAPKQagc3+GtBWakzk= @@ -274,6 +278,7 @@ google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQ google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc= google.golang.org/grpc v1.44.0 h1:weqSxi/TMs1SqFRMHCtBgXRs8k3X39QIDEZ0pRcttUg= +google.golang.org/grpc v1.45.0 h1:NEpgUqV3Z+ZjkqMsxMg11IaDrXY4RY6CQukSGK0uI1M= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= diff --git a/receiver/windowsperfcountersreceiver/testdata/config-summetric.yaml b/receiver/windowsperfcountersreceiver/testdata/config-summetric.yaml new file mode 100644 index 000000000000..0efdf4af1ebe --- /dev/null +++ b/receiver/windowsperfcountersreceiver/testdata/config-summetric.yaml @@ -0,0 +1,26 @@ +receivers: + windowsperfcounters: + metrics: + metric: + description: desc + unit: "1" + sum: + aggregation: cumulative + perfcounters: + - object: "object" + counters: + - name: counter1 + metric: metric + +processors: + nop: + +exporters: + nop: + +service: + pipelines: + metrics: + receivers: [windowsperfcounters] + processors: [nop] + exporters: [nop] diff --git a/receiver/windowsperfcountersreceiver/testdata/config-unspecifiedmetrictype.yaml b/receiver/windowsperfcountersreceiver/testdata/config-unspecifiedmetrictype.yaml new file mode 100644 index 000000000000..831b395dc24a --- /dev/null +++ b/receiver/windowsperfcountersreceiver/testdata/config-unspecifiedmetrictype.yaml @@ -0,0 +1,24 @@ +receivers: + windowsperfcounters: + metrics: + metric: + description: desc + unit: "1" + perfcounters: + - object: "object" + counters: + - name: counter1 + metric: metric + +processors: + nop: + +exporters: + nop: + +service: + pipelines: + metrics: + receivers: [windowsperfcounters] + processors: [nop] + exporters: [nop] From 09159eefc762698a0b74b5a8f7e37d13f2757d59 Mon Sep 17 00:00:00 2001 From: Mrod1598 Date: Fri, 18 Mar 2022 14:01:15 -0400 Subject: [PATCH 23/24] Add ignoreValues option --- .../windowsperfcounters_scraper_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper_test.go b/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper_test.go index 07ca3e522b7a..892080781839 100644 --- a/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper_test.go +++ b/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper_test.go @@ -218,7 +218,7 @@ func Test_WindowsPerfCounterScraper(t *testing.T) { } require.NoError(t, err) expectedMetrics, err := golden.ReadMetrics(test.expectedMetricPath) - scrapertest.CompareMetrics(expectedMetrics, actualMetrics) + scrapertest.CompareMetrics(expectedMetrics, actualMetrics, scrapertest.IgnoreMetricValues) require.NoError(t, err) }) } From 4de19cc04bb1b053f92c4a53b35c64ffb6cec82c Mon Sep 17 00:00:00 2001 From: Mrod1598 Date: Fri, 18 Mar 2022 17:38:01 -0400 Subject: [PATCH 24/24] Fix changelog --- CHANGELOG.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d3d7ab9a0bf4..0a52225b63ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### 🛑 Breaking changes 🛑 + +- `windowsperfcountersreceiver`: Added metrics configuration (#8376) + ## v0.47.0 ### 💡 Enhancements 💡 @@ -29,7 +33,6 @@ ### 🛑 Breaking changes 🛑 -- `windowsperfcountersreceiver`: Added metrics configuration (#8376) - `mongodbatlasreceiver`: rename mislabeled attribute `memory_state` to correct `disk_status` on partition disk metrics (#7747) - `mongodbatlasreceiver`: Correctly set initial lookback for querying mongodb atlas api (#8246) - `nginxreceiver`: instrumentation name updated from `otelcol/nginx` to `otelcol/nginxreceiver` (#8255)