Просмотр исходного кода

treat any text column in timeseries query as metric name unless column (#9985)

named metric is present
Sven Klemm 8 лет назад
Родитель
Сommit
373389c920
1 измененных файлов с 14 добавлено и 1 удалено
  1. 14 1
      pkg/tsdb/postgres/postgres.go

+ 14 - 1
pkg/tsdb/postgres/postgres.go

@@ -142,8 +142,13 @@ func (e PostgresQueryEndpoint) getTypedRowData(rows *core.Rows) (tsdb.RowValues,
 func (e PostgresQueryEndpoint) transformToTimeSeries(query *tsdb.Query, rows *core.Rows, result *tsdb.QueryResult) error {
 func (e PostgresQueryEndpoint) transformToTimeSeries(query *tsdb.Query, rows *core.Rows, result *tsdb.QueryResult) error {
 	pointsBySeries := make(map[string]*tsdb.TimeSeries)
 	pointsBySeries := make(map[string]*tsdb.TimeSeries)
 	seriesByQueryOrder := list.New()
 	seriesByQueryOrder := list.New()
+
 	columnNames, err := rows.Columns()
 	columnNames, err := rows.Columns()
+	if err != nil {
+		return err
+	}
 
 
+	columnTypes, err := rows.ColumnTypes()
 	if err != nil {
 	if err != nil {
 		return err
 		return err
 	}
 	}
@@ -153,13 +158,21 @@ func (e PostgresQueryEndpoint) transformToTimeSeries(query *tsdb.Query, rows *co
 	timeIndex := -1
 	timeIndex := -1
 	metricIndex := -1
 	metricIndex := -1
 
 
-	// check columns of resultset
+	// check columns of resultset: a column named time is mandatory
+	// the first text column is treated as metric name unless a column named metric is present
 	for i, col := range columnNames {
 	for i, col := range columnNames {
 		switch col {
 		switch col {
 		case "time":
 		case "time":
 			timeIndex = i
 			timeIndex = i
 		case "metric":
 		case "metric":
 			metricIndex = i
 			metricIndex = i
+		default:
+			if metricIndex == -1 {
+				switch columnTypes[i].DatabaseTypeName() {
+				case "UNKNOWN", "TEXT", "VARCHAR", "CHAR":
+					metricIndex = i
+				}
+			}
 		}
 		}
 	}
 	}