Skip to content

Commit

Permalink
Update mdatagen to create factories of init instead of new (open-tele…
Browse files Browse the repository at this point in the history
…metry#2978)

* Update mdatagen to create factories of init instead of new

Signed-off-by: Bogdan Drutu <[email protected]>

* Revert New deletion, too many tests use that

Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu committed Apr 21, 2021
1 parent 8476727 commit 0694cac
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 44 deletions.
6 changes: 3 additions & 3 deletions cmd/mdatagen/metrics.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ func (m *metricStruct) ByName(n string) MetricIntf {
return metricsByName[n]
}

func (m *metricStruct) FactoriesByName() map[string]func() pdata.Metric {
return map[string]func() pdata.Metric {
func (m *metricStruct) FactoriesByName() map[string]func(pdata.Metric) {
return map[string]func(pdata.Metric) {
{{- range $name, $metric := .Metrics }}
Metrics.{{ $name.Render }}.Name(): Metrics.{{ $name.Render }}.New,
Metrics.{{ $name.Render }}.Name(): Metrics.{{ $name.Render }}.Init,
{{- end }}
}
}
Expand Down
10 changes: 3 additions & 7 deletions consumer/simple/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type Metrics struct {
// is intended to be used with the metadata code generation modules but can
// be used apart from that just as well. The returned metrics are expected
// to be initialized.
MetricFactoriesByName map[string]func() pdata.Metric
MetricFactoriesByName map[string]func(pdata.Metric)

// If set, this instrumentation library name will be used for all metrics
// generated by this builder. This is meant to be set once at builder
Expand Down Expand Up @@ -209,18 +209,14 @@ func (mb *Metrics) getOrCreateMetric(name string, typ pdata.MetricDataType) pdat
return metricSlice.At(idx)
}

var metric pdata.Metric
metric := metricSlice.AppendEmpty()
if fac, ok := mb.MetricFactoriesByName[name]; ok {
metric = fac()
fac(metric)
} else {
metric = pdata.NewMetric()

metric.SetName(name)
metric.SetDataType(typ)
}

metricSlice.Append(metric)

mb.metricIdxByName[name] = metricSlice.Len() - 1
return metric
}
Expand Down
6 changes: 2 additions & 4 deletions consumer/simple/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,13 +292,11 @@ func TestMetrics(t *testing.T) {
func TestMetricFactories(t *testing.T) {
mb := Metrics{
Metrics: pdata.NewMetrics(),
MetricFactoriesByName: map[string]func() pdata.Metric{
"disk.ops": func() pdata.Metric {
m := pdata.NewMetric()
MetricFactoriesByName: map[string]func(pdata.Metric){
"disk.ops": func(m pdata.Metric) {
m.SetName("disk.ops")
m.SetDescription("This counts disk operations")
m.SetDataType(pdata.MetricDataTypeIntSum)
return m
},
},
InstrumentationLibraryName: "example",
Expand Down
60 changes: 30 additions & 30 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.

0 comments on commit 0694cac

Please sign in to comment.