promql: export Sample

This commit is contained in:
Fabian Reinartz 2016-12-24 11:32:10 +01:00
parent 65581a3d46
commit c5f225b920
2 changed files with 30 additions and 30 deletions

View file

@ -108,20 +108,20 @@ func (s Point) String() string {
return "" return ""
} }
// sample is a single sample belonging to a COWMetric. // Sample is a single sample belonging to a metric.
type sample struct { type Sample struct {
Point Point
Metric labels.Labels Metric labels.Labels
} }
func (s sample) String() string { func (s Sample) String() string {
return "" return ""
} }
// Vector is basically only an alias for model.Samples, but the // Vector is basically only an alias for model.Samples, but the
// contract is that in a Vector, all Samples have the same timestamp. // contract is that in a Vector, all Samples have the same timestamp.
type Vector []sample type Vector []Sample
func (vec Vector) String() string { func (vec Vector) String() string {
entries := make([]string, len(vec)) entries := make([]string, len(vec))
@ -804,7 +804,7 @@ func (ev *evaluator) VectorSelector(node *VectorSelector) Vector {
} }
} }
vec = append(vec, sample{ vec = append(vec, Sample{
Metric: node.series[i].Labels(), Metric: node.series[i].Labels(),
Point: Point{V: v, T: ev.Timestamp}, Point: Point{V: v, T: ev.Timestamp},
}) })
@ -936,7 +936,7 @@ func (ev *evaluator) VectorBinop(op itemType, lhs, rhs Vector, matching *VectorM
} }
// All samples from the rhs hashed by the matching label/values. // All samples from the rhs hashed by the matching label/values.
rightSigs := map[uint64]sample{} rightSigs := map[uint64]Sample{}
// Add all rhs samples to a map so we can easily find matches later. // Add all rhs samples to a map so we can easily find matches later.
for _, rs := range rhs { for _, rs := range rhs {
@ -1002,7 +1002,7 @@ func (ev *evaluator) VectorBinop(op itemType, lhs, rhs Vector, matching *VectorM
insertedSigs[insertSig] = struct{}{} insertedSigs[insertSig] = struct{}{}
} }
result = append(result, sample{ result = append(result, Sample{
Metric: metric, Metric: metric,
Point: Point{V: value, T: ev.Timestamp}, Point: Point{V: value, T: ev.Timestamp},
}) })
@ -1310,13 +1310,13 @@ func (ev *evaluator) aggregation(op itemType, grouping []string, without bool, k
} }
if op == itemTopK || op == itemQuantile { if op == itemTopK || op == itemQuantile {
result[groupingKey].heap = make(VectorByValueHeap, 0, k) result[groupingKey].heap = make(VectorByValueHeap, 0, k)
heap.Push(&result[groupingKey].heap, &sample{ heap.Push(&result[groupingKey].heap, &Sample{
Point: Point{V: s.V}, Point: Point{V: s.V},
Metric: s.Metric, Metric: s.Metric,
}) })
} else if op == itemBottomK { } else if op == itemBottomK {
result[groupingKey].reverseHeap = make(VectorByReverseValueHeap, 0, k) result[groupingKey].reverseHeap = make(VectorByReverseValueHeap, 0, k)
heap.Push(&result[groupingKey].reverseHeap, &sample{ heap.Push(&result[groupingKey].reverseHeap, &Sample{
Point: Point{V: s.V}, Point: Point{V: s.V},
Metric: s.Metric, Metric: s.Metric,
}) })
@ -1359,7 +1359,7 @@ func (ev *evaluator) aggregation(op itemType, grouping []string, without bool, k
if int64(len(group.heap)) == k { if int64(len(group.heap)) == k {
heap.Pop(&group.heap) heap.Pop(&group.heap)
} }
heap.Push(&group.heap, &sample{ heap.Push(&group.heap, &Sample{
Point: Point{V: s.V}, Point: Point{V: s.V},
Metric: s.Metric, Metric: s.Metric,
}) })
@ -1370,7 +1370,7 @@ func (ev *evaluator) aggregation(op itemType, grouping []string, without bool, k
if int64(len(group.reverseHeap)) == k { if int64(len(group.reverseHeap)) == k {
heap.Pop(&group.reverseHeap) heap.Pop(&group.reverseHeap)
} }
heap.Push(&group.reverseHeap, &sample{ heap.Push(&group.reverseHeap, &Sample{
Point: Point{V: s.V}, Point: Point{V: s.V},
Metric: s.Metric, Metric: s.Metric,
}) })
@ -1407,7 +1407,7 @@ func (ev *evaluator) aggregation(op itemType, grouping []string, without bool, k
// The heap keeps the lowest value on top, so reverse it. // The heap keeps the lowest value on top, so reverse it.
sort.Sort(sort.Reverse(aggr.heap)) sort.Sort(sort.Reverse(aggr.heap))
for _, v := range aggr.heap { for _, v := range aggr.heap {
resultVector = append(resultVector, sample{ resultVector = append(resultVector, Sample{
Metric: v.Metric, Metric: v.Metric,
Point: Point{V: v.V, T: ev.Timestamp}, Point: Point{V: v.V, T: ev.Timestamp},
}) })
@ -1418,7 +1418,7 @@ func (ev *evaluator) aggregation(op itemType, grouping []string, without bool, k
// The heap keeps the lowest value on top, so reverse it. // The heap keeps the lowest value on top, so reverse it.
sort.Sort(sort.Reverse(aggr.reverseHeap)) sort.Sort(sort.Reverse(aggr.reverseHeap))
for _, v := range aggr.reverseHeap { for _, v := range aggr.reverseHeap {
resultVector = append(resultVector, sample{ resultVector = append(resultVector, Sample{
Metric: v.Metric, Metric: v.Metric,
Point: Point{V: v.V, T: ev.Timestamp}, Point: Point{V: v.V, T: ev.Timestamp},
}) })
@ -1432,7 +1432,7 @@ func (ev *evaluator) aggregation(op itemType, grouping []string, without bool, k
// For other aggregations, we already have the right value. // For other aggregations, we already have the right value.
} }
resultVector = append(resultVector, sample{ resultVector = append(resultVector, Sample{
Metric: aggr.labels, Metric: aggr.labels,
Point: Point{V: aggr.value, T: ev.Timestamp}, Point: Point{V: aggr.value, T: ev.Timestamp},
}) })

View file

@ -116,7 +116,7 @@ func extrapolatedRate(ev *evaluator, arg Expr, isCounter bool, isRate bool) Valu
resultValue = resultValue / 1000 / ms.Range.Seconds() resultValue = resultValue / 1000 / ms.Range.Seconds()
} }
resultVector = append(resultVector, sample{ resultVector = append(resultVector, Sample{
Metric: copyLabels(samples.Metric, false), Metric: copyLabels(samples.Metric, false),
Point: Point{V: resultValue, T: ev.Timestamp}, Point: Point{V: resultValue, T: ev.Timestamp},
}) })
@ -179,7 +179,7 @@ func instantValue(ev *evaluator, arg Expr, isRate bool) Value {
resultValue /= float64(sampledInterval) / 1000 resultValue /= float64(sampledInterval) / 1000
} }
resultVector = append(resultVector, sample{ resultVector = append(resultVector, Sample{
Metric: copyLabels(samples.Metric, false), Metric: copyLabels(samples.Metric, false),
Point: Point{V: resultValue, T: ev.Timestamp}, Point: Point{V: resultValue, T: ev.Timestamp},
}) })
@ -272,7 +272,7 @@ func funcHoltWinters(ev *evaluator, args Expressions) Value {
s[i] = x + y s[i] = x + y
} }
resultVector = append(resultVector, sample{ resultVector = append(resultVector, Sample{
Metric: copyLabels(samples.Metric, false), Metric: copyLabels(samples.Metric, false),
Point: Point{V: s[len(s)-1], T: ev.Timestamp}, // The last value in the Vector is the smoothed result. Point: Point{V: s[len(s)-1], T: ev.Timestamp}, // The last value in the Vector is the smoothed result.
}) })
@ -413,7 +413,7 @@ func aggrOverTime(ev *evaluator, args Expressions, aggrFn func([]Point) float64)
continue continue
} }
resultVector = append(resultVector, sample{ resultVector = append(resultVector, Sample{
Metric: copyLabels(el.Metric, false), Metric: copyLabels(el.Metric, false),
Point: Point{V: aggrFn(el.Points), T: ev.Timestamp}, Point: Point{V: aggrFn(el.Points), T: ev.Timestamp},
}) })
@ -496,9 +496,9 @@ func funcQuantileOverTime(ev *evaluator, args Expressions) Value {
el.Metric = copyLabels(el.Metric, false) el.Metric = copyLabels(el.Metric, false)
values := make(VectorByValueHeap, 0, len(el.Points)) values := make(VectorByValueHeap, 0, len(el.Points))
for _, v := range el.Points { for _, v := range el.Points {
values = append(values, sample{Point: Point{V: v.V}}) values = append(values, Sample{Point: Point{V: v.V}})
} }
resultVector = append(resultVector, sample{ resultVector = append(resultVector, Sample{
Metric: el.Metric, Metric: el.Metric,
Point: Point{V: quantile(q, values), T: ev.Timestamp}, Point: Point{V: quantile(q, values), T: ev.Timestamp},
}) })
@ -559,7 +559,7 @@ func funcAbsent(ev *evaluator, args Expressions) Value {
} }
} }
return Vector{ return Vector{
sample{ Sample{
Metric: labels.New(m...), Metric: labels.New(m...),
Point: Point{V: 1, T: ev.Timestamp}, Point: Point{V: 1, T: ev.Timestamp},
}, },
@ -663,7 +663,7 @@ func funcDeriv(ev *evaluator, args Expressions) Value {
continue continue
} }
slope, _ := linearRegression(samples.Points, 0) slope, _ := linearRegression(samples.Points, 0)
resultSample := sample{ resultSample := Sample{
Metric: copyLabels(samples.Metric, false), Metric: copyLabels(samples.Metric, false),
Point: Point{V: slope, T: ev.Timestamp}, Point: Point{V: slope, T: ev.Timestamp},
} }
@ -687,7 +687,7 @@ func funcPredictLinear(ev *evaluator, args Expressions) Value {
} }
slope, intercept := linearRegression(samples.Points, ev.Timestamp) slope, intercept := linearRegression(samples.Points, ev.Timestamp)
resultVector = append(resultVector, sample{ resultVector = append(resultVector, Sample{
Metric: copyLabels(samples.Metric, false), Metric: copyLabels(samples.Metric, false),
Point: Point{V: slope*duration + intercept, T: ev.Timestamp}, Point: Point{V: slope*duration + intercept, T: ev.Timestamp},
}) })
@ -727,7 +727,7 @@ func funcHistogramQuantile(ev *evaluator, args Expressions) Value {
} }
for _, mb := range signatureToMetricWithBuckets { for _, mb := range signatureToMetricWithBuckets {
outVec = append(outVec, sample{ outVec = append(outVec, Sample{
Metric: mb.metric, Metric: mb.metric,
Point: Point{V: bucketQuantile(q, mb.buckets), T: ev.Timestamp}, Point: Point{V: bucketQuantile(q, mb.buckets), T: ev.Timestamp},
}) })
@ -752,7 +752,7 @@ func funcResets(ev *evaluator, args Expressions) Value {
prev = current prev = current
} }
out = append(out, sample{ out = append(out, Sample{
Metric: copyLabels(samples.Metric, false), Metric: copyLabels(samples.Metric, false),
Point: Point{V: float64(resets), T: ev.Timestamp}, Point: Point{V: float64(resets), T: ev.Timestamp},
}) })
@ -776,7 +776,7 @@ func funcChanges(ev *evaluator, args Expressions) Value {
prev = current prev = current
} }
out = append(out, sample{ out = append(out, Sample{
Metric: copyLabels(samples.Metric, false), Metric: copyLabels(samples.Metric, false),
Point: Point{V: float64(changes), T: ev.Timestamp}, Point: Point{V: float64(changes), T: ev.Timestamp},
}) })
@ -832,7 +832,7 @@ func funcLabelReplace(ev *evaluator, args Expressions) Value {
// === Vector(s Scalar) Vector === // === Vector(s Scalar) Vector ===
func funcVector(ev *evaluator, args Expressions) Value { func funcVector(ev *evaluator, args Expressions) Value {
return Vector{ return Vector{
sample{ Sample{
Metric: labels.Labels{}, Metric: labels.Labels{},
Point: Point{V: ev.evalFloat(args[0]), T: ev.Timestamp}, Point: Point{V: ev.evalFloat(args[0]), T: ev.Timestamp},
}, },
@ -844,7 +844,7 @@ func dateWrapper(ev *evaluator, args Expressions, f func(time.Time) float64) Val
var v Vector var v Vector
if len(args) == 0 { if len(args) == 0 {
v = Vector{ v = Vector{
sample{ Sample{
Metric: labels.Labels{}, Metric: labels.Labels{},
Point: Point{V: float64(ev.Timestamp) / 1000}, Point: Point{V: float64(ev.Timestamp) / 1000},
}, },
@ -1220,7 +1220,7 @@ func (s VectorByValueHeap) Swap(i, j int) {
} }
func (s *VectorByValueHeap) Push(x interface{}) { func (s *VectorByValueHeap) Push(x interface{}) {
*s = append(*s, x.(sample)) *s = append(*s, x.(Sample))
} }
func (s *VectorByValueHeap) Pop() interface{} { func (s *VectorByValueHeap) Pop() interface{} {
@ -1249,7 +1249,7 @@ func (s VectorByReverseValueHeap) Swap(i, j int) {
} }
func (s *VectorByReverseValueHeap) Push(x interface{}) { func (s *VectorByReverseValueHeap) Push(x interface{}) {
*s = append(*s, x.(sample)) *s = append(*s, x.(Sample))
} }
func (s *VectorByReverseValueHeap) Pop() interface{} { func (s *VectorByReverseValueHeap) Pop() interface{} {