package prometheus
import (
"errors"
"fmt"
"strconv"
jsoniter "github.com/json-iterator/go"
)
const (
MetricTypeMatrix = "matrix"
MetricTypeVector = "vector"
)
type Metadata struct {
Metric string `json:"metric,omitempty" description:"metric name"`
Type string `json:"type,omitempty" description:"metric type"`
Help string `json:"help,omitempty" description:"metric description"`
}
type Metric struct {
MetricName string `json:"metric_name,omitempty" description:"metric name, eg. scheduler_up_sum"`
MetricData `json:"data,omitempty" description:"actual metric result"`
Error string `json:"error,omitempty"`
}
type MetricData struct {
MetricType string `json:"resultType,omitempty" description:"result type, one of matrix, vector"`
MetricValues []MetricValue `json:"result,omitempty" description:"metric data including labels, time series and values"`
}
type Point [2]float64
type MetricValue struct {
Metadata map[string]string `json:"metric,omitempty" description:"time series labels"`
Sample *Point `json:"value,omitempty" description:"time series, values of vector type"`
Series []Point `json:"values,omitempty" description:"time series, values of matrix type"`
}
func (p Point) Timestamp() float64 {
return p[0]
}
func (p Point) Value() float64 {
return p[1]
}
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
}
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
}