mirror of
https://github.com/hanzoai/cd.git
synced 2026-10-30 14:56:45 +00:00
Renames local identifiers (argoCDServiceSet, argoCDSettings, argoDB, argoCM, acdWebhookHandler, newArgoCDSettings, etc.) and import aliases that collided with gitops-engine's own common/diff packages (argocommon -> cdcommon, argodiff -> cddiff). Rewords comments, log/error strings, and test fixture placeholders (usernames, example URLs, an HTML title fixture) to our own voice. Fixes a stale test assertion in applicationset_test.go that still expected the old "Argo CD API" error text the production code had already moved past (server/applicationset now passes). Left the generated ArgoprojV1alpha1 clientset accessor, the common.ClientAppID-family OAuth constants, the Argocd-* extension HTTP headers, and every .proto's wire-format package path untouched -- all external contract, not our voice.
132 lines
4.1 KiB
Go
132 lines
4.1 KiB
Go
package metrics
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
|
|
"github.com/hanzoai/cd/common"
|
|
"github.com/hanzoai/cd/util/metrics/kubectl"
|
|
"github.com/hanzoai/cd/util/profile"
|
|
)
|
|
|
|
type MetricsServer struct {
|
|
*http.Server
|
|
redisRequestCounter *prometheus.CounterVec
|
|
redisRequestHistogram *prometheus.HistogramVec
|
|
extensionRequestCounter *prometheus.CounterVec
|
|
extensionRequestDuration *prometheus.HistogramVec
|
|
loginRequestCounter *prometheus.CounterVec
|
|
PrometheusRegistry *prometheus.Registry
|
|
}
|
|
|
|
var (
|
|
redisRequestCounter = prometheus.NewCounterVec(
|
|
prometheus.CounterOpts{
|
|
Name: "cd_redis_request_total",
|
|
Help: "Number of kubernetes requests executed during application reconciliation.",
|
|
},
|
|
[]string{"initiator", "failed"},
|
|
)
|
|
redisRequestHistogram = prometheus.NewHistogramVec(
|
|
prometheus.HistogramOpts{
|
|
Name: "cd_redis_request_duration",
|
|
Help: "Redis requests duration.",
|
|
Buckets: []float64{0.1, 0.25, .5, 1, 2},
|
|
},
|
|
[]string{"initiator"},
|
|
)
|
|
extensionRequestCounter = prometheus.NewCounterVec(
|
|
prometheus.CounterOpts{
|
|
Name: "cd_proxy_extension_request_total",
|
|
Help: "Number of requests sent to configured proxy extensions.",
|
|
},
|
|
[]string{"extension", "status"},
|
|
)
|
|
extensionRequestDuration = prometheus.NewHistogramVec(
|
|
prometheus.HistogramOpts{
|
|
Name: "cd_proxy_extension_request_duration_seconds",
|
|
Help: "Request duration in seconds between the Hanzo CD API server and the extension backend.",
|
|
Buckets: []float64{0.1, 0.25, .5, 1, 2, 5, 10},
|
|
},
|
|
[]string{"extension"},
|
|
)
|
|
loginRequestCounter = prometheus.NewCounterVec(
|
|
prometheus.CounterOpts{
|
|
Name: "cd_login_request_total",
|
|
Help: "Number of login requests to the Hanzo CD API server.",
|
|
},
|
|
[]string{"status"},
|
|
)
|
|
versionGauge = prometheus.NewGaugeVec(
|
|
prometheus.GaugeOpts{
|
|
Name: "cd_info",
|
|
Help: "Hanzo CD version information",
|
|
},
|
|
[]string{"version"},
|
|
)
|
|
)
|
|
|
|
// NewMetricsServer returns a new prometheus server which collects api server metrics
|
|
func NewMetricsServer(host string, port int) *MetricsServer {
|
|
mux := http.NewServeMux()
|
|
registry := prometheus.NewRegistry()
|
|
mux.Handle("/metrics", promhttp.HandlerFor(prometheus.Gatherers{
|
|
registry,
|
|
prometheus.DefaultGatherer,
|
|
}, promhttp.HandlerOpts{}))
|
|
versionGauge.WithLabelValues(common.GetVersion().Version).Set(1)
|
|
|
|
profile.RegisterProfiler(mux)
|
|
|
|
registry.MustRegister(redisRequestCounter)
|
|
registry.MustRegister(redisRequestHistogram)
|
|
registry.MustRegister(extensionRequestCounter)
|
|
registry.MustRegister(extensionRequestDuration)
|
|
registry.MustRegister(loginRequestCounter)
|
|
registry.MustRegister(versionGauge)
|
|
|
|
kubectl.RegisterWithClientGo()
|
|
kubectl.RegisterWithPrometheus(registry)
|
|
|
|
return &MetricsServer{
|
|
Server: &http.Server{
|
|
Addr: fmt.Sprintf("%s:%d", host, port),
|
|
Handler: mux,
|
|
},
|
|
redisRequestCounter: redisRequestCounter,
|
|
redisRequestHistogram: redisRequestHistogram,
|
|
extensionRequestCounter: extensionRequestCounter,
|
|
extensionRequestDuration: extensionRequestDuration,
|
|
loginRequestCounter: loginRequestCounter,
|
|
PrometheusRegistry: registry,
|
|
}
|
|
}
|
|
|
|
func (m *MetricsServer) IncRedisRequest(failed bool) {
|
|
m.redisRequestCounter.WithLabelValues("cd-server", strconv.FormatBool(failed)).Inc()
|
|
}
|
|
|
|
// ObserveRedisRequestDuration observes redis request duration
|
|
func (m *MetricsServer) ObserveRedisRequestDuration(duration time.Duration) {
|
|
m.redisRequestHistogram.WithLabelValues("cd-server").Observe(duration.Seconds())
|
|
}
|
|
|
|
func (m *MetricsServer) IncExtensionRequestCounter(extension string, status int) {
|
|
m.extensionRequestCounter.WithLabelValues(extension, strconv.Itoa(status)).Inc()
|
|
}
|
|
|
|
func (m *MetricsServer) ObserveExtensionRequestDuration(extension string, duration time.Duration) {
|
|
m.extensionRequestDuration.WithLabelValues(extension).Observe(duration.Seconds())
|
|
}
|
|
|
|
// IncLoginRequestCounter increments the login request counter with the given status
|
|
// status can be "success" or "failure"
|
|
func (m *MetricsServer) IncLoginRequestCounter(status string) {
|
|
m.loginRequestCounter.WithLabelValues(status).Inc()
|
|
}
|