Skip to content

Commit ada07a5

Browse files
authored
Implement unmarshal metrics with jsoniter (#5433)
* Implement unmarshal metrics with jsoniter * Implement unmarshal metrics with jsoniter AND add test file * add unit testing * add unit testing * git rebase main * do a matrix testing for all 8 cases for a combination of these 3 bool options. * some func is deprecated * go imports * go imports * Update change log * generate the 8 cases per tested func to avoid repeating test case definition * 1. Remove instrumentationLibraryMetrics and instrumentation_library_metrics. 2. Some functions move to pdata/internal/json 3.Ignore unknown fields and have a debug log for them * add test unit * update CHANGELOG * remove the new API AND reuse the other API NewJSONUnmarshaler * update CHANGELOG * update CHANGELOG * go mod tidy * remove unnecessary return true on the default branch
1 parent 9e90e25 commit ada07a5

File tree

7 files changed

+1436
-111
lines changed

7 files changed

+1436
-111
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- Deprecate `exporterhelper.New[Traces|Metrics|Logs]ExporterWithContext` in favor of `exporterhelper.New[Traces|Metrics|Logs]Exporter` (#5914)
1616

1717
### 💡 Enhancements 💡
18+
- Add support to unmarshalls bytes into pmetric.Metrics with `jsoniter` in jsonUnmarshaler(#5433)
1819

1920
- Add httpprovider to allow loading config files stored in HTTP (#5810)
2021

pdata/go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ require (
1616
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
1717
github.com/modern-go/reflect2 v1.0.2 // indirect
1818
github.com/pmezard/go-difflib v1.0.0 // indirect
19-
golang.org/x/net v0.0.0-20201021035429-f5854403a974 // indirect
20-
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4 // indirect
19+
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect
20+
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
2121
golang.org/x/text v0.3.3 // indirect
2222
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect
2323
gopkg.in/yaml.v3 v3.0.1 // indirect

pdata/go.sum

+7-2
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn
9191
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
9292
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
9393
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
94-
golang.org/x/net v0.0.0-20201021035429-f5854403a974 h1:IX6qOQeG5uLjB/hjjwjedwfjND0hgjPMMyO1RoIXQNI=
9594
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
95+
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 h1:4nGaVu0QrbjT/AK2PRLuQfQuh6DJve+pELhqTdAj3x0=
96+
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
9697
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
9798
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
9899
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -106,8 +107,12 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
106107
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
107108
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
108109
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
109-
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4 h1:myAQVi0cGEoqQVR5POX+8RR2mrocKqNN1hmeMqhX27k=
110+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
110111
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
112+
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
113+
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE=
114+
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
115+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
111116
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
112117
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
113118
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=

pdata/internal/json/common.go

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package json // import "go.opentelemetry.io/collector/pdata/internal/json"
16+
17+
import (
18+
"encoding/base64"
19+
"fmt"
20+
21+
jsoniter "github.com/json-iterator/go"
22+
23+
otlpcommon "go.opentelemetry.io/collector/pdata/internal/data/protogen/common/v1"
24+
)
25+
26+
// ReadAttribute Unmarshal JSON data and return otlpcommon.KeyValue
27+
func ReadAttribute(iter *jsoniter.Iterator) otlpcommon.KeyValue {
28+
kv := otlpcommon.KeyValue{}
29+
iter.ReadObjectCB(func(iter *jsoniter.Iterator, f string) bool {
30+
switch f {
31+
case "key":
32+
kv.Key = iter.ReadString()
33+
case "value":
34+
iter.ReadObjectCB(func(iter *jsoniter.Iterator, f string) bool {
35+
kv.Value = readAnyValue(iter, f)
36+
return true
37+
})
38+
default:
39+
iter.Skip()
40+
return true
41+
}
42+
return true
43+
})
44+
return kv
45+
}
46+
47+
// ReadInt64 Unmarshal JSON data and return int64
48+
func ReadInt64(iter *jsoniter.Iterator) int64 {
49+
return iter.ReadAny().ToInt64()
50+
}
51+
52+
func readAnyValue(iter *jsoniter.Iterator, f string) otlpcommon.AnyValue {
53+
switch f {
54+
case "stringValue", "string_value":
55+
return otlpcommon.AnyValue{
56+
Value: &otlpcommon.AnyValue_StringValue{
57+
StringValue: iter.ReadString(),
58+
},
59+
}
60+
case "boolValue", "bool_value":
61+
return otlpcommon.AnyValue{
62+
Value: &otlpcommon.AnyValue_BoolValue{
63+
BoolValue: iter.ReadBool(),
64+
},
65+
}
66+
case "intValue", "int_value":
67+
return otlpcommon.AnyValue{
68+
Value: &otlpcommon.AnyValue_IntValue{
69+
IntValue: ReadInt64(iter),
70+
},
71+
}
72+
case "doubleValue", "double_value":
73+
return otlpcommon.AnyValue{
74+
Value: &otlpcommon.AnyValue_DoubleValue{
75+
DoubleValue: iter.ReadFloat64(),
76+
},
77+
}
78+
case "bytesValue", "bytes_value":
79+
v, err := base64.StdEncoding.DecodeString(iter.ReadString())
80+
if err != nil {
81+
iter.ReportError("bytesValue", fmt.Sprintf("base64 decode:%v", err))
82+
return otlpcommon.AnyValue{}
83+
}
84+
return otlpcommon.AnyValue{
85+
Value: &otlpcommon.AnyValue_BytesValue{
86+
BytesValue: v,
87+
},
88+
}
89+
case "arrayValue", "array_value":
90+
return otlpcommon.AnyValue{
91+
Value: &otlpcommon.AnyValue_ArrayValue{
92+
ArrayValue: readArray(iter),
93+
},
94+
}
95+
case "kvlistValue", "kvlist_value":
96+
return otlpcommon.AnyValue{
97+
Value: &otlpcommon.AnyValue_KvlistValue{
98+
KvlistValue: readKvlistValue(iter),
99+
},
100+
}
101+
default:
102+
return otlpcommon.AnyValue{}
103+
}
104+
}
105+
106+
func readArray(iter *jsoniter.Iterator) *otlpcommon.ArrayValue {
107+
v := &otlpcommon.ArrayValue{}
108+
iter.ReadObjectCB(func(iter *jsoniter.Iterator, f string) bool {
109+
switch f {
110+
case "values":
111+
iter.ReadArrayCB(func(iter *jsoniter.Iterator) bool {
112+
iter.ReadObjectCB(func(iter *jsoniter.Iterator, f string) bool {
113+
v.Values = append(v.Values, readAnyValue(iter, f))
114+
return true
115+
})
116+
return true
117+
})
118+
default:
119+
iter.Skip()
120+
return true
121+
}
122+
return true
123+
})
124+
return v
125+
}
126+
127+
func readKvlistValue(iter *jsoniter.Iterator) *otlpcommon.KeyValueList {
128+
v := &otlpcommon.KeyValueList{}
129+
iter.ReadObjectCB(func(iter *jsoniter.Iterator, f string) bool {
130+
switch f {
131+
case "values":
132+
iter.ReadArrayCB(func(iter *jsoniter.Iterator) bool {
133+
v.Values = append(v.Values, ReadAttribute(iter))
134+
return true
135+
})
136+
default:
137+
iter.Skip()
138+
return true
139+
}
140+
return true
141+
})
142+
return v
143+
}

0 commit comments

Comments
 (0)