Skip to content

Commit

Permalink
hostmetricsreceiver: refactor load metrics to use metadata generated …
Browse files Browse the repository at this point in the history
…metrics
  • Loading branch information
Mark Stumpf committed Jan 15, 2021
1 parent 6f772bf commit db35667
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 70 deletions.
8 changes: 4 additions & 4 deletions cmd/mdatagen/metrics.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
// Type is the component type name.
const Type configmodels.Type = "{{ .Name }}"

type metricIntf interface {
type Metric interface {
Name() string
New() pdata.Metric
Init(metric pdata.Metric)
Expand Down Expand Up @@ -55,7 +55,7 @@ func (m *metricImpl) Init(metric pdata.Metric) {

type metricStruct struct {
{{- range $name, $metric := .Metrics }}
{{ $name.Render }} metricIntf
{{ $name.Render }} Metric
{{- end }}
}

Expand All @@ -68,13 +68,13 @@ func (m *metricStruct) Names() []string {
}
}

var metricsByName = map[string]metricIntf {
var metricsByName = map[string]Metric {
{{- range $name, $metric := .Metrics }}
"{{ $name }}": Metrics.{{ $name.Render }},
{{- end }}
}

func (m *metricStruct) ByName(n string) metricIntf {
func (m *metricStruct) ByName(n string) Metric {
return metricsByName[n]
}

Expand Down
1 change: 1 addition & 0 deletions receiver/hostmetricsreceiver/internal/metadata/.nocover
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
We test the generated code in the package it is used.

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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal"
"go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal/metadata"
)

const metricsLen = 3
Expand Down Expand Up @@ -64,15 +65,15 @@ func (s *scraper) scrape(_ context.Context) (pdata.MetricSlice, error) {
}

metrics.Resize(metricsLen)
initializeLoadMetric(metrics.At(0), loadAvg1MDescriptor, now, avgLoadValues.Load1)
initializeLoadMetric(metrics.At(1), loadAvg5mDescriptor, now, avgLoadValues.Load5)
initializeLoadMetric(metrics.At(2), loadAvg15mDescriptor, now, avgLoadValues.Load15)

initializeLoadMetric(metrics.At(0), metadata.Metrics.SystemCPULoadAverage1m, now, avgLoadValues.Load1)
initializeLoadMetric(metrics.At(1), metadata.Metrics.SystemCPULoadAverage5m, now, avgLoadValues.Load5)
initializeLoadMetric(metrics.At(2), metadata.Metrics.SystemCPULoadAverage15m, now, avgLoadValues.Load15)
return metrics, nil
}

func initializeLoadMetric(metric pdata.Metric, metricDescriptor pdata.Metric, now pdata.TimestampUnixNano, value float64) {
metricDescriptor.CopyTo(metric)

func initializeLoadMetric(metric pdata.Metric, metricDescriptor metadata.Metric, now pdata.TimestampUnixNano, value float64) {
metricDescriptor.Init(metric)
idps := metric.DoubleGauge().DataPoints()
idps.Resize(1)
dp := idps.At(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal"
"go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal/metadata"
)

func TestScrape(t *testing.T) {
Expand Down Expand Up @@ -77,9 +78,9 @@ func TestScrape(t *testing.T) {
assert.Equal(t, 3, metrics.Len())

// expect a single datapoint for 1m, 5m & 15m load metrics
assertMetricHasSingleDatapoint(t, metrics.At(0), loadAvg1MDescriptor)
assertMetricHasSingleDatapoint(t, metrics.At(1), loadAvg5mDescriptor)
assertMetricHasSingleDatapoint(t, metrics.At(2), loadAvg15mDescriptor)
assertMetricHasSingleDatapoint(t, metrics.At(0), metadata.Metrics.SystemCPULoadAverage1m.New())
assertMetricHasSingleDatapoint(t, metrics.At(1), metadata.Metrics.SystemCPULoadAverage5m.New())
assertMetricHasSingleDatapoint(t, metrics.At(2), metadata.Metrics.SystemCPULoadAverage15m.New())

internal.AssertSameTimeStampForAllMetrics(t, metrics)
})
Expand Down
18 changes: 18 additions & 0 deletions receiver/hostmetricsreceiver/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,21 @@ metrics:
type: int sum
aggregation: cumulative
monotonic: false

system.cpu.load_average.1m:
description: Average CPU Load over 1 minute.
unit: 1
data:
type: double gauge

system.cpu.load_average.5m:
description: Average CPU Load over 5 minutes.
unit: 1
data:
type: double gauge

system.cpu.load_average.15m:
description: Average CPU Load over 15 minutes.
unit: 1
data:
type: double gauge

0 comments on commit db35667

Please sign in to comment.