mirror of
https://github.com/ClusterCockpit/cc-metric-store.git
synced 2024-11-10 05:07:25 +01:00
Use type with custom MarshalJSON for metrics
This commit is contained in:
parent
f7310bc70a
commit
8d827e44b4
3
api.go
3
api.go
@ -8,13 +8,14 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/ClusterCockpit/cc-metric-store/lineprotocol"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
type HostData struct {
|
type HostData struct {
|
||||||
Host string `json:"host"`
|
Host string `json:"host"`
|
||||||
Start int64 `json:"start"`
|
Start int64 `json:"start"`
|
||||||
Data []float64 `json:"data"`
|
Data []lineprotocol.Float `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type MetricData struct {
|
type MetricData struct {
|
||||||
|
@ -2,14 +2,29 @@ package lineprotocol
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"math"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Go's JSON encoder for floats does not support NaN (https://github.com/golang/go/issues/3480).
|
||||||
|
// This program uses NaN as a signal for missing data.
|
||||||
|
// For the HTTP JSON API to be able to handle NaN values,
|
||||||
|
// we have to use our own type which implements encoding/json.Marshaler itself.
|
||||||
|
type Float float64
|
||||||
|
|
||||||
|
func (f Float) MarshalJSON() ([]byte, error) {
|
||||||
|
if math.IsNaN(float64(f)) {
|
||||||
|
return []byte("null"), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return []byte(strconv.FormatFloat(float64(f), 'f', -1, 64)), nil
|
||||||
|
}
|
||||||
|
|
||||||
type Metric struct {
|
type Metric struct {
|
||||||
Name string
|
Name string
|
||||||
Value float64
|
Value Float
|
||||||
}
|
}
|
||||||
|
|
||||||
// measurement: node or cpu
|
// measurement: node or cpu
|
||||||
@ -59,7 +74,7 @@ func Parse(rawline string) (*Line, error) {
|
|||||||
|
|
||||||
line.Fields = append(line.Fields, Metric{
|
line.Fields = append(line.Fields, Metric{
|
||||||
Name: pair[0],
|
Name: pair[0],
|
||||||
Value: field,
|
Value: Float(field),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user