Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue-147: show the correct response with milliseconds precision #155

Merged
merged 6 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

* FEATURE: add compatibility for Grafana `v10.x.x` to ensure `/select/logs/hits` displays precise logs volume on the Explore page. See [this comment](https://github.com/VictoriaMetrics/victorialogs-datasource/pull/146#issuecomment-2533419498).

* BUGFIX: properly parse timestamps with milliseconds precision in datasource response. See [this issue](https://github.com/VictoriaMetrics/victorialogs-datasource/issues/147).

## v0.11.1

* BUGFIX: fix the check for the stats pipe functions in expressions.
Expand Down
19 changes: 14 additions & 5 deletions pkg/plugin/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,10 +378,17 @@ func (ls logStats) vectorDataFrames() (data.Frames, error) {
for i, res := range ls.Result {
f, err := strconv.ParseFloat(res.Value[1].(string), 64)
if err != nil {
return nil, fmt.Errorf("metric %v, unable to parse float64 from %s: %w", res, res.Value[1], err)
return nil, fmt.Errorf("metric %v, unable to parse timestamp to float64 from %s: %w", res, res.Value[1], err)
}

ts := time.Unix(int64(res.Value[0].(float64)), 0)
v, ok := res.Value[0].(float64)
if !ok {
return nil, fmt.Errorf("metric %v, unable to convert metrics value to float64 from %s", res, res.Value[0])
}

seconds := int64(v) // get only seconds
nanoseconds := int64((v - float64(seconds)) * 1e9) // get only nanoseconds
ts := time.Unix(seconds, nanoseconds)
frames[i] = data.NewFrame("",
data.NewField(data.TimeSeriesTimeFieldName, nil, []time.Time{ts}),
data.NewField(data.TimeSeriesValueFieldName, data.Labels(res.Labels), []float64{f}))
Expand All @@ -398,13 +405,15 @@ func (ls logStats) matrixDataFrames() (data.Frames, error) {
for j, value := range res.Values {
v, ok := value[0].(float64)
if !ok {
return nil, fmt.Errorf("error get time from dataframes")
return nil, fmt.Errorf("metric %v, value: %v unable to parse timestamp to float64 from %s", res, value, value[0])
}
timestamps[j] = time.Unix(int64(v), 0)
seconds := int64(v) // get only seconds
nanoseconds := int64((v - float64(seconds)) * 1e9) // get only nanoseconds
timestamps[j] = time.Unix(seconds, nanoseconds)

f, err := strconv.ParseFloat(value[1].(string), 64)
if err != nil {
return nil, fmt.Errorf("erro get value from dataframes: %s", err)
return nil, fmt.Errorf("metric %v, value: %v unable to convert metrics value to float64 from %s", res, value, value[1])
}
values[j] = f
}
Expand Down
27 changes: 26 additions & 1 deletion pkg/plugin/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,31 @@ func Test_getStatsResponse(t *testing.T) {
return rsp
},
},
{
name: "response with milliseconds in timestamps",
filename: "test-data/stats_response_milliseconds",
q: &Query{
DataQuery: backend.DataQuery{
RefID: "A",
},
LegendFormat: "legend {{app}}",
Step: "10ms",
},
want: func() backend.DataResponse {
frames := []*data.Frame{
data.NewFrame("legend ",
data.NewField(data.TimeSeriesTimeFieldName, nil, []time.Time{
time.Unix(1733187134, 0),
time.Unix(1733187134, 449999809),
}),
data.NewField(data.TimeSeriesValueFieldName, data.Labels{"__name__": "count(*)"}, []float64{58, 1}).SetConfig(&data.FieldConfig{DisplayNameFromDS: "legend "}),
),
}
rsp := backend.DataResponse{}
rsp.Frames = append(rsp.Frames, frames...)
return rsp
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -611,7 +636,7 @@ func Test_getStatsResponse(t *testing.T) {
t.Fatalf("error marshal want response: %s", err)
}
if !bytes.Equal(got, want) {
t.Fatalf("got value: %s, want value: %s", got, want)
t.Fatalf("\n got value: %s, \n want value: %s", got, want)
}
}
})
Expand Down
1 change: 1 addition & 0 deletions pkg/plugin/test-data/stats_response_milliseconds
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"status":"success","data":{"resultType":"matrix","result":[{"metric":{"__name__":"count(*)"},"values":[[1733187134,"58"],[1733187134.4499998,"1"]]}]}}
Loading