Prometheus: Update FrameType and make __name__ the field name (#62694)
These changes would make the response more in line with the dataplane contract, changes are under the feature toggle prometheusDataplane
This commit is contained in:
@@ -454,5 +454,11 @@ var (
|
||||
State: FeatureStateBeta,
|
||||
Owner: grafanaObservabilityLogsSquad,
|
||||
},
|
||||
{
|
||||
Name: "prometheusDataplane",
|
||||
Description: "Changes responses to from Prometheus to be compliant with the dataplane specification. In particular it sets the numeric Field.Name from 'Value' to the value of the `__name__` label when present.",
|
||||
State: FeatureStateAlpha,
|
||||
Owner: grafanaObservabilityMetricsSquad,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
@@ -67,3 +67,4 @@ timeSeriesTable,alpha,@grafana/app-o11y,false,false,false,true
|
||||
influxdbBackendMigration,alpha,@grafana/observability-metrics,false,false,false,true
|
||||
clientTokenRotation,alpha,@grafana/grafana-authnz-team,false,false,false,false
|
||||
disableElasticsearchBackendExploreQuery,beta,@grafana/observability-logs,false,false,false,false
|
||||
prometheusDataplane,alpha,@grafana/observability-metrics,false,false,false,false
|
||||
|
||||
|
@@ -278,4 +278,8 @@ const (
|
||||
// FlagDisableElasticsearchBackendExploreQuery
|
||||
// Disable executing of Elasticsearch Explore queries trough backend
|
||||
FlagDisableElasticsearchBackendExploreQuery = "disableElasticsearchBackendExploreQuery"
|
||||
|
||||
// FlagPrometheusDataplane
|
||||
// Changes responses to from Prometheus to be compliant with the dataplane specification. In particular it sets the numeric Field.Name from 'Value' to the value of the `__name__` label when present.
|
||||
FlagPrometheusDataplane = "prometheusDataplane"
|
||||
)
|
||||
|
||||
@@ -43,6 +43,7 @@ type QueryData struct {
|
||||
URL string
|
||||
TimeInterval string
|
||||
enableWideSeries bool
|
||||
enableDataplane bool
|
||||
exemplarSampler func() exemplar.Sampler
|
||||
}
|
||||
|
||||
@@ -82,6 +83,7 @@ func New(
|
||||
ID: settings.ID,
|
||||
URL: settings.URL,
|
||||
enableWideSeries: features.IsEnabled(featuremgmt.FlagPrometheusWideSeries),
|
||||
enableDataplane: features.IsEnabled(featuremgmt.FlagPrometheusDataplane),
|
||||
exemplarSampler: exemplarSampler,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ func (s *QueryData) parseResponse(ctx context.Context, q *models.Query, res *htt
|
||||
r := converter.ReadPrometheusStyleResult(iter, converter.Options{
|
||||
MatrixWideSeries: s.enableWideSeries,
|
||||
VectorWideSeries: s.enableWideSeries,
|
||||
Dataplane: s.enableDataplane,
|
||||
})
|
||||
|
||||
// Add frame to attach metadata
|
||||
|
||||
@@ -21,6 +21,7 @@ func logf(format string, a ...interface{}) {
|
||||
type Options struct {
|
||||
MatrixWideSeries bool
|
||||
VectorWideSeries bool
|
||||
Dataplane bool
|
||||
}
|
||||
|
||||
// ReadPrometheusStyleResult will read results from a prometheus or loki server and return data frames
|
||||
@@ -115,15 +116,15 @@ func readPrometheusData(iter *jsoniter.Iterator, opt Options) backend.DataRespon
|
||||
switch resultType {
|
||||
case "matrix":
|
||||
if opt.MatrixWideSeries {
|
||||
rsp = readMatrixOrVectorWide(iter, resultType)
|
||||
rsp = readMatrixOrVectorWide(iter, resultType, opt)
|
||||
} else {
|
||||
rsp = readMatrixOrVectorMulti(iter, resultType)
|
||||
rsp = readMatrixOrVectorMulti(iter, resultType, opt)
|
||||
}
|
||||
case "vector":
|
||||
if opt.VectorWideSeries {
|
||||
rsp = readMatrixOrVectorWide(iter, resultType)
|
||||
rsp = readMatrixOrVectorWide(iter, resultType, opt)
|
||||
} else {
|
||||
rsp = readMatrixOrVectorMulti(iter, resultType)
|
||||
rsp = readMatrixOrVectorMulti(iter, resultType, opt)
|
||||
}
|
||||
case "streams":
|
||||
rsp = readStream(iter)
|
||||
@@ -355,7 +356,7 @@ func readScalar(iter *jsoniter.Iterator) backend.DataResponse {
|
||||
|
||||
frame := data.NewFrame("", timeField, valueField)
|
||||
frame.Meta = &data.FrameMeta{
|
||||
Type: data.FrameTypeTimeSeriesMulti,
|
||||
Type: data.FrameTypeNumericMulti,
|
||||
Custom: resultTypeToCustomMeta("scalar"),
|
||||
}
|
||||
|
||||
@@ -364,16 +365,25 @@ func readScalar(iter *jsoniter.Iterator) backend.DataResponse {
|
||||
}
|
||||
}
|
||||
|
||||
func readMatrixOrVectorWide(iter *jsoniter.Iterator, resultType string) backend.DataResponse {
|
||||
func readMatrixOrVectorWide(iter *jsoniter.Iterator, resultType string, opt Options) backend.DataResponse {
|
||||
rowIdx := 0
|
||||
timeMap := map[int64]int{}
|
||||
timeField := data.NewFieldFromFieldType(data.FieldTypeTime, 0)
|
||||
timeField.Name = data.TimeSeriesTimeFieldName
|
||||
frame := data.NewFrame("", timeField)
|
||||
frame.Meta = &data.FrameMeta{
|
||||
|
||||
frame.Meta = &data.FrameMeta{ // Overwritten if histogram
|
||||
Type: data.FrameTypeTimeSeriesWide,
|
||||
Custom: resultTypeToCustomMeta(resultType),
|
||||
}
|
||||
|
||||
if opt.Dataplane && resultType == "vector" {
|
||||
frame.Meta.Type = data.FrameTypeNumericWide
|
||||
}
|
||||
if opt.Dataplane {
|
||||
frame.Meta.TypeVersion = data.FrameTypeVersion{0, 1}
|
||||
}
|
||||
|
||||
rsp := backend.DataResponse{
|
||||
Frames: []*data.Frame{},
|
||||
}
|
||||
@@ -390,6 +400,11 @@ func readMatrixOrVectorWide(iter *jsoniter.Iterator, resultType string) backend.
|
||||
switch l1Field {
|
||||
case "metric":
|
||||
iter.ReadVal(&valueField.Labels)
|
||||
if opt.Dataplane {
|
||||
if n, ok := valueField.Labels["__name__"]; ok {
|
||||
valueField.Name = n
|
||||
}
|
||||
}
|
||||
|
||||
case "value":
|
||||
timeMap, rowIdx = addValuePairToFrame(frame, timeMap, rowIdx, iter)
|
||||
@@ -472,7 +487,7 @@ func addValuePairToFrame(frame *data.Frame, timeMap map[int64]int, rowIdx int, i
|
||||
return timeMap, rowIdx
|
||||
}
|
||||
|
||||
func readMatrixOrVectorMulti(iter *jsoniter.Iterator, resultType string) backend.DataResponse {
|
||||
func readMatrixOrVectorMulti(iter *jsoniter.Iterator, resultType string, opt Options) backend.DataResponse {
|
||||
rsp := backend.DataResponse{}
|
||||
|
||||
for iter.ReadArray() {
|
||||
@@ -488,6 +503,11 @@ func readMatrixOrVectorMulti(iter *jsoniter.Iterator, resultType string) backend
|
||||
switch l1Field {
|
||||
case "metric":
|
||||
iter.ReadVal(&valueField.Labels)
|
||||
if opt.Dataplane {
|
||||
if n, ok := valueField.Labels["__name__"]; ok {
|
||||
valueField.Name = n
|
||||
}
|
||||
}
|
||||
|
||||
case "value":
|
||||
t, v, err := readTimeValuePair(iter)
|
||||
@@ -548,6 +568,12 @@ func readMatrixOrVectorMulti(iter *jsoniter.Iterator, resultType string) backend
|
||||
Type: data.FrameTypeTimeSeriesMulti,
|
||||
Custom: resultTypeToCustomMeta(resultType),
|
||||
}
|
||||
if opt.Dataplane && resultType == "vector" {
|
||||
frame.Meta.Type = data.FrameTypeNumericMulti
|
||||
}
|
||||
if opt.Dataplane {
|
||||
frame.Meta.TypeVersion = data.FrameTypeVersion{0, 1}
|
||||
}
|
||||
rsp.Frames = append(rsp.Frames, frame)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user