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
This commit is contained in:
ismail simsek
2024-10-23 16:07:00 +02:00
committed by GitHub
parent 61b9ffd324
commit 77cb47773f
2 changed files with 18 additions and 0 deletions

View File

@@ -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

View File

@@ -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)
})
}