diff --git a/controllers/ssp_controller.go b/controllers/ssp_controller.go index fc1b49135..44001e059 100644 --- a/controllers/ssp_controller.go +++ b/controllers/ssp_controller.go @@ -50,6 +50,7 @@ import ( "kubevirt.io/ssp-operator/internal/controller/predicates" crd_watch "kubevirt.io/ssp-operator/internal/crd-watch" "kubevirt.io/ssp-operator/internal/operands" + "kubevirt.io/ssp-operator/pkg/monitoring/metrics/ssp-operator" ) const ( @@ -128,7 +129,7 @@ func (r *sspReconciler) setupController(mgr ctrl.Manager) error { func (r *sspReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res ctrl.Result, err error) { defer func() { if err != nil { - common.SSPOperatorReconcileSucceeded.Set(0) + metrics.SetSspOperatorReconcileSucceeded(false) } }() reqLogger := r.log.WithValues("ssp", req.NamespacedName) @@ -221,9 +222,9 @@ func (r *sspReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res ct sspRequest.Logger.Info("CR status updated") if sspRequest.Instance.Status.Phase == lifecycleapi.PhaseDeployed { - common.SSPOperatorReconcileSucceeded.Set(1) + metrics.SetSspOperatorReconcileSucceeded(true) } else { - common.SSPOperatorReconcileSucceeded.Set(0) + metrics.SetSspOperatorReconcileSucceeded(false) } return ctrl.Result{}, nil diff --git a/controllers/vm_controller.go b/controllers/vm_controller.go index 0a2612301..9ffb3eab4 100644 --- a/controllers/vm_controller.go +++ b/controllers/vm_controller.go @@ -11,7 +11,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/reconcile" - "kubevirt.io/ssp-operator/pkg/monitoring/metrics" + "kubevirt.io/ssp-operator/pkg/monitoring/metrics/ssp-operator" ) // +kubebuilder:rbac:groups=kubevirt.io,resources=virtualmachines,verbs=get;list;watch diff --git a/internal/common/resource.go b/internal/common/resource.go index e8a336365..d69a0b6f9 100644 --- a/internal/common/resource.go +++ b/internal/common/resource.go @@ -8,7 +8,6 @@ import ( routev1 "github.com/openshift/api/route/v1" libhandler "github.com/operator-framework/operator-lib/handler" promv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" - "github.com/prometheus/client_golang/prometheus" tekton "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" apps "k8s.io/api/apps/v1" core "k8s.io/api/core/v1" @@ -114,13 +113,6 @@ type reconcileBuilder struct { var _ ReconcileBuilder = &reconcileBuilder{} -var ( - SSPOperatorReconcileSucceeded = prometheus.NewGauge(prometheus.GaugeOpts{ - Name: "kubevirt_ssp_operator_reconcile_succeeded", - Help: "Set to 1 if the reconcile process of all operands completes with no errors, and to 0 otherwise", - }) -) - func (r *reconcileBuilder) NamespacedResource(resource client.Object) ReconcileBuilder { r.resource = resource r.isClusterResource = false diff --git a/internal/operands/common-templates/reconcile.go b/internal/operands/common-templates/reconcile.go index fcef6ad33..44664bfa4 100644 --- a/internal/operands/common-templates/reconcile.go +++ b/internal/operands/common-templates/reconcile.go @@ -14,17 +14,10 @@ import ( "github.com/blang/semver/v4" "github.com/go-logr/logr" - "github.com/prometheus/client_golang/prometheus" "kubevirt.io/ssp-operator/internal/common" "kubevirt.io/ssp-operator/internal/operands" -) - -var ( - CommonTemplatesRestored = prometheus.NewCounter(prometheus.CounterOpts{ - Name: "kubevirt_ssp_common_templates_restored_total", - Help: "The total number of common templates restored by the operator back to their original state", - }) + metrics "kubevirt.io/ssp-operator/pkg/monitoring/metrics/template-validator" ) // Define RBAC rules needed by this operand: @@ -109,7 +102,7 @@ func incrementTemplatesRestoredMetric(reconcileResults []common.ReconcileResult, if reconcileResult.OperationResult == common.OperationResultUpdated && oldVersion == newVersion { logger.Info(fmt.Sprintf("Changes reverted in common template: %s", reconcileResult.Resource.GetName())) - CommonTemplatesRestored.Inc() + metrics.IncCommonTemplatesRestored() } } } diff --git a/internal/operands/common-templates/reconcile_test.go b/internal/operands/common-templates/reconcile_test.go index 2d76e914d..73a17504a 100644 --- a/internal/operands/common-templates/reconcile_test.go +++ b/internal/operands/common-templates/reconcile_test.go @@ -10,8 +10,6 @@ import ( templatev1 "github.com/openshift/api/template/v1" libhandler "github.com/operator-framework/operator-lib/handler" - "github.com/prometheus/client_golang/prometheus" - io_prometheus_client "github.com/prometheus/client_model/go" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" lifecycleapi "kubevirt.io/controller-lifecycle-operator-sdk/api" @@ -24,6 +22,7 @@ import ( "kubevirt.io/ssp-operator/internal/common" "kubevirt.io/ssp-operator/internal/operands" . "kubevirt.io/ssp-operator/internal/test-utils" + metrics "kubevirt.io/ssp-operator/pkg/monitoring/metrics/template-validator" ) var log = logf.Log.WithName("common-templates-operand") @@ -98,8 +97,8 @@ var _ = Describe("Common-Templates operand", func() { ExpectResourceExists(&template, request) } - desc, value := getCommonTemplatesRestoredMetric() - Expect(desc).To(ContainSubstring("kubevirt_ssp_common_templates_restored_total")) + value, err := metrics.GetCommonTemplatesRestored() + Expect(err).ToNot(HaveOccurred()) Expect(value).To(BeZero()) }) @@ -131,8 +130,8 @@ var _ = Describe("Common-Templates operand", func() { Expect(template.Labels).To(HaveKey(testLabel)) } - desc, value := getCommonTemplatesRestoredMetric() - Expect(desc).To(ContainSubstring("kubevirt_ssp_common_templates_restored_total")) + value, err := metrics.GetCommonTemplatesRestored() + Expect(err).ToNot(HaveOccurred()) Expect(value).To(Equal(float64(len(testTemplates)))) }) @@ -289,8 +288,8 @@ var _ = Describe("Common-Templates operand", func() { template = getTemplate(request, &testTemplates[0]) template.Namespace = namespace - desc, value := getCommonTemplatesRestoredMetric() - Expect(desc).To(ContainSubstring("kubevirt_ssp_common_templates_restored_total")) + value, err := metrics.GetCommonTemplatesRestored() + Expect(err).ToNot(HaveOccurred()) initialMetricValue = value }) @@ -305,8 +304,8 @@ var _ = Describe("Common-Templates operand", func() { updatedTpl := getTemplate(request, template) Expect(updatedTpl.Labels[TemplateTypeLabel]).To(Equal(testTemplates[0].Labels[TemplateTypeLabel])) - desc, value := getCommonTemplatesRestoredMetric() - Expect(desc).To(ContainSubstring("kubevirt_ssp_common_templates_restored_total")) + value, err := metrics.GetCommonTemplatesRestored() + Expect(err).ToNot(HaveOccurred()) Expect(value).To(Equal(initialMetricValue + 1)) }) @@ -322,8 +321,8 @@ var _ = Describe("Common-Templates operand", func() { updatedTpl := getTemplate(request, template) Expect(updatedTpl.Labels[TemplateTypeLabel]).To(Equal(testTemplates[0].Labels[TemplateTypeLabel])) - desc, value := getCommonTemplatesRestoredMetric() - Expect(desc).To(ContainSubstring("kubevirt_ssp_common_templates_restored_total")) + value, err := metrics.GetCommonTemplatesRestored() + Expect(err).ToNot(HaveOccurred()) Expect(value).To(Equal(initialMetricValue)) }) }) @@ -355,18 +354,6 @@ func getTestTemplates() []templatev1.Template { }} } -func getCommonTemplatesRestoredMetric() (string, float64) { - ch := make(chan prometheus.Metric, 1) - CommonTemplatesRestored.Collect(ch) - close(ch) - m := <-ch - metric := &io_prometheus_client.Metric{} - err := m.Write(metric) - Expect(err).ToNot(HaveOccurred()) - - return m.Desc().String(), metric.GetCounter().GetValue() -} - func getTemplate(req common.Request, template *templatev1.Template) *templatev1.Template { key := client.ObjectKeyFromObject(template) updatedTpl := &templatev1.Template{} diff --git a/internal/template-validator/main.go b/internal/template-validator/main.go index 86fec6109..421be19fe 100644 --- a/internal/template-validator/main.go +++ b/internal/template-validator/main.go @@ -5,11 +5,16 @@ import ( "kubevirt.io/ssp-operator/internal/template-validator/service" "kubevirt.io/ssp-operator/internal/template-validator/validator" + validatorMetrics "kubevirt.io/ssp-operator/pkg/monitoring/metrics/template-validator" ) func Main() int { app := &validator.App{} service.Setup(app) + + // setup monitoring + validatorMetrics.SetupMetrics() + app.Run() return 0 } diff --git a/internal/template-validator/webhooks/hook.go b/internal/template-validator/webhooks/hook.go index e6bced859..b725ea2f5 100644 --- a/internal/template-validator/webhooks/hook.go +++ b/internal/template-validator/webhooks/hook.go @@ -24,8 +24,6 @@ import ( "net/http" "strings" - "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/client_golang/prometheus/promauto" admissionv1 "k8s.io/api/admission/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/controller-runtime/pkg/client" @@ -34,13 +32,7 @@ import ( "kubevirt.io/ssp-operator/internal/template-validator/labels" "kubevirt.io/ssp-operator/internal/template-validator/logger" "kubevirt.io/ssp-operator/internal/template-validator/virtinformers" -) - -var ( - templateValidatorRejected = promauto.NewCounter(prometheus.CounterOpts{ - Name: "kubevirt_ssp_template_validator_rejected_total", - Help: "The total number of rejected template validators", - }) + "kubevirt.io/ssp-operator/pkg/monitoring/metrics/template-validator" ) const ( @@ -102,7 +94,7 @@ func (w *webhooks) admitVm(ar *admissionv1.AdmissionReview) *admissionv1.Admissi causes := ValidateVm(rules, vm) if len(causes) > 0 { - templateValidatorRejected.Inc() + metrics.IncTemplateValidatorRejected() return ToAdmissionResponse(causes) } diff --git a/main.go b/main.go index a7df04710..199ead2b7 100644 --- a/main.go +++ b/main.go @@ -43,8 +43,7 @@ import ( ssp "kubevirt.io/ssp-operator/api/v1beta2" "kubevirt.io/ssp-operator/controllers" "kubevirt.io/ssp-operator/internal/common" - common_templates "kubevirt.io/ssp-operator/internal/operands/common-templates" - sspMetrics "kubevirt.io/ssp-operator/pkg/monitoring/metrics" + sspMetrics "kubevirt.io/ssp-operator/pkg/monitoring/metrics/ssp-operator" "kubevirt.io/ssp-operator/webhooks" // +kubebuilder:scaffold:imports ) @@ -202,8 +201,8 @@ func main() { opts := zap.Options{} opts.BindFlags(flag.CommandLine) flag.Parse() - metrics.Registry.MustRegister(common_templates.CommonTemplatesRestored) - metrics.Registry.MustRegister(common.SSPOperatorReconcileSucceeded) + + // setup monitoring sspMetrics.SetupMetrics() ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts))) diff --git a/pkg/monitoring/metrics/metrics.go b/pkg/monitoring/metrics/ssp-operator/metrics.go similarity index 53% rename from pkg/monitoring/metrics/metrics.go rename to pkg/monitoring/metrics/ssp-operator/metrics.go index b8fe50941..7d6e4a29a 100644 --- a/pkg/monitoring/metrics/metrics.go +++ b/pkg/monitoring/metrics/ssp-operator/metrics.go @@ -5,22 +5,13 @@ import ( runtimemetrics "sigs.k8s.io/controller-runtime/pkg/metrics" ) -const metricPrefix = "kubevirt_ssp_" - -var ( - metrics = [][]operatormetrics.Metric{ - rbdMetrics, - } -) - func SetupMetrics() { operatormetrics.Register = runtimemetrics.Registry.Register - if err := operatormetrics.RegisterMetrics(metrics...); err != nil { + if err := operatormetrics.RegisterMetrics( + operatorMetrics, + rbdMetrics, + ); err != nil { panic(err) } } - -func ListMetrics() []operatormetrics.Metric { - return operatormetrics.ListMetrics() -} diff --git a/pkg/monitoring/metrics/metrics_suite_test.go b/pkg/monitoring/metrics/ssp-operator/metrics_suite_test.go similarity index 100% rename from pkg/monitoring/metrics/metrics_suite_test.go rename to pkg/monitoring/metrics/ssp-operator/metrics_suite_test.go diff --git a/pkg/monitoring/metrics/ssp-operator/operator_metrics.go b/pkg/monitoring/metrics/ssp-operator/operator_metrics.go new file mode 100644 index 000000000..256649a02 --- /dev/null +++ b/pkg/monitoring/metrics/ssp-operator/operator_metrics.go @@ -0,0 +1,24 @@ +package metrics + +import "github.com/machadovilaca/operator-observability/pkg/operatormetrics" + +var ( + operatorMetrics = []operatormetrics.Metric{ + sspOperatorReconcileSucceeded, + } + + sspOperatorReconcileSucceeded = operatormetrics.NewGauge( + operatormetrics.MetricOpts{ + Name: "kubevirt_ssp_operator_reconcile_succeeded", + Help: "Set to 1 if the reconcile process of all operands completes with no errors, and to 0 otherwise", + }, + ) +) + +func SetSspOperatorReconcileSucceeded(isSucceeded bool) { + value := 0.0 + if isSucceeded { + value = 1.0 + } + sspOperatorReconcileSucceeded.Set(value) +} diff --git a/pkg/monitoring/metrics/rbd_metrics.go b/pkg/monitoring/metrics/ssp-operator/rbd_metrics.go similarity index 96% rename from pkg/monitoring/metrics/rbd_metrics.go rename to pkg/monitoring/metrics/ssp-operator/rbd_metrics.go index 3c30b711f..f70bb56bb 100644 --- a/pkg/monitoring/metrics/rbd_metrics.go +++ b/pkg/monitoring/metrics/ssp-operator/rbd_metrics.go @@ -16,7 +16,7 @@ var ( vmRbdVolume = operatormetrics.NewGaugeVec( operatormetrics.MetricOpts{ - Name: metricPrefix + "vm_rbd_volume", + Name: "kubevirt_ssp_vm_rbd_volume", Help: "VM with RBD mounted volume", ExtraFields: map[string]string{ "StabilityLevel": "ALPHA", diff --git a/pkg/monitoring/metrics/rbd_metrics_test.go b/pkg/monitoring/metrics/ssp-operator/rbd_metrics_test.go similarity index 100% rename from pkg/monitoring/metrics/rbd_metrics_test.go rename to pkg/monitoring/metrics/ssp-operator/rbd_metrics_test.go diff --git a/pkg/monitoring/metrics/template-validator/metrics.go b/pkg/monitoring/metrics/template-validator/metrics.go new file mode 100644 index 000000000..6c9706e4f --- /dev/null +++ b/pkg/monitoring/metrics/template-validator/metrics.go @@ -0,0 +1,16 @@ +package metrics + +import ( + "github.com/machadovilaca/operator-observability/pkg/operatormetrics" + runtimemetrics "sigs.k8s.io/controller-runtime/pkg/metrics" +) + +func SetupMetrics() { + operatormetrics.Register = runtimemetrics.Registry.Register + + if err := operatormetrics.RegisterMetrics( + templateMetrics, + ); err != nil { + panic(err) + } +} diff --git a/pkg/monitoring/metrics/template-validator/template_metrics.go b/pkg/monitoring/metrics/template-validator/template_metrics.go new file mode 100644 index 000000000..059a79eb4 --- /dev/null +++ b/pkg/monitoring/metrics/template-validator/template_metrics.go @@ -0,0 +1,44 @@ +package metrics + +import ( + "github.com/machadovilaca/operator-observability/pkg/operatormetrics" + ioprometheusclient "github.com/prometheus/client_model/go" +) + +var ( + templateMetrics = []operatormetrics.Metric{ + commonTemplatesRestored, + templateValidatorRejected, + } + + commonTemplatesRestored = operatormetrics.NewCounter( + operatormetrics.MetricOpts{ + Name: "kubevirt_ssp_common_templates_restored_total", + Help: "The total number of common templates restored by the operator back to their original state", + }, + ) + + templateValidatorRejected = operatormetrics.NewCounter( + operatormetrics.MetricOpts{ + Name: "kubevirt_ssp_template_validator_rejected_total", + Help: "The total number of rejected template validators", + }, + ) +) + +func IncCommonTemplatesRestored() { + commonTemplatesRestored.Inc() +} + +func GetCommonTemplatesRestored() (float64, error) { + dto := &ioprometheusclient.Metric{} + err := commonTemplatesRestored.Write(dto) + if err != nil { + return 0, err + } + return dto.Counter.GetValue(), nil +} + +func IncTemplateValidatorRejected() { + templateValidatorRejected.Inc() +} diff --git a/tools/metricsdocs/metricsdocs.go b/tools/metricsdocs/metricsdocs.go index a69d47787..f5bdcce16 100644 --- a/tools/metricsdocs/metricsdocs.go +++ b/tools/metricsdocs/metricsdocs.go @@ -2,11 +2,13 @@ package main import ( "fmt" + "github.com/machadovilaca/operator-observability/pkg/operatormetrics" "sort" "strings" "kubevirt.io/ssp-operator/internal/operands/metrics" - sspMetrics "kubevirt.io/ssp-operator/pkg/monitoring/metrics" + sspMetrics "kubevirt.io/ssp-operator/pkg/monitoring/metrics/ssp-operator" + validatorMetrics "kubevirt.io/ssp-operator/pkg/monitoring/metrics/template-validator" ) const ( @@ -27,11 +29,12 @@ const ( ) func main() { - metricsList := getMetricsList() - metricsList = append(metricsList, recordRulesDescToMetricList(metrics.RecordRulesDescList)...) + metricsList := recordRulesDescToMetricList(metrics.RecordRulesDescList) sspMetrics.SetupMetrics() - for _, m := range sspMetrics.ListMetrics() { + validatorMetrics.SetupMetrics() + + for _, m := range operatormetrics.ListMetrics() { metricsList = append(metricsList, metric{ name: m.GetOpts().Name, description: m.GetOpts().Help, @@ -72,28 +75,6 @@ func metricDescriptionToMetric(rrd metrics.RecordRulesDesc) metric { } } -func getMetricsList() metricList { - metrics := metricList{ - { - name: "kubevirt_ssp_template_validator_rejected_total", - description: "The total number of rejected template validators", - mtype: "Counter", - }, - { - name: "kubevirt_ssp_operator_reconcile_succeeded", - description: "Set to 1 if the reconcile process of all operands completes with no errors, and to 0 otherwise", - mtype: "Gauge", - }, - { - name: "kubevirt_ssp_common_templates_restored_total", - description: "The total number of common templates restored by the operator back to their original state", - mtype: "Counter", - }, - } - - return metrics -} - func (m metric) writeOut() { fmt.Println("###", m.name) fmt.Println(m.description + ". Type: " + m.mtype + ".") diff --git a/validator.Dockerfile b/validator.Dockerfile index eb823a7c3..39a7c5c60 100644 --- a/validator.Dockerfile +++ b/validator.Dockerfile @@ -21,6 +21,7 @@ COPY main.go main.go COPY api/ api/ COPY controllers/ controllers/ COPY internal/ internal/ +COPY pkg/ pkg/ RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -ldflags="-X 'kubevirt.io/ssp-operator/internal/template-validator/version.COMPONENT=$COMPONENT'\ -X 'kubevirt.io/ssp-operator/internal/template-validator/version.VERSION=$VERSION'\ diff --git a/vendor/github.com/prometheus/client_golang/prometheus/promauto/auto.go b/vendor/github.com/prometheus/client_golang/prometheus/promauto/auto.go deleted file mode 100644 index 8031e8704..000000000 --- a/vendor/github.com/prometheus/client_golang/prometheus/promauto/auto.go +++ /dev/null @@ -1,376 +0,0 @@ -// Copyright 2018 The Prometheus Authors -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// Package promauto provides alternative constructors for the fundamental -// Prometheus metric types and their …Vec and …Func variants. The difference to -// their counterparts in the prometheus package is that the promauto -// constructors register the Collectors with a registry before returning them. -// There are two sets of constructors. The constructors in the first set are -// top-level functions, while the constructors in the other set are methods of -// the Factory type. The top-level function return Collectors registered with -// the global registry (prometheus.DefaultRegisterer), while the methods return -// Collectors registered with the registry the Factory was constructed with. All -// constructors panic if the registration fails. -// -// The following example is a complete program to create a histogram of normally -// distributed random numbers from the math/rand package: -// -// package main -// -// import ( -// "math/rand" -// "net/http" -// -// "github.com/prometheus/client_golang/prometheus" -// "github.com/prometheus/client_golang/prometheus/promauto" -// "github.com/prometheus/client_golang/prometheus/promhttp" -// ) -// -// var histogram = promauto.NewHistogram(prometheus.HistogramOpts{ -// Name: "random_numbers", -// Help: "A histogram of normally distributed random numbers.", -// Buckets: prometheus.LinearBuckets(-3, .1, 61), -// }) -// -// func Random() { -// for { -// histogram.Observe(rand.NormFloat64()) -// } -// } -// -// func main() { -// go Random() -// http.Handle("/metrics", promhttp.Handler()) -// http.ListenAndServe(":1971", nil) -// } -// -// Prometheus's version of a minimal hello-world program: -// -// package main -// -// import ( -// "fmt" -// "net/http" -// -// "github.com/prometheus/client_golang/prometheus" -// "github.com/prometheus/client_golang/prometheus/promauto" -// "github.com/prometheus/client_golang/prometheus/promhttp" -// ) -// -// func main() { -// http.Handle("/", promhttp.InstrumentHandlerCounter( -// promauto.NewCounterVec( -// prometheus.CounterOpts{ -// Name: "hello_requests_total", -// Help: "Total number of hello-world requests by HTTP code.", -// }, -// []string{"code"}, -// ), -// http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { -// fmt.Fprint(w, "Hello, world!") -// }), -// )) -// http.Handle("/metrics", promhttp.Handler()) -// http.ListenAndServe(":1971", nil) -// } -// -// A Factory is created with the With(prometheus.Registerer) function, which -// enables two usage pattern. With(prometheus.Registerer) can be called once per -// line: -// -// var ( -// reg = prometheus.NewRegistry() -// randomNumbers = promauto.With(reg).NewHistogram(prometheus.HistogramOpts{ -// Name: "random_numbers", -// Help: "A histogram of normally distributed random numbers.", -// Buckets: prometheus.LinearBuckets(-3, .1, 61), -// }) -// requestCount = promauto.With(reg).NewCounterVec( -// prometheus.CounterOpts{ -// Name: "http_requests_total", -// Help: "Total number of HTTP requests by status code and method.", -// }, -// []string{"code", "method"}, -// ) -// ) -// -// Or it can be used to create a Factory once to be used multiple times: -// -// var ( -// reg = prometheus.NewRegistry() -// factory = promauto.With(reg) -// randomNumbers = factory.NewHistogram(prometheus.HistogramOpts{ -// Name: "random_numbers", -// Help: "A histogram of normally distributed random numbers.", -// Buckets: prometheus.LinearBuckets(-3, .1, 61), -// }) -// requestCount = factory.NewCounterVec( -// prometheus.CounterOpts{ -// Name: "http_requests_total", -// Help: "Total number of HTTP requests by status code and method.", -// }, -// []string{"code", "method"}, -// ) -// ) -// -// This appears very handy. So why are these constructors locked away in a -// separate package? -// -// The main problem is that registration may fail, e.g. if a metric inconsistent -// with or equal to the newly to be registered one is already registered. -// Therefore, the Register method in the prometheus.Registerer interface returns -// an error, and the same is the case for the top-level prometheus.Register -// function that registers with the global registry. The prometheus package also -// provides MustRegister versions for both. They panic if the registration -// fails, and they clearly call this out by using the Must… idiom. Panicking is -// problematic in this case because it doesn't just happen on input provided by -// the caller that is invalid on its own. Things are a bit more subtle here: -// Metric creation and registration tend to be spread widely over the -// codebase. It can easily happen that an incompatible metric is added to an -// unrelated part of the code, and suddenly code that used to work perfectly -// fine starts to panic (provided that the registration of the newly added -// metric happens before the registration of the previously existing -// metric). This may come as an even bigger surprise with the global registry, -// where simply importing another package can trigger a panic (if the newly -// imported package registers metrics in its init function). At least, in the -// prometheus package, creation of metrics and other collectors is separate from -// registration. You first create the metric, and then you decide explicitly if -// you want to register it with a local or the global registry, and if you want -// to handle the error or risk a panic. With the constructors in the promauto -// package, registration is automatic, and if it fails, it will always -// panic. Furthermore, the constructors will often be called in the var section -// of a file, which means that panicking will happen as a side effect of merely -// importing a package. -// -// A separate package allows conservative users to entirely ignore it. And -// whoever wants to use it, will do so explicitly, with an opportunity to read -// this warning. -// -// Enjoy promauto responsibly! -package promauto - -import "github.com/prometheus/client_golang/prometheus" - -// NewCounter works like the function of the same name in the prometheus package -// but it automatically registers the Counter with the -// prometheus.DefaultRegisterer. If the registration fails, NewCounter panics. -func NewCounter(opts prometheus.CounterOpts) prometheus.Counter { - return With(prometheus.DefaultRegisterer).NewCounter(opts) -} - -// NewCounterVec works like the function of the same name in the prometheus -// package but it automatically registers the CounterVec with the -// prometheus.DefaultRegisterer. If the registration fails, NewCounterVec -// panics. -func NewCounterVec(opts prometheus.CounterOpts, labelNames []string) *prometheus.CounterVec { - return With(prometheus.DefaultRegisterer).NewCounterVec(opts, labelNames) -} - -// NewCounterFunc works like the function of the same name in the prometheus -// package but it automatically registers the CounterFunc with the -// prometheus.DefaultRegisterer. If the registration fails, NewCounterFunc -// panics. -func NewCounterFunc(opts prometheus.CounterOpts, function func() float64) prometheus.CounterFunc { - return With(prometheus.DefaultRegisterer).NewCounterFunc(opts, function) -} - -// NewGauge works like the function of the same name in the prometheus package -// but it automatically registers the Gauge with the -// prometheus.DefaultRegisterer. If the registration fails, NewGauge panics. -func NewGauge(opts prometheus.GaugeOpts) prometheus.Gauge { - return With(prometheus.DefaultRegisterer).NewGauge(opts) -} - -// NewGaugeVec works like the function of the same name in the prometheus -// package but it automatically registers the GaugeVec with the -// prometheus.DefaultRegisterer. If the registration fails, NewGaugeVec panics. -func NewGaugeVec(opts prometheus.GaugeOpts, labelNames []string) *prometheus.GaugeVec { - return With(prometheus.DefaultRegisterer).NewGaugeVec(opts, labelNames) -} - -// NewGaugeFunc works like the function of the same name in the prometheus -// package but it automatically registers the GaugeFunc with the -// prometheus.DefaultRegisterer. If the registration fails, NewGaugeFunc panics. -func NewGaugeFunc(opts prometheus.GaugeOpts, function func() float64) prometheus.GaugeFunc { - return With(prometheus.DefaultRegisterer).NewGaugeFunc(opts, function) -} - -// NewSummary works like the function of the same name in the prometheus package -// but it automatically registers the Summary with the -// prometheus.DefaultRegisterer. If the registration fails, NewSummary panics. -func NewSummary(opts prometheus.SummaryOpts) prometheus.Summary { - return With(prometheus.DefaultRegisterer).NewSummary(opts) -} - -// NewSummaryVec works like the function of the same name in the prometheus -// package but it automatically registers the SummaryVec with the -// prometheus.DefaultRegisterer. If the registration fails, NewSummaryVec -// panics. -func NewSummaryVec(opts prometheus.SummaryOpts, labelNames []string) *prometheus.SummaryVec { - return With(prometheus.DefaultRegisterer).NewSummaryVec(opts, labelNames) -} - -// NewHistogram works like the function of the same name in the prometheus -// package but it automatically registers the Histogram with the -// prometheus.DefaultRegisterer. If the registration fails, NewHistogram panics. -func NewHistogram(opts prometheus.HistogramOpts) prometheus.Histogram { - return With(prometheus.DefaultRegisterer).NewHistogram(opts) -} - -// NewHistogramVec works like the function of the same name in the prometheus -// package but it automatically registers the HistogramVec with the -// prometheus.DefaultRegisterer. If the registration fails, NewHistogramVec -// panics. -func NewHistogramVec(opts prometheus.HistogramOpts, labelNames []string) *prometheus.HistogramVec { - return With(prometheus.DefaultRegisterer).NewHistogramVec(opts, labelNames) -} - -// NewUntypedFunc works like the function of the same name in the prometheus -// package but it automatically registers the UntypedFunc with the -// prometheus.DefaultRegisterer. If the registration fails, NewUntypedFunc -// panics. -func NewUntypedFunc(opts prometheus.UntypedOpts, function func() float64) prometheus.UntypedFunc { - return With(prometheus.DefaultRegisterer).NewUntypedFunc(opts, function) -} - -// Factory provides factory methods to create Collectors that are automatically -// registered with a Registerer. Create a Factory with the With function, -// providing a Registerer to auto-register created Collectors with. The zero -// value of a Factory creates Collectors that are not registered with any -// Registerer. All methods of the Factory panic if the registration fails. -type Factory struct { - r prometheus.Registerer -} - -// With creates a Factory using the provided Registerer for registration of the -// created Collectors. If the provided Registerer is nil, the returned Factory -// creates Collectors that are not registered with any Registerer. -func With(r prometheus.Registerer) Factory { return Factory{r} } - -// NewCounter works like the function of the same name in the prometheus package -// but it automatically registers the Counter with the Factory's Registerer. -func (f Factory) NewCounter(opts prometheus.CounterOpts) prometheus.Counter { - c := prometheus.NewCounter(opts) - if f.r != nil { - f.r.MustRegister(c) - } - return c -} - -// NewCounterVec works like the function of the same name in the prometheus -// package but it automatically registers the CounterVec with the Factory's -// Registerer. -func (f Factory) NewCounterVec(opts prometheus.CounterOpts, labelNames []string) *prometheus.CounterVec { - c := prometheus.NewCounterVec(opts, labelNames) - if f.r != nil { - f.r.MustRegister(c) - } - return c -} - -// NewCounterFunc works like the function of the same name in the prometheus -// package but it automatically registers the CounterFunc with the Factory's -// Registerer. -func (f Factory) NewCounterFunc(opts prometheus.CounterOpts, function func() float64) prometheus.CounterFunc { - c := prometheus.NewCounterFunc(opts, function) - if f.r != nil { - f.r.MustRegister(c) - } - return c -} - -// NewGauge works like the function of the same name in the prometheus package -// but it automatically registers the Gauge with the Factory's Registerer. -func (f Factory) NewGauge(opts prometheus.GaugeOpts) prometheus.Gauge { - g := prometheus.NewGauge(opts) - if f.r != nil { - f.r.MustRegister(g) - } - return g -} - -// NewGaugeVec works like the function of the same name in the prometheus -// package but it automatically registers the GaugeVec with the Factory's -// Registerer. -func (f Factory) NewGaugeVec(opts prometheus.GaugeOpts, labelNames []string) *prometheus.GaugeVec { - g := prometheus.NewGaugeVec(opts, labelNames) - if f.r != nil { - f.r.MustRegister(g) - } - return g -} - -// NewGaugeFunc works like the function of the same name in the prometheus -// package but it automatically registers the GaugeFunc with the Factory's -// Registerer. -func (f Factory) NewGaugeFunc(opts prometheus.GaugeOpts, function func() float64) prometheus.GaugeFunc { - g := prometheus.NewGaugeFunc(opts, function) - if f.r != nil { - f.r.MustRegister(g) - } - return g -} - -// NewSummary works like the function of the same name in the prometheus package -// but it automatically registers the Summary with the Factory's Registerer. -func (f Factory) NewSummary(opts prometheus.SummaryOpts) prometheus.Summary { - s := prometheus.NewSummary(opts) - if f.r != nil { - f.r.MustRegister(s) - } - return s -} - -// NewSummaryVec works like the function of the same name in the prometheus -// package but it automatically registers the SummaryVec with the Factory's -// Registerer. -func (f Factory) NewSummaryVec(opts prometheus.SummaryOpts, labelNames []string) *prometheus.SummaryVec { - s := prometheus.NewSummaryVec(opts, labelNames) - if f.r != nil { - f.r.MustRegister(s) - } - return s -} - -// NewHistogram works like the function of the same name in the prometheus -// package but it automatically registers the Histogram with the Factory's -// Registerer. -func (f Factory) NewHistogram(opts prometheus.HistogramOpts) prometheus.Histogram { - h := prometheus.NewHistogram(opts) - if f.r != nil { - f.r.MustRegister(h) - } - return h -} - -// NewHistogramVec works like the function of the same name in the prometheus -// package but it automatically registers the HistogramVec with the Factory's -// Registerer. -func (f Factory) NewHistogramVec(opts prometheus.HistogramOpts, labelNames []string) *prometheus.HistogramVec { - h := prometheus.NewHistogramVec(opts, labelNames) - if f.r != nil { - f.r.MustRegister(h) - } - return h -} - -// NewUntypedFunc works like the function of the same name in the prometheus -// package but it automatically registers the UntypedFunc with the Factory's -// Registerer. -func (f Factory) NewUntypedFunc(opts prometheus.UntypedOpts, function func() float64) prometheus.UntypedFunc { - u := prometheus.NewUntypedFunc(opts, function) - if f.r != nil { - f.r.MustRegister(u) - } - return u -} diff --git a/vendor/modules.txt b/vendor/modules.txt index 1fa4c713b..3cd449d75 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -251,7 +251,6 @@ github.com/prometheus/client_golang/api/prometheus/v1 github.com/prometheus/client_golang/prometheus github.com/prometheus/client_golang/prometheus/collectors github.com/prometheus/client_golang/prometheus/internal -github.com/prometheus/client_golang/prometheus/promauto github.com/prometheus/client_golang/prometheus/promhttp # github.com/prometheus/client_model v0.4.0 ## explicit; go 1.18