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

Commit

Permalink
fix json encoding of NaN/nulls. fix #66
Browse files Browse the repository at this point in the history
otherwise we would see errors like `json: error calling MarshalJSON for
type main.Point: invalid character 'N' looking for beginning of value`

the data can contain NaN values, i verified they were not coming from
or stored in the chunks, but happened in divide(), like when we did 0/0.
Graphite-web has some special cases in divideSeries() but we don't need to, we can just
use NaN in the data arrays and execute our logic/math as usual,
this works fine in Go, we just need to encode them properly.

for the record, in the official json encoder, NaN/null isn't even
supported:
golang/go#3480
  • Loading branch information
Dieterbe committed Dec 3, 2015
1 parent 0aa4c41 commit 5de7d6e
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions metric_tank/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"github.com/grafana/grafana/pkg/log"
"github.com/raintank/raintank-metric/metric_tank/consolidation"
"math"
"net/http"
_ "net/http/pprof"
"strconv"
Expand All @@ -17,6 +18,9 @@ type Point struct {
}

func (p *Point) MarshalJSON() ([]byte, error) {
if math.IsNaN(p.Val) {
return []byte(fmt.Sprintf("[null, %d]", p.Ts)), nil
}
return []byte(fmt.Sprintf("[%f, %d]", p.Val, p.Ts)), nil
}

Expand Down

0 comments on commit 5de7d6e

Please sign in to comment.