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

Generic SQL join results of multiple queries #32394

Merged

Conversation

lalit-satapathy
Copy link
Contributor

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

  • Update default sql.yml file

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • [TBD] I have made corresponding change to the default configuration files
  • I have added tests that prove my fix is effective or that my feature works
  • I have added an entry in CHANGELOG.next.asciidoc or CHANGELOG-developer.next.asciidoc.

Author's Checklist

  • Integration test updated for multiple sql_queries.

How to test this PR locally

  • host any database instance like postgresql.
  • Run metricbeat by enabling request_merge with multiple sql_queries
  • Review the generated metrics

Related issues

 * 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.
@lalit-satapathy lalit-satapathy requested a review from a team July 19, 2022 03:44
@lalit-satapathy lalit-satapathy self-assigned this Jul 19, 2022
@botelastic botelastic bot added needs_team Indicates that the issue/PR needs a Team:* label and removed needs_team Indicates that the issue/PR needs a Team:* label labels Jul 19, 2022
@mergify
Copy link
Contributor

mergify bot commented Jul 19, 2022

This pull request is now in conflicts. Could you fix it? 🙏
To fixup this pull request, you can check out it locally. See documentation: https://help.github.com/articles/checking-out-pull-requests-locally/

git fetch upstream
git checkout -b generic-sql-event-merge-1 upstream/generic-sql-event-merge-1
git merge upstream/main
git push upstream generic-sql-event-merge-1

@elasticmachine
Copy link
Collaborator

elasticmachine commented Jul 19, 2022

💚 Build Succeeded

the below badges are clickable and redirect to their specific view in the CI or DOCS
Pipeline View Test View Changes Artifacts preview preview

Expand to view the summary

Build stats

  • Start Time: 2022-07-21T14:03:35.261+0000

  • Duration: 96 min 59 sec

Test stats 🧪

Test Results
Failed 0
Passed 3875
Skipped 993
Total 4868

💚 Flaky test report

Tests succeeded.

🤖 GitHub comments

To re-run your PR in the CI, just comment with:

  • /test : Re-trigger the build.

  • /package : Generate the packages and run the E2E tests.

  • /beats-tester : Run the installation tests with beats-tester.

  • run elasticsearch-ci/docs : Re-trigger the docs validation. (use unformatted text in the comment!)

@mergify
Copy link
Contributor

mergify bot commented Jul 20, 2022

This pull request is now in conflicts. Could you fix it? 🙏
To fixup this pull request, you can check out it locally. See documentation: https://help.github.com/articles/checking-out-pull-requests-locally/

git fetch upstream
git checkout -b generic-sql-event-merge-1 upstream/generic-sql-event-merge-1
git merge upstream/main
git push upstream generic-sql-event-merge-1

@lalit-satapathy lalit-satapathy marked this pull request as ready for review July 21, 2022 04:35
@lalit-satapathy lalit-satapathy requested a review from a team as a code owner July 21, 2022 04:35
@lalit-satapathy lalit-satapathy requested a review from jsoriano July 21, 2022 04:35
Comment on lines 493 to 498
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
Copy link
Member

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?

Suggested change
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.

Copy link
Contributor Author

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.
Copy link
Member

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.

Copy link
Contributor Author

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.
Copy link
Member

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.

Copy link
Contributor Author

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"}},
Copy link
Member

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:

Suggested change
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"},
},

Copy link
Contributor Author

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"`
Copy link
Member

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.

Suggested change
MergeQueries bool `config:"request_merge"`
MergeResults bool `config:"merge_results"`
Suggested change
MergeQueries bool `config:"request_merge"`
SingleEvent bool `config:"single_event"`

Copy link
Contributor Author

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")
Copy link
Member

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.

Suggested change
m.reportEvent(merged, reporter, "MULTIPLE")
m.reportEvent(merged, reporter, "")

Copy link
Contributor Author

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
Copy link
Member

@jsoriano jsoriano left a 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": "",
Copy link
Member

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.

Suggested change
"query": "",

Copy link
Contributor Author

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 {

Copy link
Member

@jsoriano jsoriano Jul 21, 2022

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,
})

Copy link
Contributor Author

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.

Copy link
Member

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)

Copy link
Member

@jsoriano jsoriano left a 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)
}
Copy link
Member

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.

Copy link
Contributor

@ManojS-shetty ManojS-shetty left a 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.

@lalit-satapathy lalit-satapathy merged commit ef523eb into elastic:main Jul 22, 2022
chrisberkhout pushed a commit that referenced this pull request Jun 1, 2023
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
8.4-candidate enhancement Team:Service-Integrations Label for the Service Integrations team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generic SQL join results of multiple queries - discussion draft
4 participants