Graphite: Interpolate Graphite alerts with tags (#35887)

This commit is contained in:
Piotr Jamróz
2021-06-27 23:58:50 +02:00
committed by GitHub
parent 035eec1502
commit faf25bc4ea
3 changed files with 98 additions and 18 deletions
+41
View File
@@ -1,9 +1,17 @@
package graphite
import (
"encoding/json"
"io/ioutil"
"net/http"
"reflect"
"strings"
"testing"
"time"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestFormatTimeRange(t *testing.T) {
@@ -58,4 +66,37 @@ func TestFixIntervalFormat(t *testing.T) {
assert.Equal(t, tc.expected, tr)
})
}
executor := &GraphiteExecutor{}
t.Run("Converts response 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{
"fooTag": "fooValue",
"barTag": "barValue",
}, []*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 := executor.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)
}
})
}