-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Generic SQL join results of multiple queries #32394
Generic SQL join results of multiple queries #32394
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the example with example queries from two different tables.
}, | ||
} | ||
|
||
// Need to add this. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed
}, | ||
} | ||
|
||
// Need to add this. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also here, please explain why this is needed, or remove the comment if no explanation is needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Re-formatted.
@@ -49,7 +49,8 @@ type config struct { | |||
ResponseFormat string `config:"sql_response_format"` | |||
Query string `config:"sql_query" ` | |||
|
|||
Queries []query `config:"sql_queries" ` | |||
Queries []query `config:"sql_queries" ` | |||
MergeQueries bool `config:"request_merge"` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
"address": "localhost" | ||
}, | ||
"sql": { | ||
"query": "", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit. I was suggesting to don't set this value if it is empty.
"query": "", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. I hope we can stay with the current structure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean, you can do it this way in case you feel it complicates less the code 🙂 #32394 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit. This is getting more complicated now, maybe the code for variable and table format could be moved to separated functions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.asciidoc
orCHANGELOG-developer.next.asciidoc
.Author's Checklist
How to test this PR locally
Related issues