mirror of
https://github.com/grafana/grafana.git
synced 2025-08-01 03:01:51 +08:00
Graphite: Interpolate Graphite alerts with tags (#35887)
This commit is contained in:
@ -12,9 +12,11 @@ import (
|
|||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"golang.org/x/net/context/ctxhttp"
|
"golang.org/x/net/context/ctxhttp"
|
||||||
|
|
||||||
|
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||||
"github.com/grafana/grafana/pkg/infra/httpclient"
|
"github.com/grafana/grafana/pkg/infra/httpclient"
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
@ -123,7 +125,7 @@ func (e *GraphiteExecutor) DataQuery(ctx context.Context, dsInfo *models.DataSou
|
|||||||
return plugins.DataResponse{}, err
|
return plugins.DataResponse{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := e.parseResponse(res)
|
frames, err := e.toDataFrames(res)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return plugins.DataResponse{}, err
|
return plugins.DataResponse{}, err
|
||||||
}
|
}
|
||||||
@ -131,19 +133,10 @@ func (e *GraphiteExecutor) DataQuery(ctx context.Context, dsInfo *models.DataSou
|
|||||||
result := plugins.DataResponse{
|
result := plugins.DataResponse{
|
||||||
Results: make(map[string]plugins.DataQueryResult),
|
Results: make(map[string]plugins.DataQueryResult),
|
||||||
}
|
}
|
||||||
queryRes := plugins.DataQueryResult{}
|
result.Results["A"] = plugins.DataQueryResult{
|
||||||
for _, series := range data {
|
RefID: "A",
|
||||||
queryRes.Series = append(queryRes.Series, plugins.DataTimeSeries{
|
Dataframes: plugins.NewDecodedDataFrames(frames),
|
||||||
Name: series.Target,
|
|
||||||
Points: series.DataPoints,
|
|
||||||
})
|
|
||||||
|
|
||||||
if setting.Env == setting.Dev {
|
|
||||||
glog.Debug("Graphite response", "target", series.Target, "datapoints", len(series.DataPoints))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
result.Results["A"] = queryRes
|
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -170,13 +163,39 @@ func (e *GraphiteExecutor) parseResponse(res *http.Response) ([]TargetResponseDT
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for si := range data {
|
return data, nil
|
||||||
// Convert Response to timestamps MS
|
}
|
||||||
for pi, point := range data[si].DataPoints {
|
|
||||||
data[si].DataPoints[pi][1].Float64 = point[1].Float64 * 1000
|
func (e *GraphiteExecutor) toDataFrames(response *http.Response) (frames data.Frames, error error) {
|
||||||
|
responseData, err := e.parseResponse(response)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
frames = data.Frames{}
|
||||||
|
for _, series := range responseData {
|
||||||
|
timeVector := make([]time.Time, 0, len(series.DataPoints))
|
||||||
|
values := make([]*float64, 0, len(series.DataPoints))
|
||||||
|
name := series.Target
|
||||||
|
|
||||||
|
for _, dataPoint := range series.DataPoints {
|
||||||
|
var timestamp, value, err = parseDataTimePoint(dataPoint)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
timeVector = append(timeVector, timestamp)
|
||||||
|
values = append(values, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
frames = append(frames, data.NewFrame(name,
|
||||||
|
data.NewField("time", nil, timeVector),
|
||||||
|
data.NewField("value", series.Tags, values).SetConfig(&data.FieldConfig{DisplayNameFromDS: name})))
|
||||||
|
|
||||||
|
if setting.Env == setting.Dev {
|
||||||
|
glog.Debug("Graphite response", "target", series.Target, "datapoints", len(series.DataPoints))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return data, nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *GraphiteExecutor) createRequest(dsInfo *models.DataSource, data url.Values) (*http.Request, error) {
|
func (e *GraphiteExecutor) createRequest(dsInfo *models.DataSource, data url.Values) (*http.Request, error) {
|
||||||
@ -242,3 +261,22 @@ func epochMStoGraphiteTime(tr plugins.DataTimeRange) (string, string, error) {
|
|||||||
|
|
||||||
return fmt.Sprintf("%d", from/1000), fmt.Sprintf("%d", to/1000), nil
|
return fmt.Sprintf("%d", from/1000), fmt.Sprintf("%d", to/1000), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Graphite should always return timestamp as a number but values might be nil when data is missing
|
||||||
|
*/
|
||||||
|
func parseDataTimePoint(dataTimePoint plugins.DataTimePoint) (time.Time, *float64, error) {
|
||||||
|
if !dataTimePoint[1].Valid {
|
||||||
|
return time.Time{}, nil, errors.New("failed to parse data point timestamp")
|
||||||
|
}
|
||||||
|
|
||||||
|
timestamp := time.Unix(int64(dataTimePoint[1].Float64), 0).UTC()
|
||||||
|
|
||||||
|
if dataTimePoint[0].Valid {
|
||||||
|
var value = new(float64)
|
||||||
|
*value = dataTimePoint[0].Float64
|
||||||
|
return timestamp, value, nil
|
||||||
|
} else {
|
||||||
|
return timestamp, nil, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,9 +1,17 @@
|
|||||||
package graphite
|
package graphite
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"reflect"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFormatTimeRange(t *testing.T) {
|
func TestFormatTimeRange(t *testing.T) {
|
||||||
@ -58,4 +66,37 @@ func TestFixIntervalFormat(t *testing.T) {
|
|||||||
assert.Equal(t, tc.expected, tr)
|
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)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
@ -5,4 +5,5 @@ import "github.com/grafana/grafana/pkg/plugins"
|
|||||||
type TargetResponseDTO struct {
|
type TargetResponseDTO struct {
|
||||||
Target string `json:"target"`
|
Target string `json:"target"`
|
||||||
DataPoints plugins.DataTimeSeriesPoints `json:"datapoints"`
|
DataPoints plugins.DataTimeSeriesPoints `json:"datapoints"`
|
||||||
|
Tags map[string]string `json:"tags"`
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user