Skip to content

Commit 31528ce

Browse files
authored
Support parsing metric.metadata from OTLP JSON (#10026)
Follow-up to #10006, which added metric.metadata to pmetric. I forgot to add support for parsing the field from JSON in that PR. This PR adds the missing support. #### Testing Unit tests
1 parent 6dfa6bc commit 31528ce

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

.chloggen/json-metric-metadata.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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. otlpreceiver)
7+
component: pmetric
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Support parsing metric.metadata from OTLP JSON.
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [10026]
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+
# Optional: The change log or logs in which this entry should be included.
21+
# e.g. '[user]' or '[user, api]'
22+
# Include 'user' if the change is relevant to end users.
23+
# Include 'api' if there is a change to a library API.
24+
# Default: '[user]'
25+
change_logs: []

pdata/pmetric/json.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ func (ms Metric) unmarshalJsoniter(iter *jsoniter.Iterator) {
106106
ms.orig.Description = iter.ReadString()
107107
case "unit":
108108
ms.orig.Unit = iter.ReadString()
109+
case "metadata":
110+
iter.ReadArrayCB(func(iter *jsoniter.Iterator) bool {
111+
ms.orig.Metadata = append(ms.orig.Metadata, json.ReadAttribute(iter))
112+
return true
113+
})
109114
case "sum":
110115
ms.SetEmptySum().unmarshalJsoniter(iter)
111116
case "gauge":

pdata/pmetric/json_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ var metricsOTLP = func() Metrics {
2121
il := rm.ScopeMetrics().AppendEmpty()
2222
il.Scope().SetName("name")
2323
il.Scope().SetVersion("version")
24-
il.Metrics().AppendEmpty().SetName("testMetric")
24+
m := il.Metrics().AppendEmpty()
25+
m.SetName("testMetric")
26+
m.Metadata().PutStr("metadatakey", "metadatavalue")
2527
return md
2628
}()
2729

28-
var metricsJSON = `{"resourceMetrics":[{"resource":{"attributes":[{"key":"host.name","value":{"stringValue":"testHost"}}]},"scopeMetrics":[{"scope":{"name":"name","version":"version"},"metrics":[{"name":"testMetric"}]}]}]}`
30+
var metricsJSON = `{"resourceMetrics":[{"resource":{"attributes":[{"key":"host.name","value":{"stringValue":"testHost"}}]},"scopeMetrics":[{"scope":{"name":"name","version":"version"},"metrics":[{"name":"testMetric","metadata":[{"key":"metadatakey","value":{"stringValue":"metadatavalue"}}]}]}]}]}`
2931

3032
func TestMetricsJSON(t *testing.T) {
3133
encoder := &JSONMarshaler{}

0 commit comments

Comments
 (0)