Generic SQL join results of multiple queries#32394
Generic SQL join results of multiple queries#32394lalit-satapathy merged 11 commits intoelastic:mainfrom
Conversation
* Add flag request_merge to request merge for sql_queries. * Collect metrics when request_merge is enabled and report collectively at the end. * Flag error/warning when conditions not met.
* Add flag request_merge to request merge for sql_queries. * Collect metrics when request_merge is enabled and report collectively at the end. * Flag error/warning when conditions not met.
Fixed integration to add merged mode. Fixed earlier integration test issues.
|
This pull request is now in conflicts. Could you fix it? 🙏 |
|
This pull request is now in conflicts. Could you fix it? 🙏 |
| request_merge: true | ||
| sql_queries: | ||
| - query: "SELECT blks_hit FROM pg_stat_database limit 1;" | ||
| response_format: table | ||
| - query: "SELECT blks_read FROM pg_stat_database limit 1;" | ||
| response_format: table |
There was a problem hiding this comment.
How would this configuration be different to the following one?
| request_merge: true | |
| sql_queries: | |
| - query: "SELECT blks_hit FROM pg_stat_database limit 1;" | |
| response_format: table | |
| - query: "SELECT blks_read FROM pg_stat_database limit 1;" | |
| response_format: table | |
| sql_queries: | |
| - query: "SELECT blks_hit, blks_read FROM pg_stat_database limit 1;" | |
| response_format: table |
If this would generate the same result, please put here an example of a case where request_merge would be the only option. I guess that this merge is required in cases where data from multiple tables is being collected.
There was a problem hiding this comment.
Updated the example with example queries from two different tables.
| }, | ||
| } | ||
|
|
||
| // Need to add this. |
There was a problem hiding this comment.
Why is it needed? I'd assume that any code we add is "needed" 🙂
If this test requires any explanation, please comment it here. If it doesn't, please remove this comment.
| }, | ||
| } | ||
|
|
||
| // Need to add this. |
There was a problem hiding this comment.
Also here, please explain why this is needed, or remove the comment if no explanation is needed.
| cfg = testFetchConfig{ | ||
| config: config{ | ||
| Driver: "postgres", | ||
| Queries: []query{query{Query: "SELECT blks_hit FROM pg_stat_database limit 1;", ResponseFormat: "table"}, query{Query: "SELECT blks_read FROM pg_stat_database limit 1;", ResponseFormat: "table"}}, |
There was a problem hiding this comment.
Nit. For readability please split this in multiple lines. Something like this at least:
| Queries: []query{query{Query: "SELECT blks_hit FROM pg_stat_database limit 1;", ResponseFormat: "table"}, query{Query: "SELECT blks_read FROM pg_stat_database limit 1;", ResponseFormat: "table"}}, | |
| Queries: []query{ | |
| query{Query: "SELECT blks_hit FROM pg_stat_database limit 1;", ResponseFormat: "table"}, | |
| query{Query: "SELECT blks_read FROM pg_stat_database limit 1;", ResponseFormat: "table"}, | |
| }, |
There was a problem hiding this comment.
Re-formatted.
|
|
||
| Queries []query `config:"sql_queries" ` | ||
| Queries []query `config:"sql_queries" ` | ||
| MergeQueries bool `config:"request_merge"` |
There was a problem hiding this comment.
As commented in #31806 (comment), I don't like so much the name of request_merge for this option, it is not very meaningful. What are you merging? Is this a request that may not be fulfiled?
I would suggest something like merge_results, merge_events or single_event.
| MergeQueries bool `config:"request_merge"` | |
| MergeResults bool `config:"merge_results"` |
| MergeQueries bool `config:"request_merge"` | |
| SingleEvent bool `config:"single_event"` |
There was a problem hiding this comment.
Changed the flag name as merge_results and also the variable. merge_results sounds fine with me, so let's go ahead with it.
| } | ||
| if m.Config.MergeQueries { | ||
| // Report here for merged case. | ||
| m.reportEvent(merged, reporter, "MULTIPLE") |
There was a problem hiding this comment.
I think I'd prefer not to set sql.query on this case. Maybe you can pass an empty value here, and make reportEvent to don't set this value in this case.
| m.reportEvent(merged, reporter, "MULTIPLE") | |
| m.reportEvent(merged, reporter, "") |
There was a problem hiding this comment.
Made it empty.
* Make sql.query as “” for merge_results * Updated merge example in doc with queries from two separate tables.\ * Formatted query in test
…y/beats into generic-sql-event-merge-1
| "address": "localhost" | ||
| }, | ||
| "sql": { | ||
| "query": "", |
There was a problem hiding this comment.
Nit. I was suggesting to don't set this value if it is empty.
| "query": "", |
There was a problem hiding this comment.
This does complicate the code for reportEvent, but fixed as asked 😃
| @@ -187,20 +187,32 @@ func (m *MetricSet) Fetch(ctx context.Context, reporter mb.ReporterV2) error { | |||
| func (m *MetricSet) reportEvent(ms mapstr.M, reporter mb.ReporterV2, qry string) { | |||
| if m.Config.RawData.Enabled { | |||
|
|
|||
There was a problem hiding this comment.
Another way to do this would be:
moduleFields := mapstr.M{
"metrics": ms, // Individual metric
"driver": m.Config.Driver,
}
if qry != "" {
moduleFields["query"] = qry
}
reporter.Event(mb.Event{
ModuleFields: moduleFields,
})
There was a problem hiding this comment.
Yes. I hope we can stay with the current structure.
There was a problem hiding this comment.
I mean, you can do it this way in case you feel it complicates less the code 🙂 #32394 (comment)
jsoriano
left a comment
There was a problem hiding this comment.
Apart of some nitpicking on Fetch and reportEvent, it LGTM, I will leave the final approve to the team.
| } else { | ||
| // Report immediately for non-merged cases. | ||
| m.reportEvent(ms, reporter, q.Query) | ||
| } |
There was a problem hiding this comment.
Nit. This is getting more complicated now, maybe the code for variable and table format could be moved to separated functions.
ManojS-shetty
left a comment
There was a problem hiding this comment.
Tested with the few scenarios for MSSQL and able to see merged events after setting the flag merge_results: true
LGTM.
SQL events merge * Add flag merge_results to merge results for sql_queries. * Collect metrics when request_merge is enabled and report collectively at the end. * Flag error/warning when conditions not met. * Do not add sql.query when for merge_results used. * Updated documentation. * Fixed integration to add merged mode. Fixed earlier integration test issues.
What does this PR do?
This PR enable creating a single event for cases, where multiple individual queries are executed by proving a new flag.
Why is it important?
This PR enables single event generation that is needed by integration packages like mssql.
Pending
Checklist
CHANGELOG.next.asciidocorCHANGELOG-developer.next.asciidoc.Author's Checklist
How to test this PR locally
Related issues