2013-02-22 20:07:35 +00:00
|
|
|
// Copyright 2013 Prometheus Team
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2013-01-11 01:27:03 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2013-02-06 16:08:05 +00:00
|
|
|
"encoding/json"
|
2013-01-18 00:54:26 +00:00
|
|
|
"errors"
|
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-06-25 12:02:27 +00:00
|
|
|
|
|
|
|
"code.google.com/p/gorest"
|
2013-08-12 15:18:02 +00:00
|
|
|
"github.com/golang/glog"
|
2013-06-25 12:02:27 +00:00
|
|
|
|
|
|
|
clientmodel "github.com/prometheus/client_golang/model"
|
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/rules"
|
|
|
|
"github.com/prometheus/prometheus/rules/ast"
|
|
|
|
"github.com/prometheus/prometheus/stats"
|
2013-01-11 01:27:03 +00:00
|
|
|
)
|
2013-01-12 20:22:59 +00:00
|
|
|
|
2013-04-11 12:51:42 +00:00
|
|
|
func (serv MetricsService) setAccessControlHeaders(rb *gorest.ResponseBuilder) {
|
|
|
|
rb.AddHeader("Access-Control-Allow-Headers", "Accept, Authorization, Content-Type, Origin")
|
|
|
|
rb.AddHeader("Access-Control-Allow-Methods", "GET")
|
|
|
|
rb.AddHeader("Access-Control-Allow-Origin", "*")
|
|
|
|
rb.AddHeader("Access-Control-Expose-Headers", "Date")
|
|
|
|
}
|
|
|
|
|
2013-07-24 00:18:49 +00:00
|
|
|
func (serv MetricsService) Query(expr string, asText string) string {
|
2013-03-18 09:18:35 +00:00
|
|
|
exprNode, err := rules.LoadExprFromString(expr)
|
2013-01-12 20:22:59 +00:00
|
|
|
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()
|
2013-04-11 12:51:42 +00:00
|
|
|
serv.setAccessControlHeaders(rb)
|
2013-01-12 20:22:59 +00:00
|
|
|
var format ast.OutputFormat
|
2013-07-30 15:18:07 +00:00
|
|
|
// BUG(julius): Use Content-Type negotiation.
|
2013-07-24 00:18:49 +00:00
|
|
|
if asText == "" {
|
2013-01-12 20:22:59 +00:00
|
|
|
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-06-03 15:07:03 +00:00
|
|
|
queryStats := stats.NewTimerGroup()
|
|
|
|
result := ast.EvalToString(exprNode, timestamp, format, serv.Storage, queryStats)
|
2013-08-12 15:18:02 +00:00
|
|
|
glog.Infof("Instant query: %s\nQuery stats:\n%s\n", expr, queryStats)
|
2013-06-03 15:07:03 +00:00
|
|
|
return result
|
2013-01-11 01:27:03 +00:00
|
|
|
}
|
2013-01-15 10:30:55 +00:00
|
|
|
|
2013-03-18 09:18:35 +00:00
|
|
|
func (serv MetricsService) QueryRange(expr string, end int64, duration int64, step int64) string {
|
|
|
|
exprNode, err := rules.LoadExprFromString(expr)
|
2013-01-15 10:30:55 +00:00
|
|
|
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-04-11 12:51:42 +00:00
|
|
|
serv.setAccessControlHeaders(rb)
|
2013-01-17 23:07:00 +00:00
|
|
|
rb.SetContentType(gorest.Application_Json)
|
2013-01-15 10:30:55 +00:00
|
|
|
|
2013-03-18 09:18:35 +00:00
|
|
|
if end == 0 {
|
|
|
|
end = serv.time.Now().Unix()
|
2013-01-17 23:07:00 +00:00
|
|
|
}
|
2013-01-15 10:30:55 +00:00
|
|
|
|
2013-03-18 09:18:35 +00:00
|
|
|
if step < 1 {
|
|
|
|
step = 1
|
2013-01-17 23:07:00 +00:00
|
|
|
}
|
2013-01-15 10:30:55 +00:00
|
|
|
|
2013-03-18 09:18:35 +00:00
|
|
|
if end-duration < 0 {
|
|
|
|
duration = end
|
2013-01-17 23:07:00 +00:00
|
|
|
}
|
2013-01-15 10:30:55 +00:00
|
|
|
|
2013-01-17 23:07:00 +00:00
|
|
|
// Align the start to step "tick" boundary.
|
2013-03-18 09:18:35 +00:00
|
|
|
end -= end % step
|
2013-01-15 10:30:55 +00:00
|
|
|
|
2013-06-03 15:07:03 +00:00
|
|
|
queryStats := stats.NewTimerGroup()
|
|
|
|
|
|
|
|
evalTimer := queryStats.GetTimer(stats.TotalEvalTime).Start()
|
2013-03-21 17:06:15 +00:00
|
|
|
matrix, err := ast.EvalVectorRange(
|
2013-01-17 23:07:00 +00:00
|
|
|
exprNode.(ast.VectorNode),
|
2013-04-24 10:05:52 +00:00
|
|
|
time.Unix(end-duration, 0).UTC(),
|
|
|
|
time.Unix(end, 0).UTC(),
|
2013-05-07 11:15:10 +00:00
|
|
|
time.Duration(step)*time.Second,
|
2013-06-03 15:07:03 +00:00
|
|
|
serv.Storage,
|
|
|
|
queryStats)
|
2013-03-21 17:06:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return ast.ErrorToJSON(err)
|
|
|
|
}
|
2013-06-03 15:07:03 +00:00
|
|
|
evalTimer.Stop()
|
2013-01-15 10:30:55 +00:00
|
|
|
|
2013-06-03 15:07:03 +00:00
|
|
|
sortTimer := queryStats.GetTimer(stats.ResultSortTime).Start()
|
2013-01-17 23:07:00 +00:00
|
|
|
sort.Sort(matrix)
|
2013-06-03 15:07:03 +00:00
|
|
|
sortTimer.Stop()
|
|
|
|
|
|
|
|
jsonTimer := queryStats.GetTimer(stats.JsonEncodeTime).Start()
|
|
|
|
result := ast.TypedValueToJSON(matrix, "matrix")
|
|
|
|
jsonTimer.Stop()
|
|
|
|
|
2013-08-12 15:18:02 +00:00
|
|
|
glog.Infof("Range query: %s\nQuery stats:\n%s\n", expr, queryStats)
|
2013-06-03 15:07:03 +00:00
|
|
|
return result
|
2013-01-15 10:30:55 +00:00
|
|
|
}
|
2013-02-06 16:08:05 +00:00
|
|
|
|
|
|
|
func (serv MetricsService) Metrics() string {
|
2013-06-25 12:02:27 +00:00
|
|
|
metricNames, err := serv.Storage.GetAllValuesForLabel(clientmodel.MetricNameLabel)
|
2013-02-06 16:08:05 +00:00
|
|
|
rb := serv.ResponseBuilder()
|
2013-04-11 12:51:42 +00:00
|
|
|
serv.setAccessControlHeaders(rb)
|
2013-02-06 16:08:05 +00:00
|
|
|
rb.SetContentType(gorest.Application_Json)
|
|
|
|
if err != nil {
|
2013-08-12 16:22:48 +00:00
|
|
|
glog.Error("Error loading metric names: ", err)
|
2013-02-07 10:38:01 +00:00
|
|
|
rb.SetResponseCode(http.StatusInternalServerError)
|
2013-02-06 16:08:05 +00:00
|
|
|
return err.Error()
|
|
|
|
}
|
2013-03-26 10:45:56 +00:00
|
|
|
sort.Sort(metricNames)
|
2013-02-06 16:08:05 +00:00
|
|
|
resultBytes, err := json.Marshal(metricNames)
|
|
|
|
if err != nil {
|
2013-08-12 16:22:48 +00:00
|
|
|
glog.Error("Error marshalling metric names: ", 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)
|
|
|
|
}
|