diff --git a/api/api.go b/api/api.go index e6b584b72..57a8ff4c1 100644 --- a/api/api.go +++ b/api/api.go @@ -7,6 +7,6 @@ import ( type MetricsService struct { gorest.RestService `root:"/api/" consumes:"application/json" produces:"application/json"` - query gorest.EndPoint `method:"GET" path:"/query?{expr:string}&{json:string}" output:"string"` + query gorest.EndPoint `method:"GET" path:"/query?{expr:string}&{json:string}" output:"string"` queryRange gorest.EndPoint `method:"GET" path:"/query_range?{expr:string}&{end:int64}&{range:int64}&{step:int64}" output:"string"` } diff --git a/api/query.go b/api/query.go index 75fde9c68..77ceec65e 100644 --- a/api/query.go +++ b/api/query.go @@ -4,7 +4,7 @@ import ( "code.google.com/p/gorest" "github.com/matttproud/prometheus/rules" "github.com/matttproud/prometheus/rules/ast" - "sort" + "sort" "time" ) @@ -34,33 +34,33 @@ func (serv MetricsService) QueryRange(Expr string, End int64, Range int64, Step if err != nil { return err.Error() } - if exprNode.Type() != ast.VECTOR { - return "Expression does not evaluate to vector type" // TODO return errors correctly everywhere - } + if exprNode.Type() != ast.VECTOR { + return "Expression does not evaluate to vector type" // TODO return errors correctly everywhere + } rb := serv.ResponseBuilder() - rb.SetContentType(gorest.Application_Json) + rb.SetContentType(gorest.Application_Json) - if End == 0 { - End = time.Now().Unix() - } + if End == 0 { + End = time.Now().Unix() + } - if Step < 1 { - Step = 1 - } + if Step < 1 { + Step = 1 + } - if End - Range < 0 { - Range = End - } + if End-Range < 0 { + Range = End + } - // Align the start to step "tick" boundary. - End -= End % Step + // Align the start to step "tick" boundary. + End -= End % Step - matrix := ast.EvalVectorRange( - exprNode.(ast.VectorNode), - time.Unix(End - Range, 0), - time.Unix(End, 0), - time.Duration(Step) * time.Second) + matrix := ast.EvalVectorRange( + exprNode.(ast.VectorNode), + time.Unix(End-Range, 0), + time.Unix(End, 0), + time.Duration(Step)*time.Second) - sort.Sort(matrix) - return ast.TypedValueToJSON(matrix, "matrix") + sort.Sort(matrix) + return ast.TypedValueToJSON(matrix, "matrix") } diff --git a/model/metric.go b/model/metric.go index fcdf5dde0..6a517a6a7 100644 --- a/model/metric.go +++ b/model/metric.go @@ -14,8 +14,8 @@ package model import ( + "fmt" "time" - "fmt" ) // A Fingerprint is a simplified representation of an entity---e.g., a hash of @@ -47,11 +47,11 @@ type Metric map[LabelName]LabelValue type SampleValue float32 func (v SampleValue) MarshalJSON() ([]byte, error) { - return []byte(fmt.Sprintf("\"%f\"", v)), nil + return []byte(fmt.Sprintf("\"%f\"", v)), nil } func (s SamplePair) MarshalJSON() ([]byte, error) { - return []byte(fmt.Sprintf("{\"Value\": \"%f\", \"Timestamp\": %d}", s.Value, s.Timestamp.Unix())), nil + return []byte(fmt.Sprintf("{\"Value\": \"%f\", \"Timestamp\": %d}", s.Value, s.Timestamp.Unix())), nil } type Sample struct { diff --git a/rules/ast/ast.go b/rules/ast/ast.go index 21162ef02..59f9f4e79 100644 --- a/rules/ast/ast.go +++ b/rules/ast/ast.go @@ -2,11 +2,11 @@ package ast import ( "errors" - "fmt" + "fmt" "github.com/matttproud/prometheus/model" "log" "math" - "sort" + "sort" "strings" "time" ) @@ -219,41 +219,41 @@ func (node *VectorAggregation) labelsToGroupingKey(labels model.Metric) string { } func labelsToKey(labels model.Metric) string { - keyParts := []string{} - for label, value := range labels { - keyParts = append(keyParts, fmt.Sprintf("%v='%v'", label, value)) - } - sort.Strings(keyParts) - return strings.Join(keyParts, ",") // TODO not safe when label value contains comma. + keyParts := []string{} + for label, value := range labels { + keyParts = append(keyParts, fmt.Sprintf("%v='%v'", label, value)) + } + sort.Strings(keyParts) + return strings.Join(keyParts, ",") // TODO not safe when label value contains comma. } func EvalVectorRange(node VectorNode, start time.Time, end time.Time, step time.Duration) Matrix { - // TODO implement watchdog timer for long-running queries. - sampleSets := map[string]*model.SampleSet{} - for t := start; t.Before(end); t = t.Add(step) { - vector := node.Eval(&t) - for _, sample := range vector { - samplePair := model.SamplePair{ - Value: sample.Value, - Timestamp: sample.Timestamp, - } - groupingKey := labelsToKey(sample.Metric) - if sampleSets[groupingKey] == nil { - sampleSets[groupingKey] = &model.SampleSet{ - Metric: sample.Metric, - Values: []model.SamplePair{samplePair}, - } - } else { - sampleSets[groupingKey].Values = append(sampleSets[groupingKey].Values, samplePair) - } - } - } + // TODO implement watchdog timer for long-running queries. + sampleSets := map[string]*model.SampleSet{} + for t := start; t.Before(end); t = t.Add(step) { + vector := node.Eval(&t) + for _, sample := range vector { + samplePair := model.SamplePair{ + Value: sample.Value, + Timestamp: sample.Timestamp, + } + groupingKey := labelsToKey(sample.Metric) + if sampleSets[groupingKey] == nil { + sampleSets[groupingKey] = &model.SampleSet{ + Metric: sample.Metric, + Values: []model.SamplePair{samplePair}, + } + } else { + sampleSets[groupingKey].Values = append(sampleSets[groupingKey].Values, samplePair) + } + } + } - matrix := Matrix{} - for _, sampleSet := range sampleSets { - matrix = append(matrix, sampleSet) - } - return matrix + matrix := Matrix{} + for _, sampleSet := range sampleSets { + matrix = append(matrix, sampleSet) + } + return matrix } func labelIntersection(metric1, metric2 model.Metric) model.Metric { @@ -526,18 +526,17 @@ func (node *MatrixLiteral) EvalBoundaries(timestamp *time.Time) Matrix { } func (matrix Matrix) Len() int { - return len(matrix) + return len(matrix) } func (matrix Matrix) Less(i, j int) bool { - return labelsToKey(matrix[i].Metric) < labelsToKey(matrix[j].Metric) + return labelsToKey(matrix[i].Metric) < labelsToKey(matrix[j].Metric) } func (matrix Matrix) Swap(i, j int) { - matrix[i], matrix[j] = matrix[j], matrix[i] + matrix[i], matrix[j] = matrix[j], matrix[i] } - func (node *StringLiteral) Eval(timestamp *time.Time) string { return node.str } diff --git a/rules/ast/printer.go b/rules/ast/printer.go index c943ead11..ce070f4c4 100644 --- a/rules/ast/printer.go +++ b/rules/ast/printer.go @@ -85,7 +85,7 @@ func (vector Vector) ToString() string { labelStrings := []string{} for label, value := range sample.Metric { if label != "name" { - // TODO escape special chars in label values here and elsewhere. + // TODO escape special chars in label values here and elsewhere. labelStrings = append(labelStrings, fmt.Sprintf("%v='%v'", label, value)) } }