2017-03-29 15:54:07 -05:00
|
|
|
package mysql
|
|
|
|
|
|
|
|
import (
|
2017-03-30 06:46:46 -05:00
|
|
|
"database/sql"
|
2017-03-29 15:54:07 -05:00
|
|
|
"fmt"
|
2019-03-15 05:53:30 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2018-01-18 04:44:36 -06:00
|
|
|
"reflect"
|
2017-03-31 04:45:25 -05:00
|
|
|
"strconv"
|
2018-10-17 04:45:06 -05:00
|
|
|
"strings"
|
2019-01-15 06:29:56 -06:00
|
|
|
|
2017-04-21 08:07:43 -05:00
|
|
|
"github.com/go-sql-driver/mysql"
|
2017-03-31 04:45:25 -05:00
|
|
|
"github.com/go-xorm/core"
|
2019-05-13 01:45:54 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2017-03-29 15:54:07 -05:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
|
|
|
"github.com/grafana/grafana/pkg/tsdb"
|
|
|
|
)
|
|
|
|
|
2017-03-30 06:46:46 -05:00
|
|
|
func init() {
|
2018-07-26 11:10:45 -05:00
|
|
|
tsdb.RegisterTsdbQueryEndpoint("mysql", newMysqlQueryEndpoint)
|
2017-03-30 06:46:46 -05:00
|
|
|
}
|
|
|
|
|
2018-07-26 11:10:45 -05:00
|
|
|
func newMysqlQueryEndpoint(datasource *models.DataSource) (tsdb.TsdbQueryEndpoint, error) {
|
|
|
|
logger := log.New("tsdb.mysql")
|
2017-03-29 15:54:07 -05:00
|
|
|
|
2018-10-17 12:45:14 -05:00
|
|
|
protocol := "tcp"
|
2018-10-17 04:45:06 -05:00
|
|
|
if strings.HasPrefix(datasource.Url, "/") {
|
|
|
|
protocol = "unix"
|
|
|
|
}
|
2017-11-21 06:21:19 -06:00
|
|
|
cnnstr := fmt.Sprintf("%s:%s@%s(%s)/%s?collation=utf8mb4_unicode_ci&parseTime=true&loc=UTC&allowNativePasswords=true",
|
2017-10-10 08:19:14 -05:00
|
|
|
datasource.User,
|
2019-04-15 04:11:17 -05:00
|
|
|
datasource.DecryptedPassword(),
|
2018-10-17 04:45:06 -05:00
|
|
|
protocol,
|
2017-10-10 08:19:14 -05:00
|
|
|
datasource.Url,
|
|
|
|
datasource.Database,
|
|
|
|
)
|
2019-01-15 06:29:56 -06:00
|
|
|
|
2019-01-28 12:38:56 -06:00
|
|
|
tlsConfig, err := datasource.GetTLSConfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-01-15 06:29:56 -06:00
|
|
|
}
|
|
|
|
|
2019-01-28 12:38:56 -06:00
|
|
|
if tlsConfig.RootCAs != nil || len(tlsConfig.Certificates) > 0 {
|
2019-02-08 11:08:07 -06:00
|
|
|
tlsConfigString := fmt.Sprintf("ds%d", datasource.Id)
|
|
|
|
mysql.RegisterTLSConfig(tlsConfigString, tlsConfig)
|
|
|
|
cnnstr += "&tls=" + tlsConfigString
|
2019-01-15 06:29:56 -06:00
|
|
|
}
|
|
|
|
|
2019-03-15 05:53:30 -05:00
|
|
|
if setting.Env == setting.DEV {
|
|
|
|
logger.Debug("getEngine", "connection", cnnstr)
|
|
|
|
}
|
2017-03-30 06:46:46 -05:00
|
|
|
|
2018-07-26 11:10:45 -05:00
|
|
|
config := tsdb.SqlQueryEndpointConfiguration{
|
|
|
|
DriverName: "mysql",
|
|
|
|
ConnectionString: cnnstr,
|
|
|
|
Datasource: datasource,
|
|
|
|
TimeColumnNames: []string{"time", "time_sec"},
|
|
|
|
MetricColumnTypes: []string{"CHAR", "VARCHAR", "TINYTEXT", "TEXT", "MEDIUMTEXT", "LONGTEXT"},
|
2017-03-29 15:54:07 -05:00
|
|
|
}
|
|
|
|
|
2018-07-26 11:10:45 -05:00
|
|
|
rowTransformer := mysqlRowTransformer{
|
|
|
|
log: logger,
|
2017-04-19 10:26:29 -05:00
|
|
|
}
|
2017-04-21 08:07:43 -05:00
|
|
|
|
2018-07-26 11:10:45 -05:00
|
|
|
return tsdb.NewSqlQueryEndpoint(&config, &rowTransformer, newMysqlMacroEngine(), logger)
|
2017-04-21 08:07:43 -05:00
|
|
|
}
|
|
|
|
|
2018-07-26 11:10:45 -05:00
|
|
|
type mysqlRowTransformer struct {
|
|
|
|
log log.Logger
|
|
|
|
}
|
2018-01-18 04:44:36 -06:00
|
|
|
|
2018-07-26 11:10:45 -05:00
|
|
|
func (t *mysqlRowTransformer) Transform(columnTypes []*sql.ColumnType, rows *core.Rows) (tsdb.RowValues, error) {
|
|
|
|
values := make([]interface{}, len(columnTypes))
|
2017-04-21 08:07:43 -05:00
|
|
|
|
2018-01-18 04:44:36 -06:00
|
|
|
for i := range values {
|
2018-07-26 11:10:45 -05:00
|
|
|
scanType := columnTypes[i].ScanType()
|
2018-01-18 04:44:36 -06:00
|
|
|
values[i] = reflect.New(scanType).Interface()
|
|
|
|
|
2018-07-26 11:10:45 -05:00
|
|
|
if columnTypes[i].DatabaseTypeName() == "BIT" {
|
2017-08-08 05:00:39 -05:00
|
|
|
values[i] = new([]byte)
|
2017-04-21 08:07:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := rows.Scan(values...); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-26 11:10:45 -05:00
|
|
|
for i := 0; i < len(columnTypes); i++ {
|
2018-01-18 04:44:36 -06:00
|
|
|
typeName := reflect.ValueOf(values[i]).Type().String()
|
|
|
|
|
|
|
|
switch typeName {
|
|
|
|
case "*sql.RawBytes":
|
|
|
|
values[i] = string(*values[i].(*sql.RawBytes))
|
|
|
|
case "*mysql.NullTime":
|
|
|
|
sqlTime := (*values[i].(*mysql.NullTime))
|
|
|
|
if sqlTime.Valid {
|
|
|
|
values[i] = sqlTime.Time
|
|
|
|
} else {
|
|
|
|
values[i] = nil
|
|
|
|
}
|
|
|
|
case "*sql.NullInt64":
|
|
|
|
nullInt64 := (*values[i].(*sql.NullInt64))
|
|
|
|
if nullInt64.Valid {
|
|
|
|
values[i] = nullInt64.Int64
|
|
|
|
} else {
|
|
|
|
values[i] = nil
|
|
|
|
}
|
|
|
|
case "*sql.NullFloat64":
|
|
|
|
nullFloat64 := (*values[i].(*sql.NullFloat64))
|
|
|
|
if nullFloat64.Valid {
|
|
|
|
values[i] = nullFloat64.Float64
|
|
|
|
} else {
|
|
|
|
values[i] = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-26 11:10:45 -05:00
|
|
|
if columnTypes[i].DatabaseTypeName() == "DECIMAL" {
|
2018-01-18 04:44:36 -06:00
|
|
|
f, err := strconv.ParseFloat(values[i].(string), 64)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
values[i] = f
|
|
|
|
} else {
|
|
|
|
values[i] = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-21 08:07:43 -05:00
|
|
|
return values, nil
|
2017-04-19 10:26:29 -05:00
|
|
|
}
|