From 8a070097ec6d45d581f63d5c9ed18f2b5933255e Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Fri, 13 Sep 2024 09:37:43 -0700 Subject: [PATCH] [mdatagen] Avoid public APIs with internal params (#11040) Signed-off-by: Bogdan Drutu --- .chloggen/avoid-internal.yaml | 20 +++++++ .../internal/metadata/generated_metrics.go | 54 ++++++++++++------- .../internal/metadata/generated_telemetry.go | 22 +++++--- .../metadata/generated_telemetry_test.go | 4 +- cmd/mdatagen/templates/metrics.go.tmpl | 54 ++++++++++++------- cmd/mdatagen/templates/telemetry.go.tmpl | 23 +++++--- cmd/mdatagen/templates/telemetry_test.go.tmpl | 4 +- .../internal/metadata/generated_telemetry.go | 16 ++++-- .../metadata/generated_telemetry_test.go | 4 +- .../internal/metadata/generated_telemetry.go | 22 +++++--- .../metadata/generated_telemetry_test.go | 4 +- .../internal/metadata/generated_telemetry.go | 16 ++++-- .../metadata/generated_telemetry_test.go | 4 +- .../internal/metadata/generated_telemetry.go | 16 ++++-- .../metadata/generated_telemetry_test.go | 4 +- .../internal/metadata/generated_telemetry.go | 16 ++++-- .../metadata/generated_telemetry_test.go | 4 +- .../internal/metadata/generated_telemetry.go | 52 ++++++++++-------- .../metadata/generated_telemetry_test.go | 4 +- 19 files changed, 230 insertions(+), 113 deletions(-) create mode 100644 .chloggen/avoid-internal.yaml diff --git a/.chloggen/avoid-internal.yaml b/.chloggen/avoid-internal.yaml new file mode 100644 index 00000000000..70bd015768b --- /dev/null +++ b/.chloggen/avoid-internal.yaml @@ -0,0 +1,20 @@ +# Use this changelog template to create an entry for release notes. + +# 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. otlpreceiver) +component: mdatagen + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Avoid using private types in public APIs and also protect options to be implemented outside this module. + +# One or more tracking issues or pull requests related to the change +issues: [11040] + +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [api] diff --git a/cmd/mdatagen/internal/samplereceiver/internal/metadata/generated_metrics.go b/cmd/mdatagen/internal/samplereceiver/internal/metadata/generated_metrics.go index b05bf39206d..1e345a799b7 100644 --- a/cmd/mdatagen/internal/samplereceiver/internal/metadata/generated_metrics.go +++ b/cmd/mdatagen/internal/samplereceiver/internal/metadata/generated_metrics.go @@ -331,17 +331,25 @@ type MetricsBuilder struct { metricOptionalMetricEmptyUnit metricOptionalMetricEmptyUnit } -// metricBuilderOption applies changes to default metrics builder. -type metricBuilderOption func(*MetricsBuilder) +// MetricBuilderOption applies changes to default metrics builder. +type MetricBuilderOption interface { + apply(*MetricsBuilder) +} + +type metricBuilderOptionFunc func(mb *MetricsBuilder) + +func (mbof metricBuilderOptionFunc) apply(mb *MetricsBuilder) { + mbof(mb) +} // WithStartTime sets startTime on the metrics builder. -func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { - return func(mb *MetricsBuilder) { +func WithStartTime(startTime pcommon.Timestamp) MetricBuilderOption { + return metricBuilderOptionFunc(func(mb *MetricsBuilder) { mb.startTime = startTime - } + }) } -func NewMetricsBuilder(mbc MetricsBuilderConfig, settings receiver.Settings, options ...metricBuilderOption) *MetricsBuilder { +func NewMetricsBuilder(mbc MetricsBuilderConfig, settings receiver.Settings, options ...MetricBuilderOption) *MetricsBuilder { if !mbc.Metrics.DefaultMetric.enabledSetByUser { settings.Logger.Warn("[WARNING] Please set `enabled` field explicitly for `default.metric`: This metric will be disabled by default soon.") } @@ -426,7 +434,7 @@ func NewMetricsBuilder(mbc MetricsBuilderConfig, settings receiver.Settings, opt } for _, op := range options { - op(mb) + op.apply(mb) } return mb } @@ -444,20 +452,28 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption interface { + apply(pmetric.ResourceMetrics) +} + +type resourceMetricsOptionFunc func(pmetric.ResourceMetrics) + +func (rmof resourceMetricsOptionFunc) apply(rm pmetric.ResourceMetrics) { + rmof(rm) +} // WithResource sets the provided resource on the emitted ResourceMetrics. // It's recommended to use ResourceBuilder to create the resource. func WithResource(res pcommon.Resource) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { + return resourceMetricsOptionFunc(func(rm pmetric.ResourceMetrics) { res.CopyTo(rm.Resource()) - } + }) } // WithStartTimeOverride overrides start time for all the resource metrics data points. // This option should be only used if different start time has to be set on metrics coming from different resources. func WithStartTimeOverride(start pcommon.Timestamp) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { + return resourceMetricsOptionFunc(func(rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -471,7 +487,7 @@ func WithStartTimeOverride(start pcommon.Timestamp) ResourceMetricsOption { dps.At(j).SetStartTimestamp(start) } } - } + }) } // EmitForResource saves all the generated metrics under a new resource and updates the internal state to be ready for @@ -479,7 +495,7 @@ func WithStartTimeOverride(start pcommon.Timestamp) ResourceMetricsOption { // needs to emit metrics from several resources. Otherwise calling this function is not required, // just `Emit` function can be called instead. // Resource attributes should be provided as ResourceMetricsOption arguments. -func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { +func (mb *MetricsBuilder) EmitForResource(options ...ResourceMetricsOption) { rm := pmetric.NewResourceMetrics() rm.SetSchemaUrl(conventions.SchemaURL) ils := rm.ScopeMetrics().AppendEmpty() @@ -492,8 +508,8 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { mb.metricOptionalMetric.emit(ils.Metrics()) mb.metricOptionalMetricEmptyUnit.emit(ils.Metrics()) - for _, op := range rmo { - op(rm) + for _, op := range options { + op.apply(rm) } for attr, filter := range mb.resourceAttributeIncludeFilter { if val, ok := rm.Resource().Attributes().Get(attr); ok && !filter.Matches(val.AsString()) { @@ -515,8 +531,8 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { // Emit returns all the metrics accumulated by the metrics builder and updates the internal state to be ready for // recording another set of metrics. This function will be responsible for applying all the transformations required to // produce metric representation defined in metadata and user config, e.g. delta or cumulative. -func (mb *MetricsBuilder) Emit(rmo ...ResourceMetricsOption) pmetric.Metrics { - mb.EmitForResource(rmo...) +func (mb *MetricsBuilder) Emit(options ...ResourceMetricsOption) pmetric.Metrics { + mb.EmitForResource(options...) metrics := mb.metricsBuffer mb.metricsBuffer = pmetric.NewMetrics() return metrics @@ -554,9 +570,9 @@ func (mb *MetricsBuilder) RecordOptionalMetricEmptyUnitDataPoint(ts pcommon.Time // Reset resets metrics builder to its initial state. It should be used when external metrics source is restarted, // and metrics builder should update its startTime and reset it's internal state accordingly. -func (mb *MetricsBuilder) Reset(options ...metricBuilderOption) { +func (mb *MetricsBuilder) Reset(options ...MetricBuilderOption) { mb.startTime = pcommon.NewTimestampFromTime(time.Now()) for _, op := range options { - op(mb) + op.apply(mb) } } diff --git a/cmd/mdatagen/internal/samplereceiver/internal/metadata/generated_telemetry.go b/cmd/mdatagen/internal/samplereceiver/internal/metadata/generated_telemetry.go index 43104bedead..17685ad210d 100644 --- a/cmd/mdatagen/internal/samplereceiver/internal/metadata/generated_telemetry.go +++ b/cmd/mdatagen/internal/samplereceiver/internal/metadata/generated_telemetry.go @@ -38,17 +38,25 @@ type TelemetryBuilder struct { meters map[configtelemetry.Level]metric.Meter } -// telemetryBuilderOption applies changes to default builder. -type telemetryBuilderOption func(*TelemetryBuilder) +// TelemetryBuilderOption applies changes to default builder. +type TelemetryBuilderOption interface { + apply(*TelemetryBuilder) +} + +type telemetryBuilderOptionFunc func(mb *TelemetryBuilder) + +func (tbof telemetryBuilderOptionFunc) apply(mb *TelemetryBuilder) { + tbof(mb) +} // WithProcessRuntimeTotalAllocBytesCallback sets callback for observable ProcessRuntimeTotalAllocBytes metric. -func WithProcessRuntimeTotalAllocBytesCallback(cb func() int64, opts ...metric.ObserveOption) telemetryBuilderOption { - return func(builder *TelemetryBuilder) { +func WithProcessRuntimeTotalAllocBytesCallback(cb func() int64, opts ...metric.ObserveOption) TelemetryBuilderOption { + return telemetryBuilderOptionFunc(func(builder *TelemetryBuilder) { builder.observeProcessRuntimeTotalAllocBytes = func(_ context.Context, o metric.Observer) error { o.ObserveInt64(builder.ProcessRuntimeTotalAllocBytes, cb(), opts...) return nil } - } + }) } // InitQueueLength configures the QueueLength metric. @@ -71,10 +79,10 @@ func (builder *TelemetryBuilder) InitQueueLength(cb func() int64, opts ...metric // NewTelemetryBuilder provides a struct with methods to update all internal telemetry // for a component -func NewTelemetryBuilder(settings component.TelemetrySettings, options ...telemetryBuilderOption) (*TelemetryBuilder, error) { +func NewTelemetryBuilder(settings component.TelemetrySettings, options ...TelemetryBuilderOption) (*TelemetryBuilder, error) { builder := TelemetryBuilder{meters: map[configtelemetry.Level]metric.Meter{}} for _, op := range options { - op(&builder) + op.apply(&builder) } builder.meters[configtelemetry.LevelBasic] = LeveledMeter(settings, configtelemetry.LevelBasic) var err, errs error diff --git a/cmd/mdatagen/internal/samplereceiver/internal/metadata/generated_telemetry_test.go b/cmd/mdatagen/internal/samplereceiver/internal/metadata/generated_telemetry_test.go index 2d6767665ff..d34a3f5f639 100644 --- a/cmd/mdatagen/internal/samplereceiver/internal/metadata/generated_telemetry_test.go +++ b/cmd/mdatagen/internal/samplereceiver/internal/metadata/generated_telemetry_test.go @@ -75,9 +75,9 @@ func TestNewTelemetryBuilder(t *testing.T) { TracerProvider: mockTracerProvider{}, } applied := false - _, err := NewTelemetryBuilder(set, func(b *TelemetryBuilder) { + _, err := NewTelemetryBuilder(set, telemetryBuilderOptionFunc(func(b *TelemetryBuilder) { applied = true - }) + })) require.NoError(t, err) require.True(t, applied) } diff --git a/cmd/mdatagen/templates/metrics.go.tmpl b/cmd/mdatagen/templates/metrics.go.tmpl index a084d847ee8..1a7075d3de4 100644 --- a/cmd/mdatagen/templates/metrics.go.tmpl +++ b/cmd/mdatagen/templates/metrics.go.tmpl @@ -144,17 +144,25 @@ type MetricsBuilder struct { {{- end }} } -// metricBuilderOption applies changes to default metrics builder. -type metricBuilderOption func(*MetricsBuilder) +// MetricBuilderOption applies changes to default metrics builder. +type MetricBuilderOption interface { + apply(*MetricsBuilder) +} + +type metricBuilderOptionFunc func(mb *MetricsBuilder) + +func (mbof metricBuilderOptionFunc) apply(mb *MetricsBuilder) { + mbof(mb) +} // WithStartTime sets startTime on the metrics builder. -func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { - return func(mb *MetricsBuilder) { +func WithStartTime(startTime pcommon.Timestamp) MetricBuilderOption { + return metricBuilderOptionFunc(func(mb *MetricsBuilder) { mb.startTime = startTime - } + }) } -func NewMetricsBuilder(mbc MetricsBuilderConfig, settings receiver.Settings, options ...metricBuilderOption) *MetricsBuilder { +func NewMetricsBuilder(mbc MetricsBuilderConfig, settings receiver.Settings, options ...MetricBuilderOption) *MetricsBuilder { {{- range $name, $metric := .Metrics }} {{- if $metric.Warnings.IfEnabled }} if mbc.Metrics.{{ $name.Render }}.Enabled { @@ -212,7 +220,7 @@ func NewMetricsBuilder(mbc MetricsBuilderConfig, settings receiver.Settings, opt {{- end }} for _, op := range options { - op(mb) + op.apply(mb) } return mb } @@ -232,20 +240,28 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption interface { + apply(pmetric.ResourceMetrics) +} + +type resourceMetricsOptionFunc func(pmetric.ResourceMetrics) + +func (rmof resourceMetricsOptionFunc) apply(rm pmetric.ResourceMetrics) { + rmof(rm) +} // WithResource sets the provided resource on the emitted ResourceMetrics. // It's recommended to use ResourceBuilder to create the resource. func WithResource(res pcommon.Resource) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { + return resourceMetricsOptionFunc(func(rm pmetric.ResourceMetrics) { res.CopyTo(rm.Resource()) - } + }) } // WithStartTimeOverride overrides start time for all the resource metrics data points. // This option should be only used if different start time has to be set on metrics coming from different resources. func WithStartTimeOverride(start pcommon.Timestamp) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { + return resourceMetricsOptionFunc(func(rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -259,7 +275,7 @@ func WithStartTimeOverride(start pcommon.Timestamp) ResourceMetricsOption { dps.At(j).SetStartTimestamp(start) } } - } + }) } // EmitForResource saves all the generated metrics under a new resource and updates the internal state to be ready for @@ -267,7 +283,7 @@ func WithStartTimeOverride(start pcommon.Timestamp) ResourceMetricsOption { // needs to emit metrics from several resources. Otherwise calling this function is not required, // just `Emit` function can be called instead. // Resource attributes should be provided as ResourceMetricsOption arguments. -func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { +func (mb *MetricsBuilder) EmitForResource(options ...ResourceMetricsOption) { rm := pmetric.NewResourceMetrics() {{- if .SemConvVersion }} rm.SetSchemaUrl(conventions.SchemaURL) @@ -280,8 +296,8 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { mb.metric{{- $name.Render }}.emit(ils.Metrics()) {{- end }} - for _, op := range rmo { - op(rm) + for _, op := range options { + op.apply(rm) } {{ if .ResourceAttributes -}} for attr, filter := range mb.resourceAttributeIncludeFilter { @@ -305,8 +321,8 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { // Emit returns all the metrics accumulated by the metrics builder and updates the internal state to be ready for // recording another set of metrics. This function will be responsible for applying all the transformations required to // produce metric representation defined in metadata and user config, e.g. delta or cumulative. -func (mb *MetricsBuilder) Emit(rmo ...ResourceMetricsOption) pmetric.Metrics { - mb.EmitForResource(rmo...) +func (mb *MetricsBuilder) Emit(options ...ResourceMetricsOption) pmetric.Metrics { + mb.EmitForResource(options...) metrics := mb.metricsBuffer mb.metricsBuffer = pmetric.NewMetrics() return metrics @@ -344,9 +360,9 @@ func (mb *MetricsBuilder) Record{{ $name.Render }}DataPoint(ts pcommon.Timestamp // Reset resets metrics builder to its initial state. It should be used when external metrics source is restarted, // and metrics builder should update its startTime and reset it's internal state accordingly. -func (mb *MetricsBuilder) Reset(options ...metricBuilderOption) { +func (mb *MetricsBuilder) Reset(options ...MetricBuilderOption) { mb.startTime = pcommon.NewTimestampFromTime(time.Now()) for _, op := range options { - op(mb) + op.apply(mb) } } diff --git a/cmd/mdatagen/templates/telemetry.go.tmpl b/cmd/mdatagen/templates/telemetry.go.tmpl index a4b98c3359b..7460f5958c1 100644 --- a/cmd/mdatagen/templates/telemetry.go.tmpl +++ b/cmd/mdatagen/templates/telemetry.go.tmpl @@ -48,8 +48,17 @@ type TelemetryBuilder struct { meters map[configtelemetry.Level]metric.Meter } -// telemetryBuilderOption applies changes to default builder. -type telemetryBuilderOption func(*TelemetryBuilder) +// TelemetryBuilderOption applies changes to default builder. +type TelemetryBuilderOption interface { + apply(*TelemetryBuilder) +} + +type telemetryBuilderOptionFunc func(mb *TelemetryBuilder) + +func (tbof telemetryBuilderOptionFunc) apply(mb *TelemetryBuilder) { + tbof(mb) +} + {{- range $name, $metric := .Telemetry.Metrics }} {{- if $metric.Optional }} @@ -79,13 +88,13 @@ func (builder *TelemetryBuilder) Init{{ $name.Render }}({{ if $metric.Data.Async {{- else }} {{ if $metric.Data.Async -}} // With{{ $name.Render }}Callback sets callback for observable {{ $name.Render }} metric. -func With{{ $name.Render }}Callback(cb func() {{ $metric.Data.BasicType }}, opts ...metric.ObserveOption) telemetryBuilderOption { - return func(builder *TelemetryBuilder) { +func With{{ $name.Render }}Callback(cb func() {{ $metric.Data.BasicType }}, opts ...metric.ObserveOption) TelemetryBuilderOption { + return telemetryBuilderOptionFunc(func(builder *TelemetryBuilder) { builder.observe{{ $name.Render }} = func(_ context.Context, o metric.Observer) error { o.Observe{{ casesTitle $metric.Data.BasicType }}(builder.{{ $name.Render }}, cb(), opts...) return nil } - } + }) } {{- end }} {{- end }} @@ -94,10 +103,10 @@ func With{{ $name.Render }}Callback(cb func() {{ $metric.Data.BasicType }}, opts // NewTelemetryBuilder provides a struct with methods to update all internal telemetry // for a component -func NewTelemetryBuilder(settings component.TelemetrySettings, options ...telemetryBuilderOption) (*TelemetryBuilder, error) { +func NewTelemetryBuilder(settings component.TelemetrySettings, options ...TelemetryBuilderOption) (*TelemetryBuilder, error) { builder := TelemetryBuilder{meters: map[configtelemetry.Level]metric.Meter{}} for _, op := range options { - op(&builder) + op.apply(&builder) } {{- range $level, $val := .Telemetry.Levels }} builder.meters[configtelemetry.Level{{ casesTitle $level }}] = LeveledMeter(settings, configtelemetry.Level{{ casesTitle $level }}) diff --git a/cmd/mdatagen/templates/telemetry_test.go.tmpl b/cmd/mdatagen/templates/telemetry_test.go.tmpl index 600fe5d42d8..87cd7c2688a 100644 --- a/cmd/mdatagen/templates/telemetry_test.go.tmpl +++ b/cmd/mdatagen/templates/telemetry_test.go.tmpl @@ -76,9 +76,9 @@ func TestNewTelemetryBuilder(t *testing.T) { TracerProvider: mockTracerProvider{}, } applied := false - _, err := NewTelemetryBuilder(set, func(b *TelemetryBuilder) { + _, err := NewTelemetryBuilder(set, telemetryBuilderOptionFunc(func(b *TelemetryBuilder) { applied = true - }) + })) require.NoError(t, err) require.True(t, applied) } diff --git a/exporter/exporterhelper/internal/metadata/generated_telemetry.go b/exporter/exporterhelper/internal/metadata/generated_telemetry.go index a35625ac0a5..b61d6cda6f2 100644 --- a/exporter/exporterhelper/internal/metadata/generated_telemetry.go +++ b/exporter/exporterhelper/internal/metadata/generated_telemetry.go @@ -44,8 +44,16 @@ type TelemetryBuilder struct { meters map[configtelemetry.Level]metric.Meter } -// telemetryBuilderOption applies changes to default builder. -type telemetryBuilderOption func(*TelemetryBuilder) +// TelemetryBuilderOption applies changes to default builder. +type TelemetryBuilderOption interface { + apply(*TelemetryBuilder) +} + +type telemetryBuilderOptionFunc func(mb *TelemetryBuilder) + +func (tbof telemetryBuilderOptionFunc) apply(mb *TelemetryBuilder) { + tbof(mb) +} // InitExporterQueueCapacity configures the ExporterQueueCapacity metric. func (builder *TelemetryBuilder) InitExporterQueueCapacity(cb func() int64, opts ...metric.ObserveOption) error { @@ -85,10 +93,10 @@ func (builder *TelemetryBuilder) InitExporterQueueSize(cb func() int64, opts ... // NewTelemetryBuilder provides a struct with methods to update all internal telemetry // for a component -func NewTelemetryBuilder(settings component.TelemetrySettings, options ...telemetryBuilderOption) (*TelemetryBuilder, error) { +func NewTelemetryBuilder(settings component.TelemetrySettings, options ...TelemetryBuilderOption) (*TelemetryBuilder, error) { builder := TelemetryBuilder{meters: map[configtelemetry.Level]metric.Meter{}} for _, op := range options { - op(&builder) + op.apply(&builder) } builder.meters[configtelemetry.LevelBasic] = LeveledMeter(settings, configtelemetry.LevelBasic) var err, errs error diff --git a/exporter/exporterhelper/internal/metadata/generated_telemetry_test.go b/exporter/exporterhelper/internal/metadata/generated_telemetry_test.go index ad1906945a4..20372daee92 100644 --- a/exporter/exporterhelper/internal/metadata/generated_telemetry_test.go +++ b/exporter/exporterhelper/internal/metadata/generated_telemetry_test.go @@ -75,9 +75,9 @@ func TestNewTelemetryBuilder(t *testing.T) { TracerProvider: mockTracerProvider{}, } applied := false - _, err := NewTelemetryBuilder(set, func(b *TelemetryBuilder) { + _, err := NewTelemetryBuilder(set, telemetryBuilderOptionFunc(func(b *TelemetryBuilder) { applied = true - }) + })) require.NoError(t, err) require.True(t, applied) } diff --git a/processor/batchprocessor/internal/metadata/generated_telemetry.go b/processor/batchprocessor/internal/metadata/generated_telemetry.go index 38a97468402..f4079ad8f0e 100644 --- a/processor/batchprocessor/internal/metadata/generated_telemetry.go +++ b/processor/batchprocessor/internal/metadata/generated_telemetry.go @@ -39,25 +39,33 @@ type TelemetryBuilder struct { meters map[configtelemetry.Level]metric.Meter } -// telemetryBuilderOption applies changes to default builder. -type telemetryBuilderOption func(*TelemetryBuilder) +// TelemetryBuilderOption applies changes to default builder. +type TelemetryBuilderOption interface { + apply(*TelemetryBuilder) +} + +type telemetryBuilderOptionFunc func(mb *TelemetryBuilder) + +func (tbof telemetryBuilderOptionFunc) apply(mb *TelemetryBuilder) { + tbof(mb) +} // WithProcessorBatchMetadataCardinalityCallback sets callback for observable ProcessorBatchMetadataCardinality metric. -func WithProcessorBatchMetadataCardinalityCallback(cb func() int64, opts ...metric.ObserveOption) telemetryBuilderOption { - return func(builder *TelemetryBuilder) { +func WithProcessorBatchMetadataCardinalityCallback(cb func() int64, opts ...metric.ObserveOption) TelemetryBuilderOption { + return telemetryBuilderOptionFunc(func(builder *TelemetryBuilder) { builder.observeProcessorBatchMetadataCardinality = func(_ context.Context, o metric.Observer) error { o.ObserveInt64(builder.ProcessorBatchMetadataCardinality, cb(), opts...) return nil } - } + }) } // NewTelemetryBuilder provides a struct with methods to update all internal telemetry // for a component -func NewTelemetryBuilder(settings component.TelemetrySettings, options ...telemetryBuilderOption) (*TelemetryBuilder, error) { +func NewTelemetryBuilder(settings component.TelemetrySettings, options ...TelemetryBuilderOption) (*TelemetryBuilder, error) { builder := TelemetryBuilder{meters: map[configtelemetry.Level]metric.Meter{}} for _, op := range options { - op(&builder) + op.apply(&builder) } builder.meters[configtelemetry.LevelBasic] = LeveledMeter(settings, configtelemetry.LevelBasic) builder.meters[configtelemetry.LevelDetailed] = LeveledMeter(settings, configtelemetry.LevelDetailed) diff --git a/processor/batchprocessor/internal/metadata/generated_telemetry_test.go b/processor/batchprocessor/internal/metadata/generated_telemetry_test.go index f05add260c3..1591d07b7fb 100644 --- a/processor/batchprocessor/internal/metadata/generated_telemetry_test.go +++ b/processor/batchprocessor/internal/metadata/generated_telemetry_test.go @@ -75,9 +75,9 @@ func TestNewTelemetryBuilder(t *testing.T) { TracerProvider: mockTracerProvider{}, } applied := false - _, err := NewTelemetryBuilder(set, func(b *TelemetryBuilder) { + _, err := NewTelemetryBuilder(set, telemetryBuilderOptionFunc(func(b *TelemetryBuilder) { applied = true - }) + })) require.NoError(t, err) require.True(t, applied) } diff --git a/processor/processorhelper/internal/metadata/generated_telemetry.go b/processor/processorhelper/internal/metadata/generated_telemetry.go index 11a7a86ee4b..ff2ede1496a 100644 --- a/processor/processorhelper/internal/metadata/generated_telemetry.go +++ b/processor/processorhelper/internal/metadata/generated_telemetry.go @@ -47,15 +47,23 @@ type TelemetryBuilder struct { meters map[configtelemetry.Level]metric.Meter } -// telemetryBuilderOption applies changes to default builder. -type telemetryBuilderOption func(*TelemetryBuilder) +// TelemetryBuilderOption applies changes to default builder. +type TelemetryBuilderOption interface { + apply(*TelemetryBuilder) +} + +type telemetryBuilderOptionFunc func(mb *TelemetryBuilder) + +func (tbof telemetryBuilderOptionFunc) apply(mb *TelemetryBuilder) { + tbof(mb) +} // NewTelemetryBuilder provides a struct with methods to update all internal telemetry // for a component -func NewTelemetryBuilder(settings component.TelemetrySettings, options ...telemetryBuilderOption) (*TelemetryBuilder, error) { +func NewTelemetryBuilder(settings component.TelemetrySettings, options ...TelemetryBuilderOption) (*TelemetryBuilder, error) { builder := TelemetryBuilder{meters: map[configtelemetry.Level]metric.Meter{}} for _, op := range options { - op(&builder) + op.apply(&builder) } builder.meters[configtelemetry.LevelBasic] = LeveledMeter(settings, configtelemetry.LevelBasic) var err, errs error diff --git a/processor/processorhelper/internal/metadata/generated_telemetry_test.go b/processor/processorhelper/internal/metadata/generated_telemetry_test.go index dc6fbeca903..21457f0a3fd 100644 --- a/processor/processorhelper/internal/metadata/generated_telemetry_test.go +++ b/processor/processorhelper/internal/metadata/generated_telemetry_test.go @@ -75,9 +75,9 @@ func TestNewTelemetryBuilder(t *testing.T) { TracerProvider: mockTracerProvider{}, } applied := false - _, err := NewTelemetryBuilder(set, func(b *TelemetryBuilder) { + _, err := NewTelemetryBuilder(set, telemetryBuilderOptionFunc(func(b *TelemetryBuilder) { applied = true - }) + })) require.NoError(t, err) require.True(t, applied) } diff --git a/receiver/receiverhelper/internal/metadata/generated_telemetry.go b/receiver/receiverhelper/internal/metadata/generated_telemetry.go index 766735f943c..86c263792cb 100644 --- a/receiver/receiverhelper/internal/metadata/generated_telemetry.go +++ b/receiver/receiverhelper/internal/metadata/generated_telemetry.go @@ -38,15 +38,23 @@ type TelemetryBuilder struct { meters map[configtelemetry.Level]metric.Meter } -// telemetryBuilderOption applies changes to default builder. -type telemetryBuilderOption func(*TelemetryBuilder) +// TelemetryBuilderOption applies changes to default builder. +type TelemetryBuilderOption interface { + apply(*TelemetryBuilder) +} + +type telemetryBuilderOptionFunc func(mb *TelemetryBuilder) + +func (tbof telemetryBuilderOptionFunc) apply(mb *TelemetryBuilder) { + tbof(mb) +} // NewTelemetryBuilder provides a struct with methods to update all internal telemetry // for a component -func NewTelemetryBuilder(settings component.TelemetrySettings, options ...telemetryBuilderOption) (*TelemetryBuilder, error) { +func NewTelemetryBuilder(settings component.TelemetrySettings, options ...TelemetryBuilderOption) (*TelemetryBuilder, error) { builder := TelemetryBuilder{meters: map[configtelemetry.Level]metric.Meter{}} for _, op := range options { - op(&builder) + op.apply(&builder) } builder.meters[configtelemetry.LevelBasic] = LeveledMeter(settings, configtelemetry.LevelBasic) var err, errs error diff --git a/receiver/receiverhelper/internal/metadata/generated_telemetry_test.go b/receiver/receiverhelper/internal/metadata/generated_telemetry_test.go index c96d8509125..b8162a38f07 100644 --- a/receiver/receiverhelper/internal/metadata/generated_telemetry_test.go +++ b/receiver/receiverhelper/internal/metadata/generated_telemetry_test.go @@ -75,9 +75,9 @@ func TestNewTelemetryBuilder(t *testing.T) { TracerProvider: mockTracerProvider{}, } applied := false - _, err := NewTelemetryBuilder(set, func(b *TelemetryBuilder) { + _, err := NewTelemetryBuilder(set, telemetryBuilderOptionFunc(func(b *TelemetryBuilder) { applied = true - }) + })) require.NoError(t, err) require.True(t, applied) } diff --git a/receiver/scraperhelper/internal/metadata/generated_telemetry.go b/receiver/scraperhelper/internal/metadata/generated_telemetry.go index 498746b0e87..5bcbf186364 100644 --- a/receiver/scraperhelper/internal/metadata/generated_telemetry.go +++ b/receiver/scraperhelper/internal/metadata/generated_telemetry.go @@ -34,15 +34,23 @@ type TelemetryBuilder struct { meters map[configtelemetry.Level]metric.Meter } -// telemetryBuilderOption applies changes to default builder. -type telemetryBuilderOption func(*TelemetryBuilder) +// TelemetryBuilderOption applies changes to default builder. +type TelemetryBuilderOption interface { + apply(*TelemetryBuilder) +} + +type telemetryBuilderOptionFunc func(mb *TelemetryBuilder) + +func (tbof telemetryBuilderOptionFunc) apply(mb *TelemetryBuilder) { + tbof(mb) +} // NewTelemetryBuilder provides a struct with methods to update all internal telemetry // for a component -func NewTelemetryBuilder(settings component.TelemetrySettings, options ...telemetryBuilderOption) (*TelemetryBuilder, error) { +func NewTelemetryBuilder(settings component.TelemetrySettings, options ...TelemetryBuilderOption) (*TelemetryBuilder, error) { builder := TelemetryBuilder{meters: map[configtelemetry.Level]metric.Meter{}} for _, op := range options { - op(&builder) + op.apply(&builder) } builder.meters[configtelemetry.LevelBasic] = LeveledMeter(settings, configtelemetry.LevelBasic) var err, errs error diff --git a/receiver/scraperhelper/internal/metadata/generated_telemetry_test.go b/receiver/scraperhelper/internal/metadata/generated_telemetry_test.go index d2d189e7643..ad6bc072985 100644 --- a/receiver/scraperhelper/internal/metadata/generated_telemetry_test.go +++ b/receiver/scraperhelper/internal/metadata/generated_telemetry_test.go @@ -75,9 +75,9 @@ func TestNewTelemetryBuilder(t *testing.T) { TracerProvider: mockTracerProvider{}, } applied := false - _, err := NewTelemetryBuilder(set, func(b *TelemetryBuilder) { + _, err := NewTelemetryBuilder(set, telemetryBuilderOptionFunc(func(b *TelemetryBuilder) { applied = true - }) + })) require.NoError(t, err) require.True(t, applied) } diff --git a/service/internal/metadata/generated_telemetry.go b/service/internal/metadata/generated_telemetry.go index c08e6cd7cd3..5c60998cb83 100644 --- a/service/internal/metadata/generated_telemetry.go +++ b/service/internal/metadata/generated_telemetry.go @@ -45,75 +45,83 @@ type TelemetryBuilder struct { meters map[configtelemetry.Level]metric.Meter } -// telemetryBuilderOption applies changes to default builder. -type telemetryBuilderOption func(*TelemetryBuilder) +// TelemetryBuilderOption applies changes to default builder. +type TelemetryBuilderOption interface { + apply(*TelemetryBuilder) +} + +type telemetryBuilderOptionFunc func(mb *TelemetryBuilder) + +func (tbof telemetryBuilderOptionFunc) apply(mb *TelemetryBuilder) { + tbof(mb) +} // WithProcessCPUSecondsCallback sets callback for observable ProcessCPUSeconds metric. -func WithProcessCPUSecondsCallback(cb func() float64, opts ...metric.ObserveOption) telemetryBuilderOption { - return func(builder *TelemetryBuilder) { +func WithProcessCPUSecondsCallback(cb func() float64, opts ...metric.ObserveOption) TelemetryBuilderOption { + return telemetryBuilderOptionFunc(func(builder *TelemetryBuilder) { builder.observeProcessCPUSeconds = func(_ context.Context, o metric.Observer) error { o.ObserveFloat64(builder.ProcessCPUSeconds, cb(), opts...) return nil } - } + }) } // WithProcessMemoryRssCallback sets callback for observable ProcessMemoryRss metric. -func WithProcessMemoryRssCallback(cb func() int64, opts ...metric.ObserveOption) telemetryBuilderOption { - return func(builder *TelemetryBuilder) { +func WithProcessMemoryRssCallback(cb func() int64, opts ...metric.ObserveOption) TelemetryBuilderOption { + return telemetryBuilderOptionFunc(func(builder *TelemetryBuilder) { builder.observeProcessMemoryRss = func(_ context.Context, o metric.Observer) error { o.ObserveInt64(builder.ProcessMemoryRss, cb(), opts...) return nil } - } + }) } // WithProcessRuntimeHeapAllocBytesCallback sets callback for observable ProcessRuntimeHeapAllocBytes metric. -func WithProcessRuntimeHeapAllocBytesCallback(cb func() int64, opts ...metric.ObserveOption) telemetryBuilderOption { - return func(builder *TelemetryBuilder) { +func WithProcessRuntimeHeapAllocBytesCallback(cb func() int64, opts ...metric.ObserveOption) TelemetryBuilderOption { + return telemetryBuilderOptionFunc(func(builder *TelemetryBuilder) { builder.observeProcessRuntimeHeapAllocBytes = func(_ context.Context, o metric.Observer) error { o.ObserveInt64(builder.ProcessRuntimeHeapAllocBytes, cb(), opts...) return nil } - } + }) } // WithProcessRuntimeTotalAllocBytesCallback sets callback for observable ProcessRuntimeTotalAllocBytes metric. -func WithProcessRuntimeTotalAllocBytesCallback(cb func() int64, opts ...metric.ObserveOption) telemetryBuilderOption { - return func(builder *TelemetryBuilder) { +func WithProcessRuntimeTotalAllocBytesCallback(cb func() int64, opts ...metric.ObserveOption) TelemetryBuilderOption { + return telemetryBuilderOptionFunc(func(builder *TelemetryBuilder) { builder.observeProcessRuntimeTotalAllocBytes = func(_ context.Context, o metric.Observer) error { o.ObserveInt64(builder.ProcessRuntimeTotalAllocBytes, cb(), opts...) return nil } - } + }) } // WithProcessRuntimeTotalSysMemoryBytesCallback sets callback for observable ProcessRuntimeTotalSysMemoryBytes metric. -func WithProcessRuntimeTotalSysMemoryBytesCallback(cb func() int64, opts ...metric.ObserveOption) telemetryBuilderOption { - return func(builder *TelemetryBuilder) { +func WithProcessRuntimeTotalSysMemoryBytesCallback(cb func() int64, opts ...metric.ObserveOption) TelemetryBuilderOption { + return telemetryBuilderOptionFunc(func(builder *TelemetryBuilder) { builder.observeProcessRuntimeTotalSysMemoryBytes = func(_ context.Context, o metric.Observer) error { o.ObserveInt64(builder.ProcessRuntimeTotalSysMemoryBytes, cb(), opts...) return nil } - } + }) } // WithProcessUptimeCallback sets callback for observable ProcessUptime metric. -func WithProcessUptimeCallback(cb func() float64, opts ...metric.ObserveOption) telemetryBuilderOption { - return func(builder *TelemetryBuilder) { +func WithProcessUptimeCallback(cb func() float64, opts ...metric.ObserveOption) TelemetryBuilderOption { + return telemetryBuilderOptionFunc(func(builder *TelemetryBuilder) { builder.observeProcessUptime = func(_ context.Context, o metric.Observer) error { o.ObserveFloat64(builder.ProcessUptime, cb(), opts...) return nil } - } + }) } // NewTelemetryBuilder provides a struct with methods to update all internal telemetry // for a component -func NewTelemetryBuilder(settings component.TelemetrySettings, options ...telemetryBuilderOption) (*TelemetryBuilder, error) { +func NewTelemetryBuilder(settings component.TelemetrySettings, options ...TelemetryBuilderOption) (*TelemetryBuilder, error) { builder := TelemetryBuilder{meters: map[configtelemetry.Level]metric.Meter{}} for _, op := range options { - op(&builder) + op.apply(&builder) } builder.meters[configtelemetry.LevelBasic] = LeveledMeter(settings, configtelemetry.LevelBasic) var err, errs error diff --git a/service/internal/metadata/generated_telemetry_test.go b/service/internal/metadata/generated_telemetry_test.go index c8f5f6fb6e7..6e64350d4c5 100644 --- a/service/internal/metadata/generated_telemetry_test.go +++ b/service/internal/metadata/generated_telemetry_test.go @@ -75,9 +75,9 @@ func TestNewTelemetryBuilder(t *testing.T) { TracerProvider: mockTracerProvider{}, } applied := false - _, err := NewTelemetryBuilder(set, func(b *TelemetryBuilder) { + _, err := NewTelemetryBuilder(set, telemetryBuilderOptionFunc(func(b *TelemetryBuilder) { applied = true - }) + })) require.NoError(t, err) require.True(t, applied) }