@@ -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+
790808func (s * sqlServerScraperHelper ) retrieveValue (
791809 row sqlquery.StringMap ,
792810 column string ,
0 commit comments