Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove datasource option from SQL module and add tests #15686

Merged
merged 9 commits into from
Jan 21, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions metricbeat/docs/modules/sql.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This file is generated! See scripts/mage/docs_collector.go

beta[]

This is the sql module that fetches metrics from a SQL database. You can define driver, datasource and SQL query.
This is the sql module that fetches metrics from a SQL database. You can define driver and SQL query.



Expand All @@ -26,10 +26,9 @@ metricbeat.modules:
metricsets:
- query
period: 10s
hosts: ["localhost"]
hosts: ["user=myuser password=mypassword dbname=mydb sslmode=disable"]

driver: "postgres"
datasource: "user=myuser password=mypassword dbname=mydb sslmode=disable"
sql_query: "select now()"

----
Expand Down
3 changes: 1 addition & 2 deletions x-pack/metricbeat/metricbeat.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -965,10 +965,9 @@ metricbeat.modules:
metricsets:
- query
period: 10s
hosts: ["localhost"]
hosts: ["user=myuser password=mypassword dbname=mydb sslmode=disable"]

driver: "postgres"
datasource: "user=myuser password=mypassword dbname=mydb sslmode=disable"
sql_query: "select now()"


Expand Down
3 changes: 1 addition & 2 deletions x-pack/metricbeat/module/sql/_meta/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
metricsets:
- query
period: 10s
hosts: ["localhost"]
hosts: ["user=myuser password=mypassword dbname=mydb sslmode=disable"]

driver: "postgres"
datasource: "user=myuser password=mypassword dbname=mydb sslmode=disable"
sql_query: "select now()"

2 changes: 1 addition & 1 deletion x-pack/metricbeat/module/sql/_meta/docs.asciidoc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
This is the sql module that fetches metrics from a SQL database. You can define driver, datasource and SQL query.
This is the sql module that fetches metrics from a SQL database. You can define driver and SQL query.


12 changes: 12 additions & 0 deletions x-pack/metricbeat/module/sql/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: '2.3'

services:
mysql:
extends:
file: ../../../../metricbeat/module/mysql/docker-compose.yml
service: mysql

postgresql:
extends:
file: ../../../../metricbeat/module/postgresql/docker-compose.yml
service: postgresql
46 changes: 25 additions & 21 deletions x-pack/metricbeat/module/sql/query/_meta/data.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
{
"@timestamp":"2016-05-23T08:05:34.853Z",
"beat":{
"hostname":"beathost",
"name":"beathost"
"@timestamp": "2017-10-12T08:05:34.853Z",
"event": {
"dataset": "sql.query",
"duration": 115000,
"module": "sql"
},
"metricset":{
"host":"localhost",
"module":"sql",
"name":"query",
"rtt":44269
"metricset": {
"name": "query",
"period": 10000
},
"sql":{
"metrics":{
"numeric":{
"mynumericfield":1
},
"string":{
"mystringfield":"abc"
}
"service": {
"address": "root:test@tcp(172.22.0.3:3306)/",
jsoriano marked this conversation as resolved.
Show resolved Hide resolved
"type": "sql"
},
"sql": {
"driver": "mysql",
"metrics": {
"numeric": {
"table_rows": 6
},
"string": {
"engine": "InnoDB",
"table_name": "sys_config",
"table_schema": "sys"
}
},
"driver":"postgres",
"query":"select * from mytable"
},
"type":"metricsets"
"query": "select table_schema, table_name, engine, table_rows from information_schema.tables where table_rows \u003e 0;"
}
}
45 changes: 45 additions & 0 deletions x-pack/metricbeat/module/sql/query/_meta/data_postgres.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"@timestamp": "2017-10-12T08:05:34.853Z",
"event": {
"dataset": "sql.query",
"duration": 115000,
"module": "sql"
},
"metricset": {
"name": "query",
"period": 10000
},
"service": {
"address": "user=postgres password=postgres sslmode=disable host=172.22.0.2 port=5432",
"type": "sql"
},
"sql": {
"driver": "postgres",
"metrics": {
"numeric": {
"blk_read_time": 0,
"blk_write_time": 0,
"blks_hit": 4251,
"blks_read": 103,
"conflicts": 0,
"datid": 12379,
"deadlocks": 0,
"numbackends": 1,
"temp_bytes": 0,
"temp_files": 0,
"tup_deleted": 0,
"tup_fetched": 2847,
"tup_inserted": 0,
"tup_returned": 4877,
"tup_updated": 0,
"xact_commit": 49,
"xact_rollback": 0
},
"string": {
"datname": "postgres",
"stats_reset": "2020-01-20 19:48:28.217"
}
},
"query": "select * from pg_stat_database"
}
}
20 changes: 11 additions & 9 deletions x-pack/metricbeat/module/sql/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/common/cfgwarn"
"github.com/elastic/beats/metricbeat/mb"
"github.com/elastic/beats/metricbeat/mb/parse"

"github.com/jmoiron/sqlx"
)
Expand All @@ -24,7 +25,9 @@ import (
// the MetricSet for each host defined in the module's configuration. After the
// MetricSet has been created then Fetch will begin to be called periodically.
func init() {
mb.Registry.MustAddMetricSet("sql", "query", New)
mb.Registry.MustAddMetricSet("sql", "query", New,
mb.WithHostParser(parse.PassThruHostParser),
)
}

// MetricSet holds any configuration or state information. It must implement
Expand All @@ -33,9 +36,10 @@ func init() {
// interface methods except for Fetch.
type MetricSet struct {
mb.BaseMetricSet
Driver string
Datasource string
Query string
Driver string
Query string

db *sqlx.DB
}

// New creates a new instance of the MetricSet. New is responsible for unpacking
Expand All @@ -44,9 +48,8 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
cfgwarn.Beta("The sql query metricset is beta.")

config := struct {
Driver string `config:"driver"`
Datasource string `config:"datasource"`
Query string `config:"sql_query"`
Driver string `config:"driver"`
Query string `config:"sql_query"`
}{}

if err := base.Module().UnpackConfig(&config); err != nil {
Expand All @@ -56,7 +59,6 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
return &MetricSet{
BaseMetricSet: base,
Driver: config.Driver,
Datasource: config.Datasource,
Query: config.Query,
}, nil
}
Expand All @@ -65,7 +67,7 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
// format. It publishes the event which is then forwarded to the output. In case
// of an error set the Error field of mb.Event or simply call report.Error().
func (m *MetricSet) Fetch(report mb.ReporterV2) error {
db, err := sqlx.Open(m.Driver, m.Datasource)
db, err := sqlx.Open(m.Driver, m.HostData().URI)
if err != nil {
return errors.Wrap(err, "error opening connection")
}
Expand Down
89 changes: 89 additions & 0 deletions x-pack/metricbeat/module/sql/query/query_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// +build integration

package query

import (
"fmt"
"net"
"testing"

"github.com/stretchr/testify/require"

// Drivers
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"

"github.com/elastic/beats/libbeat/tests/compose"
mbtest "github.com/elastic/beats/metricbeat/mb/testing"
"github.com/elastic/beats/metricbeat/module/mysql"
"github.com/elastic/beats/metricbeat/module/postgresql"
)

type testFetchConfig struct {
Driver string
Query string
Host string
}

func TestMySQL(t *testing.T) {
service := compose.EnsureUp(t, "mysql")
config := testFetchConfig{
Driver: "mysql",
Query: "select table_schema, table_name, engine, table_rows from information_schema.tables where table_rows > 0;",
Host: mysql.GetMySQLEnvDSN(service.Host()),
}

t.Run("fetch", func(t *testing.T) {
testFetch(t, config)
})

t.Run("data", func(t *testing.T) {
testData(t, config, "")
})
}

func TestPostgreSQL(t *testing.T) {
service := compose.EnsureUp(t, "postgresql")
host, port, err := net.SplitHostPort(service.Host())
require.NoError(t, err)

user := postgresql.GetEnvUsername()
password := postgresql.GetEnvPassword()

config := testFetchConfig{
Driver: "postgres",
Query: "select * from pg_stat_database",
Host: fmt.Sprintf("user=%s password=%s sslmode=disable host=%s port=%s", user, password, host, port),
}

t.Run("fetch", func(t *testing.T) {
testFetch(t, config)
})

t.Run("data", func(t *testing.T) {
testData(t, config, "./_meta/data_postgres.json")
})
}

func testFetch(t *testing.T, cfg testFetchConfig) {
m := mbtest.NewFetcher(t, getConfig(cfg))
events, errs := m.FetchEvents()
require.Empty(t, errs)
require.NotEmpty(t, events)
t.Logf("%s/%s event: %+v", m.Module().Name(), m.Name(), events[0])
}

func testData(t *testing.T, cfg testFetchConfig, postfix string) {
m := mbtest.NewFetcher(t, getConfig(cfg))
m.WriteEvents(t, postfix)
}

func getConfig(cfg testFetchConfig) map[string]interface{} {
return map[string]interface{}{
"module": "sql",
"metricsets": []string{"query"},
"hosts": []string{cfg.Host},
"driver": cfg.Driver,
"sql_query": cfg.Query,
}
}
3 changes: 1 addition & 2 deletions x-pack/metricbeat/modules.d/sql.yml.disabled
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
metricsets:
- query
period: 10s
hosts: ["localhost"]
hosts: ["user=myuser password=mypassword dbname=mydb sslmode=disable"]

driver: "postgres"
datasource: "user=myuser password=mypassword dbname=mydb sslmode=disable"
sql_query: "select now()"