Skip to content

Commit

Permalink
Implement unmarshal metrics with jsoniter (#5433)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
atingchen authored Aug 16, 2022
1 parent 9e90e25 commit ada07a5
Show file tree
Hide file tree
Showing 7 changed files with 1,436 additions and 111 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- Deprecate `exporterhelper.New[Traces|Metrics|Logs]ExporterWithContext` in favor of `exporterhelper.New[Traces|Metrics|Logs]Exporter` (#5914)

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

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

Expand Down
4 changes: 2 additions & 2 deletions pdata/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ require (
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/net v0.0.0-20201021035429-f5854403a974 // indirect
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4 // indirect
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
golang.org/x/text v0.3.3 // indirect
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
9 changes: 7 additions & 2 deletions pdata/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20201021035429-f5854403a974 h1:IX6qOQeG5uLjB/hjjwjedwfjND0hgjPMMyO1RoIXQNI=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 h1:4nGaVu0QrbjT/AK2PRLuQfQuh6DJve+pELhqTdAj3x0=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -106,8 +107,12 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4 h1:myAQVi0cGEoqQVR5POX+8RR2mrocKqNN1hmeMqhX27k=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand Down
143 changes: 143 additions & 0 deletions pdata/internal/json/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package json // import "go.opentelemetry.io/collector/pdata/internal/json"

import (
"encoding/base64"
"fmt"

jsoniter "github.com/json-iterator/go"

otlpcommon "go.opentelemetry.io/collector/pdata/internal/data/protogen/common/v1"
)

// ReadAttribute Unmarshal JSON data and return otlpcommon.KeyValue
func ReadAttribute(iter *jsoniter.Iterator) otlpcommon.KeyValue {
kv := otlpcommon.KeyValue{}
iter.ReadObjectCB(func(iter *jsoniter.Iterator, f string) bool {
switch f {
case "key":
kv.Key = iter.ReadString()
case "value":
iter.ReadObjectCB(func(iter *jsoniter.Iterator, f string) bool {
kv.Value = readAnyValue(iter, f)
return true
})
default:
iter.Skip()
return true
}
return true
})
return kv
}

// ReadInt64 Unmarshal JSON data and return int64
func ReadInt64(iter *jsoniter.Iterator) int64 {
return iter.ReadAny().ToInt64()
}

func readAnyValue(iter *jsoniter.Iterator, f string) otlpcommon.AnyValue {
switch f {
case "stringValue", "string_value":
return otlpcommon.AnyValue{
Value: &otlpcommon.AnyValue_StringValue{
StringValue: iter.ReadString(),
},
}
case "boolValue", "bool_value":
return otlpcommon.AnyValue{
Value: &otlpcommon.AnyValue_BoolValue{
BoolValue: iter.ReadBool(),
},
}
case "intValue", "int_value":
return otlpcommon.AnyValue{
Value: &otlpcommon.AnyValue_IntValue{
IntValue: ReadInt64(iter),
},
}
case "doubleValue", "double_value":
return otlpcommon.AnyValue{
Value: &otlpcommon.AnyValue_DoubleValue{
DoubleValue: iter.ReadFloat64(),
},
}
case "bytesValue", "bytes_value":
v, err := base64.StdEncoding.DecodeString(iter.ReadString())
if err != nil {
iter.ReportError("bytesValue", fmt.Sprintf("base64 decode:%v", err))
return otlpcommon.AnyValue{}
}
return otlpcommon.AnyValue{
Value: &otlpcommon.AnyValue_BytesValue{
BytesValue: v,
},
}
case "arrayValue", "array_value":
return otlpcommon.AnyValue{
Value: &otlpcommon.AnyValue_ArrayValue{
ArrayValue: readArray(iter),
},
}
case "kvlistValue", "kvlist_value":
return otlpcommon.AnyValue{
Value: &otlpcommon.AnyValue_KvlistValue{
KvlistValue: readKvlistValue(iter),
},
}
default:
return otlpcommon.AnyValue{}
}
}

func readArray(iter *jsoniter.Iterator) *otlpcommon.ArrayValue {
v := &otlpcommon.ArrayValue{}
iter.ReadObjectCB(func(iter *jsoniter.Iterator, f string) bool {
switch f {
case "values":
iter.ReadArrayCB(func(iter *jsoniter.Iterator) bool {
iter.ReadObjectCB(func(iter *jsoniter.Iterator, f string) bool {
v.Values = append(v.Values, readAnyValue(iter, f))
return true
})
return true
})
default:
iter.Skip()
return true
}
return true
})
return v
}

func readKvlistValue(iter *jsoniter.Iterator) *otlpcommon.KeyValueList {
v := &otlpcommon.KeyValueList{}
iter.ReadObjectCB(func(iter *jsoniter.Iterator, f string) bool {
switch f {
case "values":
iter.ReadArrayCB(func(iter *jsoniter.Iterator) bool {
v.Values = append(v.Values, ReadAttribute(iter))
return true
})
default:
iter.Skip()
return true
}
return true
})
return v
}
Loading

0 comments on commit ada07a5

Please sign in to comment.