mirror of
https://github.com/prometheus/prometheus.git
synced 2025-03-05 20:59:13 -08:00
Return API errors in JSON format.
This commit is contained in:
parent
a20bf35997
commit
93670aa129
|
@ -2,6 +2,7 @@ package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"code.google.com/p/gorest"
|
"code.google.com/p/gorest"
|
||||||
|
"errors"
|
||||||
"github.com/matttproud/prometheus/rules"
|
"github.com/matttproud/prometheus/rules"
|
||||||
"github.com/matttproud/prometheus/rules/ast"
|
"github.com/matttproud/prometheus/rules/ast"
|
||||||
"sort"
|
"sort"
|
||||||
|
@ -11,7 +12,7 @@ import (
|
||||||
func (serv MetricsService) Query(Expr string, Json string) (result string) {
|
func (serv MetricsService) Query(Expr string, Json string) (result string) {
|
||||||
exprNode, err := rules.LoadExprFromString(Expr)
|
exprNode, err := rules.LoadExprFromString(Expr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err.Error()
|
return ast.ErrorToJSON(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
timestamp := time.Now()
|
timestamp := time.Now()
|
||||||
|
@ -32,10 +33,10 @@ func (serv MetricsService) Query(Expr string, Json string) (result string) {
|
||||||
func (serv MetricsService) QueryRange(Expr string, End int64, Range int64, Step int64) string {
|
func (serv MetricsService) QueryRange(Expr string, End int64, Range int64, Step int64) string {
|
||||||
exprNode, err := rules.LoadExprFromString(Expr)
|
exprNode, err := rules.LoadExprFromString(Expr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err.Error()
|
return ast.ErrorToJSON(err)
|
||||||
}
|
}
|
||||||
if exprNode.Type() != ast.VECTOR {
|
if exprNode.Type() != ast.VECTOR {
|
||||||
return "Expression does not evaluate to vector type" // TODO return errors correctly everywhere
|
return ast.ErrorToJSON(errors.New("Expression does not evaluate to vector type"))
|
||||||
}
|
}
|
||||||
rb := serv.ResponseBuilder()
|
rb := serv.ResponseBuilder()
|
||||||
rb.SetContentType(gorest.Application_Json)
|
rb.SetContentType(gorest.Application_Json)
|
||||||
|
|
|
@ -129,7 +129,7 @@ func (matrix Matrix) ToString() string {
|
||||||
return strings.Join(metricStrings, "\n")
|
return strings.Join(metricStrings, "\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
func errorToJSON(err error) string {
|
func ErrorToJSON(err error) string {
|
||||||
errorStruct := struct {
|
errorStruct := struct {
|
||||||
Type string
|
Type string
|
||||||
Error string
|
Error string
|
||||||
|
@ -155,7 +155,7 @@ func TypedValueToJSON(data interface{}, typeStr string) string {
|
||||||
}
|
}
|
||||||
dataJSON, err := json.MarshalIndent(dataStruct, "", "\t")
|
dataJSON, err := json.MarshalIndent(dataStruct, "", "\t")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errorToJSON(err)
|
return ErrorToJSON(err)
|
||||||
}
|
}
|
||||||
return string(dataJSON)
|
return string(dataJSON)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue