@@ -178,6 +178,63 @@ status:
178178 status: Healthy
179179`
180180
181+ const fakeAppOperationRunning = `
182+ apiVersion: argoproj.io/v1alpha1
183+ kind: Application
184+ metadata:
185+ name: my-app
186+ namespace: argocd
187+ labels:
188+ team-name: my-team
189+ team-bu: bu-id
190+ argoproj.io/cluster: test-cluster
191+ spec:
192+ destination:
193+ namespace: dummy-namespace
194+ name: cluster1
195+ project: important-project
196+ source:
197+ path: some/path
198+ repoURL: https://github.com/argoproj/argocd-example-apps.git
199+ status:
200+ sync:
201+ status: OutOfSync
202+ health:
203+ status: Progressing
204+ operationState:
205+ phase: Running
206+ startedAt: "2025-01-29T08:42:34Z"
207+ `
208+
209+ const fakeAppOperationFinished = `
210+ apiVersion: argoproj.io/v1alpha1
211+ kind: Application
212+ metadata:
213+ name: my-app
214+ namespace: argocd
215+ labels:
216+ team-name: my-team
217+ team-bu: bu-id
218+ argoproj.io/cluster: test-cluster
219+ spec:
220+ destination:
221+ namespace: dummy-namespace
222+ name: cluster1
223+ project: important-project
224+ source:
225+ path: some/path
226+ repoURL: https://github.com/argoproj/argocd-example-apps.git
227+ status:
228+ sync:
229+ status: Synced
230+ health:
231+ status: Healthy
232+ operationState:
233+ phase: Succeeded
234+ startedAt: "2025-01-29T08:42:34Z"
235+ finishedAt: "2025-01-29T08:42:35Z"
236+ `
237+
181238var noOpHealthCheck = func (_ * http.Request ) error {
182239 return nil
183240}
@@ -468,6 +525,43 @@ func assertMetricsNotPrinted(t *testing.T, expectedLines, body string) {
468525 }
469526}
470527
528+ func TestMetricsSyncDuration (t * testing.T ) {
529+ cancel , appLister := newFakeLister ()
530+ defer cancel ()
531+ mockDB := mocks .NewArgoDB (t )
532+ metricsServ , err := NewMetricsServer ("localhost:8082" , appLister , appFilter , noOpHealthCheck , []string {}, []string {}, mockDB )
533+ require .NoError (t , err )
534+
535+ fakeAppOperationRunning := newFakeApp (fakeAppOperationRunning )
536+ metricsServ .IncAppSyncDuration (fakeAppOperationRunning , "https://localhost:6443" , fakeAppOperationRunning .Status .OperationState )
537+
538+ req , err := http .NewRequest (http .MethodGet , "/metrics" , nil )
539+ require .NoError (t , err )
540+ rr := httptest .NewRecorder ()
541+ metricsServ .Handler .ServeHTTP (rr , req )
542+ assert .Equal (t , http .StatusOK , rr .Code )
543+ body := rr .Body .String ()
544+ log .Println (body )
545+ assert .NotContains (t , body , "argocd_app_sync_duration_seconds_total" )
546+
547+ fakeAppOperationFinished := newFakeApp (fakeAppOperationFinished )
548+ metricsServ .IncAppSyncDuration (fakeAppOperationFinished , "https://localhost:6443" , fakeAppOperationFinished .Status .OperationState )
549+
550+ req , err = http .NewRequest (http .MethodGet , "/metrics" , nil )
551+ require .NoError (t , err )
552+ rr = httptest .NewRecorder ()
553+ metricsServ .Handler .ServeHTTP (rr , req )
554+ assert .Equal (t , http .StatusOK , rr .Code )
555+ body = rr .Body .String ()
556+ appSyncDurationTotal := `
557+ # HELP argocd_app_sync_duration_seconds_total Application sync performance in seconds total.
558+ # TYPE argocd_app_sync_duration_seconds_total counter
559+ argocd_app_sync_duration_seconds_total{dest_server="https://localhost:6443",name="my-app",namespace="argocd",project="important-project"} 1
560+ `
561+ log .Println (body )
562+ assertMetricsPrinted (t , appSyncDurationTotal , body )
563+ }
564+
471565func TestReconcileMetrics (t * testing.T ) {
472566 cancel , appLister := newFakeLister ()
473567 defer cancel ()
0 commit comments