github.com/argoproj/argo-cd@v1.8.7/server/metrics/metrics.go (about)

     1  package metrics
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"strconv"
     7  	"time"
     8  
     9  	"github.com/prometheus/client_golang/prometheus"
    10  	"github.com/prometheus/client_golang/prometheus/promhttp"
    11  )
    12  
    13  type MetricsServer struct {
    14  	*http.Server
    15  	redisRequestCounter   *prometheus.CounterVec
    16  	redisRequestHistogram *prometheus.HistogramVec
    17  }
    18  
    19  var (
    20  	redisRequestCounter = prometheus.NewCounterVec(
    21  		prometheus.CounterOpts{
    22  			Name: "argocd_redis_request_total",
    23  			Help: "Number of kubernetes requests executed during application reconciliation.",
    24  		},
    25  		[]string{"initiator", "failed"},
    26  	)
    27  	redisRequestHistogram = prometheus.NewHistogramVec(
    28  		prometheus.HistogramOpts{
    29  			Name:    "argocd_redis_request_duration",
    30  			Help:    "Redis requests duration.",
    31  			Buckets: []float64{0.1, 0.25, .5, 1, 2},
    32  		},
    33  		[]string{"initiator"},
    34  	)
    35  )
    36  
    37  // NewMetricsServer returns a new prometheus server which collects api server metrics
    38  func NewMetricsServer(port int) *MetricsServer {
    39  	mux := http.NewServeMux()
    40  	registry := prometheus.NewRegistry()
    41  	mux.Handle("/metrics", promhttp.HandlerFor(prometheus.Gatherers{
    42  		registry,
    43  		prometheus.DefaultGatherer,
    44  	}, promhttp.HandlerOpts{}))
    45  
    46  	registry.MustRegister(redisRequestCounter)
    47  	registry.MustRegister(redisRequestHistogram)
    48  
    49  	return &MetricsServer{
    50  		Server: &http.Server{
    51  			Addr:    fmt.Sprintf("0.0.0.0:%d", port),
    52  			Handler: mux,
    53  		},
    54  		redisRequestCounter:   redisRequestCounter,
    55  		redisRequestHistogram: redisRequestHistogram,
    56  	}
    57  }
    58  
    59  func (m *MetricsServer) IncRedisRequest(failed bool) {
    60  	m.redisRequestCounter.WithLabelValues("argocd-server", strconv.FormatBool(failed)).Inc()
    61  }
    62  
    63  // ObserveRedisRequestDuration observes redis request duration
    64  func (m *MetricsServer) ObserveRedisRequestDuration(duration time.Duration) {
    65  	m.redisRequestHistogram.WithLabelValues("argocd-server").Observe(duration.Seconds())
    66  }