x-pack/metricbeat/module/sql: Add option to execute SQL queries for all databases#35688
Merged
shmsr merged 4 commits intoelastic:mainfrom Aug 10, 2023
Merged
x-pack/metricbeat/module/sql: Add option to execute SQL queries for all databases#35688shmsr merged 4 commits intoelastic:mainfrom
shmsr merged 4 commits intoelastic:mainfrom
Conversation
Contributor
|
This pull request does not have a backport label.
To fixup this pull request, you need to add the backport labels for the needed
|
Contributor
2593f3b to
3275971
Compare
Contributor
lalit-satapathy
left a comment
There was a problem hiding this comment.
- Need description summary of fetch_from_all_db
- If "fetch from all databases feature is not supported for driver" it has to be added to beats user document.
- Need to add a beats testcase for fetch_from_all_db for beats
- Need to see a simple example at beats level what happens when fetch_from_all_db is enabled.
- Interested to see the diff between the docs created for each DB, when fetch_from_all_db is enabled.
d18efee to
56df53a
Compare
muthu-mps
reviewed
Jul 13, 2023
bac4c6a to
e59d7d0
Compare
74ee9db to
5c1a214
Compare
Member
Author
|
Cherry-picked the commit to fix CI errors temporarily. See #36091 for more details. |
df03fab to
c3f4148
Compare
lucian-ioan
reviewed
Aug 2, 2023
60df722 to
7ae6948
Compare
Contributor
|
Pinging @elastic/elastic-agent (Team:Elastic-Agent) |
Member
Author
|
I need someone from the elastic-agent-data-plane team to review this PR. Thanks! |
lalit-satapathy
approved these changes
Aug 9, 2023
Contributor
lalit-satapathy
left a comment
There was a problem hiding this comment.
Approving as the comments here are resolved.
ccaf445 to
7366a5f
Compare
7366a5f to
7476250
Compare
fearful-symmetry
approved these changes
Aug 10, 2023
4 tasks
Scholar-Li
pushed a commit
to Scholar-Li/beats
that referenced
this pull request
Feb 5, 2024
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Add support for executing a set of given queries for all databases present in a server. Currently, this feature is only supported for mssql driver.
It expects a field
fetch_from_all_databasesin the configuration to be set totruein order to enable the feature. This feature executes queries agnostic of database names (i.e., where database names are not already part of the query) for all databases in a server. On every call toFetch, the database names are refreshed and to fire the given set of queries for a particular database,USE @commandstatement is used by prefixing the same to each query when iterating over database names.merge_resultsfeature merges results per database basis and not merging all results from all databases.Please read the comments (
NOTE) in between the code changes to know more about why mssql is only supported and more.In this PR some doc-related updates are also there. Also, handle the exit of metricbeat gracefully by handling return value of reporter.Event.
Why is it important?
Currently, we allow users to manually enter the databases when they are setting up the integration and that allows them to execute the SQL queries required for fetching the desired metrics. But, assuming a user could have 100s of databases on their server and then it becomes cumbersome to add them manually. There should be some easy way to fetch metrics from all databases that are there on the server.
To summarize, we already have a solution in place which enables users to manually feed the user databases along with the already set system databases (as default). But to close this issue we’d ideally want to have a solution that gets all accessible databases on a server.
Checklist
CHANGELOG.next.asciidocorCHANGELOG-developer.next.asciidoc.Author's Checklist
fetch_from_all_databasesset tofalseandtrue.How to test this PR locally
fetch_from_all_dbstotrueand make adjustments to the SQL query, if necessary.Related issues
Logs
For an mssql instance with only 4 databases — master, model, msdb, tempdb; here are some sample documents:
fetch_from_all_databasesisfalse:Configuration file:
Response:
{ "@timestamp": "2023-07-16T19:32:30.906Z", "@metadata": { "beat": "metricbeat", "type": "_doc", "version": "8.10.0" }, "metricset": { "name": "query", "period": 50000 }, "service": { "address": "localhost", "type": "sql" }, "sql": { "driver": "mssql", "query": "SELECT @@servername AS server_name, @@servicename AS instance_name, name As 'database_name', database_id FROM sys.databases WHERE name='master';", "metrics": { "database_id": 1, "server_name": "857e71351450", "instance_name": "MSSQLSERVER", "database_name": "master" } }, "agent": { "name": "host-machine", "type": "metricbeat", "version": "8.10.0", "ephemeral_id": "<redacted>", "id": "<redacted>" }, "ecs": { "version": "8.0.0" }, "host": { "hostname": "host-machine", "name": "host-machine", "architecture": "arm64", "os": { "name": "macOS", "kernel": "22.4.0", "build": "<redacted>", "type": "macos", "platform": "darwin", "version": "13.3.1", "family": "darwin" }, "id": "<redacted>", "ip": [ "<redacted>" ], "mac": [ "<redacted>" ] }, "event": { "dataset": "sql.query", "module": "sql", "duration": 27494708 } }fetch_from_all_databasesistrue:Configuration file:
Response:
{ "@timestamp": "2023-07-16T19:34:15.179Z", "@metadata": { "beat": "metricbeat", "type": "_doc", "version": "8.10.0" }, "agent": { "name": "host-machine", "type": "metricbeat", "version": "8.10.0", "ephemeral_id": "<redacted>", "id": "<redacted>" }, "service": { "address": "localhost", "type": "sql" }, "event": { "duration": 65829541, "dataset": "sql.query", "module": "sql" }, "metricset": { "name": "query", "period": 50000 }, "sql": { "metrics": { "database_id": 1, "server_name": "857e71351450", "instance_name": "MSSQLSERVER", "database_name": "master" }, "driver": "mssql", "query": "USE [master]; SELECT @@servername AS server_name, @@servicename AS instance_name, DB_NAME() AS 'database_name', DB_ID() AS database_id;" }, "ecs": { "version": "8.0.0" }, "host": { "ip": [ "<redacted>" ], "mac": [ "<redacted>" ], "name": "host-machine", "hostname": "host-machine", "architecture": "arm64", "os": { "name": "macOS", "kernel": "22.4.0", "build": "<redacted>", "type": "macos", "platform": "darwin", "version": "13.3.1", "family": "darwin" }, "id": "<redacted>" } } { "@timestamp": "2023-07-16T19:34:15.179Z", "@metadata": { "beat": "metricbeat", "type": "_doc", "version": "8.10.0" }, "agent": { "version": "8.10.0", "ephemeral_id": "<redacted>", "id": "<redacted>", "name": "host-machine", "type": "metricbeat" }, "sql": { "query": "USE [tempdb]; SELECT @@servername AS server_name, @@servicename AS instance_name, DB_NAME() AS 'database_name', DB_ID() AS database_id;", "metrics": { "database_name": "tempdb", "database_id": 2, "server_name": "857e71351450", "instance_name": "MSSQLSERVER" }, "driver": "mssql" }, "event": { "dataset": "sql.query", "module": "sql", "duration": 67029500 }, "metricset": { "period": 50000, "name": "query" }, "service": { "type": "sql", "address": "localhost" }, "ecs": { "version": "8.0.0" }, "host": { "name": "host-machine", "id": "<redacted>", "ip": [ "<redacted>" ], "mac": [ "<redacted>" ], "hostname": "host-machine", "architecture": "arm64", "os": { "version": "13.3.1", "family": "darwin", "name": "macOS", "kernel": "22.4.0", "build": "<redacted>", "type": "macos", "platform": "darwin" } } } { "@timestamp": "2023-07-16T19:34:15.179Z", "@metadata": { "beat": "metricbeat", "type": "_doc", "version": "8.10.0" }, "service": { "address": "localhost", "type": "sql" }, "sql": { "driver": "mssql", "query": "USE [model]; SELECT @@servername AS server_name, @@servicename AS instance_name, DB_NAME() AS 'database_name', DB_ID() AS database_id;", "metrics": { "instance_name": "MSSQLSERVER", "database_name": "model", "database_id": 3, "server_name": "857e71351450" } }, "event": { "dataset": "sql.query", "module": "sql", "duration": 68272250 }, "metricset": { "name": "query", "period": 50000 }, "agent": { "type": "metricbeat", "version": "8.10.0", "ephemeral_id": "<redacted>", "id": "<redacted>", "name": "host-machine" }, "ecs": { "version": "8.0.0" }, "host": { "architecture": "arm64", "os": { "kernel": "22.4.0", "build": "<redacted>", "type": "macos", "platform": "darwin", "version": "13.3.1", "family": "darwin", "name": "macOS" }, "id": "<redacted>", "ip": [ "<redacted>" ], "mac": [ "<redacted>" ], "name": "host-machine", "hostname": "host-machine" } } { "@timestamp": "2023-07-16T19:34:15.179Z", "@metadata": { "beat": "metricbeat", "type": "_doc", "version": "8.10.0" }, "metricset": { "name": "query", "period": 50000 }, "service": { "address": "localhost", "type": "sql" }, "sql": { "metrics": { "database_name": "msdb", "database_id": 4, "server_name": "857e71351450", "instance_name": "MSSQLSERVER" }, "driver": "mssql", "query": "USE [msdb]; SELECT @@servername AS server_name, @@servicename AS instance_name, DB_NAME() AS 'database_name', DB_ID() AS database_id;" }, "event": { "dataset": "sql.query", "module": "sql", "duration": 69424666 }, "agent": { "type": "metricbeat", "version": "8.10.0", "ephemeral_id": "<redacted>", "id": "<redacted>", "name": "host-machine" }, "ecs": { "version": "8.0.0" }, "host": { "os": { "family": "darwin", "name": "macOS", "kernel": "22.4.0", "build": "<redacted>", "type": "macos", "platform": "darwin", "version": "13.3.1" }, "name": "host-machine", "id": "<redacted>", "ip": [ "<redacted>" ], "mac": [ "<redacted>" ], "hostname": "host-machine", "architecture": "arm64" } }