Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore] Improve startup error message #5978

Merged
merged 4 commits into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix reading scope attributes for trace JSON, remove duplicate code
Signed-off-by: Bogdan <[email protected]>
  • Loading branch information
bogdandrutu committed Aug 17, 2022
commit cd44c3c59ec52ed59436b5a5508ba41016309678
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

### 🧰 Bug fixes 🧰

- Fix reading scope attributes for trace JSON, remove duplicate code. (#5930)
- Fix bug in setting the correct collector state after a configuration change event. (#5830)
- Fix json trace unmarshalling for numbers (#5924):
- Accept both string and number for int32/uint32.
Expand Down
42 changes: 42 additions & 0 deletions pdata/internal/json/scope.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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:https://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 (
jsoniter "github.com/json-iterator/go"

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

func ReadScope(iter *jsoniter.Iterator, scope *otlpcommon.InstrumentationScope) {
iter.ReadObjectCB(func(iter *jsoniter.Iterator, f string) bool {
switch f {
case "name":
scope.Name = iter.ReadString()
case "version":
scope.Version = iter.ReadString()
case "attributes":
iter.ReadArrayCB(func(iter *jsoniter.Iterator) bool {
scope.Attributes = append(scope.Attributes, ReadAttribute(iter))
return true
})
case "droppedAttributesCount", "dropped_attributes_count":
scope.DroppedAttributesCount = ReadUint32(iter)
default:
iter.Skip()
}
return true
})
}
118 changes: 118 additions & 0 deletions pdata/internal/json/scope_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// 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:https://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 (
"testing"

jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"

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

func TestReadScope(t *testing.T) {
tests := []struct {
name string
jsonStr string
want *otlpcommon.InstrumentationScope
}{
{
name: "scope",
jsonStr: `{
"name": "name_value",
"version": "version_value",
}`,
want: &otlpcommon.InstrumentationScope{
Name: "name_value",
Version: "version_value",
},
},
{
name: "with attributes",
jsonStr: `{
"name": "my_name",
"version": "my_version",
"attributes": [
{
"key":"string_key",
"value":{"stringValue": "value"}
},
{
"key":"bool_key",
"value":{"boolValue": true}
},
{
"key":"int_key",
"value":{"intValue": 314}
},
{
"key":"double_key",
"value":{"doubleValue": 3.14}
}
],
"dropped_attributes_count": 1,
}`,
want: &otlpcommon.InstrumentationScope{
Name: "my_name",
Version: "my_version",
Attributes: []otlpcommon.KeyValue{
{
Key: "string_key",
Value: otlpcommon.AnyValue{
Value: &otlpcommon.AnyValue_StringValue{
StringValue: "value",
},
},
},
{
Key: "bool_key",
Value: otlpcommon.AnyValue{
Value: &otlpcommon.AnyValue_BoolValue{
BoolValue: true,
},
},
},
{
Key: "int_key",
Value: otlpcommon.AnyValue{
Value: &otlpcommon.AnyValue_IntValue{
IntValue: 314,
},
},
},
{
Key: "double_key",
Value: otlpcommon.AnyValue{
Value: &otlpcommon.AnyValue_DoubleValue{
DoubleValue: 3.14,
},
},
},
},
DroppedAttributesCount: 1,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
iter := jsoniter.ConfigFastest.BorrowIterator([]byte(tt.jsonStr))
defer jsoniter.ConfigFastest.ReturnIterator(iter)
got := &otlpcommon.InstrumentationScope{}
ReadScope(iter, got)
assert.EqualValues(t, tt.want, got)
})
}
}
20 changes: 1 addition & 19 deletions pdata/pmetric/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,25 +122,7 @@ func (d *jsonUnmarshaler) readScopeMetrics(iter *jsoniter.Iterator) *otlpmetrics
iter.ReadObjectCB(func(iter *jsoniter.Iterator, f string) bool {
switch f {
case "scope":
iter.ReadObjectCB(func(iter *jsoniter.Iterator, f string) bool {
switch f {
case "name":
ils.Scope.Name = iter.ReadString()
case "version":
ils.Scope.Version = iter.ReadString()
case "attributes":
iter.ReadArrayCB(func(iter *jsoniter.Iterator) bool {
ils.Scope.Attributes = append(ils.Scope.Attributes,
json.ReadAttribute(iter))
return true
})
case "droppedAttributesCount", "dropped_attributes_count":
ils.Scope.DroppedAttributesCount = json.ReadUint32(iter)
default:
iter.Skip()
}
return true
})
json.ReadScope(iter, &ils.Scope)
case "metrics":
iter.ReadArrayCB(func(iter *jsoniter.Iterator) bool {
ils.Metrics = append(ils.Metrics, d.readMetric(iter))
Expand Down
15 changes: 2 additions & 13 deletions pdata/ptrace/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ func readResourceSpans(iter *jsoniter.Iterator) *otlptrace.ResourceSpans {
})
case "scopeSpans", "scope_spans":
iter.ReadArrayCB(func(iter *jsoniter.Iterator) bool {
rs.ScopeSpans = append(rs.ScopeSpans,
readScopeSpans(iter))
rs.ScopeSpans = append(rs.ScopeSpans, readScopeSpans(iter))
return true
})
case "schemaUrl", "schema_url":
Expand All @@ -121,17 +120,7 @@ func readScopeSpans(iter *jsoniter.Iterator) *otlptrace.ScopeSpans {
iter.ReadObjectCB(func(iter *jsoniter.Iterator, f string) bool {
switch f {
case "scope":
iter.ReadObjectCB(func(iter *jsoniter.Iterator, f string) bool {
switch f {
case "name":
ils.Scope.Name = iter.ReadString()
case "version":
ils.Scope.Version = iter.ReadString()
default:
iter.ReportError("readScopeSpans.instrumentationLibrary", fmt.Sprintf("unknown field:%v", f))
}
return true
})
json.ReadScope(iter, &ils.Scope)
case "spans":
iter.ReadArrayCB(func(iter *jsoniter.Iterator) bool {
ils.Spans = append(ils.Spans, readSpan(iter))
Expand Down
10 changes: 0 additions & 10 deletions pdata/ptrace/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,6 @@ func TestReadScopeSpansUnknownField(t *testing.T) {
}
}

func TestReadScopeSpansUnknownScopeField(t *testing.T) {
jsonStr := `{"scope":{"extra":""}}`
iter := jsoniter.ConfigFastest.BorrowIterator([]byte(jsonStr))
defer jsoniter.ConfigFastest.ReturnIterator(iter)
readScopeSpans(iter)
if assert.Error(t, iter.Error) {
assert.Contains(t, iter.Error.Error(), "unknown field")
}
}

func TestReadSpanUnknownField(t *testing.T) {
jsonStr := `{"extra":""}`
iter := jsoniter.ConfigFastest.BorrowIterator([]byte(jsonStr))
Expand Down