forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove datasource option from SQL module and add tests (elastic#15686)
Remove datasource option from SQL module. This option was intended to set the DSN of a database connection, and we were ignoring the hosts setting. In other SQL modules we are using the values in hosts as DSNs, do here the same for consistency. Host is redacted when we cannot parse it as it can contain passwords. StandardizeEvent is exposed in mbtest.Fetcher interface so we can more easily check contents of events in tests. Add integration tests of the module with MySQL and PostgreSQL. Add real data.json with data from MySQL and PostgreSQL.
- Loading branch information
Showing
12 changed files
with
279 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": "172.22.0.3:3306", | ||
"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
45
x-pack/metricbeat/module/sql/query/_meta/data_postgres.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": "172.22.0.2:5432", | ||
"type": "sql" | ||
}, | ||
"sql": { | ||
"driver": "postgres", | ||
"metrics": { | ||
"numeric": { | ||
"blk_read_time": 0, | ||
"blk_write_time": 0, | ||
"blks_hit": 1923, | ||
"blks_read": 111, | ||
"conflicts": 0, | ||
"datid": 12379, | ||
"deadlocks": 0, | ||
"numbackends": 1, | ||
"temp_bytes": 0, | ||
"temp_files": 0, | ||
"tup_deleted": 0, | ||
"tup_fetched": 1249, | ||
"tup_inserted": 0, | ||
"tup_returned": 1356, | ||
"tup_updated": 0, | ||
"xact_commit": 18, | ||
"xact_rollback": 0 | ||
}, | ||
"string": { | ||
"datname": "postgres", | ||
"stats_reset": "2020-01-21 11:23:56.53" | ||
} | ||
}, | ||
"query": "select * from pg_stat_database" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package query | ||
|
||
import ( | ||
"net/url" | ||
|
||
"github.com/go-sql-driver/mysql" | ||
|
||
"github.com/elastic/beats/metricbeat/mb" | ||
) | ||
|
||
// ParseDSN tries to parse the host | ||
func ParseDSN(mod mb.Module, host string) (mb.HostData, error) { | ||
// TODO: Add support for `username` and `password` as module options | ||
|
||
sanitized := sanitize(host) | ||
|
||
return mb.HostData{ | ||
URI: host, | ||
SanitizedURI: sanitized, | ||
Host: sanitized, | ||
}, nil | ||
} | ||
|
||
func sanitize(host string) string { | ||
// Host is a standard URL | ||
if url, err := url.Parse(host); err == nil && len(url.Host) > 0 { | ||
return url.Host | ||
} | ||
|
||
// Host is a MySQL DSN | ||
if config, err := mysql.ParseDSN(host); err == nil { | ||
return config.Addr | ||
} | ||
|
||
// TODO: Add support for PostgreSQL connection strings and other formats | ||
|
||
return "(redacted)" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.