Skip to content

Commit fbe5c0a

Browse files
committed
Making planCache expirable
1 parent 435a1d0 commit fbe5c0a

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

receiver/sqlserverreceiver/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type TopQueryCollection struct {
3030
MaxQuerySampleCount uint `mapstructure:"max_query_sample_count"`
3131
TopQueryCount uint `mapstructure:"top_query_count"`
3232
CollectionInterval time.Duration `mapstructure:"collection_interval"`
33+
QueryPlanCacheTTL time.Duration `mapstructure:"query_plan_cache_ttl"`
3334
}
3435

3536
// Config defines configuration for a sqlserver receiver.

receiver/sqlserverreceiver/factory.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"time"
1212

1313
lru "github.com/hashicorp/golang-lru/v2"
14+
"github.com/hashicorp/golang-lru/v2/expirable"
1415
_ "github.com/microsoft/go-mssqldb" // register Db driver
1516
_ "github.com/microsoft/go-mssqldb/integratedauth/krb5" // register Db driver
1617
"go.opentelemetry.io/collector/component"
@@ -59,6 +60,7 @@ func createDefaultConfig() component.Config {
5960
MaxQuerySampleCount: 1000,
6061
TopQueryCount: 200,
6162
CollectionInterval: time.Minute,
63+
QueryPlanCacheTTL: time.Hour,
6264
},
6365
}
6466
}
@@ -132,7 +134,7 @@ func setupSQLServerScrapers(params receiver.Settings, cfg *Config) []*sqlServerS
132134

133135
// lru only returns error when the size is less than 0
134136
metricCache := newCache(1)
135-
planCache, _ := lru.New[string, string](1)
137+
planCache := expirable.NewLRU[string, string](1, nil, time.Second)
136138

137139
sqlServerScraper := newSQLServerScraper(id, query,
138140
sqlquery.TelemetryConfig{},
@@ -174,7 +176,7 @@ func setupSQLServerLogsScrapers(params receiver.Settings, cfg *Config) []*sqlSer
174176
id := component.NewIDWithName(metadata.Type, fmt.Sprintf("logs-query-%d: %s", i, query))
175177

176178
cache := newCache(1)
177-
planCache, _ := lru.New[string, string](int(cfg.MaxQuerySampleCount))
179+
planCache := expirable.NewLRU[string, string](1, nil, cfg.TopQueryCollection.QueryPlanCacheTTL)
178180

179181
if query == getSQLServerQueryTextAndPlanQuery() {
180182
// we have 8 metrics in this query and multiple 2 to allow to cache more queries.

receiver/sqlserverreceiver/scraper.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"time"
1717

1818
lru "github.com/hashicorp/golang-lru/v2"
19+
"github.com/hashicorp/golang-lru/v2/expirable"
1920
"go.opentelemetry.io/collector/component"
2021
"go.opentelemetry.io/collector/featuregate"
2122
"go.opentelemetry.io/collector/pdata/pcommon"
@@ -62,7 +63,7 @@ type sqlServerScraperHelper struct {
6263
mb *metadata.MetricsBuilder
6364
lb *metadata.LogsBuilder
6465
metricCache *lru.Cache[string, int64]
65-
planCache *lru.Cache[string, string]
66+
planCache *expirable.LRU[string, string]
6667
lastExecutionTimestamp time.Time
6768
obfuscator *obfuscator
6869
serviceInstanceID string
@@ -81,7 +82,7 @@ func newSQLServerScraper(id component.ID,
8182
params receiver.Settings,
8283
cfg *Config,
8384
metricCache *lru.Cache[string, int64],
84-
planCache *lru.Cache[string, string],
85+
planCache *expirable.LRU[string, string],
8586
) *sqlServerScraperHelper {
8687
// Compute service instance ID
8788
serviceInstanceID, err := computeServiceInstanceID(cfg)

0 commit comments

Comments
 (0)