Skip to content

Commit

Permalink
add a counter for github cache
Browse files Browse the repository at this point in the history
Signed-off-by: aburan28 <[email protected]>
  • Loading branch information
aburan28 committed Jan 14, 2025
1 parent 27e2982 commit 6915912
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
21 changes: 14 additions & 7 deletions applicationset/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ var (
descAppsetDefaultLabels,
nil,
)

descAppsetGithubCacheCounter = prometheus.NewDesc(
"argocd_appset_github_cached_items_count",
"Number of github api requests cached",
[]string{},
nil,
)
)

type ApplicationsetMetrics struct {
Expand All @@ -54,15 +61,15 @@ func NewApplicationsetMetrics(appsetLister applisters.ApplicationSetLister, apps
)
githubCacheHistogram := prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "argocd_github_cache_latency",
Name: "argocd_appset_github_cache_latency",
Help: "",
},
descAppsetDefaultLabels,
)

githubCacheCounter := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "argocd_github_cache_items",
Name: "argocd_appset_github_cached_items_count",
Help: "Number of items in the github cache",
},
descAppsetDefaultLabels,
Expand All @@ -77,9 +84,8 @@ func NewApplicationsetMetrics(appsetLister applisters.ApplicationSetLister, apps
metrics.Registry.MustRegister(githubCacheCounter)

return ApplicationsetMetrics{
reconcileHistogram: reconcileHistogram,
githubCacheHistogram: githubCacheHistogram,
githubCacheCounter: githubCacheCounter,
reconcileHistogram: reconcileHistogram,
githubCacheCounter: githubCacheCounter,
}
}

Expand All @@ -88,11 +94,12 @@ func (m *ApplicationsetMetrics) ObserveReconcile(appset *argoappv1.ApplicationSe
}

func (m *ApplicationsetMetrics) ObserveGithubCacheLatency(appset *argoappv1.ApplicationSet, duration time.Duration) {
// TODO: come back to this
m.githubCacheHistogram.WithLabelValues(appset.Namespace, appset.Name).Observe(duration.Seconds())
}

func (m *ApplicationsetMetrics) IncGithubCacheItems(appset *argoappv1.ApplicationSet, count int) {
m.githubCacheCounter.WithLabelValues(appset.Namespace, appset.Name).Add(float64(count))
func (m *ApplicationsetMetrics) IncGithubCacheItems() {
m.githubCacheCounter.WithLabelValues().Inc()
}

func newAppsetCollector(lister applisters.ApplicationSetLister, labels []string, filter func(appset *argoappv1.ApplicationSet) bool) *appsetCollector {
Expand Down
19 changes: 19 additions & 0 deletions applicationset/metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,25 @@ argocd_appset_owned_applications{name="test2",namespace="argocd"} 0
assert.NotContains(t, rr.Body.String(), `name="should-be-filtered-out"`)
}

func IncGithubCacheItems(t *testing.T) {
appsetList := newFakeAppsets(fakeAppsetList)
client := initializeClient(appsetList)
metrics.Registry = prometheus.NewRegistry()

appsetMetrics := NewApplicationsetMetrics(utils.NewAppsetLister(client), collectedLabels, filter)

req, err := http.NewRequest(http.MethodGet, "/metrics", nil)
require.NoError(t, err)
rr := httptest.NewRecorder()
handler := promhttp.HandlerFor(metrics.Registry, promhttp.HandlerOpts{})
appsetMetrics.IncGithubCacheItems()
handler.ServeHTTP(rr, req)
assert.Contains(t, rr.Body.String(), `
argocd_appset_github_cached_items_count{} 1
`)

}

func TestObserveReconcile(t *testing.T) {
appsetList := newFakeAppsets(fakeAppsetList)
client := initializeClient(appsetList)
Expand Down

0 comments on commit 6915912

Please sign in to comment.