mirror of
https://github.com/grafana/grafana.git
synced 2024-12-28 18:01:40 -06:00
Graphite: Convert tag values returned as numbers to strings (#37882)
* Convert tag values returned as numbers to string This is a bug in Graphite <= 1.1.7. Since 1.1.8 all values are converted to strings. * Simplify type conversions * Fix linting errors
This commit is contained in:
parent
76b891b001
commit
f7501ff77f
@ -10,6 +10,7 @@ import (
|
||||
"net/url"
|
||||
"path"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@ -248,15 +249,25 @@ func (s *Service) toDataFrames(response *http.Response) (frames data.Frames, err
|
||||
values = append(values, value)
|
||||
}
|
||||
|
||||
tags := make(map[string]string)
|
||||
for name, value := range series.Tags {
|
||||
switch value := value.(type) {
|
||||
case string:
|
||||
tags[name] = value
|
||||
case float64:
|
||||
tags[name] = strconv.FormatFloat(value, 'f', -1, 64)
|
||||
}
|
||||
}
|
||||
|
||||
frames = append(frames, data.NewFrame(name,
|
||||
data.NewField("time", nil, timeVector),
|
||||
data.NewField("value", series.Tags, values).SetConfig(&data.FieldConfig{DisplayNameFromDS: name})))
|
||||
data.NewField("value", tags, values).SetConfig(&data.FieldConfig{DisplayNameFromDS: name})))
|
||||
|
||||
if setting.Env == setting.Dev {
|
||||
s.logger.Debug("Graphite response", "target", series.Target, "datapoints", len(series.DataPoints))
|
||||
}
|
||||
}
|
||||
return
|
||||
return frames, nil
|
||||
}
|
||||
|
||||
func (s *Service) createRequest(dsInfo *datasourceInfo, data url.Values) (*http.Request, error) {
|
||||
|
@ -52,12 +52,39 @@ func TestFixIntervalFormat(t *testing.T) {
|
||||
|
||||
service := &Service{logger: log.New("tsdb.graphite")}
|
||||
|
||||
t.Run("Converts response to data frames", func(*testing.T) {
|
||||
t.Run("Converts response without tags to data frames", func(*testing.T) {
|
||||
body := `
|
||||
[
|
||||
{
|
||||
"target": "target",
|
||||
"tags": { "fooTag": "fooValue", "barTag": "barValue" },
|
||||
"datapoints": [[50, 1], [null, 2], [100, 3]]
|
||||
}
|
||||
]`
|
||||
a := 50.0
|
||||
b := 100.0
|
||||
expectedFrame := data.NewFrame("target",
|
||||
data.NewField("time", nil, []time.Time{time.Unix(1, 0).UTC(), time.Unix(2, 0).UTC(), time.Unix(3, 0).UTC()}),
|
||||
data.NewField("value", data.Labels{}, []*float64{&a, nil, &b}).SetConfig(&data.FieldConfig{DisplayNameFromDS: "target"}),
|
||||
)
|
||||
expectedFrames := data.Frames{expectedFrame}
|
||||
|
||||
httpResponse := &http.Response{StatusCode: 200, Body: ioutil.NopCloser(strings.NewReader(body))}
|
||||
dataFrames, err := service.toDataFrames(httpResponse)
|
||||
|
||||
require.NoError(t, err)
|
||||
if !reflect.DeepEqual(expectedFrames, dataFrames) {
|
||||
expectedFramesJSON, _ := json.Marshal(expectedFrames)
|
||||
dataFramesJSON, _ := json.Marshal(dataFrames)
|
||||
t.Errorf("Data frames should have been equal but was, expected:\n%s\nactual:\n%s", expectedFramesJSON, dataFramesJSON)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Converts response with tags to data frames", func(*testing.T) {
|
||||
body := `
|
||||
[
|
||||
{
|
||||
"target": "target",
|
||||
"tags": { "fooTag": "fooValue", "barTag": "barValue", "int": 100, "float": 3.14 },
|
||||
"datapoints": [[50, 1], [null, 2], [100, 3]]
|
||||
}
|
||||
]`
|
||||
@ -68,6 +95,8 @@ func TestFixIntervalFormat(t *testing.T) {
|
||||
data.NewField("value", data.Labels{
|
||||
"fooTag": "fooValue",
|
||||
"barTag": "barValue",
|
||||
"int": "100",
|
||||
"float": "3.14",
|
||||
}, []*float64{&a, nil, &b}).SetConfig(&data.FieldConfig{DisplayNameFromDS: "target"}),
|
||||
)
|
||||
expectedFrames := data.Frames{expectedFrame}
|
||||
|
@ -5,5 +5,6 @@ import "github.com/grafana/grafana/pkg/plugins"
|
||||
type TargetResponseDTO struct {
|
||||
Target string `json:"target"`
|
||||
DataPoints plugins.DataTimeSeriesPoints `json:"datapoints"`
|
||||
Tags map[string]string `json:"tags"`
|
||||
// Graphite <=1.1.7 may return some tags as numbers requiring extra conversion. See https://github.com/grafana/grafana/issues/37614
|
||||
Tags map[string]interface{} `json:"tags"`
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user