Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

Infinity values to not produce invalid JSONs #2049

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions api/models/series.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func (series SeriesByTarget) MarshalJSONFast(b []byte) ([]byte, error) {
b = append(b, `,"datapoints":[`...)
for _, p := range s.Datapoints {
b = append(b, '[')
if math.IsNaN(p.Val) {
if math.IsNaN(p.Val) || math.IsInf(p.Val, 1) || math.IsInf(p.Val, -1) {
npazosmendez marked this conversation as resolved.
Show resolved Hide resolved
b = append(b, `null,`...)
} else {
b = strconv.AppendFloat(b, p.Val, 'f', -1, 64)
Expand Down Expand Up @@ -330,7 +330,7 @@ func (series SeriesByTarget) MarshalJSONFastWithMeta(b []byte) ([]byte, error) {
b = append(b, `,"datapoints":[`...)
for _, p := range s.Datapoints {
b = append(b, '[')
if math.IsNaN(p.Val) {
if math.IsNaN(p.Val) || math.IsInf(p.Val, 1) || math.IsInf(p.Val, -1) {
npazosmendez marked this conversation as resolved.
Show resolved Hide resolved
b = append(b, `null,`...)
} else {
b = strconv.AppendFloat(b, p.Val, 'f', -1, 64)
Expand Down
12 changes: 12 additions & 0 deletions api/models/series_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ func TestJsonMarshal(t *testing.T) {
},
out: `[{"target":"a\\b","datapoints":[]}]`,
},
{
in: []Series{
{
Target: `a`,
Datapoints: []schema.Point{
{Val: math.Inf(1), Ts: 60},
},
Interval: 60,
},
},
out: `[{"target":"a","datapoints":[[null,60]]}]`,
},
{
in: []Series{
{
Expand Down