Skip to content

Commit 488362a

Browse files
committed
planCache enhancement for top_query collection
1 parent 320ef1c commit 488362a

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

receiver/sqlserverreceiver/factory.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,16 @@ func setupSQLServerScrapers(params receiver.Settings, cfg *Config) []*sqlServerS
132132

133133
// lru only returns error when the size is less than 0
134134
cache := newCache(1)
135+
planCache, _ := lru.New[string, string](1)
135136

136137
sqlServerScraper := newSQLServerScraper(id, query,
137138
sqlquery.TelemetryConfig{},
138139
dbProviderFunc,
139140
sqlquery.NewDbClient,
140141
params,
141142
cfg,
142-
cache)
143+
cache,
144+
planCache)
143145

144146
scrapers = append(scrapers, sqlServerScraper)
145147
}
@@ -172,6 +174,7 @@ func setupSQLServerLogsScrapers(params receiver.Settings, cfg *Config) []*sqlSer
172174
id := component.NewIDWithName(metadata.Type, fmt.Sprintf("logs-query-%d: %s", i, query))
173175

174176
cache := newCache(1)
177+
planCache, _ := lru.New[string, string](int(cfg.MaxQuerySampleCount))
175178

176179
if query == getSQLServerQueryTextAndPlanQuery() {
177180
// we have 8 metrics in this query and multiple 2 to allow to cache more queries.
@@ -188,7 +191,8 @@ func setupSQLServerLogsScrapers(params receiver.Settings, cfg *Config) []*sqlSer
188191
sqlquery.NewDbClient,
189192
params,
190193
cfg,
191-
cache)
194+
cache,
195+
planCache)
192196

193197
scrapers = append(scrapers, sqlServerScraper)
194198
}

receiver/sqlserverreceiver/scraper.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ type sqlServerScraperHelper struct {
6262
mb *metadata.MetricsBuilder
6363
lb *metadata.LogsBuilder
6464
cache *lru.Cache[string, int64]
65+
planCache *lru.Cache[string, string]
6566
lastExecutionTimestamp time.Time
6667
obfuscator *obfuscator
6768
serviceInstanceID string
@@ -80,6 +81,7 @@ func newSQLServerScraper(id component.ID,
8081
params receiver.Settings,
8182
cfg *Config,
8283
cache *lru.Cache[string, int64],
84+
planCache *lru.Cache[string, string],
8385
) *sqlServerScraperHelper {
8486
// Compute service instance ID
8587
serviceInstanceID, err := computeServiceInstanceID(cfg)
@@ -99,6 +101,7 @@ func newSQLServerScraper(id component.ID,
99101
mb: metadata.NewMetricsBuilder(cfg.MetricsBuilderConfig, params),
100102
lb: metadata.NewLogsBuilder(cfg.LogsBuilderConfig, params),
101103
cache: cache,
104+
planCache: planCache,
102105
lastExecutionTimestamp: time.Unix(0, 0),
103106
obfuscator: newObfuscator(),
104107
serviceInstanceID: serviceInstanceID,
@@ -685,6 +688,7 @@ func (s *sqlServerScraperHelper) recordDatabaseQueryTextAndPlan(ctx context.Cont
685688
now := time.Now()
686689
timestamp := pcommon.NewTimestampFromTime(now)
687690
s.lastExecutionTimestamp = now
691+
s.logger.Debug("Current planCache size: " + strconv.Itoa(s.planCache.Len()))
688692
for i, row := range rows {
689693
// skipping the rest of the rows as totalElapsedTimeDiffs is sorted in descending order
690694
if totalElapsedTimeDiffsMicrosecond[i] == 0 {
@@ -696,6 +700,8 @@ func (s *sqlServerScraperHelper) recordDatabaseQueryTextAndPlan(ctx context.Cont
696700
queryHashVal := hex.EncodeToString([]byte(row[queryHash]))
697701
queryPlanHashVal := hex.EncodeToString([]byte(row[queryPlanHash]))
698702

703+
isNewPlan := s.cacheIfNewPlan(queryHashVal, queryPlanHashVal)
704+
699705
queryTextVal := s.retrieveValue(row, queryText, &errs, func(row sqlquery.StringMap, columnName string) (any, error) {
700706
statement := row[columnName]
701707
obfuscated, err := s.obfuscator.obfuscateSQLString(statement)
@@ -731,9 +737,12 @@ func (s *sqlServerScraperHelper) recordDatabaseQueryTextAndPlan(ctx context.Cont
731737
physicalReadsVal = int64(0)
732738
}
733739

734-
queryPlanVal := s.retrieveValue(row, queryPlan, &errs, func(row sqlquery.StringMap, columnName string) (any, error) {
735-
return s.obfuscator.obfuscateXMLPlan(row[columnName])
736-
})
740+
var queryPlanVal any = ""
741+
if isNewPlan {
742+
queryPlanVal = s.retrieveValue(row, queryPlan, &errs, func(row sqlquery.StringMap, columnName string) (any, error) {
743+
return s.obfuscator.obfuscateXMLPlan(row[columnName])
744+
})
745+
}
737746

738747
rowsReturnedVal := s.retrieveValue(row, rowsReturned, &errs, retrieveInt)
739748
cached, rowsReturnedVal = s.cacheAndDiff(queryHashVal, queryPlanHashVal, rowsReturned, rowsReturnedVal.(int64))
@@ -787,6 +796,15 @@ func (s *sqlServerScraperHelper) recordDatabaseQueryTextAndPlan(ctx context.Cont
787796
return resources, errors.Join(errs...)
788797
}
789798

799+
func (s *sqlServerScraperHelper) cacheIfNewPlan(queryHashVal string, queryPlanHashVal string) bool {
800+
cachedPlanHashValue, isPlanHashPresent := s.planCache.Get(queryHashVal)
801+
if !isPlanHashPresent || cachedPlanHashValue != queryPlanHashVal {
802+
s.planCache.Add(queryHashVal, queryPlanHashVal)
803+
return true
804+
}
805+
return false
806+
}
807+
790808
func (s *sqlServerScraperHelper) retrieveValue(
791809
row sqlquery.StringMap,
792810
column string,

0 commit comments

Comments
 (0)