Skip to content

Commit

Permalink
Added MarshalerSizer Interface to facilitate creation of Sizer implem…
Browse files Browse the repository at this point in the history
…entations (#5929) (#5931)

* Added NewProtoSizer to instantiate Sizer implementations

Signed-off-by: Corbin Phelps <corbin.phelps@bluemedora.com>

* Updated changelog with PR number

Signed-off-by: Corbin Phelps <corbin.phelps@bluemedora.com>

* Refactored to create a MarshalSizer interface that is returned by NewProtoMarshaller

Signed-off-by: Corbin Phelps <corbin.phelps@bluemedora.com>

* Updated NewProtoMarshaler comments to inidicate return interface

Signed-off-by: Corbin Phelps <corbin.phelps@bluemedora.com>

* Corrected interface name

Signed-off-by: Corbin Phelps <corbin.phelps@bluemedora.com>

Signed-off-by: Corbin Phelps <corbin.phelps@bluemedora.com>

Signed-off-by: Corbin Phelps <corbin.phelps@bluemedora.com>
Co-authored-by: Corbin Phelps <corbin.phelps@bluemedora.com>
  • Loading branch information
bogdandrutu and Corbin Phelps authored Aug 22, 2022
1 parent 07de5b2 commit 03c0f1e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 40 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
### 🧰 Bug fixes 🧰

- Fix reading scope attributes for trace JSON, remove duplicate code. (#5930)
- otlpjson/trace: skip unknown fields instead of error. (#5931)
- 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
16 changes: 8 additions & 8 deletions pdata/ptrace/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func readTraceData(iter *jsoniter.Iterator) otlptrace.TracesData {
return true
})
default:
iter.ReportError("root", fmt.Sprintf("unknown field:%v", f))
iter.Skip()
}
return true
})
Expand All @@ -95,7 +95,7 @@ func readResourceSpans(iter *jsoniter.Iterator) *otlptrace.ResourceSpans {
case "droppedAttributesCount", "dropped_attributes_count":
rs.Resource.DroppedAttributesCount = json.ReadUint32(iter)
default:
iter.ReportError("readResourceSpans.resource", fmt.Sprintf("unknown field:%v", f))
iter.Skip()
}
return true
})
Expand All @@ -107,7 +107,7 @@ func readResourceSpans(iter *jsoniter.Iterator) *otlptrace.ResourceSpans {
case "schemaUrl", "schema_url":
rs.SchemaUrl = iter.ReadString()
default:
iter.ReportError("readResourceSpans", fmt.Sprintf("unknown field:%v", f))
iter.Skip()
}
return true
})
Expand All @@ -129,7 +129,7 @@ func readScopeSpans(iter *jsoniter.Iterator) *otlptrace.ScopeSpans {
case "schemaUrl", "schema_url":
ils.SchemaUrl = iter.ReadString()
default:
iter.ReportError("readScopeSpans", fmt.Sprintf("unknown field:%v", f))
iter.Skip()
}
return true
})
Expand Down Expand Up @@ -192,12 +192,12 @@ func readSpan(iter *jsoniter.Iterator) *otlptrace.Span {
case "code":
sp.Status.Code = otlptrace.Status_StatusCode(json.ReadEnumValue(iter, otlptrace.Status_StatusCode_value))
default:
iter.ReportError("readSpan.status", fmt.Sprintf("unknown field:%v", f))
iter.Skip()
}
return true
})
default:
iter.ReportError("readSpan", fmt.Sprintf("unknown field:%v", f))
iter.Skip()
}
return true
})
Expand Down Expand Up @@ -227,7 +227,7 @@ func readSpanLink(iter *jsoniter.Iterator) *otlptrace.Span_Link {
case "droppedAttributesCount", "dropped_attributes_count":
link.DroppedAttributesCount = json.ReadUint32(iter)
default:
iter.ReportError("readSpanLink", fmt.Sprintf("unknown field:%v", f))
iter.Skip()
}
return true
})
Expand All @@ -251,7 +251,7 @@ func readSpanEvent(iter *jsoniter.Iterator) *otlptrace.Span_Event {
case "droppedAttributesCount", "dropped_attributes_count":
event.DroppedAttributesCount = json.ReadUint32(iter)
default:
iter.ReportError("readSpanEvent", fmt.Sprintf("unknown field:%v", f))
iter.Skip()
}
return true
})
Expand Down
59 changes: 27 additions & 32 deletions pdata/ptrace/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,61 +161,56 @@ func TestReadTraceDataUnknownField(t *testing.T) {
jsonStr := `{"extra":""}`
iter := jsoniter.ConfigFastest.BorrowIterator([]byte(jsonStr))
defer jsoniter.ConfigFastest.ReturnIterator(iter)
readTraceData(iter)
if assert.Error(t, iter.Error) {
assert.Contains(t, iter.Error.Error(), "unknown field")
}
val := readTraceData(iter)
assert.NoError(t, iter.Error)
assert.Equal(t, otlptrace.TracesData{}, val)
}

func TestReadResourceSpansUnknownField(t *testing.T) {
jsonStr := `{"extra":""}`
iter := jsoniter.ConfigFastest.BorrowIterator([]byte(jsonStr))
defer jsoniter.ConfigFastest.ReturnIterator(iter)
readResourceSpans(iter)
if assert.Error(t, iter.Error) {
assert.Contains(t, iter.Error.Error(), "unknown field")
}
val := readResourceSpans(iter)
assert.NoError(t, iter.Error)
assert.Equal(t, &otlptrace.ResourceSpans{}, val)
}

func TestReadResourceSpansUnknownResourceField(t *testing.T) {
jsonStr := `{"resource":{"extra":""}}`
iter := jsoniter.ConfigFastest.BorrowIterator([]byte(jsonStr))
defer jsoniter.ConfigFastest.ReturnIterator(iter)
readResourceSpans(iter)
if assert.Error(t, iter.Error) {
assert.Contains(t, iter.Error.Error(), "unknown field")
}
val := readResourceSpans(iter)
assert.NoError(t, iter.Error)
assert.Equal(t, &otlptrace.ResourceSpans{}, val)
}

func TestReadScopeSpansUnknownField(t *testing.T) {
jsonStr := `{"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")
}
val := readScopeSpans(iter)
assert.NoError(t, iter.Error)
assert.Equal(t, &otlptrace.ScopeSpans{}, val)
}

func TestReadSpanUnknownField(t *testing.T) {
jsonStr := `{"extra":""}`
iter := jsoniter.ConfigFastest.BorrowIterator([]byte(jsonStr))
defer jsoniter.ConfigFastest.ReturnIterator(iter)
readSpan(iter)
if assert.Error(t, iter.Error) {
assert.Contains(t, iter.Error.Error(), "unknown field")
}
val := readSpan(iter)
assert.NoError(t, iter.Error)
assert.Equal(t, &otlptrace.Span{}, val)
}

func TestReadSpanUnknownStatusField(t *testing.T) {
jsonStr := `{"status":{"extra":""}}`
iter := jsoniter.ConfigFastest.BorrowIterator([]byte(jsonStr))
defer jsoniter.ConfigFastest.ReturnIterator(iter)
readSpan(iter)
if assert.Error(t, iter.Error) {
assert.Contains(t, iter.Error.Error(), "unknown field")
}
val := readSpan(iter)
assert.NoError(t, iter.Error)
assert.Equal(t, &otlptrace.Span{}, val)
}

func TestReadSpanInvalidTraceIDField(t *testing.T) {
jsonStr := `{"trace_id":"--"}`
iter := jsoniter.ConfigFastest.BorrowIterator([]byte(jsonStr))
Expand All @@ -225,6 +220,7 @@ func TestReadSpanInvalidTraceIDField(t *testing.T) {
assert.Contains(t, iter.Error.Error(), "parse trace_id")
}
}

func TestReadSpanInvalidSpanIDField(t *testing.T) {
jsonStr := `{"span_id":"--"}`
iter := jsoniter.ConfigFastest.BorrowIterator([]byte(jsonStr))
Expand All @@ -234,6 +230,7 @@ func TestReadSpanInvalidSpanIDField(t *testing.T) {
assert.Contains(t, iter.Error.Error(), "parse span_id")
}
}

func TestReadSpanInvalidParentSpanIDField(t *testing.T) {
jsonStr := `{"parent_span_id":"--"}`
iter := jsoniter.ConfigFastest.BorrowIterator([]byte(jsonStr))
Expand All @@ -248,10 +245,9 @@ func TestReadSpanLinkUnknownField(t *testing.T) {
jsonStr := `{"extra":""}`
iter := jsoniter.ConfigFastest.BorrowIterator([]byte(jsonStr))
defer jsoniter.ConfigFastest.ReturnIterator(iter)
readSpanLink(iter)
if assert.Error(t, iter.Error) {
assert.Contains(t, iter.Error.Error(), "unknown field")
}
val := readSpanLink(iter)
assert.NoError(t, iter.Error)
assert.Equal(t, &otlptrace.Span_Link{}, val)
}

func TestReadSpanLinkInvalidTraceIDField(t *testing.T) {
Expand All @@ -278,8 +274,7 @@ func TestReadSpanEventUnknownField(t *testing.T) {
jsonStr := `{"extra":""}`
iter := jsoniter.ConfigFastest.BorrowIterator([]byte(jsonStr))
defer jsoniter.ConfigFastest.ReturnIterator(iter)
readSpanEvent(iter)
if assert.Error(t, iter.Error) {
assert.Contains(t, iter.Error.Error(), "unknown field")
}
val := readSpanEvent(iter)
assert.NoError(t, iter.Error)
assert.Equal(t, &otlptrace.Span_Event{}, val)
}

0 comments on commit 03c0f1e

Please sign in to comment.