From be43a45f6882879c5a7d0bfae9f570e6cc7cea50 Mon Sep 17 00:00:00 2001 From: Jim Chen Date: Sun, 11 Jun 2023 23:23:03 +0800 Subject: [PATCH] fix info (#7) Co-authored-by: jimchen --- .../scraper/infoscraper/documentation.md | 18 ++--- .../scraper/infoscraper/info_scraper.go | 14 ++-- .../internal/metadata/generated_config.go | 32 +------- .../metadata/generated_config_test.go | 12 +-- .../internal/metadata/generated_metrics.go | 79 +++++++------------ .../metadata/generated_metrics_test.go | 48 +++++------ .../internal/metadata/testdata/config.yaml | 14 ---- .../scraper/infoscraper/metadata.yaml | 20 ++--- 8 files changed, 77 insertions(+), 160 deletions(-) diff --git a/receiver/hostmetricsreceiver/internal/scraper/infoscraper/documentation.md b/receiver/hostmetricsreceiver/internal/scraper/infoscraper/documentation.md index b6d4b6310997..752afb9a8909 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/infoscraper/documentation.md +++ b/receiver/hostmetricsreceiver/internal/scraper/infoscraper/documentation.md @@ -16,14 +16,14 @@ metrics: unix timestamp. -| Unit | Metric Type | Value Type | -| ---- | ----------- | ---------- | -| 1 | Gauge | Double | +| Unit | Metric Type | Value Type | Aggregation Temporality | Monotonic | +| ---- | ----------- | ---------- | ----------------------- | --------- | +| 1 | Sum | Int | Cumulative | false | -## Resource Attributes +#### Attributes -| Name | Description | Values | Enabled | -| ---- | ----------- | ------ | ------- | -| info.cpu.num | cpu num. | Any Int | true | -| info.hostname | hostname. | Any Str | true | -| info.org | common org. | Any Str | true | +| Name | Description | Values | +| ---- | ----------- | ------ | +| org | org | Any Str | +| hostname | hostname. | Any Str | +| cpu.num | cpu num. | Any Int | diff --git a/receiver/hostmetricsreceiver/internal/scraper/infoscraper/info_scraper.go b/receiver/hostmetricsreceiver/internal/scraper/infoscraper/info_scraper.go index 0e10fcbffe85..9d5bed6d4d96 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/infoscraper/info_scraper.go +++ b/receiver/hostmetricsreceiver/internal/scraper/infoscraper/info_scraper.go @@ -65,17 +65,19 @@ func (s *scraper) shutdown(ctx context.Context) error { // scrape func (s *scraper) scrape(_ context.Context) (pmetric.Metrics, error) { now := pcommon.NewTimestampFromTime(s.now()) - s.mb.RecordInfoNowDataPoint(now, float64(now.AsTime().Unix())) hostname, err := s.hostname() if err != nil { return pmetric.NewMetrics(), scrapererror.NewPartialScrapeError(err, metricsLen) } - return s.mb.Emit( - metadata.WithInfoOrg(org()), - metadata.WithInfoCPUNum(int64(s.cpuNum())), - metadata.WithInfoHostname(hostname), - ), nil + s.mb.RecordInfoNowDataPoint( + now, now.AsTime().Unix(), + org(), + hostname, + int64(s.cpuNum()), + ) + + return s.mb.Emit(), nil } func org() string { diff --git a/receiver/hostmetricsreceiver/internal/scraper/infoscraper/internal/metadata/generated_config.go b/receiver/hostmetricsreceiver/internal/scraper/infoscraper/internal/metadata/generated_config.go index ab21febc4468..7cf311c98207 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/infoscraper/internal/metadata/generated_config.go +++ b/receiver/hostmetricsreceiver/internal/scraper/infoscraper/internal/metadata/generated_config.go @@ -36,41 +36,13 @@ func DefaultMetricsConfig() MetricsConfig { } } -// ResourceAttributeConfig provides common config for a particular resource attribute. -type ResourceAttributeConfig struct { - Enabled bool `mapstructure:"enabled"` -} - -// ResourceAttributesConfig provides config for hostmetricsreceiver/info resource attributes. -type ResourceAttributesConfig struct { - InfoCPUNum ResourceAttributeConfig `mapstructure:"info.cpu.num"` - InfoHostname ResourceAttributeConfig `mapstructure:"info.hostname"` - InfoOrg ResourceAttributeConfig `mapstructure:"info.org"` -} - -func DefaultResourceAttributesConfig() ResourceAttributesConfig { - return ResourceAttributesConfig{ - InfoCPUNum: ResourceAttributeConfig{ - Enabled: true, - }, - InfoHostname: ResourceAttributeConfig{ - Enabled: true, - }, - InfoOrg: ResourceAttributeConfig{ - Enabled: true, - }, - } -} - // MetricsBuilderConfig is a configuration for hostmetricsreceiver/info metrics builder. type MetricsBuilderConfig struct { - Metrics MetricsConfig `mapstructure:"metrics"` - ResourceAttributes ResourceAttributesConfig `mapstructure:"resource_attributes"` + Metrics MetricsConfig `mapstructure:"metrics"` } func DefaultMetricsBuilderConfig() MetricsBuilderConfig { return MetricsBuilderConfig{ - Metrics: DefaultMetricsConfig(), - ResourceAttributes: DefaultResourceAttributesConfig(), + Metrics: DefaultMetricsConfig(), } } diff --git a/receiver/hostmetricsreceiver/internal/scraper/infoscraper/internal/metadata/generated_config_test.go b/receiver/hostmetricsreceiver/internal/scraper/infoscraper/internal/metadata/generated_config_test.go index 348f8457d9d3..73a7329fd993 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/infoscraper/internal/metadata/generated_config_test.go +++ b/receiver/hostmetricsreceiver/internal/scraper/infoscraper/internal/metadata/generated_config_test.go @@ -28,11 +28,6 @@ func TestMetricsBuilderConfig(t *testing.T) { Metrics: MetricsConfig{ InfoNow: MetricConfig{Enabled: true}, }, - ResourceAttributes: ResourceAttributesConfig{ - InfoCPUNum: ResourceAttributeConfig{Enabled: true}, - InfoHostname: ResourceAttributeConfig{Enabled: true}, - InfoOrg: ResourceAttributeConfig{Enabled: true}, - }, }, }, { @@ -41,18 +36,13 @@ func TestMetricsBuilderConfig(t *testing.T) { Metrics: MetricsConfig{ InfoNow: MetricConfig{Enabled: false}, }, - ResourceAttributes: ResourceAttributesConfig{ - InfoCPUNum: ResourceAttributeConfig{Enabled: false}, - InfoHostname: ResourceAttributeConfig{Enabled: false}, - InfoOrg: ResourceAttributeConfig{Enabled: false}, - }, }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { cfg := loadMetricsBuilderConfig(t, tt.name) - if diff := cmp.Diff(tt.want, cfg, cmpopts.IgnoreUnexported(MetricConfig{}, ResourceAttributeConfig{})); diff != "" { + if diff := cmp.Diff(tt.want, cfg, cmpopts.IgnoreUnexported(MetricConfig{})); diff != "" { t.Errorf("Config mismatch (-expected +actual):\n%s", diff) } }) diff --git a/receiver/hostmetricsreceiver/internal/scraper/infoscraper/internal/metadata/generated_metrics.go b/receiver/hostmetricsreceiver/internal/scraper/infoscraper/internal/metadata/generated_metrics.go index 04dec3500ca2..5f86a02cf4f6 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/infoscraper/internal/metadata/generated_metrics.go +++ b/receiver/hostmetricsreceiver/internal/scraper/infoscraper/internal/metadata/generated_metrics.go @@ -23,29 +23,35 @@ func (m *metricInfoNow) init() { m.data.SetName("info.now") m.data.SetDescription("unix timestamp.") m.data.SetUnit("1") - m.data.SetEmptyGauge() + m.data.SetEmptySum() + m.data.Sum().SetIsMonotonic(false) + m.data.Sum().SetAggregationTemporality(pmetric.AggregationTemporalityCumulative) + m.data.Sum().DataPoints().EnsureCapacity(m.capacity) } -func (m *metricInfoNow) recordDataPoint(start pcommon.Timestamp, ts pcommon.Timestamp, val float64) { +func (m *metricInfoNow) recordDataPoint(start pcommon.Timestamp, ts pcommon.Timestamp, val int64, orgAttributeValue string, hostnameAttributeValue string, cpuNumAttributeValue int64) { if !m.config.Enabled { return } - dp := m.data.Gauge().DataPoints().AppendEmpty() + dp := m.data.Sum().DataPoints().AppendEmpty() dp.SetStartTimestamp(start) dp.SetTimestamp(ts) - dp.SetDoubleValue(val) + dp.SetIntValue(val) + dp.Attributes().PutStr("org", orgAttributeValue) + dp.Attributes().PutStr("hostname", hostnameAttributeValue) + dp.Attributes().PutInt("cpu.num", cpuNumAttributeValue) } // updateCapacity saves max length of data point slices that will be used for the slice capacity. func (m *metricInfoNow) updateCapacity() { - if m.data.Gauge().DataPoints().Len() > m.capacity { - m.capacity = m.data.Gauge().DataPoints().Len() + if m.data.Sum().DataPoints().Len() > m.capacity { + m.capacity = m.data.Sum().DataPoints().Len() } } // emit appends recorded metric data to a metrics slice and prepares it for recording another set of data points. func (m *metricInfoNow) emit(metrics pmetric.MetricSlice) { - if m.config.Enabled && m.data.Gauge().DataPoints().Len() > 0 { + if m.config.Enabled && m.data.Sum().DataPoints().Len() > 0 { m.updateCapacity() m.data.MoveTo(metrics.AppendEmpty()) m.init() @@ -64,13 +70,12 @@ func newMetricInfoNow(cfg MetricConfig) metricInfoNow { // MetricsBuilder provides an interface for scrapers to report metrics while taking care of all the transformations // required to produce metric representation defined in metadata and user config. type MetricsBuilder struct { - startTime pcommon.Timestamp // start time that will be applied to all recorded data points. - metricsCapacity int // maximum observed number of metrics per resource. - resourceCapacity int // maximum observed number of resource attributes. - metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. - buildInfo component.BuildInfo // contains version information - resourceAttributesConfig ResourceAttributesConfig - metricInfoNow metricInfoNow + startTime pcommon.Timestamp // start time that will be applied to all recorded data points. + metricsCapacity int // maximum observed number of metrics per resource. + resourceCapacity int // maximum observed number of resource attributes. + metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. + buildInfo component.BuildInfo // contains version information + metricInfoNow metricInfoNow } // metricBuilderOption applies changes to default metrics builder. @@ -85,11 +90,10 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { func NewMetricsBuilder(mbc MetricsBuilderConfig, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { mb := &MetricsBuilder{ - startTime: pcommon.NewTimestampFromTime(time.Now()), - metricsBuffer: pmetric.NewMetrics(), - buildInfo: settings.BuildInfo, - resourceAttributesConfig: mbc.ResourceAttributes, - metricInfoNow: newMetricInfoNow(mbc.Metrics.InfoNow), + startTime: pcommon.NewTimestampFromTime(time.Now()), + metricsBuffer: pmetric.NewMetrics(), + buildInfo: settings.BuildInfo, + metricInfoNow: newMetricInfoNow(mbc.Metrics.InfoNow), } for _, op := range options { op(mb) @@ -108,39 +112,12 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(ResourceAttributesConfig, pmetric.ResourceMetrics) - -// WithInfoCPUNum sets provided value as "info.cpu.num" attribute for current resource. -func WithInfoCPUNum(val int64) ResourceMetricsOption { - return func(rac ResourceAttributesConfig, rm pmetric.ResourceMetrics) { - if rac.InfoCPUNum.Enabled { - rm.Resource().Attributes().PutInt("info.cpu.num", val) - } - } -} - -// WithInfoHostname sets provided value as "info.hostname" attribute for current resource. -func WithInfoHostname(val string) ResourceMetricsOption { - return func(rac ResourceAttributesConfig, rm pmetric.ResourceMetrics) { - if rac.InfoHostname.Enabled { - rm.Resource().Attributes().PutStr("info.hostname", val) - } - } -} - -// WithInfoOrg sets provided value as "info.org" attribute for current resource. -func WithInfoOrg(val string) ResourceMetricsOption { - return func(rac ResourceAttributesConfig, rm pmetric.ResourceMetrics) { - if rac.InfoOrg.Enabled { - rm.Resource().Attributes().PutStr("info.org", val) - } - } -} +type ResourceMetricsOption func(pmetric.ResourceMetrics) // WithStartTimeOverride overrides start time for all the resource metrics data points. // This option should be only used if different start time has to be set on metrics coming from different resources. func WithStartTimeOverride(start pcommon.Timestamp) ResourceMetricsOption { - return func(_ ResourceAttributesConfig, rm pmetric.ResourceMetrics) { + return func(rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -173,7 +150,7 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { mb.metricInfoNow.emit(ils.Metrics()) for _, op := range rmo { - op(mb.resourceAttributesConfig, rm) + op(rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) @@ -192,8 +169,8 @@ func (mb *MetricsBuilder) Emit(rmo ...ResourceMetricsOption) pmetric.Metrics { } // RecordInfoNowDataPoint adds a data point to info.now metric. -func (mb *MetricsBuilder) RecordInfoNowDataPoint(ts pcommon.Timestamp, val float64) { - mb.metricInfoNow.recordDataPoint(mb.startTime, ts, val) +func (mb *MetricsBuilder) RecordInfoNowDataPoint(ts pcommon.Timestamp, val int64, orgAttributeValue string, hostnameAttributeValue string, cpuNumAttributeValue int64) { + mb.metricInfoNow.recordDataPoint(mb.startTime, ts, val, orgAttributeValue, hostnameAttributeValue, cpuNumAttributeValue) } // Reset resets metrics builder to its initial state. It should be used when external metrics source is restarted, diff --git a/receiver/hostmetricsreceiver/internal/scraper/infoscraper/internal/metadata/generated_metrics_test.go b/receiver/hostmetricsreceiver/internal/scraper/infoscraper/internal/metadata/generated_metrics_test.go index a2c8fd9e41e3..0d523a22de1e 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/infoscraper/internal/metadata/generated_metrics_test.go +++ b/receiver/hostmetricsreceiver/internal/scraper/infoscraper/internal/metadata/generated_metrics_test.go @@ -56,9 +56,9 @@ func TestMetricsBuilder(t *testing.T) { defaultMetricsCount++ allMetricsCount++ - mb.RecordInfoNowDataPoint(ts, 1) + mb.RecordInfoNowDataPoint(ts, 1, "attr-val", "attr-val", 1) - metrics := mb.Emit(WithInfoCPUNum(1), WithInfoHostname("attr-val"), WithInfoOrg("attr-val")) + metrics := mb.Emit() if test.configSet == testSetNone { assert.Equal(t, 0, metrics.ResourceMetrics().Len()) @@ -69,29 +69,8 @@ func TestMetricsBuilder(t *testing.T) { rm := metrics.ResourceMetrics().At(0) attrCount := 0 enabledAttrCount := 0 - attrVal, ok := rm.Resource().Attributes().Get("info.cpu.num") - attrCount++ - assert.Equal(t, mb.resourceAttributesConfig.InfoCPUNum.Enabled, ok) - if mb.resourceAttributesConfig.InfoCPUNum.Enabled { - enabledAttrCount++ - assert.EqualValues(t, 1, attrVal.Int()) - } - attrVal, ok = rm.Resource().Attributes().Get("info.hostname") - attrCount++ - assert.Equal(t, mb.resourceAttributesConfig.InfoHostname.Enabled, ok) - if mb.resourceAttributesConfig.InfoHostname.Enabled { - enabledAttrCount++ - assert.EqualValues(t, "attr-val", attrVal.Str()) - } - attrVal, ok = rm.Resource().Attributes().Get("info.org") - attrCount++ - assert.Equal(t, mb.resourceAttributesConfig.InfoOrg.Enabled, ok) - if mb.resourceAttributesConfig.InfoOrg.Enabled { - enabledAttrCount++ - assert.EqualValues(t, "attr-val", attrVal.Str()) - } assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) - assert.Equal(t, attrCount, 3) + assert.Equal(t, attrCount, 0) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() @@ -107,15 +86,26 @@ func TestMetricsBuilder(t *testing.T) { case "info.now": assert.False(t, validatedMetrics["info.now"], "Found a duplicate in the metrics slice: info.now") validatedMetrics["info.now"] = true - assert.Equal(t, pmetric.MetricTypeGauge, ms.At(i).Type()) - assert.Equal(t, 1, ms.At(i).Gauge().DataPoints().Len()) + assert.Equal(t, pmetric.MetricTypeSum, ms.At(i).Type()) + assert.Equal(t, 1, ms.At(i).Sum().DataPoints().Len()) assert.Equal(t, "unix timestamp.", ms.At(i).Description()) assert.Equal(t, "1", ms.At(i).Unit()) - dp := ms.At(i).Gauge().DataPoints().At(0) + assert.Equal(t, false, ms.At(i).Sum().IsMonotonic()) + assert.Equal(t, pmetric.AggregationTemporalityCumulative, ms.At(i).Sum().AggregationTemporality()) + dp := ms.At(i).Sum().DataPoints().At(0) assert.Equal(t, start, dp.StartTimestamp()) assert.Equal(t, ts, dp.Timestamp()) - assert.Equal(t, pmetric.NumberDataPointValueTypeDouble, dp.ValueType()) - assert.Equal(t, float64(1), dp.DoubleValue()) + assert.Equal(t, pmetric.NumberDataPointValueTypeInt, dp.ValueType()) + assert.Equal(t, int64(1), dp.IntValue()) + attrVal, ok := dp.Attributes().Get("org") + assert.True(t, ok) + assert.EqualValues(t, "attr-val", attrVal.Str()) + attrVal, ok = dp.Attributes().Get("hostname") + assert.True(t, ok) + assert.EqualValues(t, "attr-val", attrVal.Str()) + attrVal, ok = dp.Attributes().Get("cpu.num") + assert.True(t, ok) + assert.EqualValues(t, 1, attrVal.Int()) } } }) diff --git a/receiver/hostmetricsreceiver/internal/scraper/infoscraper/internal/metadata/testdata/config.yaml b/receiver/hostmetricsreceiver/internal/scraper/infoscraper/internal/metadata/testdata/config.yaml index 07925d6f54ae..2ea75e030a07 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/infoscraper/internal/metadata/testdata/config.yaml +++ b/receiver/hostmetricsreceiver/internal/scraper/infoscraper/internal/metadata/testdata/config.yaml @@ -3,21 +3,7 @@ all_set: metrics: info.now: enabled: true - resource_attributes: - info.cpu.num: - enabled: true - info.hostname: - enabled: true - info.org: - enabled: true none_set: metrics: info.now: enabled: false - resource_attributes: - info.cpu.num: - enabled: false - info.hostname: - enabled: false - info.org: - enabled: false diff --git a/receiver/hostmetricsreceiver/internal/scraper/infoscraper/metadata.yaml b/receiver/hostmetricsreceiver/internal/scraper/infoscraper/metadata.yaml index a709d9bbba7d..b735561ef814 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/infoscraper/metadata.yaml +++ b/receiver/hostmetricsreceiver/internal/scraper/infoscraper/metadata.yaml @@ -2,18 +2,15 @@ type: hostmetricsreceiver/info sem_conv_version: 1.9.0 -resource_attributes: - info.org: - description: common org. - enabled: true +attributes: + org: + description: org type: string - info.hostname: + hostname: description: hostname. - enabled: true type: string - info.cpu.num: + cpu.num: description: cpu num. - enabled: true type: int metrics: @@ -21,5 +18,8 @@ metrics: enabled: true description: unix timestamp. unit: 1 - gauge: - value_type: double + sum: + value_type: int + aggregation: cumulative + monotonic: false + attributes: [org, hostname, cpu.num]