prometheus/web/api/query.go

139 lines
3.8 KiB
Go
Raw Normal View History

// 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.
package api
import (
"encoding/json"
2013-01-17 16:54:26 -08:00
"errors"
"net/http"
2013-01-17 15:07:00 -08:00
"sort"
"time"
"code.google.com/p/gorest"
"github.com/golang/glog"
clientmodel "github.com/prometheus/client_golang/model"
"github.com/prometheus/prometheus/rules"
"github.com/prometheus/prometheus/rules/ast"
"github.com/prometheus/prometheus/stats"
)
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")
}
func (serv MetricsService) Query(expr string, asText string) string {
2013-03-18 02:18:35 -07:00
exprNode, err := rules.LoadExprFromString(expr)
if err != nil {
2013-01-17 16:54:26 -08:00
return ast.ErrorToJSON(err)
}
timestamp := serv.time.Now()
rb := serv.ResponseBuilder()
serv.setAccessControlHeaders(rb)
var format ast.OutputFormat
// BUG(julius): Use Content-Type negotiation.
if asText == "" {
format = ast.JSON
rb.SetContentType(gorest.Application_Json)
} else {
format = ast.TEXT
rb.SetContentType(gorest.Text_Plain)
}
queryStats := stats.NewTimerGroup()
result := ast.EvalToString(exprNode, timestamp, format, serv.Storage, queryStats)
glog.Infof("Instant query: %s\nQuery stats:\n%s\n", expr, queryStats)
return result
}
2013-01-15 02:30:55 -08:00
2013-03-18 02:18:35 -07:00
func (serv MetricsService) QueryRange(expr string, end int64, duration int64, step int64) string {
exprNode, err := rules.LoadExprFromString(expr)
2013-01-15 02:30:55 -08:00
if err != nil {
2013-01-17 16:54:26 -08:00
return ast.ErrorToJSON(err)
2013-01-15 02:30:55 -08:00
}
2013-01-17 15:07:00 -08:00
if exprNode.Type() != ast.VECTOR {
2013-01-17 16:54:26 -08:00
return ast.ErrorToJSON(errors.New("Expression does not evaluate to vector type"))
2013-01-17 15:07:00 -08:00
}
2013-01-15 02:30:55 -08:00
rb := serv.ResponseBuilder()
serv.setAccessControlHeaders(rb)
2013-01-17 15:07:00 -08:00
rb.SetContentType(gorest.Application_Json)
2013-01-15 02:30:55 -08:00
2013-03-18 02:18:35 -07:00
if end == 0 {
end = serv.time.Now().Unix()
2013-01-17 15:07:00 -08:00
}
2013-01-15 02:30:55 -08:00
2013-03-18 02:18:35 -07:00
if step < 1 {
step = 1
2013-01-17 15:07:00 -08:00
}
2013-01-15 02:30:55 -08:00
2013-03-18 02:18:35 -07:00
if end-duration < 0 {
duration = end
2013-01-17 15:07:00 -08:00
}
2013-01-15 02:30:55 -08:00
2013-01-17 15:07:00 -08:00
// Align the start to step "tick" boundary.
2013-03-18 02:18:35 -07:00
end -= end % step
2013-01-15 02:30:55 -08:00
queryStats := stats.NewTimerGroup()
evalTimer := queryStats.GetTimer(stats.TotalEvalTime).Start()
matrix, err := ast.EvalVectorRange(
2013-01-17 15:07:00 -08:00
exprNode.(ast.VectorNode),
time.Unix(end-duration, 0).UTC(),
time.Unix(end, 0).UTC(),
2013-05-07 04:15:10 -07:00
time.Duration(step)*time.Second,
serv.Storage,
queryStats)
if err != nil {
return ast.ErrorToJSON(err)
}
evalTimer.Stop()
2013-01-15 02:30:55 -08:00
sortTimer := queryStats.GetTimer(stats.ResultSortTime).Start()
2013-01-17 15:07:00 -08:00
sort.Sort(matrix)
sortTimer.Stop()
jsonTimer := queryStats.GetTimer(stats.JsonEncodeTime).Start()
result := ast.TypedValueToJSON(matrix, "matrix")
jsonTimer.Stop()
glog.Infof("Range query: %s\nQuery stats:\n%s\n", expr, queryStats)
return result
2013-01-15 02:30:55 -08:00
}
func (serv MetricsService) Metrics() string {
metricNames, err := serv.Storage.GetAllValuesForLabel(clientmodel.MetricNameLabel)
rb := serv.ResponseBuilder()
serv.setAccessControlHeaders(rb)
rb.SetContentType(gorest.Application_Json)
if err != nil {
2013-08-12 09:22:48 -07:00
glog.Error("Error loading metric names: ", err)
rb.SetResponseCode(http.StatusInternalServerError)
return err.Error()
}
sort.Sort(metricNames)
resultBytes, err := json.Marshal(metricNames)
if err != nil {
2013-08-12 09:22:48 -07:00
glog.Error("Error marshalling metric names: ", err)
rb.SetResponseCode(http.StatusInternalServerError)
return err.Error()
}
return string(resultBytes)
}