Switch per-step stats to serialize ints

Per Julien's feedback on #10369, we're choosing to be consistent with
data types inside the stats structure (ints) rather than with the points
format that is part of the normal query responses (strings). We have
this option because this data cannot be NaN/Inf.

Signed-off-by: Andrew Bloomgarden <blmgrdn@amazon.com>
pull/10485/head
Andrew Bloomgarden 3 years ago committed by Julien Pivotto
parent 9d34ddc00e
commit 20dbb128d9

@ -17,7 +17,6 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"strconv"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"go.opentelemetry.io/otel" "go.opentelemetry.io/otel"
@ -81,18 +80,16 @@ func (s QueryTiming) SpanOperation() string {
// stepStat represents a single statistic for a given step timestamp. // stepStat represents a single statistic for a given step timestamp.
type stepStat struct { type stepStat struct {
T int64 T int64
V float64 V int64
} }
func (s stepStat) String() string { func (s stepStat) String() string {
v := strconv.FormatFloat(s.V, 'f', -1, 64) return fmt.Sprintf("%v @[%v]", s.V, s.T)
return fmt.Sprintf("%v @[%v]", v, s.T)
} }
// MarshalJSON implements json.Marshaler. // MarshalJSON implements json.Marshaler.
func (s stepStat) MarshalJSON() ([]byte, error) { func (s stepStat) MarshalJSON() ([]byte, error) {
v := strconv.FormatFloat(s.V, 'f', -1, 64) return json.Marshal([...]interface{}{float64(s.T) / 1000, s.V})
return json.Marshal([...]interface{}{float64(s.T) / 1000, v})
} }
// queryTimings with all query timers mapped to durations. // queryTimings with all query timers mapped to durations.
@ -185,7 +182,7 @@ func (qs *QuerySamples) totalSamplesPerStepPoints() []stepStat {
ts := make([]stepStat, len(qs.TotalSamplesPerStep)) ts := make([]stepStat, len(qs.TotalSamplesPerStep))
for i, c := range qs.TotalSamplesPerStep { for i, c := range qs.TotalSamplesPerStep {
ts[i] = stepStat{T: qs.startTimestamp + int64(i)*qs.interval, V: float64(c)} ts[i] = stepStat{T: qs.startTimestamp + int64(i)*qs.interval, V: int64(c)}
} }
return ts return ts
} }

@ -61,7 +61,7 @@ func TestQueryStatsWithTimersAndSamples(t *testing.T) {
require.True(t, match, "Expected timings with one non-zero entry.") require.True(t, match, "Expected timings with one non-zero entry.")
require.Regexpf(t, `[,{]"totalQueryableSamples":10[,}]`, string(actual), "expected totalQueryableSamples") require.Regexpf(t, `[,{]"totalQueryableSamples":10[,}]`, string(actual), "expected totalQueryableSamples")
require.Regexpf(t, `[,{]"totalQueryableSamplesPerStep":\[\[20001,"5"\],\[21001,"0"\],\[22001,"0"\],\[23001,"0"\],\[24001,"0"\],\[25001,"5"\]\]`, string(actual), "expected totalQueryableSamplesPerStep") require.Regexpf(t, `[,{]"totalQueryableSamplesPerStep":\[\[20001,5\],\[21001,0\],\[22001,0\],\[23001,0\],\[24001,0\],\[25001,5\]\]`, string(actual), "expected totalQueryableSamplesPerStep")
} }
func TestQueryStatsWithSpanTimers(t *testing.T) { func TestQueryStatsWithSpanTimers(t *testing.T) {

Loading…
Cancel
Save