From 37a58a4736c21567e2926e4463043701d4d9de58 Mon Sep 17 00:00:00 2001 From: David Ashpole Date: Fri, 19 Apr 2024 18:49:28 +0000 Subject: [PATCH] Support metric.metadata in pdata/pmetric --- .chloggen/metric-metadata.yaml | 25 +++++++++++++++++++ .../cmd/pdatagen/internal/pmetric_package.go | 4 +++ pdata/pmetric/generated_metric.go | 7 ++++++ pdata/pmetric/generated_metric_test.go | 9 +++++++ 4 files changed, 45 insertions(+) create mode 100644 .chloggen/metric-metadata.yaml diff --git a/.chloggen/metric-metadata.yaml b/.chloggen/metric-metadata.yaml new file mode 100644 index 00000000000..623c54562d5 --- /dev/null +++ b/.chloggen/metric-metadata.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: pmetric + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Support metric.metadata in pdata/pmetric + +# One or more tracking issues or pull requests related to the change +issues: [] + +# (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: [api] diff --git a/pdata/internal/cmd/pdatagen/internal/pmetric_package.go b/pdata/internal/cmd/pdatagen/internal/pmetric_package.go index 95487c00f8f..68aee5219f9 100644 --- a/pdata/internal/cmd/pdatagen/internal/pmetric_package.go +++ b/pdata/internal/cmd/pdatagen/internal/pmetric_package.go @@ -115,6 +115,10 @@ var metric = &messageValueStruct{ defaultVal: `""`, testVal: `"1"`, }, + &sliceField{ + fieldName: "Metadata", + returnSlice: mapStruct, + }, &oneOfField{ typeName: "MetricType", originFieldName: "Data", diff --git a/pdata/pmetric/generated_metric.go b/pdata/pmetric/generated_metric.go index 839628520df..ecf2dba29ef 100644 --- a/pdata/pmetric/generated_metric.go +++ b/pdata/pmetric/generated_metric.go @@ -9,6 +9,7 @@ package pmetric import ( "go.opentelemetry.io/collector/pdata/internal" otlpmetrics "go.opentelemetry.io/collector/pdata/internal/data/protogen/metrics/v1" + "go.opentelemetry.io/collector/pdata/pcommon" ) // Metric represents one metric as a collection of datapoints. @@ -79,6 +80,11 @@ func (ms Metric) SetUnit(v string) { ms.orig.Unit = v } +// Metadata returns the Metadata associated with this Metric. +func (ms Metric) Metadata() pcommon.Map { + return pcommon.Map(internal.NewMap(&ms.orig.Metadata, ms.state)) +} + // Type returns the type of the data for this Metric. // Calling this function on zero-initialized Metric will cause a panic. func (ms Metric) Type() MetricType { @@ -233,6 +239,7 @@ func (ms Metric) CopyTo(dest Metric) { dest.SetName(ms.Name()) dest.SetDescription(ms.Description()) dest.SetUnit(ms.Unit()) + ms.Metadata().CopyTo(dest.Metadata()) switch ms.Type() { case MetricTypeGauge: ms.Gauge().CopyTo(dest.SetEmptyGauge()) diff --git a/pdata/pmetric/generated_metric_test.go b/pdata/pmetric/generated_metric_test.go index 920891b6096..e761f14a321 100644 --- a/pdata/pmetric/generated_metric_test.go +++ b/pdata/pmetric/generated_metric_test.go @@ -13,6 +13,7 @@ import ( "go.opentelemetry.io/collector/pdata/internal" otlpmetrics "go.opentelemetry.io/collector/pdata/internal/data/protogen/metrics/v1" + "go.opentelemetry.io/collector/pdata/pcommon" ) func TestMetric_MoveTo(t *testing.T) { @@ -65,6 +66,13 @@ func TestMetric_Unit(t *testing.T) { assert.Panics(t, func() { newMetric(&otlpmetrics.Metric{}, &sharedState).SetUnit("1") }) } +func TestMetric_Metadata(t *testing.T) { + ms := NewMetric() + assert.Equal(t, pcommon.NewMap(), ms.Metadata()) + internal.FillTestMap(internal.Map(ms.Metadata())) + assert.Equal(t, pcommon.Map(internal.GenerateTestMap()), ms.Metadata()) +} + func TestMetric_Type(t *testing.T) { tv := NewMetric() assert.Equal(t, MetricTypeEmpty, tv.Type()) @@ -175,6 +183,7 @@ func fillTestMetric(tv Metric) { tv.orig.Name = "test_name" tv.orig.Description = "test_description" tv.orig.Unit = "1" + internal.FillTestMap(internal.NewMap(&tv.orig.Metadata, tv.state)) tv.orig.Data = &otlpmetrics.Metric_Sum{Sum: &otlpmetrics.Sum{}} fillTestSum(newSum(tv.orig.GetSum(), tv.state)) }