2013-01-11 01:27:03 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2013-01-12 20:22:59 +00:00
|
|
|
"code.google.com/p/gorest"
|
2013-02-06 16:08:05 +00:00
|
|
|
"encoding/json"
|
2013-01-18 00:54:26 +00:00
|
|
|
"errors"
|
2013-01-27 17:49:45 +00:00
|
|
|
"github.com/prometheus/prometheus/rules"
|
|
|
|
"github.com/prometheus/prometheus/rules/ast"
|
2013-02-06 16:08:05 +00:00
|
|
|
"log"
|
2013-02-07 10:38:01 +00:00
|
|
|
"net/http"
|
2013-01-17 23:07:00 +00:00
|
|
|
"sort"
|
2013-01-12 20:22:59 +00:00
|
|
|
"time"
|
2013-01-11 01:27:03 +00:00
|
|
|
)
|
2013-01-12 20:22:59 +00:00
|
|
|
|
2013-01-15 10:30:55 +00:00
|
|
|
func (serv MetricsService) Query(Expr string, Json string) (result string) {
|
2013-01-12 20:22:59 +00:00
|
|
|
exprNode, err := rules.LoadExprFromString(Expr)
|
|
|
|
if err != nil {
|
2013-01-18 00:54:26 +00:00
|
|
|
return ast.ErrorToJSON(err)
|
2013-01-12 20:22:59 +00:00
|
|
|
}
|
2013-01-11 01:27:03 +00:00
|
|
|
|
2013-01-27 18:59:20 +00:00
|
|
|
timestamp := serv.time.Now()
|
2013-01-11 01:27:03 +00:00
|
|
|
|
2013-01-12 20:22:59 +00:00
|
|
|
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)
|
|
|
|
}
|
2013-01-11 02:17:58 +00:00
|
|
|
|
2013-01-12 20:22:59 +00:00
|
|
|
return ast.EvalToString(exprNode, ×tamp, format)
|
2013-01-11 01:27:03 +00:00
|
|
|
}
|
2013-01-15 10:30:55 +00:00
|
|
|
|
|
|
|
func (serv MetricsService) QueryRange(Expr string, End int64, Range int64, Step int64) string {
|
|
|
|
exprNode, err := rules.LoadExprFromString(Expr)
|
|
|
|
if err != nil {
|
2013-01-18 00:54:26 +00:00
|
|
|
return ast.ErrorToJSON(err)
|
2013-01-15 10:30:55 +00:00
|
|
|
}
|
2013-01-17 23:07:00 +00:00
|
|
|
if exprNode.Type() != ast.VECTOR {
|
2013-01-18 00:54:26 +00:00
|
|
|
return ast.ErrorToJSON(errors.New("Expression does not evaluate to vector type"))
|
2013-01-17 23:07:00 +00:00
|
|
|
}
|
2013-01-15 10:30:55 +00:00
|
|
|
rb := serv.ResponseBuilder()
|
2013-01-17 23:07:00 +00:00
|
|
|
rb.SetContentType(gorest.Application_Json)
|
2013-01-15 10:30:55 +00:00
|
|
|
|
2013-01-17 23:07:00 +00:00
|
|
|
if End == 0 {
|
2013-01-27 18:59:20 +00:00
|
|
|
End = serv.time.Now().Unix()
|
2013-01-17 23:07:00 +00:00
|
|
|
}
|
2013-01-15 10:30:55 +00:00
|
|
|
|
2013-01-17 23:07:00 +00:00
|
|
|
if Step < 1 {
|
|
|
|
Step = 1
|
|
|
|
}
|
2013-01-15 10:30:55 +00:00
|
|
|
|
2013-01-17 23:07:00 +00:00
|
|
|
if End-Range < 0 {
|
|
|
|
Range = End
|
|
|
|
}
|
2013-01-15 10:30:55 +00:00
|
|
|
|
2013-01-17 23:07:00 +00:00
|
|
|
// Align the start to step "tick" boundary.
|
|
|
|
End -= End % Step
|
2013-01-15 10:30:55 +00:00
|
|
|
|
2013-01-17 23:07:00 +00:00
|
|
|
matrix := ast.EvalVectorRange(
|
|
|
|
exprNode.(ast.VectorNode),
|
|
|
|
time.Unix(End-Range, 0),
|
|
|
|
time.Unix(End, 0),
|
|
|
|
time.Duration(Step)*time.Second)
|
2013-01-15 10:30:55 +00:00
|
|
|
|
2013-01-17 23:07:00 +00:00
|
|
|
sort.Sort(matrix)
|
|
|
|
return ast.TypedValueToJSON(matrix, "matrix")
|
2013-01-15 10:30:55 +00:00
|
|
|
}
|
2013-02-06 16:08:05 +00:00
|
|
|
|
|
|
|
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)
|
2013-02-07 10:38:01 +00:00
|
|
|
rb.SetResponseCode(http.StatusInternalServerError)
|
2013-02-06 16:08:05 +00:00
|
|
|
return err.Error()
|
|
|
|
}
|
|
|
|
sort.Strings(metricNames)
|
|
|
|
resultBytes, err := json.Marshal(metricNames)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error marshalling metric names: %v", err)
|
2013-02-07 10:38:01 +00:00
|
|
|
rb.SetResponseCode(http.StatusInternalServerError)
|
2013-02-06 16:08:05 +00:00
|
|
|
return err.Error()
|
|
|
|
}
|
|
|
|
return string(resultBytes)
|
|
|
|
}
|