forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
7 changed files
with
1,436 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.