From 1d6e99bc95b8844b4e1bf83bcf9327f2a9c30d45 Mon Sep 17 00:00:00 2001 From: Ryan McKinley Date: Thu, 17 Jun 2021 23:05:23 -0700 Subject: [PATCH] SQL: Fixes issues with showing value column name prefix in legends (#35839) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * rename strategy * Update pkg/tsdb/sqleng/sql_engine.go Co-authored-by: Torkel Ödegaard * more strict constraints * Fixed so that it works on multi series results * only apply the logic when original query returns 3 fields * removed part of comment * Update mysql test Co-authored-by: Torkel Ödegaard Co-authored-by: Torkel Ödegaard --- pkg/tsdb/mysql/mysql_test.go | 4 ++-- pkg/tsdb/postgres/postgres_test.go | 4 ++-- pkg/tsdb/sqleng/sql_engine.go | 16 ++++++++++++++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/pkg/tsdb/mysql/mysql_test.go b/pkg/tsdb/mysql/mysql_test.go index b0025ae14e7..dd848138b05 100644 --- a/pkg/tsdb/mysql/mysql_test.go +++ b/pkg/tsdb/mysql/mysql_test.go @@ -732,8 +732,8 @@ func TestMySQL(t *testing.T) { frames, _ := queryResult.Dataframes.Decoded() require.Len(t, frames, 1) require.Len(t, frames[0].Fields, 3) - require.Equal(t, data.Labels{"metric": "Metric A - value one"}, frames[0].Fields[1].Labels) - require.Equal(t, data.Labels{"metric": "Metric B - value one"}, frames[0].Fields[2].Labels) + require.Equal(t, "Metric A - value one", frames[0].Fields[1].Name) + require.Equal(t, "Metric B - value one", frames[0].Fields[2].Name) }) t.Run("When doing a metric query with metric column and multiple value columns", func(t *testing.T) { diff --git a/pkg/tsdb/postgres/postgres_test.go b/pkg/tsdb/postgres/postgres_test.go index bc64256c32f..7d9ca7e739e 100644 --- a/pkg/tsdb/postgres/postgres_test.go +++ b/pkg/tsdb/postgres/postgres_test.go @@ -795,8 +795,8 @@ func TestPostgres(t *testing.T) { frames, _ := queryResult.Dataframes.Decoded() require.Equal(t, 1, len(frames)) require.Equal(t, 3, len(frames[0].Fields)) - require.Equal(t, data.Labels{"metric": "Metric A - value one"}, frames[0].Fields[1].Labels) - require.Equal(t, data.Labels{"metric": "Metric B - value one"}, frames[0].Fields[2].Labels) + require.Equal(t, "Metric A - value one", frames[0].Fields[1].Name) + require.Equal(t, "Metric B - value one", frames[0].Fields[2].Name) }) t.Run("When doing a metric query with metric column and multiple value columns", func(t *testing.T) { diff --git a/pkg/tsdb/sqleng/sql_engine.go b/pkg/tsdb/sqleng/sql_engine.go index 5f8112590c7..20a23c55034 100644 --- a/pkg/tsdb/sqleng/sql_engine.go +++ b/pkg/tsdb/sqleng/sql_engine.go @@ -298,11 +298,27 @@ func (e *dataPlugin) executeQuery(query plugins.DataSubQuery, wg *sync.WaitGroup tsSchema := frame.TimeSeriesSchema() if tsSchema.Type == data.TimeSeriesTypeLong { var err error + originalData := frame frame, err = data.LongToWide(frame, qm.FillMissing) if err != nil { errAppendDebug("failed to convert long to wide series when converting from dataframe", err, interpolatedQuery) return } + + // Before 8x, a special metric column was used to name time series. The LongToWide transforms that into a metric label on the value field. + // But that makes series name have both the value column name AND the metric name. So here we are removing the metric label here and moving it to the + // field name to get the same naming for the series as pre v8 + if len(originalData.Fields) == 3 { + for _, field := range frame.Fields { + if len(field.Labels) == 1 { // 7x only supported one label + name, ok := field.Labels["metric"] + if ok { + field.Name = name + field.Labels = nil + } + } + } + } } if qm.FillMissing != nil { var err error