Skip to content

Commit

Permalink
mdatagen: Add Init function for intializing existing metrics (open-te…
Browse files Browse the repository at this point in the history
…lemetry#2358)

* mdatagen: Add Init function for intiializing existing metrics

pdata.Metric instances may already be initialized and we just want to fill in
the metric descriptor values. Removes the unnecessary need to New() then
CopyTo().

* regenerate

Co-authored-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
jrcamp and bogdandrutu committed Jan 13, 2021
1 parent 0852610 commit 8b04bef
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 19 deletions.
19 changes: 13 additions & 6 deletions cmd/mdatagen/metrics.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,30 @@ const Type configmodels.Type = "{{ .Name }}"
type metricIntf interface {
Name() string
New() pdata.Metric
Init(metric pdata.Metric)
}

// Intentionally not exposing this so that it is opaque and can change freely.
type metricImpl struct {
name string
newFunc func() pdata.Metric
initFunc func(pdata.Metric)
}

// Name returns the metric name.
func (m *metricImpl) Name() string {
return m.name
}

// New creates a metric object preinitialized.
func (m *metricImpl) New() pdata.Metric {
return m.newFunc()
metric := pdata.NewMetric()
m.Init(metric)
return metric
}

// Init initializes the provided metric object.
func (m *metricImpl) Init(metric pdata.Metric) {
m.initFunc(metric)
}

type metricStruct struct {
Expand Down Expand Up @@ -84,8 +94,7 @@ var Metrics = &metricStruct{
{{- range $name, $metric := .Metrics }}
&metricImpl{
"{{ $name }}",
func() pdata.Metric {
metric := pdata.NewMetric()
func(metric pdata.Metric) {
metric.SetName("{{ $name }}")
metric.SetDescription("{{ $metric.Description }}")
metric.SetUnit("{{ $metric.Unit }}")
Expand All @@ -96,8 +105,6 @@ var Metrics = &metricStruct{
{{- if $metric.Data.HasAggregated }}
metric.{{ $metric.Data.Type }}().SetAggregationTemporality({{ $metric.Data.Aggregated.Type }})
{{- end }}

return metric
},
},
{{- end }}
Expand Down
1 change: 1 addition & 0 deletions internal/tools/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/gookit/color v1.3.1 h1:PPD/C7sf8u2L8XQPdPgsWRoAiLQGZEZOzU3cf5IYYUk=
github.com/gookit/color v1.3.1/go.mod h1:R3ogXq2B9rTbXoSHJ1HyUVAZ3poOJHpd9nQmyGZsfvQ=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
Expand Down
26 changes: 15 additions & 11 deletions receiver/hostmetricsreceiver/internal/metadata/generated_metrics.go

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

Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (s *scraper) scrape(_ context.Context) (pdata.MetricSlice, error) {
}

func initializeCPUTimeMetric(metric pdata.Metric, startTime, now pdata.TimestampUnixNano, cpuTimes []cpu.TimesStat) {
metadata.Metrics.SystemCPUTime.New().CopyTo(metric)
metadata.Metrics.SystemCPUTime.Init(metric)

ddps := metric.DoubleSum().DataPoints()
ddps.Resize(len(cpuTimes) * cpuStatesLen)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (s *scraper) Scrape(_ context.Context) (pdata.MetricSlice, error) {
}

func initializeMemoryUsageMetric(metric pdata.Metric, now pdata.TimestampUnixNano, memInfo *mem.VirtualMemoryStat) {
metadata.Metrics.SystemMemoryUsage.New().CopyTo(metric)
metadata.Metrics.SystemMemoryUsage.Init(metric)

idps := metric.IntSum().DataPoints()
idps.Resize(memStatesLen)
Expand Down

0 comments on commit 8b04bef

Please sign in to comment.