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

[cmd/mdatagen] Avoid reusing the same ResourceBuilder instance #24762

Merged
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
15 changes: 15 additions & 0 deletions .chloggen/mdatagen-avoid-reusing-resource-builder.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Use this changelog template to create an entry for release notes.
# If your change doesn't affect end users, such as a test fix or a tooling change,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: 'enhancement'

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: cmd/mdatagen

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Avoid reusing the same ResourceBuilder instance for multiple ResourceMetrics

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [24762]
15 changes: 11 additions & 4 deletions cmd/mdatagen/internal/metadata/generated_metrics.go

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

9 changes: 7 additions & 2 deletions cmd/mdatagen/internal/metadata/generated_metrics_test.go

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

17 changes: 13 additions & 4 deletions cmd/mdatagen/templates/metrics.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,11 @@ func newMetric{{ $name.Render }}(cfg MetricConfig) metric{{ $name.Render }} {
// 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.
metricsBuffer pmetric.Metrics // accumulates metrics data before emitting.
buildInfo component.BuildInfo // contains version information
config MetricsBuilderConfig // config of the metrics builder.
startTime pcommon.Timestamp // start time that will be applied to all recorded data points.
metricsCapacity int // maximum observed number of metrics per resource.
metricsBuffer pmetric.Metrics // accumulates metrics data before emitting.
buildInfo component.BuildInfo // contains version information.
{{- range $name, $metric := .Metrics }}
metric{{ $name.Render }} metric{{ $name.Render }}
{{- end }}
Expand Down Expand Up @@ -165,6 +166,7 @@ func NewMetricsBuilder(mbc MetricsBuilderConfig, settings receiver.CreateSetting
{{- end }}
{{- end }}
mb := &MetricsBuilder{
config: mbc,
startTime: pcommon.NewTimestampFromTime(time.Now()),
metricsBuffer: pmetric.NewMetrics(),
buildInfo: settings.BuildInfo,
Expand All @@ -178,6 +180,13 @@ func NewMetricsBuilder(mbc MetricsBuilderConfig, settings receiver.CreateSetting
return mb
}

{{- if .ResourceAttributes }}
// NewResourceBuilder returns a new resource builder that should be used to build a resource associated with for the emitted metrics.
func (mb *MetricsBuilder) NewResourceBuilder() *ResourceBuilder {
return NewResourceBuilder(mb.config.ResourceAttributes)
}
{{- end }}

// updateCapacity updates max length of metrics and resource attributes that will be used for the slice capacity.
func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) {
if mb.metricsCapacity < rm.ScopeMetrics().At(0).Metrics().Len() {
Expand Down
13 changes: 12 additions & 1 deletion cmd/mdatagen/templates/metrics_test.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,19 @@ func TestMetricsBuilder(t *testing.T) {
{{- end }})
{{- end }}

{{ if .ResourceAttributes }}
rb := mb.NewResourceBuilder()
{{- range $name, $attr := .ResourceAttributes }}
{{- if $attr.Enum }}
rb.Set{{ $attr.Name.Render }}{{ index $attr.Enum 0 | publicVar }}()
{{- else }}
rb.Set{{ $attr.Name.Render }}({{ $attr.TestValue }})
{{- end }}
{{- end }}
res := rb.Emit()
{{- else }}
res := pcommon.NewResource()
res.Attributes().PutStr("k1", "v1")
{{- end }}
metrics := mb.Emit(WithResource(res))

if test.configSet == testSetNone {
Expand Down

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

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

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

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

14 changes: 7 additions & 7 deletions receiver/aerospikereceiver/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ type aerospikeReceiver struct {
consumer consumer.Metrics
clientFactory clientFactoryFunc
client Aerospike
rb *metadata.ResourceBuilder
mb *metadata.MetricsBuilder
logger *zap.SugaredLogger
}
Expand Down Expand Up @@ -83,7 +82,6 @@ func newAerospikeReceiver(params receiver.CreateSettings, cfg *Config, consumer
nodeGetterFactory,
)
},
rb: metadata.NewResourceBuilder(cfg.MetricsBuilderConfig.ResourceAttributes),
mb: metadata.NewMetricsBuilder(cfg.MetricsBuilderConfig, params),
}, nil
}
Expand Down Expand Up @@ -168,8 +166,9 @@ func (r *aerospikeReceiver) emitNode(info map[string]string, now pcommon.Timesta
}
}

r.rb.SetAerospikeNodeName(info["node"])
r.mb.EmitForResource(metadata.WithResource(r.rb.Emit()))
rb := r.mb.NewResourceBuilder()
rb.SetAerospikeNodeName(info["node"])
r.mb.EmitForResource(metadata.WithResource(rb.Emit()))
r.logger.Debug("finished emitNode")
}

Expand Down Expand Up @@ -388,9 +387,10 @@ func (r *aerospikeReceiver) emitNamespace(info map[string]string, now pcommon.Ti
}
}

r.rb.SetAerospikeNamespace(info["name"])
r.rb.SetAerospikeNodeName(info["node"])
r.mb.EmitForResource(metadata.WithResource(r.rb.Emit()))
rb := r.mb.NewResourceBuilder()
rb.SetAerospikeNamespace(info["name"])
rb.SetAerospikeNodeName(info["node"])
r.mb.EmitForResource(metadata.WithResource(rb.Emit()))
r.logger.Debug("finished emitNamespace")
}

Expand Down
1 change: 0 additions & 1 deletion receiver/aerospikereceiver/scraper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ func TestScrape_CollectClusterMetrics(t *testing.T) {

receiver := &aerospikeReceiver{
clientFactory: clientFactory,
rb: metadata.NewResourceBuilder(metadata.DefaultResourceAttributesConfig()),
mb: metadata.NewMetricsBuilder(metadata.DefaultMetricsBuilderConfig(), receivertest.NewNopCreateSettings()),
logger: logger.Sugar(),
config: &Config{
Expand Down
15 changes: 11 additions & 4 deletions receiver/apachereceiver/internal/metadata/generated_metrics.go

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

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

9 changes: 4 additions & 5 deletions receiver/apachereceiver/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ type apacheScraper struct {
settings component.TelemetrySettings
cfg *Config
httpClient *http.Client
rb *metadata.ResourceBuilder
mb *metadata.MetricsBuilder
serverName string
port string
Expand All @@ -41,7 +40,6 @@ func newApacheScraper(
a := &apacheScraper{
settings: settings.TelemetrySettings,
cfg: cfg,
rb: metadata.NewResourceBuilder(cfg.MetricsBuilderConfig.ResourceAttributes),
mb: metadata.NewMetricsBuilder(cfg.MetricsBuilderConfig, settings),
serverName: serverName,
port: port,
Expand Down Expand Up @@ -129,9 +127,10 @@ func (r *apacheScraper) scrape(context.Context) (pmetric.Metrics, error) {
}
}

r.rb.SetApacheServerName(r.serverName)
r.rb.SetApacheServerPort(r.port)
return r.mb.Emit(metadata.WithResource(r.rb.Emit())), errs.Combine()
rb := r.mb.NewResourceBuilder()
rb.SetApacheServerName(r.serverName)
rb.SetApacheServerPort(r.port)
return r.mb.Emit(metadata.WithResource(rb.Emit())), errs.Combine()
}

func addPartialIfError(errs *scrapererror.ScrapeErrors, err error) {
Expand Down
Loading