mirror of
https://github.com/prometheus/prometheus.git
synced 2025-03-05 20:59:13 -08:00
promql: implement JSON array format for scalar and string
This commit is contained in:
parent
834cec44cf
commit
3d67d75935
|
@ -19,6 +19,7 @@ import (
|
||||||
"math"
|
"math"
|
||||||
"runtime"
|
"runtime"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
|
@ -43,12 +44,6 @@ type Sample struct {
|
||||||
Timestamp clientmodel.Timestamp `json:"timestamp"`
|
Timestamp clientmodel.Timestamp `json:"timestamp"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scalar is a scalar value evaluated at the set timestamp.
|
|
||||||
type Scalar struct {
|
|
||||||
Value clientmodel.SampleValue `json:"value"`
|
|
||||||
Timestamp clientmodel.Timestamp `json:"timestamp"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// MarshalJSON implements json.Marshaler.
|
// MarshalJSON implements json.Marshaler.
|
||||||
func (s *Sample) MarshalJSON() ([]byte, error) {
|
func (s *Sample) MarshalJSON() ([]byte, error) {
|
||||||
v := struct {
|
v := struct {
|
||||||
|
@ -65,16 +60,33 @@ func (s *Sample) MarshalJSON() ([]byte, error) {
|
||||||
return json.Marshal(&v)
|
return json.Marshal(&v)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Scalar is a scalar value evaluated at the set timestamp.
|
||||||
|
type Scalar struct {
|
||||||
|
Value clientmodel.SampleValue `json:"value"`
|
||||||
|
Timestamp clientmodel.Timestamp `json:"timestamp"`
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Scalar) String() string {
|
func (s *Scalar) String() string {
|
||||||
return fmt.Sprintf("scalar: %v @[%v]", s.Value, s.Timestamp)
|
return fmt.Sprintf("scalar: %v @[%v]", s.Value, s.Timestamp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MarshalJSON implements json.Marshaler.
|
||||||
|
func (s *Scalar) MarshalJSON() ([]byte, error) {
|
||||||
|
v := strconv.FormatFloat(float64(s.Value), 'f', -1, 64)
|
||||||
|
return json.Marshal([]interface{}{s.Timestamp, string(v)})
|
||||||
|
}
|
||||||
|
|
||||||
// String is a string value evaluated at the set timestamp.
|
// String is a string value evaluated at the set timestamp.
|
||||||
type String struct {
|
type String struct {
|
||||||
Value string `json:"value"`
|
Value string `json:"value"`
|
||||||
Timestamp clientmodel.Timestamp `json:"timestamp"`
|
Timestamp clientmodel.Timestamp `json:"timestamp"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MarshalJSON implements json.Marshaler.
|
||||||
|
func (s *String) MarshalJSON() ([]byte, error) {
|
||||||
|
return json.Marshal([]interface{}{s.Timestamp, s.Value})
|
||||||
|
}
|
||||||
|
|
||||||
func (s *String) String() string {
|
func (s *String) String() string {
|
||||||
return s.Value
|
return s.Value
|
||||||
}
|
}
|
||||||
|
|
|
@ -94,11 +94,19 @@ func (api *API) Query(w http.ResponseWriter, r *http.Request) {
|
||||||
respondJSON(w, plainVec(vec))
|
respondJSON(w, plainVec(vec))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if sca, ok := res.Value.(*promql.Scalar); ok {
|
||||||
|
respondJSON(w, (*plainScalar)(sca))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if str, ok := res.Value.(*promql.String); ok {
|
||||||
|
respondJSON(w, (*plainString)(str))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
respondJSON(w, res.Value)
|
respondJSON(w, res.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// plainVec is an indirection that hides the original MarshalJSON methods
|
// plainVec is an indirection that hides the original MarshalJSON method
|
||||||
// which does not fit the response format for the legacy API.
|
// which does not fit the response format for the legacy API.
|
||||||
type plainVec promql.Vector
|
type plainVec promql.Vector
|
||||||
|
|
||||||
|
@ -121,6 +129,30 @@ func (pv plainVec) String() string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// plainScalar is an indirection that hides the original MarshalJSON method
|
||||||
|
// which does not fit the response format for the legacy API.
|
||||||
|
type plainScalar promql.Scalar
|
||||||
|
|
||||||
|
func (pv plainScalar) Type() promql.ExprType {
|
||||||
|
return promql.ExprScalar
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pv plainScalar) String() string {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// plainString is an indirection that hides the original MarshalJSON method
|
||||||
|
// which does not fit the response format for the legacy API.
|
||||||
|
type plainString promql.String
|
||||||
|
|
||||||
|
func (pv plainString) Type() promql.ExprType {
|
||||||
|
return promql.ExprString
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pv plainString) String() string {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
// QueryRange handles the /api/query_range endpoint.
|
// QueryRange handles the /api/query_range endpoint.
|
||||||
func (api *API) QueryRange(w http.ResponseWriter, r *http.Request) {
|
func (api *API) QueryRange(w http.ResponseWriter, r *http.Request) {
|
||||||
setAccessControlHeaders(w)
|
setAccessControlHeaders(w)
|
||||||
|
|
Loading…
Reference in a new issue