github.com/argoproj/argo-cd@v1.8.7/reposerver/metrics/metrics.go (about) 1 package metrics 2 3 import ( 4 "net/http" 5 "strconv" 6 "time" 7 8 "github.com/prometheus/client_golang/prometheus" 9 "github.com/prometheus/client_golang/prometheus/promhttp" 10 ) 11 12 type MetricsServer struct { 13 handler http.Handler 14 gitRequestCounter *prometheus.CounterVec 15 gitRequestHistogram *prometheus.HistogramVec 16 repoPendingRequestsGauge *prometheus.GaugeVec 17 redisRequestCounter *prometheus.CounterVec 18 redisRequestHistogram *prometheus.HistogramVec 19 } 20 21 type GitRequestType string 22 23 const ( 24 GitRequestTypeLsRemote = "ls-remote" 25 GitRequestTypeFetch = "fetch" 26 ) 27 28 // NewMetricsServer returns a new prometheus server which collects application metrics. 29 func NewMetricsServer() *MetricsServer { 30 registry := prometheus.NewRegistry() 31 registry.MustRegister(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{})) 32 registry.MustRegister(prometheus.NewGoCollector()) 33 34 gitRequestCounter := prometheus.NewCounterVec( 35 prometheus.CounterOpts{ 36 Name: "argocd_git_request_total", 37 Help: "Number of git requests performed by repo server", 38 }, 39 []string{"repo", "request_type"}, 40 ) 41 registry.MustRegister(gitRequestCounter) 42 43 gitRequestHistogram := prometheus.NewHistogramVec( 44 prometheus.HistogramOpts{ 45 Name: "argocd_git_request_duration_seconds", 46 Help: "Git requests duration seconds.", 47 Buckets: []float64{0.1, 0.25, .5, 1, 2, 4, 10, 20}, 48 }, 49 []string{"repo", "request_type"}, 50 ) 51 registry.MustRegister(gitRequestHistogram) 52 53 repoPendingRequestsGauge := prometheus.NewGaugeVec( 54 prometheus.GaugeOpts{ 55 Name: "argocd_repo_pending_request_total", 56 Help: "Number of pending requests requiring repository lock", 57 }, 58 []string{"repo"}, 59 ) 60 registry.MustRegister(repoPendingRequestsGauge) 61 62 redisRequestCounter := prometheus.NewCounterVec( 63 prometheus.CounterOpts{ 64 Name: "argocd_redis_request_total", 65 Help: "Number of kubernetes requests executed during application reconciliation.", 66 }, 67 []string{"initiator", "failed"}, 68 ) 69 registry.MustRegister(redisRequestCounter) 70 71 redisRequestHistogram := prometheus.NewHistogramVec( 72 prometheus.HistogramOpts{ 73 Name: "argocd_redis_request_duration_seconds", 74 Help: "Redis requests duration seconds.", 75 Buckets: []float64{0.1, 0.25, .5, 1, 2}, 76 }, 77 []string{"initiator"}, 78 ) 79 registry.MustRegister(redisRequestHistogram) 80 81 return &MetricsServer{ 82 handler: promhttp.HandlerFor(registry, promhttp.HandlerOpts{}), 83 gitRequestCounter: gitRequestCounter, 84 gitRequestHistogram: gitRequestHistogram, 85 repoPendingRequestsGauge: repoPendingRequestsGauge, 86 redisRequestCounter: redisRequestCounter, 87 redisRequestHistogram: redisRequestHistogram, 88 } 89 } 90 91 func (m *MetricsServer) GetHandler() http.Handler { 92 return m.handler 93 } 94 95 // IncGitRequest increments the git requests counter 96 func (m *MetricsServer) IncGitRequest(repo string, requestType GitRequestType) { 97 m.gitRequestCounter.WithLabelValues(repo, string(requestType)).Inc() 98 } 99 100 func (m *MetricsServer) IncPendingRepoRequest(repo string) { 101 m.repoPendingRequestsGauge.WithLabelValues(repo).Inc() 102 } 103 104 func (m *MetricsServer) ObserveGitRequestDuration(repo string, requestType GitRequestType, duration time.Duration) { 105 m.gitRequestHistogram.WithLabelValues(repo, string(requestType)).Observe(duration.Seconds()) 106 } 107 108 func (m *MetricsServer) DecPendingRepoRequest(repo string) { 109 m.repoPendingRequestsGauge.WithLabelValues(repo).Dec() 110 } 111 112 func (m *MetricsServer) IncRedisRequest(failed bool) { 113 m.redisRequestCounter.WithLabelValues("argocd-repo-server", strconv.FormatBool(failed)).Inc() 114 } 115 116 func (m *MetricsServer) ObserveRedisRequestDuration(duration time.Duration) { 117 m.redisRequestHistogram.WithLabelValues("argocd-repo-server").Observe(duration.Seconds()) 118 }