Skip to content

Commit

Permalink
fix issue with doubled labels in the response
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitryk-dk committed Sep 10, 2024
1 parent edd3259 commit a78a294
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* FEATURE: add support of the `$__interval` variable in queries. See [this issue](https://github.com/VictoriaMetrics/victorialogs-datasource/issues/61).
Thanks to @yincongcyincong for [the pull request](https://github.com/VictoriaMetrics/victorialogs-datasource/pull/69).

* BUGFIX: fix issue collecting labels in different log lines. Removed duplicated labels from previous log lines. See [this issue](https://github.com/VictoriaMetrics/victorialogs-datasource/issues/60).

## v0.4.0

* FEATURE: make retry attempt for datasource requests if returned error is a temporary network error. See [this issue](https://github.com/VictoriaMetrics/victoriametrics-datasource/issues/193)
Expand Down
1 change: 1 addition & 0 deletions pkg/plugin/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func parseStreamResponse(reader io.Reader) backend.DataResponse {
return newResponseError(err, backend.StatusInternal)
}
labelsField.Append(d)
labels = data.Labels{}
}

// Grafana expects lineFields to be always non-empty.
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 a78a294

Please sign in to comment.