mirror of
https://github.com/hanzoai/deploy.git
synced 2026-10-07 08:44:50 +00:00
go.mod still declared `module github.com/hanzoai/deploy` after the repo was renamed. A Go module path is not cosmetic — it is the resolvable location. So `go get github.com/hanzoai/cd` could not work, and anything importing this module had to spell a name that no longer exists at that address. The build log of v3.6.0 is where it surfaced: the compiler was still emitting github.com/hanzoai/deploy/cmd/cd/commands. 3541 import paths across 690 .go, 19 .proto, 4 .yaml, 3 .sh, 2 .mod, Procfile and Makefile. Plus the 62 ghcr.io/hanzoai/deploy image references, moved to ghcr.io/hanzoai/cd to match hanzo.yml's build target — a distinct string from the module path, replaced separately so neither could clobber the other. go mod tidy clean, CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build ./... clean. Co-authored-by: Hanzo Dev <[email protected]>
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"
|
|
|
|
argoappv1 "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 *argoappv1.ApplicationSet) bool
|
|
}
|
|
|
|
func NewApplicationsetMetrics(appsetLister applisters.ApplicationSetLister, appsetLabels []string, appsetFilter func(appset *argoappv1.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 *argoappv1.ApplicationSet, duration time.Duration) {
|
|
m.reconcileHistogram.WithLabelValues(appset.Namespace, appset.Name).Observe(duration.Seconds())
|
|
}
|
|
|
|
func newAppsetCollector(lister applisters.ApplicationSetLister, labels []string, filter func(appset *argoappv1.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 *argoappv1.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 == argoappv1.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)
|
|
}
|