Skip to content

Commit 2811823

Browse files
authored
Merge pull request #30252 from listx/inrepoconfig-metrics
inrepoconfig: add additional metrics
2 parents 31759a0 + b297439 commit 2811823

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

prow/config/cache.go

+68
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ var inRepoConfigCacheMetrics = struct {
4949
// How many times have we tried to remove a cached key because its value
5050
// construction failed?
5151
evictionsManual *prometheus.CounterVec
52+
// How many entries are in the cache?
53+
cacheUsageSize *prometheus.GaugeVec
54+
// How long does it take for GetProwYAML() to run?
55+
getProwYAMLDuration *prometheus.HistogramVec
5256
}{
5357
lookups: prometheus.NewCounterVec(prometheus.CounterOpts{
5458
Name: "inRepoConfigCache_lookups",
@@ -88,6 +92,21 @@ var inRepoConfigCacheMetrics = struct {
8892
"org",
8993
"repo",
9094
}),
95+
cacheUsageSize: prometheus.NewGaugeVec(prometheus.GaugeOpts{
96+
Name: "inRepoConfigCache_cache_usage_size",
97+
Help: "Size of the cache (how many entries it is holding) by org and repo.",
98+
}, []string{
99+
"org",
100+
"repo",
101+
}),
102+
getProwYAMLDuration: prometheus.NewHistogramVec(prometheus.HistogramOpts{
103+
Name: "inRepoConfigCache_GetProwYAML_duration",
104+
Help: "Histogram of seconds spent retrieving the ProwYAML (inrepoconfig), by org and repo.",
105+
Buckets: []float64{0.5, 1, 2, 5, 10, 20, 30, 60, 120, 180, 300, 600},
106+
}, []string{
107+
"org",
108+
"repo",
109+
}),
91110
}
92111

93112
func init() {
@@ -96,6 +115,8 @@ func init() {
96115
prometheus.MustRegister(inRepoConfigCacheMetrics.misses)
97116
prometheus.MustRegister(inRepoConfigCacheMetrics.evictionsForced)
98117
prometheus.MustRegister(inRepoConfigCacheMetrics.evictionsManual)
118+
prometheus.MustRegister(inRepoConfigCacheMetrics.cacheUsageSize)
119+
prometheus.MustRegister(inRepoConfigCacheMetrics.getProwYAMLDuration)
99120
}
100121

101122
func mkCacheEventCallback(counterVec *prometheus.CounterVec) cache.EventCallback {
@@ -167,6 +188,47 @@ func NewInRepoConfigCache(
167188
return nil, err
168189
}
169190

191+
// This records all OrgRepos we've seen so far during the lifetime of the
192+
// process. The main purpose is to allow reporting of 0 counts for OrgRepos
193+
// whose keys have been evicted by the lruCache.
194+
seenOrgRepos := make(map[OrgRepo]int)
195+
196+
cacheSizeMetrics := func() {
197+
// Record all unique orgRepo combinations we've seen so far.
198+
for _, key := range lruCache.Keys() {
199+
org, repo, err := keyToOrgRepo(key)
200+
if err != nil {
201+
// This should only happen if we are deliberately using things
202+
// other than a CacheKey as the key.
203+
logrus.Warnf("programmer error: could not report cache size metrics for a key entry: %v", err)
204+
continue
205+
}
206+
orgRepo := OrgRepo{org, repo}
207+
if count, ok := seenOrgRepos[orgRepo]; ok {
208+
seenOrgRepos[orgRepo] = count + 1
209+
} else {
210+
seenOrgRepos[orgRepo] = 1
211+
}
212+
}
213+
// For every single org and repo in the cache, report how many key
214+
// entries there are.
215+
for orgRepo, count := range seenOrgRepos {
216+
inRepoConfigCacheMetrics.cacheUsageSize.WithLabelValues(
217+
orgRepo.Org, orgRepo.Repo).Set(float64(count))
218+
// Reset the counter back down to 0 because it may be that by the
219+
// time of the next interval, the last key for this orgRepo will be
220+
// evicted. At that point we still want to report a count of 0.
221+
seenOrgRepos[orgRepo] = 0
222+
}
223+
}
224+
225+
go func() {
226+
for {
227+
cacheSizeMetrics()
228+
time.Sleep(30 * time.Second)
229+
}
230+
}()
231+
170232
cache := &InRepoConfigCache{
171233
lruCache,
172234
// Know how to default the retrieved ProwYAML values against the latest Config.
@@ -265,6 +327,12 @@ func (cache *InRepoConfigCache) GetPostsubmits(identifier string, baseSHAGetter
265327

266328
// GetProwYAML returns the ProwYAML value stored in the InRepoConfigCache.
267329
func (cache *InRepoConfigCache) GetProwYAML(identifier string, baseSHAGetter RefGetter, headSHAGetters ...RefGetter) (*ProwYAML, error) {
330+
timeGetProwYAML := time.Now()
331+
defer func() {
332+
orgRepo := NewOrgRepo(identifier)
333+
inRepoConfigCacheMetrics.getProwYAMLDuration.WithLabelValues(orgRepo.Org, orgRepo.Repo).Observe((float64(time.Since(timeGetProwYAML).Seconds())))
334+
}()
335+
268336
c := cache.configAgent.Config()
269337

270338
prowYAML, err := cache.getProwYAML(c.getProwYAML, identifier, baseSHAGetter, headSHAGetters...)

prow/gerrit/adapter/adapter.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,9 @@ func (c *Controller) Sync() {
292292
gerritMetrics.changeSyncDuration.WithLabelValues(instance, project).Observe(float64(time.Since(now).Seconds()))
293293
}()
294294

295-
// Ignore the error. It is already logged.
296295
timeQueryChangesForProject := time.Now()
297296

297+
// Ignore the error. It is already logged.
298298
changes, err := c.gc.QueryChangesForProject(instance, project, syncTime, c.config().Gerrit.RateLimit)
299299
queryResult := func() string {
300300
if err == nil {

0 commit comments

Comments
 (0)