Skip to content

Commit

Permalink
[mdatagen] Avoid public APIs with internal params (#11040)
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu authored Sep 13, 2024
1 parent 5102a1e commit 8a07009
Show file tree
Hide file tree
Showing 19 changed files with 230 additions and 113 deletions.
20 changes: 20 additions & 0 deletions .chloggen/avoid-internal.yaml
Original file line number Diff line number Diff line change
@@ -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]

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.

54 changes: 35 additions & 19 deletions cmd/mdatagen/templates/metrics.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -212,7 +220,7 @@ func NewMetricsBuilder(mbc MetricsBuilderConfig, settings receiver.Settings, opt
{{- end }}

for _, op := range options {
op(mb)
op.apply(mb)
}
return mb
}
Expand All @@ -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++ {
Expand All @@ -259,15 +275,15 @@ 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
// recording another set of data points as part of another resource. This function can be helpful when one scraper
// 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)
Expand All @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
}
23 changes: 16 additions & 7 deletions cmd/mdatagen/templates/telemetry.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down Expand Up @@ -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 }}
Expand All @@ -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 }})
Expand Down
4 changes: 2 additions & 2 deletions cmd/mdatagen/templates/telemetry_test.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Loading

0 comments on commit 8a07009

Please sign in to comment.