Skip to content

Commit

Permalink
use json for writing floats as strings
Browse files Browse the repository at this point in the history
  • Loading branch information
dashpole committed Mar 1, 2022
1 parent 4493a5e commit 25562d2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
- Deprecated `receiverhelper.NewFactory` in favour of `component.NewReceiverFactory`
- Remove support for legacy otlp/http port. (#4916)
- Remove deprecated funcs in pdata (#4809)
- AsString for pdata.AttributeValue now returns the JSON-encoded string of floats. (#4934)

### 💡 Enhancements 💡

Expand Down
3 changes: 2 additions & 1 deletion model/pdata/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,8 @@ func (a AttributeValue) AsString() string {
return strconv.FormatBool(a.BoolVal())

case AttributeValueTypeDouble:
return strconv.FormatFloat(a.DoubleVal(), 'f', -1, 64)
jsonStr, _ := json.Marshal(a.DoubleVal())
return string(jsonStr)

case AttributeValueTypeInt:
return strconv.FormatInt(a.IntVal(), 10)
Expand Down
5 changes: 5 additions & 0 deletions model/pdata/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,11 @@ func TestAsString(t *testing.T) {
input: NewAttributeValueDouble(1.61803399),
expected: "1.61803399",
},
{
name: "small float64",
input: NewAttributeValueDouble(.000000009),
expected: "9e-9",
},
{
name: "boolean",
input: NewAttributeValueBool(true),
Expand Down

0 comments on commit 25562d2

Please sign in to comment.