Skip to content

Commit

Permalink
fix issue with doubled labels in the response (#74)
Browse files Browse the repository at this point in the history
* fix issue with doubled labels in the response

* cleanup

* fix comments
  • Loading branch information
dmitryk-dk authored Sep 12, 2024
1 parent 9916a93 commit 5f142ea
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Thanks to @yincongcyincong for [the pull request](https://github.com/VictoriaMetrics/victorialogs-datasource/pull/69).

* BUGFIX: correctly pass time range filter when querying variable values. Before, time filter wasn't applied for `/field_values` and `/field_names` API calls. See [this](https://github.com/VictoriaMetrics/victorialogs-datasource/issues/71) and [this](https://github.com/VictoriaMetrics/victorialogs-datasource/issues/72) issues.
* BUGFIX: fix the issue with displaying incorrect subfields when requesting logs with different set of fields. See [this issue](https://github.com/VictoriaMetrics/victorialogs-datasource/issues/60).

## v0.4.0

Expand Down
4 changes: 2 additions & 2 deletions pkg/plugin/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ func parseStreamResponse(reader io.Reader) backend.DataResponse {
lineField := data.NewFieldFromFieldType(data.FieldTypeString, 0)
lineField.Name = gLineField

labels := data.Labels{}

scanner := bufio.NewScanner(reader)

for scanner.Scan() {
Expand All @@ -63,6 +61,8 @@ func parseStreamResponse(reader io.Reader) backend.DataResponse {
}
timeFd.Append(getTime)
}

labels := data.Labels{}
if value.Exists(streamField) {
stream := value.GetStringBytes(streamField)
expr, err := metricsql.Parse(string(stream))
Expand Down
65 changes: 65 additions & 0 deletions pkg/plugin/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,70 @@ func Test_parseStreamResponse(t *testing.T) {
frame.Meta = &data.FrameMeta{}
rsp.Frames = append(rsp.Frames, frame)

return rsp
},
},
{
name: "new test",
response: `{"_time": "2024-09-10T12:24:38.124811Z","_stream_id": "00000000000000002e3bd2bdc376279a6418761ca20c417c","_stream": "{path=\"/var/lib/docker/containers/c01cbe414773fa6b3e4e0976fb27c3583b1a5cd4b7007662477df66987f97f89/c01cbe414773fa6b3e4e0976fb27c3583b1a5cd4b7007662477df66987f97f89-json.log\",stream=\"stderr\"}","_msg": "1","path": "/var/lib/docker/containers/c01cbe414773fa6b3e4e0976fb27c3583b1a5cd4b7007662477df66987f97f89/c01cbe414773fa6b3e4e0976fb27c3583b1a5cd4b7007662477df66987f97f89-json.log","stream": "stderr","time": "2024-09-10T12:24:38.124811792Z"}
{"_time": "2024-09-10T12:36:10.664553169Z","_stream_id": "0000000000000000356bfe9e3c71128c750d94c15df6b908","_stream": "{stream=\"stream1\"}","_msg": "2","date": "0","stream": "stream1","log.level": "info"}
{"_time": "2024-09-10T13:06:56.45147Z","_stream_id": "00000000000000002e3bd2bdc376279a6418761ca20c417c","_stream": "{path=\"/var/lib/docker/containers/c01cbe414773fa6b3e4e0976fb27c3583b1a5cd4b7007662477df66987f97f89/c01cbe414773fa6b3e4e0976fb27c3583b1a5cd4b7007662477df66987f97f89-json.log\",stream=\"stderr\"}","_msg": "3","path": "/var/lib/docker/containers/c01cbe414773fa6b3e4e0976fb27c3583b1a5cd4b7007662477df66987f97f89/c01cbe414773fa6b3e4e0976fb27c3583b1a5cd4b7007662477df66987f97f89-json.log","stream": "stderr","time": "2024-09-10T13:06:56.451470093Z"}`,
want: func() backend.DataResponse {
labelsField := data.NewFieldFromFieldType(data.FieldTypeJSON, 0)
labelsField.Name = gLabelsField

timeFd := data.NewFieldFromFieldType(data.FieldTypeTime, 0)
timeFd.Name = gTimeField

lineField := data.NewFieldFromFieldType(data.FieldTypeString, 0)
lineField.Name = gLineField

timeFd.Append(time.Date(2024, 9, 10, 12, 24, 38, 124000000, time.UTC))
timeFd.Append(time.Date(2024, 9, 10, 12, 36, 10, 664000000, time.UTC))
timeFd.Append(time.Date(2024, 9, 10, 13, 06, 56, 451000000, time.UTC))

lineField.Append("1")

labels := data.Labels{
"_stream_id": "00000000000000002e3bd2bdc376279a6418761ca20c417c",
"path": "/var/lib/docker/containers/c01cbe414773fa6b3e4e0976fb27c3583b1a5cd4b7007662477df66987f97f89/c01cbe414773fa6b3e4e0976fb27c3583b1a5cd4b7007662477df66987f97f89-json.log",
"stream": "stderr",
"time": "2024-09-10T12:24:38.124811792Z",
}

b, _ := labelsToJSON(labels)
labelsField.Append(b)

lineField.Append("2")

labels = data.Labels{
"_stream_id": "0000000000000000356bfe9e3c71128c750d94c15df6b908",
"date": "0",
"stream": "stream1",
"log.level": "info",
}

b, _ = labelsToJSON(labels)
labelsField.Append(b)

lineField.Append("3")

labels = data.Labels{
"_stream_id": "00000000000000002e3bd2bdc376279a6418761ca20c417c",
"path": "/var/lib/docker/containers/c01cbe414773fa6b3e4e0976fb27c3583b1a5cd4b7007662477df66987f97f89/c01cbe414773fa6b3e4e0976fb27c3583b1a5cd4b7007662477df66987f97f89-json.log",
"stream": "stderr",
"time": "2024-09-10T13:06:56.451470093Z",
}

b, _ = labelsToJSON(labels)
labelsField.Append(b)

frame := data.NewFrame("", timeFd, lineField, labelsField)

rsp := backend.DataResponse{}
frame.Meta = &data.FrameMeta{}
rsp.Frames = append(rsp.Frames, frame)

return rsp
},
},
Expand All @@ -369,6 +433,7 @@ func Test_parseStreamResponse(t *testing.T) {
if len(resp.Frames) != 1 {
t.Fatalf("expected for response to always contain 1 Frame; got %d", len(resp.Frames))
}

got := resp.Frames[0]
want := w.Frames[0]
expFieldsLen := got.Fields[0].Len()
Expand Down

0 comments on commit 5f142ea

Please sign in to comment.