diff --git a/changelog/fragments/helm-ansible-version-metric.yaml b/changelog/fragments/helm-ansible-version-metric.yaml new file mode 100644 index 00000000000..67a740de210 --- /dev/null +++ b/changelog/fragments/helm-ansible-version-metric.yaml @@ -0,0 +1,9 @@ +entries: + - description: > + In Ansible-based operators, added the `ansible_operator_build_info` + metric to instrument commit and version information. + kind: "addition" + - description: > + In Helm-based operators, added the `helm_operator_build_info` + metric to instrument commit and version information. + kind: "addition" diff --git a/internal/ansible/metrics/metrics.go b/internal/ansible/metrics/metrics.go index 4b238c04bb8..c3a58a51fb5 100644 --- a/internal/ansible/metrics/metrics.go +++ b/internal/ansible/metrics/metrics.go @@ -20,6 +20,8 @@ import ( "github.com/prometheus/client_golang/prometheus" logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/metrics" + + sdkVersion "github.com/operator-framework/operator-sdk/internal/version" ) const ( @@ -27,6 +29,17 @@ const ( ) var ( + buildInfo = prometheus.NewGauge( + prometheus.GaugeOpts{ + Subsystem: subsystem, + Name: "build_info", + Help: "Build information for the ansible-operator binary", + ConstLabels: map[string]string{ + "commit": sdkVersion.GitCommit, + "version": sdkVersion.Version, + }, + }) + reconcileResults = prometheus.NewGaugeVec( prometheus.GaugeOpts{ Subsystem: subsystem, @@ -50,6 +63,7 @@ var ( ) func init() { + metrics.Registry.MustRegister(buildInfo) metrics.Registry.MustRegister(reconcileResults) metrics.Registry.MustRegister(reconciles) } @@ -64,6 +78,11 @@ func recoverMetricPanic() { } } +func RegisterBuildInfo(r prometheus.Registerer) { + buildInfo.Set(1) + r.MustRegister(buildInfo) +} + func ReconcileSucceeded(gvk string) { defer recoverMetricPanic() reconcileResults.WithLabelValues(gvk, "succeeded").Inc() diff --git a/internal/cmd/ansible-operator/run/cmd.go b/internal/cmd/ansible-operator/run/cmd.go index b7183cdc448..58661752b7c 100644 --- a/internal/cmd/ansible-operator/run/cmd.go +++ b/internal/cmd/ansible-operator/run/cmd.go @@ -33,9 +33,11 @@ import ( zapf "sigs.k8s.io/controller-runtime/pkg/log/zap" "sigs.k8s.io/controller-runtime/pkg/manager" "sigs.k8s.io/controller-runtime/pkg/manager/signals" + crmetrics "sigs.k8s.io/controller-runtime/pkg/metrics" "github.com/operator-framework/operator-sdk/internal/ansible/controller" "github.com/operator-framework/operator-sdk/internal/ansible/flags" + "github.com/operator-framework/operator-sdk/internal/ansible/metrics" "github.com/operator-framework/operator-sdk/internal/ansible/proxy" "github.com/operator-framework/operator-sdk/internal/ansible/proxy/controllermap" "github.com/operator-framework/operator-sdk/internal/ansible/runner" @@ -81,6 +83,7 @@ func NewCmd() *cobra.Command { func run(cmd *cobra.Command, f *flags.Flags) { printVersion() + metrics.RegisterBuildInfo(crmetrics.Registry) cfg, err := config.GetConfig() if err != nil { diff --git a/internal/cmd/helm-operator/run/cmd.go b/internal/cmd/helm-operator/run/cmd.go index b5f672d8e7c..7b99c33a0bd 100644 --- a/internal/cmd/helm-operator/run/cmd.go +++ b/internal/cmd/helm-operator/run/cmd.go @@ -31,9 +31,11 @@ import ( zapf "sigs.k8s.io/controller-runtime/pkg/log/zap" "sigs.k8s.io/controller-runtime/pkg/manager" "sigs.k8s.io/controller-runtime/pkg/manager/signals" + crmetrics "sigs.k8s.io/controller-runtime/pkg/metrics" "github.com/operator-framework/operator-sdk/internal/helm/controller" "github.com/operator-framework/operator-sdk/internal/helm/flags" + "github.com/operator-framework/operator-sdk/internal/helm/metrics" "github.com/operator-framework/operator-sdk/internal/helm/release" "github.com/operator-framework/operator-sdk/internal/helm/watches" "github.com/operator-framework/operator-sdk/internal/util/k8sutil" @@ -73,6 +75,7 @@ func NewCmd() *cobra.Command { func run(cmd *cobra.Command, f *flags.Flags) { printVersion() + metrics.RegisterBuildInfo(crmetrics.Registry) cfg, err := config.GetConfig() if err != nil { diff --git a/internal/helm/metrics/metrics.go b/internal/helm/metrics/metrics.go new file mode 100644 index 00000000000..992eb405eb0 --- /dev/null +++ b/internal/helm/metrics/metrics.go @@ -0,0 +1,44 @@ +// Copyright 2020 The Operator-SDK 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 metrics + +import ( + "github.com/prometheus/client_golang/prometheus" + + sdkVersion "github.com/operator-framework/operator-sdk/internal/version" +) + +const ( + subsystem = "helm_operator" +) + +var ( + buildInfo = prometheus.NewGauge( + prometheus.GaugeOpts{ + Subsystem: subsystem, + Name: "build_info", + Help: "Build information for the helm-operator binary", + ConstLabels: map[string]string{ + "commit": sdkVersion.GitCommit, + "version": sdkVersion.Version, + }, + }, + ) +) + +func RegisterBuildInfo(r prometheus.Registerer) { + buildInfo.Set(1) + r.MustRegister(buildInfo) +}