mirror of
https://github.com/hanzoai/cd.git
synced 2026-10-30 14:56:45 +00:00
Rename test names, comments, fixture data, and mock GitHub/webhook responses that still said Argo/ArgoCD/argocd/argoproj in our own voice, matching the hanzocd-* binary/configmap names and the apps.hanzo.ai CRD/annotation domain already established elsewhere in the fork. Also drop the dated argov1alpha1/argoprojiov1alpha1 import aliases for our own v1alpha1 API package. Restore the CRD groups a prior blind rename had mangled on a vendored third-party Argo Workflows example manifest back to the real argoproj.io, and fix several places where an earlier half-finished rename had gone silently wrong instead of just being cosmetic: stale CD_BINARY_NAME values and a stale --argocd-repo-server flag no longer recognized by the binary, cert filenames and an ssh_known_hosts alias that didn't match what's actually on disk, a namespace and a kustomize image substitution that were silent no-ops, and a test assertion regex that could never match its own fixture. External lookup keys we don't control are left byte-exact: quay.io/argoprojlabs images, the docker.io/argoproj CI builder image, live argoproj/argo-workflows release URLs, the generated ArgoprojV1alpha1() clientset method, and the Makefile-coupled /tmp/argo-e2e and GOPATH mount conventions.
136 lines
4.1 KiB
Go
136 lines
4.1 KiB
Go
package metrics
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
"k8s.io/apimachinery/pkg/labels"
|
|
"sigs.k8s.io/controller-runtime/pkg/metrics"
|
|
|
|
"github.com/hanzoai/cd/pkg/apis/application/v1alpha1"
|
|
applisters "github.com/hanzoai/cd/pkg/client/listers/application/v1alpha1"
|
|
metricsutil "github.com/hanzoai/cd/util/metrics"
|
|
"github.com/hanzoai/cd/util/metrics/kubectl"
|
|
)
|
|
|
|
var (
|
|
descAppsetLabels *prometheus.Desc
|
|
descAppsetDefaultLabels = []string{"namespace", "name"}
|
|
descAppsetInfo = prometheus.NewDesc(
|
|
"cd_appset_info",
|
|
"Information about applicationset",
|
|
append(descAppsetDefaultLabels, "resource_update_status"),
|
|
nil,
|
|
)
|
|
|
|
descAppsetGeneratedApps = prometheus.NewDesc(
|
|
"cd_appset_owned_applications",
|
|
"Number of applications owned by the applicationset",
|
|
descAppsetDefaultLabels,
|
|
nil,
|
|
)
|
|
)
|
|
|
|
type ApplicationsetMetrics struct {
|
|
reconcileHistogram *prometheus.HistogramVec
|
|
}
|
|
|
|
type appsetCollector struct {
|
|
lister applisters.ApplicationSetLister
|
|
// appsClientSet appclientset.Interface
|
|
labels []string
|
|
filter func(appset *v1alpha1.ApplicationSet) bool
|
|
}
|
|
|
|
func NewApplicationsetMetrics(appsetLister applisters.ApplicationSetLister, appsetLabels []string, appsetFilter func(appset *v1alpha1.ApplicationSet) bool) ApplicationsetMetrics {
|
|
reconcileHistogram := prometheus.NewHistogramVec(
|
|
prometheus.HistogramOpts{
|
|
Name: "cd_appset_reconcile",
|
|
Help: "Application reconciliation performance in seconds.",
|
|
// Buckets can be set later on after observing median time
|
|
},
|
|
descAppsetDefaultLabels,
|
|
)
|
|
|
|
appsetCollector := newAppsetCollector(appsetLister, appsetLabels, appsetFilter)
|
|
|
|
// Register collectors and metrics
|
|
metrics.Registry.MustRegister(reconcileHistogram)
|
|
metrics.Registry.MustRegister(appsetCollector)
|
|
|
|
kubectl.RegisterWithClientGo()
|
|
kubectl.RegisterWithPrometheus(metrics.Registry)
|
|
|
|
return ApplicationsetMetrics{
|
|
reconcileHistogram: reconcileHistogram,
|
|
}
|
|
}
|
|
|
|
func (m *ApplicationsetMetrics) ObserveReconcile(appset *v1alpha1.ApplicationSet, duration time.Duration) {
|
|
m.reconcileHistogram.WithLabelValues(appset.Namespace, appset.Name).Observe(duration.Seconds())
|
|
}
|
|
|
|
func newAppsetCollector(lister applisters.ApplicationSetLister, labels []string, filter func(appset *v1alpha1.ApplicationSet) bool) *appsetCollector {
|
|
descAppsetDefaultLabels = []string{"namespace", "name"}
|
|
|
|
if len(labels) > 0 {
|
|
descAppsetLabels = prometheus.NewDesc(
|
|
"cd_appset_labels",
|
|
"Applicationset labels translated to Prometheus labels",
|
|
append(descAppsetDefaultLabels, metricsutil.NormalizeLabels("label", labels)...),
|
|
nil,
|
|
)
|
|
}
|
|
|
|
return &appsetCollector{
|
|
lister: lister,
|
|
labels: labels,
|
|
filter: filter,
|
|
}
|
|
}
|
|
|
|
// Describe implements the prometheus.Collector interface
|
|
func (c *appsetCollector) Describe(ch chan<- *prometheus.Desc) {
|
|
ch <- descAppsetInfo
|
|
ch <- descAppsetGeneratedApps
|
|
|
|
if len(c.labels) > 0 {
|
|
ch <- descAppsetLabels
|
|
}
|
|
}
|
|
|
|
// Collect implements the prometheus.Collector interface
|
|
func (c *appsetCollector) Collect(ch chan<- prometheus.Metric) {
|
|
appsets, _ := c.lister.List(labels.NewSelector())
|
|
|
|
for _, appset := range appsets {
|
|
if c.filter(appset) {
|
|
collectAppset(appset, c.labels, ch)
|
|
}
|
|
}
|
|
}
|
|
|
|
func collectAppset(appset *v1alpha1.ApplicationSet, labelsToCollect []string, ch chan<- prometheus.Metric) {
|
|
labelValues := make([]string, 0)
|
|
commonLabelValues := []string{appset.Namespace, appset.Name}
|
|
|
|
for _, label := range labelsToCollect {
|
|
labelValues = append(labelValues, appset.GetLabels()[label])
|
|
}
|
|
|
|
resourceUpdateStatus := "Unknown"
|
|
|
|
for _, condition := range appset.Status.Conditions {
|
|
if condition.Type == v1alpha1.ApplicationSetConditionResourcesUpToDate {
|
|
resourceUpdateStatus = condition.Reason
|
|
}
|
|
}
|
|
|
|
if len(labelsToCollect) > 0 {
|
|
ch <- prometheus.MustNewConstMetric(descAppsetLabels, prometheus.GaugeValue, 1, append(commonLabelValues, labelValues...)...)
|
|
}
|
|
|
|
ch <- prometheus.MustNewConstMetric(descAppsetInfo, prometheus.GaugeValue, 1, appset.Namespace, appset.Name, resourceUpdateStatus)
|
|
ch <- prometheus.MustNewConstMetric(descAppsetGeneratedApps, prometheus.GaugeValue, float64(len(appset.Status.Resources)), appset.Namespace, appset.Name)
|
|
}
|