mirror of
https://github.com/prometheus/prometheus.git
synced 2024-12-29 07:29:42 -08:00
e86f4d9dfd
Go's time.Time represents time as UTC in its fundamental data type. That said, when using ``time.Unix(...)``, it sets the zone for the time representation to the local. Unfortunately with diagnosis and our tests, it is a PITA to jump between various zones, even though the serialized version remains the same. To keep things easy, all places where times are generated or read are converted into UTC. These conversions are cheap, for ``Time.In`` merely changes a pointer reference in the struct, nothing more. This enables me to diagnose test failures with fixture data very easily.
117 lines
3.2 KiB
Go
117 lines
3.2 KiB
Go
// 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 (
|
|
"code.google.com/p/gorest"
|
|
"encoding/json"
|
|
"errors"
|
|
"github.com/prometheus/prometheus/model"
|
|
"github.com/prometheus/prometheus/rules"
|
|
"github.com/prometheus/prometheus/rules/ast"
|
|
"log"
|
|
"net/http"
|
|
"sort"
|
|
"time"
|
|
)
|
|
|
|
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, formatJson string) (result string) {
|
|
exprNode, err := rules.LoadExprFromString(expr)
|
|
if err != nil {
|
|
return ast.ErrorToJSON(err)
|
|
}
|
|
|
|
timestamp := serv.time.Now()
|
|
|
|
rb := serv.ResponseBuilder()
|
|
serv.setAccessControlHeaders(rb)
|
|
var format ast.OutputFormat
|
|
if formatJson != "" {
|
|
format = ast.JSON
|
|
rb.SetContentType(gorest.Application_Json)
|
|
} else {
|
|
format = ast.TEXT
|
|
rb.SetContentType(gorest.Text_Plain)
|
|
}
|
|
|
|
return ast.EvalToString(exprNode, timestamp, format)
|
|
}
|
|
|
|
func (serv MetricsService) QueryRange(expr string, end int64, duration int64, step int64) string {
|
|
exprNode, err := rules.LoadExprFromString(expr)
|
|
if err != nil {
|
|
return ast.ErrorToJSON(err)
|
|
}
|
|
if exprNode.Type() != ast.VECTOR {
|
|
return ast.ErrorToJSON(errors.New("Expression does not evaluate to vector type"))
|
|
}
|
|
rb := serv.ResponseBuilder()
|
|
serv.setAccessControlHeaders(rb)
|
|
rb.SetContentType(gorest.Application_Json)
|
|
|
|
if end == 0 {
|
|
end = serv.time.Now().Unix()
|
|
}
|
|
|
|
if step < 1 {
|
|
step = 1
|
|
}
|
|
|
|
if end-duration < 0 {
|
|
duration = end
|
|
}
|
|
|
|
// Align the start to step "tick" boundary.
|
|
end -= end % step
|
|
|
|
matrix, err := ast.EvalVectorRange(
|
|
exprNode.(ast.VectorNode),
|
|
time.Unix(end-duration, 0).UTC(),
|
|
time.Unix(end, 0).UTC(),
|
|
time.Duration(step)*time.Second)
|
|
if err != nil {
|
|
return ast.ErrorToJSON(err)
|
|
}
|
|
|
|
sort.Sort(matrix)
|
|
return ast.TypedValueToJSON(matrix, "matrix")
|
|
}
|
|
|
|
func (serv MetricsService) Metrics() string {
|
|
metricNames, err := serv.appState.Storage.GetAllValuesForLabel(model.MetricNameLabel)
|
|
rb := serv.ResponseBuilder()
|
|
serv.setAccessControlHeaders(rb)
|
|
rb.SetContentType(gorest.Application_Json)
|
|
if err != nil {
|
|
log.Printf("Error loading metric names: %v", err)
|
|
rb.SetResponseCode(http.StatusInternalServerError)
|
|
return err.Error()
|
|
}
|
|
sort.Sort(metricNames)
|
|
resultBytes, err := json.Marshal(metricNames)
|
|
if err != nil {
|
|
log.Printf("Error marshalling metric names: %v", err)
|
|
rb.SetResponseCode(http.StatusInternalServerError)
|
|
return err.Error()
|
|
}
|
|
return string(resultBytes)
|
|
}
|