Skip to content

Commit

Permalink
Fix json parsing for float64 (#5960)
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan <[email protected]>

Signed-off-by: Bogdan <[email protected]>
  • Loading branch information
bogdandrutu authored Aug 25, 2022
1 parent 8801f8c commit e092fc7
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
- 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 float64.
- Accept both string and number for int32/uint32.
- Read uint64 numbers without converting from int64.

Expand Down
2 changes: 1 addition & 1 deletion pdata/internal/json/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func readAnyValue(iter *jsoniter.Iterator, f string) otlpcommon.AnyValue {
case "doubleValue", "double_value":
return otlpcommon.AnyValue{
Value: &otlpcommon.AnyValue_DoubleValue{
DoubleValue: iter.ReadFloat64(),
DoubleValue: ReadFloat64(iter),
},
}
case "bytesValue", "bytes_value":
Expand Down
17 changes: 17 additions & 0 deletions pdata/internal/json/number.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,20 @@ func ReadUint64(iter *jsoniter.Iterator) uint64 {
return 0
}
}

func ReadFloat64(iter *jsoniter.Iterator) float64 {
switch iter.WhatIsNext() {
case jsoniter.NumberValue:
return iter.ReadFloat64()
case jsoniter.StringValue:
val, err := strconv.ParseFloat(iter.ReadString(), 64)
if err != nil {
iter.ReportError("ReadUint64", err.Error())
return 0
}
return val
default:
iter.ReportError("ReadUint64", "unsupported value type")
return 0
}
}
53 changes: 53 additions & 0 deletions pdata/internal/json/number_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,56 @@ func TestReadUint64(t *testing.T) {
})
}
}

func TestReadFloat64(t *testing.T) {
tests := []struct {
name string
jsonStr string
want float64
wantErr bool
}{
{
name: "number",
jsonStr: `3.14 `,
want: 3.14,
},
{
name: "string",
jsonStr: `"3.14"`,
want: 3.14,
},
{
name: "negative number",
jsonStr: `-3.14 `,
want: -3.14,
},
{
name: "negative string",
jsonStr: `"-3.14"`,
want: -3.14,
},
{
name: "wrong string",
jsonStr: `"3.f14"`,
wantErr: true,
},
{
name: "wrong type",
jsonStr: `true`,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
iter := jsoniter.ConfigFastest.BorrowIterator([]byte(tt.jsonStr))
defer jsoniter.ConfigFastest.ReturnIterator(iter)
val := ReadFloat64(iter)
if tt.wantErr {
assert.Error(t, iter.Error)
return
}
assert.NoError(t, iter.Error)
assert.Equal(t, tt.want, val)
})
}
}
24 changes: 12 additions & 12 deletions pdata/pmetric/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func (d *jsonUnmarshaler) readExemplar(iter *jsoniter.Iterator) otlpmetrics.Exem
}
case "as_double", "asDouble":
exemplar.Value = &otlpmetrics.Exemplar_AsDouble{
AsDouble: iter.ReadFloat64(),
AsDouble: json.ReadFloat64(iter),
}
case "traceId", "trace_id":
if err := exemplar.TraceId.UnmarshalJSON([]byte(iter.ReadString())); err != nil {
Expand Down Expand Up @@ -327,7 +327,7 @@ func (d *jsonUnmarshaler) readNumberDataPoint(iter *jsoniter.Iterator) *otlpmetr
}
case "as_double", "asDouble":
point.Value = &otlpmetrics.NumberDataPoint_AsDouble{
AsDouble: iter.ReadFloat64(),
AsDouble: json.ReadFloat64(iter),
}
case "attributes":
iter.ReadArrayCB(func(iter *jsoniter.Iterator) bool {
Expand Down Expand Up @@ -365,15 +365,15 @@ func (d *jsonUnmarshaler) readHistogramDataPoint(iter *jsoniter.Iterator) *otlpm
case "count":
point.Count = json.ReadUint64(iter)
case "sum":
point.Sum_ = &otlpmetrics.HistogramDataPoint_Sum{Sum: iter.ReadFloat64()}
point.Sum_ = &otlpmetrics.HistogramDataPoint_Sum{Sum: json.ReadFloat64(iter)}
case "bucket_counts", "bucketCounts":
iter.ReadArrayCB(func(iter *jsoniter.Iterator) bool {
point.BucketCounts = append(point.BucketCounts, json.ReadUint64(iter))
return true
})
case "explicit_bounds", "explicitBounds":
iter.ReadArrayCB(func(iter *jsoniter.Iterator) bool {
point.ExplicitBounds = append(point.ExplicitBounds, iter.ReadFloat64())
point.ExplicitBounds = append(point.ExplicitBounds, json.ReadFloat64(iter))
return true
})
case "exemplars":
Expand All @@ -385,11 +385,11 @@ func (d *jsonUnmarshaler) readHistogramDataPoint(iter *jsoniter.Iterator) *otlpm
point.Flags = json.ReadUint32(iter)
case "max":
point.Max_ = &otlpmetrics.HistogramDataPoint_Max{
Max: iter.ReadFloat64(),
Max: json.ReadFloat64(iter),
}
case "min":
point.Min_ = &otlpmetrics.HistogramDataPoint_Min{
Min: iter.ReadFloat64(),
Min: json.ReadFloat64(iter),
}
default:
iter.Skip()
Expand All @@ -416,7 +416,7 @@ func (d *jsonUnmarshaler) readExponentialHistogramDataPoint(iter *jsoniter.Itera
point.Count = json.ReadUint64(iter)
case "sum":
point.Sum_ = &otlpmetrics.ExponentialHistogramDataPoint_Sum{
Sum: iter.ReadFloat64(),
Sum: json.ReadFloat64(iter),
}
case "scale":
point.Scale = iter.ReadInt32()
Expand Down Expand Up @@ -465,11 +465,11 @@ func (d *jsonUnmarshaler) readExponentialHistogramDataPoint(iter *jsoniter.Itera
point.Flags = json.ReadUint32(iter)
case "max":
point.Max_ = &otlpmetrics.ExponentialHistogramDataPoint_Max{
Max: iter.ReadFloat64(),
Max: json.ReadFloat64(iter),
}
case "min":
point.Min_ = &otlpmetrics.ExponentialHistogramDataPoint_Min{
Min: iter.ReadFloat64(),
Min: json.ReadFloat64(iter),
}
default:
iter.Skip()
Expand All @@ -495,7 +495,7 @@ func (d *jsonUnmarshaler) readSummaryDataPoint(iter *jsoniter.Iterator) *otlpmet
case "count":
point.Count = json.ReadUint64(iter)
case "sum":
point.Sum = iter.ReadFloat64()
point.Sum = json.ReadFloat64(iter)
case "quantile_values", "quantileValues":
iter.ReadArrayCB(func(iter *jsoniter.Iterator) bool {
point.QuantileValues = append(point.QuantileValues, d.readQuantileValue(iter))
Expand All @@ -516,9 +516,9 @@ func (d *jsonUnmarshaler) readQuantileValue(iter *jsoniter.Iterator) *otlpmetric
iter.ReadObjectCB(func(iter *jsoniter.Iterator, f string) bool {
switch f {
case "quantile":
point.Quantile = iter.ReadFloat64()
point.Quantile = json.ReadFloat64(iter)
case "value":
point.Value = iter.ReadFloat64()
point.Value = json.ReadFloat64(iter)
default:
iter.Skip()
}
Expand Down

0 comments on commit e092fc7

Please sign in to comment.