Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hostmetricsreceiver: refactor load metrics to use generated metrics #2375

Merged
merged 1 commit into from
Jan 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions cmd/mdatagen/metrics.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import (
// Type is the component type name.
const Type configmodels.Type = "{{ .Name }}"

type metricIntf interface {
// MetricIntf is an interface to generically interact with generated metric.
type MetricIntf interface {
mstumpfx marked this conversation as resolved.
Show resolved Hide resolved
Name() string
New() pdata.Metric
Init(metric pdata.Metric)
Expand Down Expand Up @@ -55,7 +56,7 @@ func (m *metricImpl) Init(metric pdata.Metric) {

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

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

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

func (m *metricStruct) ByName(n string) metricIntf {
func (m *metricStruct) ByName(n string) MetricIntf {
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.MetricIntf, 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