Skip to content

Commit

Permalink
fix issue when one of the stream field is defined (#36)
Browse files Browse the repository at this point in the history
* fix issue when one of the stream field is defined

* update CHANGELOG.md
  • Loading branch information
dmitryk-dk authored Jun 27, 2024
1 parent 179a802 commit 9ff02b3
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## tip

* BUGFIX: fix bug with displaying response when one of the stream field is defined and lines are not collected. See [this issue](https://github.com/VictoriaMetrics/victorialogs-datasource/issues/34).

## v0.2.2

* BUGFIX: fix bug with displaying responses with a custom set of fields. See [this issue](https://github.com/VictoriaMetrics/victorialogs-datasource/issues/23).
Expand Down
8 changes: 8 additions & 0 deletions pkg/plugin/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ func parseStreamResponse(reader io.Reader) backend.DataResponse {
}
}

// Grafana expects lineFields to be always non-empty.
if lineField.Len() == 0 {
for i := 0; i < labelsField.Len(); i++ {
label := labelsField.At(i)
lineField.Append(fmt.Sprintf("%s", label))
}
}

// Grafana expects time field to be always non-empty.
if timeFd.Len() == 0 {
now := time.Now()
Expand Down
43 changes: 43 additions & 0 deletions pkg/plugin/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,49 @@ func Test_parseStreamResponse(t *testing.T) {
frame.Meta = &data.FrameMeta{}
rsp.Frames = append(rsp.Frames, frame)

return rsp
},
},
{
name: "response when one stream field is defined and other is free fields",
response: `{"_time":"2024-06-26T13:00:00Z","logs":"1400"}
{"_time":"2024-06-26T14:00:00Z","logs":"374"}`,
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, 06, 26, 13, 00, 00, 0, time.UTC))
timeFd.Append(time.Date(2024, 06, 26, 14, 00, 00, 0, time.UTC))

lineField.Append(`{"logs":"1400"}`)
lineField.Append(`{"logs":"374"}`)

labels := data.Labels{
"logs": "1400",
}

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

labels = data.Labels{
"logs": "374",
}

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 Down

0 comments on commit 9ff02b3

Please sign in to comment.