mirror of https://github.com/prometheus/prometheus
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
90 lines
2.0 KiB
90 lines
2.0 KiB
12 years ago
|
package api
|
||
|
|
||
|
import (
|
||
12 years ago
|
"code.google.com/p/gorest"
|
||
12 years ago
|
"encoding/json"
|
||
12 years ago
|
"errors"
|
||
12 years ago
|
"github.com/prometheus/prometheus/rules"
|
||
|
"github.com/prometheus/prometheus/rules/ast"
|
||
12 years ago
|
"log"
|
||
12 years ago
|
"net/http"
|
||
12 years ago
|
"sort"
|
||
12 years ago
|
"time"
|
||
12 years ago
|
)
|
||
12 years ago
|
|
||
12 years ago
|
func (serv MetricsService) Query(Expr string, Json string) (result string) {
|
||
12 years ago
|
exprNode, err := rules.LoadExprFromString(Expr)
|
||
|
if err != nil {
|
||
12 years ago
|
return ast.ErrorToJSON(err)
|
||
12 years ago
|
}
|
||
12 years ago
|
|
||
12 years ago
|
timestamp := serv.time.Now()
|
||
12 years ago
|
|
||
12 years ago
|
rb := serv.ResponseBuilder()
|
||
|
var format ast.OutputFormat
|
||
|
if Json != "" {
|
||
|
format = ast.JSON
|
||
|
rb.SetContentType(gorest.Application_Json)
|
||
|
} else {
|
||
|
format = ast.TEXT
|
||
|
rb.SetContentType(gorest.Text_Plain)
|
||
|
}
|
||
12 years ago
|
|
||
12 years ago
|
return ast.EvalToString(exprNode, ×tamp, format)
|
||
12 years ago
|
}
|
||
12 years ago
|
|
||
|
func (serv MetricsService) QueryRange(Expr string, End int64, Range int64, Step int64) string {
|
||
|
exprNode, err := rules.LoadExprFromString(Expr)
|
||
|
if err != nil {
|
||
12 years ago
|
return ast.ErrorToJSON(err)
|
||
12 years ago
|
}
|
||
12 years ago
|
if exprNode.Type() != ast.VECTOR {
|
||
12 years ago
|
return ast.ErrorToJSON(errors.New("Expression does not evaluate to vector type"))
|
||
12 years ago
|
}
|
||
12 years ago
|
rb := serv.ResponseBuilder()
|
||
12 years ago
|
rb.SetContentType(gorest.Application_Json)
|
||
12 years ago
|
|
||
12 years ago
|
if End == 0 {
|
||
12 years ago
|
End = serv.time.Now().Unix()
|
||
12 years ago
|
}
|
||
12 years ago
|
|
||
12 years ago
|
if Step < 1 {
|
||
|
Step = 1
|
||
|
}
|
||
12 years ago
|
|
||
12 years ago
|
if End-Range < 0 {
|
||
|
Range = End
|
||
|
}
|
||
12 years ago
|
|
||
12 years ago
|
// Align the start to step "tick" boundary.
|
||
|
End -= End % Step
|
||
12 years ago
|
|
||
12 years ago
|
matrix := ast.EvalVectorRange(
|
||
|
exprNode.(ast.VectorNode),
|
||
|
time.Unix(End-Range, 0),
|
||
|
time.Unix(End, 0),
|
||
|
time.Duration(Step)*time.Second)
|
||
12 years ago
|
|
||
12 years ago
|
sort.Sort(matrix)
|
||
|
return ast.TypedValueToJSON(matrix, "matrix")
|
||
12 years ago
|
}
|
||
12 years ago
|
|
||
|
func (serv MetricsService) Metrics() string {
|
||
|
metricNames, err := serv.persistence.GetAllMetricNames()
|
||
|
rb := serv.ResponseBuilder()
|
||
|
rb.SetContentType(gorest.Application_Json)
|
||
|
if err != nil {
|
||
|
log.Printf("Error loading metric names: %v", err)
|
||
12 years ago
|
rb.SetResponseCode(http.StatusInternalServerError)
|
||
12 years ago
|
return err.Error()
|
||
|
}
|
||
|
sort.Strings(metricNames)
|
||
|
resultBytes, err := json.Marshal(metricNames)
|
||
|
if err != nil {
|
||
|
log.Printf("Error marshalling metric names: %v", err)
|
||
12 years ago
|
rb.SetResponseCode(http.StatusInternalServerError)
|
||
12 years ago
|
return err.Error()
|
||
|
}
|
||
|
return string(resultBytes)
|
||
|
}
|