From 77cb47773fd57feeaafd01e252390534ac663752 Mon Sep 17 00:00:00 2001 From: ismail simsek Date: Wed, 23 Oct 2024 16:07:00 +0200 Subject: [PATCH] Prometheus: Fix field name setting for heatmap-cells type (#95097) * add unit test * ignore setting valueField name if the frame type is heatmap-cells * return early --- pkg/promlib/querydata/response.go | 6 ++++++ pkg/promlib/querydata/response_test.go | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/pkg/promlib/querydata/response.go b/pkg/promlib/querydata/response.go index b1cfec8e7fa..93cff2080dc 100644 --- a/pkg/promlib/querydata/response.go +++ b/pkg/promlib/querydata/response.go @@ -118,6 +118,12 @@ func addMetadataToMultiFrame(q *models.Query, frame *data.Frame) { frame.Fields[1].Config = &data.FieldConfig{DisplayNameFromDS: customName} } + // For heatmap-cells type we don't want to set field name + // prometheus native histograms have their own field name structure + if frame.Meta.Type == "heatmap-cells" { + return + } + valueField := frame.Fields[1] if n, ok := valueField.Labels["__name__"]; ok { valueField.Name = n diff --git a/pkg/promlib/querydata/response_test.go b/pkg/promlib/querydata/response_test.go index 5c7a2e6c555..6d12465fee8 100644 --- a/pkg/promlib/querydata/response_test.go +++ b/pkg/promlib/querydata/response_test.go @@ -56,3 +56,15 @@ func TestQueryData_parseResponse(t *testing.T) { assert.Equal(t, result.Error.Error(), "unknown result type: ") }) } + +func TestAddMetadataToMultiFrame(t *testing.T) { + t.Run("when you have native histogram result", func(t *testing.T) { + qd := QueryData{exemplarSampler: exemplar.NewStandardDeviationSampler} + resBody := `{"status":"success","data":{"resultType":"matrix","result":[{"metric":{"__name__":"rpc_durations_native_histogram_seconds","instance":"nativehisto:8080","job":"prometheus"},"histograms":[[1729529685,{"count":"7243102","sum":"72460202.93145595","buckets":[[0,"1.8340080864093422","2","10"],[0,"2","2.1810154653305154","68"]]}],[1729529700,{"count":"7243490","sum":"72464056.03309634","buckets":[[0,"1.8340080864093422","2","10"],[0,"2","2.1810154653305154","68"]]}],[1729529715,{"count":"7243880","sum":"72467935.35871512","buckets":[[0,"1.8340080864093422","2","10"],[0,"2","2.1810154653305154","68"]]}]]}]}}` + res := &http.Response{Body: io.NopCloser(bytes.NewBufferString(resBody))} + result := qd.parseResponse(context.Background(), &models.Query{}, res) + assert.Nil(t, result.Error) + assert.Len(t, result.Frames, 1) + assert.Equal(t, "yMin", result.Frames[0].Fields[1].Name) + }) +}