|
|
|
|
@ -2,8 +2,11 @@ package prometheus |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"context" |
|
|
|
|
"encoding/json" |
|
|
|
|
"errors" |
|
|
|
|
"fmt" |
|
|
|
|
"reflect" |
|
|
|
|
"sync" |
|
|
|
|
|
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend" |
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend/datasource" |
|
|
|
|
@ -17,6 +20,8 @@ import ( |
|
|
|
|
"github.com/grafana/grafana/pkg/tsdb/prometheus/querydata" |
|
|
|
|
"github.com/grafana/grafana/pkg/tsdb/prometheus/resource" |
|
|
|
|
apiv1 "github.com/prometheus/client_golang/api/prometheus/v1" |
|
|
|
|
"github.com/yudai/gojsondiff" |
|
|
|
|
"github.com/yudai/gojsondiff/formatter" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
var plog = log.New("tsdb.prometheus") |
|
|
|
|
@ -91,6 +96,36 @@ func (s *Service) QueryData(ctx context.Context, req *backend.QueryDataRequest) |
|
|
|
|
return i.queryData.Execute(ctx, req) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// To test the new client implementation this can be run and we do 2 requests and compare.
|
|
|
|
|
if s.features.IsEnabled(featuremgmt.FlagPrometheusStreamingJSONParserTest) { |
|
|
|
|
var wg sync.WaitGroup |
|
|
|
|
var streamData *backend.QueryDataResponse |
|
|
|
|
var streamError error |
|
|
|
|
|
|
|
|
|
var data *backend.QueryDataResponse |
|
|
|
|
var err error |
|
|
|
|
|
|
|
|
|
plog.Debug("PrometheusStreamingJSONParserTest", "req", req) |
|
|
|
|
|
|
|
|
|
wg.Add(1) |
|
|
|
|
go func() { |
|
|
|
|
defer wg.Done() |
|
|
|
|
streamData, streamError = i.queryData.Execute(ctx, req) |
|
|
|
|
}() |
|
|
|
|
|
|
|
|
|
wg.Add(1) |
|
|
|
|
go func() { |
|
|
|
|
defer wg.Done() |
|
|
|
|
data, err = i.buffered.ExecuteTimeSeriesQuery(ctx, req) |
|
|
|
|
}() |
|
|
|
|
|
|
|
|
|
wg.Wait() |
|
|
|
|
|
|
|
|
|
// Report can take a while and we don't really need to wait for it.
|
|
|
|
|
go reportDiff(data, err, streamData, streamError) |
|
|
|
|
return data, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return i.buffered.ExecuteTimeSeriesQuery(ctx, req) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -131,3 +166,48 @@ func ConvertAPIError(err error) error { |
|
|
|
|
} |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func reportDiff(data *backend.QueryDataResponse, err error, streamData *backend.QueryDataResponse, streamError error) { |
|
|
|
|
if err == nil && streamError != nil { |
|
|
|
|
plog.Debug("PrometheusStreamingJSONParserTest error in streaming client", "err", streamError) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if err != nil && streamError == nil { |
|
|
|
|
plog.Debug("PrometheusStreamingJSONParserTest error in buffer but not streaming", "err", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(data, streamData) { |
|
|
|
|
plog.Debug("PrometheusStreamingJSONParserTest buffer and streaming data are different") |
|
|
|
|
dataJson, jsonErr := json.MarshalIndent(data, "", "\t") |
|
|
|
|
if jsonErr != nil { |
|
|
|
|
plog.Debug("PrometheusStreamingJSONParserTest error marshaling data", "jsonErr", jsonErr) |
|
|
|
|
} |
|
|
|
|
streamingJson, jsonErr := json.MarshalIndent(streamData, "", "\t") |
|
|
|
|
if jsonErr != nil { |
|
|
|
|
plog.Debug("PrometheusStreamingJSONParserTest error marshaling streaming data", "jsonErr", jsonErr) |
|
|
|
|
} |
|
|
|
|
differ := gojsondiff.New() |
|
|
|
|
d, diffErr := differ.Compare(dataJson, streamingJson) |
|
|
|
|
if diffErr != nil { |
|
|
|
|
plog.Debug("PrometheusStreamingJSONParserTest diff error", "err", diffErr) |
|
|
|
|
} |
|
|
|
|
config := formatter.AsciiFormatterConfig{ |
|
|
|
|
ShowArrayIndex: true, |
|
|
|
|
Coloring: true, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var aJson map[string]interface{} |
|
|
|
|
unmarshallErr := json.Unmarshal(dataJson, &aJson) |
|
|
|
|
if unmarshallErr != nil { |
|
|
|
|
plog.Debug("PrometheusStreamingJSONParserTest unmarshall error", "err", unmarshallErr) |
|
|
|
|
} |
|
|
|
|
formatter := formatter.NewAsciiFormatter(aJson, config) |
|
|
|
|
diffString, diffErr := formatter.Format(d) |
|
|
|
|
if diffErr != nil { |
|
|
|
|
plog.Debug("PrometheusStreamingJSONParserTest diff format error", "err", diffErr) |
|
|
|
|
} |
|
|
|
|
fmt.Println(diffString) |
|
|
|
|
} else { |
|
|
|
|
plog.Debug("PrometheusStreamingJSONParserTest responses are the same") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|