Skip to content

Commit 4e6df1b

Browse files
pkg/ottl: add metric.metadata as a valid path in OTTL (#40217)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description Allow OTTL to get and set metric metadata. <!-- Issue number (e.g. #1234) or full URL to issue, if applicable. --> #### Link to tracking issue Fixes #40214. <!--Describe what testing was performed and which tests were added.--> #### Testing Added unit tests <!--Describe the documentation added.--> #### Documentation Updated readme Example statement that now works: - set(metric.metadata["k"], "v") where metric.metadata["prometheus.type"] == "unknown" <!--Please delete paragraphs that you did not use before submitting.--> --------- Signed-off-by: Ridwan Sharif <[email protected]> Co-authored-by: Evan Bradley <[email protected]>
1 parent 9f2d9a0 commit 4e6df1b

File tree

6 files changed

+102
-1
lines changed

6 files changed

+102
-1
lines changed

.chloggen/ottl-metadata.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: pkg/ottl
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Add metric.metadata as a valid OTTL path
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [40214]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: []

pkg/ottl/contexts/internal/ctxmetric/metric.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
1212
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxerror"
13+
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxutil"
1314
)
1415

1516
func PathGetSetter[K Context](path ottl.Path[K]) (ottl.GetSetter[K], error) {
@@ -31,6 +32,11 @@ func PathGetSetter[K Context](path ottl.Path[K]) (ottl.GetSetter[K], error) {
3132
return accessIsMonotonic[K](), nil
3233
case "data_points":
3334
return accessDataPoints[K](), nil
35+
case "metadata":
36+
if path.Keys() == nil {
37+
return accessMetadata[K](), nil
38+
}
39+
return accessMetadataKey[K](path.Keys()), nil
3440
default:
3541
return nil, ctxerror.New(path.Name(), path.String(), Name, DocRef)
3642
}
@@ -189,3 +195,25 @@ func accessDataPoints[K Context]() ottl.StandardGetSetter[K] {
189195
},
190196
}
191197
}
198+
199+
func accessMetadata[K Context]() ottl.StandardGetSetter[K] {
200+
return ottl.StandardGetSetter[K]{
201+
Getter: func(_ context.Context, tCtx K) (any, error) {
202+
return tCtx.GetMetric().Metadata(), nil
203+
},
204+
Setter: func(_ context.Context, tCtx K, val any) error {
205+
return ctxutil.SetMap(tCtx.GetMetric().Metadata(), val)
206+
},
207+
}
208+
}
209+
210+
func accessMetadataKey[K Context](keys []ottl.Key[K]) ottl.StandardGetSetter[K] {
211+
return ottl.StandardGetSetter[K]{
212+
Getter: func(ctx context.Context, tCtx K) (any, error) {
213+
return ctxutil.GetMapValue(ctx, tCtx, tCtx.GetMetric().Metadata(), keys)
214+
},
215+
Setter: func(ctx context.Context, tCtx K, val any) error {
216+
return ctxutil.SetMapValue(ctx, tCtx, tCtx.GetMetric().Metadata(), keys, val)
217+
},
218+
}
219+
}

pkg/ottl/contexts/internal/ctxmetric/metric_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"testing"
99

1010
"github.com/stretchr/testify/assert"
11+
"go.opentelemetry.io/collector/pdata/pcommon"
1112
"go.opentelemetry.io/collector/pdata/pmetric"
1213

1314
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
@@ -21,6 +22,9 @@ func TestPathGetSetter(t *testing.T) {
2122
newMetric := pmetric.NewMetric()
2223
newMetric.SetName("new name")
2324

25+
newMetadata := pcommon.NewMap()
26+
newMetadata.PutStr("new_k", "new_v")
27+
2428
newDataPoints := pmetric.NewNumberDataPointSlice()
2529
dataPoint := newDataPoints.AppendEmpty()
2630
dataPoint.SetIntValue(1)
@@ -108,6 +112,17 @@ func TestPathGetSetter(t *testing.T) {
108112
newDataPoints.CopyTo(metric.Sum().DataPoints())
109113
},
110114
},
115+
{
116+
name: "metric metadata",
117+
path: &pathtest.Path[*testContext]{
118+
N: "metadata",
119+
},
120+
orig: pcommon.NewMap(),
121+
newVal: newMetadata,
122+
modified: func(metric pmetric.Metric) {
123+
newMetadata.CopyTo(metric.Metadata())
124+
},
125+
},
111126
}
112127
for _, tt := range tests {
113128
t.Run(tt.name, func(t *testing.T) {

pkg/ottl/contexts/internal/logging/logging.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ func (m Metric) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
168168
encoder.AddString("name", mm.Name())
169169
encoder.AddString("unit", mm.Unit())
170170
encoder.AddString("type", mm.Type().String())
171+
err := encoder.AddObject("metadata", Map(mm.Metadata()))
171172

172-
var err error
173173
switch mm.Type() {
174174
case pmetric.MetricTypeSum:
175175
encoder.AddString("aggregation_temporality", mm.Sum().AggregationTemporality().String())

pkg/ottl/contexts/ottlmetric/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ The following paths are supported.
2626
| metric.description | the description of the metric | string |
2727
| metric.unit | the unit of the metric | string |
2828
| metric.type | the data type of the metric | int64 |
29+
| metric.metadata | metadata associated with the metric | pcommon.Map |
2930
| metric.aggregation_temporality | the aggregation temporality of the metric | int64 |
3031
| metric.is_monotonic | the monotonicity of the metric | bool |
3132
| metric.data_points | the data points of the metric | pmetric.NumberDataPointSlice, pmetric.HistogramDataPointSlice, pmetric.ExponentialHistogramDataPointSlice, or pmetric.SummaryDataPointSlice |

pkg/ottl/contexts/ottlmetric/metrics_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ func Test_newPathGetSetter(t *testing.T) {
2525
newCache := pcommon.NewMap()
2626
newCache.PutStr("temp", "value")
2727

28+
newMetadata := pcommon.NewMap()
29+
newMetadata.PutStr("new_k", "new_v")
30+
2831
newMetric := pmetric.NewMetric()
2932
newMetric.SetName("new name")
3033

@@ -115,6 +118,33 @@ func Test_newPathGetSetter(t *testing.T) {
115118
newDataPoints.CopyTo(metric.Sum().DataPoints())
116119
},
117120
},
121+
{
122+
name: "metadata",
123+
path: &pathtest.Path[TransformContext]{
124+
N: "metadata",
125+
},
126+
orig: pcommon.NewMap(),
127+
newVal: newMetadata,
128+
modified: func(metric pmetric.Metric, _ pcommon.Map) {
129+
newMetadata.CopyTo(metric.Metadata())
130+
},
131+
},
132+
{
133+
name: "metadata access",
134+
path: &pathtest.Path[TransformContext]{
135+
N: "metadata",
136+
KeySlice: []ottl.Key[TransformContext]{
137+
&pathtest.Key[TransformContext]{
138+
S: ottltest.Strp("temp"),
139+
},
140+
},
141+
},
142+
orig: nil,
143+
newVal: "new value",
144+
modified: func(metric pmetric.Metric, _ pcommon.Map) {
145+
metric.Metadata().PutStr("temp", "new value")
146+
},
147+
},
118148
{
119149
name: "cache",
120150
path: &pathtest.Path[TransformContext]{

0 commit comments

Comments
 (0)