From 6efe7f6c17c53dd3e0fa8bd83c38e8be2ad864c8 Mon Sep 17 00:00:00 2001 From: subham sarkar Date: Fri, 10 Jan 2025 17:09:12 +0530 Subject: [PATCH] x-pack/metricbeat/module/sql: Add debug queries when `merge_results: true` (#42271) (cherry picked from commit d288e54999a97b973536f84a1e1436ab6b62de09) --- CHANGELOG.next.asciidoc | 1 + x-pack/metricbeat/module/sql/query/query.go | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index 2903959e0bd5..12fa97a9296e 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -429,6 +429,7 @@ https://github.com/elastic/beats/compare/v8.8.1\...main[Check the HEAD diff] - Added `tier_preference`, `creation_date` and `version` fields to the `elasticsearch.index` metricset. {pull}41944[41944] - Add new OpenAI (`openai`) module for tracking usage data. {pull}41516[41516] - Add `use_performance_counters` to collect CPU metrics using performance counters on Windows for `system/cpu` and `system/core` {pull}41965[41965] +- Preserve queries for debugging when `merge_results: true` in SQL module {pull}42271[42271] *Metricbeat* - Add benchmark module {pull}41801[41801] diff --git a/x-pack/metricbeat/module/sql/query/query.go b/x-pack/metricbeat/module/sql/query/query.go index b5b50e2d88f4..dacc4841ce6e 100644 --- a/x-pack/metricbeat/module/sql/query/query.go +++ b/x-pack/metricbeat/module/sql/query/query.go @@ -146,7 +146,9 @@ func dbSelector(driver, dbName string) string { func (m *MetricSet) fetch(ctx context.Context, db *sql.DbClient, reporter mb.ReporterV2, queries []query) (bool, error) { var ok bool merged := make(mapstr.M, 0) + storeQueries := make([]string, 0, len(queries)) for _, q := range queries { + storeQueries = append(storeQueries, q.Query) if q.ResponseFormat == tableResponseFormat { // Table format mss, err := db.FetchTableMode(ctx, q.Query) @@ -196,7 +198,7 @@ func (m *MetricSet) fetch(ctx context.Context, db *sql.DbClient, reporter mb.Rep if m.Config.MergeResults { // Report here for merged case. - ok = m.reportEvent(merged, reporter, "") + ok = m.reportEvent(merged, reporter, storeQueries...) } return ok, nil @@ -296,7 +298,7 @@ func (m *MetricSet) Fetch(ctx context.Context, reporter mb.ReporterV2) error { // reportEvent using 'user' mode with keys under `sql.metrics.*` or using Raw data mode (module and metricset key spaces // provided by the user) -func (m *MetricSet) reportEvent(ms mapstr.M, reporter mb.ReporterV2, qry string) bool { +func (m *MetricSet) reportEvent(ms mapstr.M, reporter mb.ReporterV2, qry ...string) bool { var ok bool if m.Config.RawData.Enabled { // New usage.