monitoring: fix metric value NaN
Signed-off-by: huanggze <loganhuang@yunify.com>
This commit is contained in:
@@ -17,23 +17,23 @@
|
||||
"values":[
|
||||
[
|
||||
1585743925,
|
||||
1.123456
|
||||
"1.123456"
|
||||
],
|
||||
[
|
||||
1585744045,
|
||||
1.123456
|
||||
"1.123456"
|
||||
],
|
||||
[
|
||||
1585744165,
|
||||
1.123456
|
||||
"1.123456"
|
||||
],
|
||||
[
|
||||
1585744285,
|
||||
1.123456
|
||||
"1.123456"
|
||||
],
|
||||
[
|
||||
1585744405,
|
||||
1.123456
|
||||
"1.123456"
|
||||
]
|
||||
]
|
||||
},
|
||||
@@ -50,23 +50,23 @@
|
||||
"values":[
|
||||
[
|
||||
1585743925,
|
||||
1.123456
|
||||
"1.123456"
|
||||
],
|
||||
[
|
||||
1585744045,
|
||||
1.123456
|
||||
"1.123456"
|
||||
],
|
||||
[
|
||||
1585744165,
|
||||
1.123456
|
||||
"1.123456"
|
||||
],
|
||||
[
|
||||
1585744285,
|
||||
1.123456
|
||||
"1.123456"
|
||||
],
|
||||
[
|
||||
1585744405,
|
||||
1.123456
|
||||
"1.123456"
|
||||
]
|
||||
]
|
||||
},
|
||||
@@ -83,23 +83,23 @@
|
||||
"values":[
|
||||
[
|
||||
1585743925,
|
||||
1.123456
|
||||
"1.123456"
|
||||
],
|
||||
[
|
||||
1585744045,
|
||||
1.123456
|
||||
"1.123456"
|
||||
],
|
||||
[
|
||||
1585744165,
|
||||
1.123456
|
||||
"1.123456"
|
||||
],
|
||||
[
|
||||
1585744285,
|
||||
1.123456
|
||||
"1.123456"
|
||||
],
|
||||
[
|
||||
1585744405,
|
||||
1.123456
|
||||
"1.123456"
|
||||
]
|
||||
]
|
||||
},
|
||||
@@ -116,23 +116,23 @@
|
||||
"values":[
|
||||
[
|
||||
1585743925,
|
||||
1.123456
|
||||
"1.123456"
|
||||
],
|
||||
[
|
||||
1585744045,
|
||||
1.123456
|
||||
"1.123456"
|
||||
],
|
||||
[
|
||||
1585744165,
|
||||
1.123456
|
||||
"1.123456"
|
||||
],
|
||||
[
|
||||
1585744285,
|
||||
1.123456
|
||||
"1.123456"
|
||||
],
|
||||
[
|
||||
1585744405,
|
||||
1.123456
|
||||
"1.123456"
|
||||
]
|
||||
]
|
||||
},
|
||||
@@ -149,23 +149,23 @@
|
||||
"values":[
|
||||
[
|
||||
1585743925,
|
||||
1.123456
|
||||
"1.123456"
|
||||
],
|
||||
[
|
||||
1585744045,
|
||||
1.123456
|
||||
"1.123456"
|
||||
],
|
||||
[
|
||||
1585744165,
|
||||
1.123456
|
||||
"1.123456"
|
||||
],
|
||||
[
|
||||
1585744285,
|
||||
1.123456
|
||||
"1.123456"
|
||||
],
|
||||
[
|
||||
1585744405,
|
||||
1.123456
|
||||
"1.123456"
|
||||
]
|
||||
]
|
||||
},
|
||||
@@ -182,23 +182,23 @@
|
||||
"values":[
|
||||
[
|
||||
1585743925,
|
||||
1.123456
|
||||
"1.123456"
|
||||
],
|
||||
[
|
||||
1585744045,
|
||||
1.123456
|
||||
"1.123456"
|
||||
],
|
||||
[
|
||||
1585744165,
|
||||
1.123456
|
||||
"1.123456"
|
||||
],
|
||||
[
|
||||
1585744285,
|
||||
1.123456
|
||||
"1.123456"
|
||||
],
|
||||
[
|
||||
1585744405,
|
||||
1.123456
|
||||
"1.123456"
|
||||
]
|
||||
]
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
},
|
||||
"value":[
|
||||
1585743854.077,
|
||||
1.123456
|
||||
"1.123456"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -31,7 +31,7 @@
|
||||
},
|
||||
"value":[
|
||||
1585743854.077,
|
||||
1.123456
|
||||
"1.123456"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -46,7 +46,7 @@
|
||||
},
|
||||
"value":[
|
||||
1585743854.077,
|
||||
1.123456
|
||||
"1.123456"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -61,7 +61,7 @@
|
||||
},
|
||||
"value":[
|
||||
1585743854.077,
|
||||
1.123456
|
||||
"1.123456"
|
||||
]
|
||||
}
|
||||
]
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
package monitoring
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/json-iterator/go"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const (
|
||||
MetricTypeMatrix = "matrix"
|
||||
MetricTypeVector = "vector"
|
||||
@@ -22,6 +29,8 @@ type MetricData struct {
|
||||
MetricValues []MetricValue `json:"result,omitempty" description:"metric data including labels, time series and values"`
|
||||
}
|
||||
|
||||
// The first element is the timestamp, the second is the metric value.
|
||||
// eg, [1585658599.195, 0.528]
|
||||
type Point [2]float64
|
||||
|
||||
type MetricValue struct {
|
||||
@@ -40,3 +49,50 @@ func (p Point) Timestamp() float64 {
|
||||
func (p Point) Value() float64 {
|
||||
return p[1]
|
||||
}
|
||||
|
||||
// MarshalJSON implements json.Marshaler. It will be called when writing JSON to HTTP response
|
||||
// Inspired by prometheus/client_golang
|
||||
func (p Point) MarshalJSON() ([]byte, error) {
|
||||
t, err := jsoniter.Marshal(p.Timestamp())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
v, err := jsoniter.Marshal(strconv.FormatFloat(p.Value(), 'f', -1, 64))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return []byte(fmt.Sprintf("[%s,%s]", t, v)), nil
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements json.Unmarshaler. This is for unmarshaling test data.
|
||||
func (p *Point) UnmarshalJSON(b []byte) error {
|
||||
var v []interface{}
|
||||
if err := jsoniter.Unmarshal(b, &v); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if v == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(v) != 2 {
|
||||
return errors.New("unsupported array length")
|
||||
}
|
||||
|
||||
ts, ok := v[0].(float64)
|
||||
if !ok {
|
||||
return errors.New("failed to unmarshal [timestamp]")
|
||||
}
|
||||
valstr, ok := v[1].(string)
|
||||
if !ok {
|
||||
return errors.New("failed to unmarshal [value]")
|
||||
}
|
||||
valf, err := strconv.ParseFloat(valstr, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p[0] = ts
|
||||
p[1] = valf
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user