From c7c3401f39f35705c2f1c09517b912ffdec9c60f Mon Sep 17 00:00:00 2001 From: Alex Boten <223565+codeboten@users.noreply.github.com> Date: Tue, 28 May 2024 08:13:37 -0700 Subject: [PATCH] [mdatagen] add support for bucket boundaries (#10229) This allows setting of explicit boundaries for histograms. Fixes https://github.com/open-telemetry/opentelemetry-collector/issues/10218 --------- Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com> --- ..._move-batchprocessortest-mdatagentest.yaml | 25 +++++++++++++++++++ .../internal/metadata/generated_telemetry.go | 2 +- .../internal/samplereceiver/metadata.yaml | 1 + cmd/mdatagen/loader_test.go | 1 + cmd/mdatagen/metadata-schema.yaml | 2 ++ cmd/mdatagen/metricdata.go | 3 ++- cmd/mdatagen/templates/telemetry.go.tmpl | 3 +++ 7 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 .chloggen/codeboten_move-batchprocessortest-mdatagentest.yaml diff --git a/.chloggen/codeboten_move-batchprocessortest-mdatagentest.yaml b/.chloggen/codeboten_move-batchprocessortest-mdatagentest.yaml new file mode 100644 index 00000000000..b297d7b51ea --- /dev/null +++ b/.chloggen/codeboten_move-batchprocessortest-mdatagentest.yaml @@ -0,0 +1,25 @@ +# 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: add support for bucket boundaries for histograms + +# One or more tracking issues or pull requests related to the change +issues: [10218] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# 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: [] diff --git a/cmd/mdatagen/internal/samplereceiver/internal/metadata/generated_telemetry.go b/cmd/mdatagen/internal/samplereceiver/internal/metadata/generated_telemetry.go index 86a50251b2d..a3dae0317ee 100644 --- a/cmd/mdatagen/internal/samplereceiver/internal/metadata/generated_telemetry.go +++ b/cmd/mdatagen/internal/samplereceiver/internal/metadata/generated_telemetry.go @@ -67,7 +67,7 @@ func NewTelemetryBuilder(settings component.TelemetrySettings, options ...teleme builder.RequestDuration, err = meter.Float64Histogram( "request_duration", metric.WithDescription("Duration of request"), - metric.WithUnit("s"), + metric.WithUnit("s"), metric.WithExplicitBucketBoundaries([]float64{1, 10, 100}...), ) errs = errors.Join(errs, err) return &builder, errs diff --git a/cmd/mdatagen/internal/samplereceiver/metadata.yaml b/cmd/mdatagen/internal/samplereceiver/metadata.yaml index 824b1e0ecae..03955d62c0a 100644 --- a/cmd/mdatagen/internal/samplereceiver/metadata.yaml +++ b/cmd/mdatagen/internal/samplereceiver/metadata.yaml @@ -165,6 +165,7 @@ telemetry: unit: s histogram: value_type: double + bucket_boundaries: [1, 10, 100] process_runtime_total_alloc_bytes: enabled: true description: Cumulative bytes allocated for heap objects (see 'go doc runtime.MemStats.TotalAlloc') diff --git a/cmd/mdatagen/loader_test.go b/cmd/mdatagen/loader_test.go index 0c32493d283..bdd8ffeeb37 100644 --- a/cmd/mdatagen/loader_test.go +++ b/cmd/mdatagen/loader_test.go @@ -248,6 +248,7 @@ func TestLoadMetadata(t *testing.T) { Unit: strPtr("s"), Histogram: &histogram{ MetricValueType: MetricValueType{pmetric.NumberDataPointValueTypeDouble}, + Boundaries: []float64{1, 10, 100}, }, }, "process_runtime_total_alloc_bytes": { diff --git a/cmd/mdatagen/metadata-schema.yaml b/cmd/mdatagen/metadata-schema.yaml index a4c3af67b05..ebdb51fddc3 100644 --- a/cmd/mdatagen/metadata-schema.yaml +++ b/cmd/mdatagen/metadata-schema.yaml @@ -157,5 +157,7 @@ telemetry: value_type: # Required for sum metric: whether the metric is monotonic (no negative delta values). monotonic: bool + # Bucket boundaries are only available to set for histogram metrics. + bucket_boundaries: [double] # Optional: array of attributes that were defined in the attributes section that are emitted by this metric. attributes: [string] diff --git a/cmd/mdatagen/metricdata.go b/cmd/mdatagen/metricdata.go index 2acf4b77b3b..4749e03e608 100644 --- a/cmd/mdatagen/metricdata.go +++ b/cmd/mdatagen/metricdata.go @@ -223,7 +223,8 @@ type histogram struct { Mono `mapstructure:",squash"` MetricValueType `mapstructure:"value_type"` MetricInputType `mapstructure:",squash"` - Async bool `mapstructure:"async,omitempty"` + Async bool `mapstructure:"async,omitempty"` + Boundaries []float64 `mapstructure:"bucket_boundaries"` } func (d histogram) Type() string { diff --git a/cmd/mdatagen/templates/telemetry.go.tmpl b/cmd/mdatagen/templates/telemetry.go.tmpl index 5643b66967f..7f9ca171065 100644 --- a/cmd/mdatagen/templates/telemetry.go.tmpl +++ b/cmd/mdatagen/templates/telemetry.go.tmpl @@ -61,6 +61,9 @@ func NewTelemetryBuilder(settings component.TelemetrySettings, options ...teleme "{{ $name }}", metric.WithDescription("{{ $metric.Description }}"), metric.WithUnit("{{ $metric.Unit }}"), + {{- if eq $metric.Data.Type "Histogram" -}} + {{ if $metric.Data.Boundaries -}}metric.WithExplicitBucketBoundaries([]float64{ {{- range $metric.Data.Boundaries }} {{.}}, {{- end }} }...),{{- end }} + {{- end }} {{ if $metric.Data.Async -}} metric.With{{ casesTitle $metric.Data.BasicType }}Callback(func(_ context.Context, o metric.{{ casesTitle $metric.Data.BasicType }}Observer) error { o.Observe(builder.observe{{ $name.Render }}())