From b1d94d79a86d2bfa337609e79ebb17f4fcf45ee3 Mon Sep 17 00:00:00 2001 From: anmol372 Date: Mon, 23 Nov 2020 12:30:17 -0500 Subject: [PATCH 1/2] add build info metric --- go.mod | 1 + internal/cmd/run/cmd.go | 4 ++++ internal/metrics/metrics.go | 45 +++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 internal/metrics/metrics.go diff --git a/go.mod b/go.mod index 424566f3..4f40de0d 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/onsi/ginkgo v1.14.1 github.com/onsi/gomega v1.10.2 github.com/operator-framework/operator-lib v0.3.0 + github.com/prometheus/client_golang v1.7.1 github.com/sirupsen/logrus v1.7.0 github.com/spf13/afero v1.2.2 github.com/spf13/cobra v1.1.1 diff --git a/internal/cmd/run/cmd.go b/internal/cmd/run/cmd.go index 0086549a..84ccee95 100644 --- a/internal/cmd/run/cmd.go +++ b/internal/cmd/run/cmd.go @@ -29,7 +29,9 @@ import ( "sigs.k8s.io/controller-runtime/pkg/healthz" logf "sigs.k8s.io/controller-runtime/pkg/log" zapl "sigs.k8s.io/controller-runtime/pkg/log/zap" + crmetrics "sigs.k8s.io/controller-runtime/pkg/metrics" + "github.com/joelanford/helm-operator/internal/metrics" "github.com/joelanford/helm-operator/internal/version" "github.com/joelanford/helm-operator/pkg/annotation" "github.com/joelanford/helm-operator/pkg/manager" @@ -96,6 +98,8 @@ func printVersion() { func (r *run) run(cmd *cobra.Command) { printVersion() + metrics.RegisterBuildInfo(crmetrics.Registry) + // Deprecated: OPERATOR_NAME environment variable is an artifact of the legacy operator-sdk project scaffolding. // Flag `--leader-election-id` should be used instead. if operatorName, found := os.LookupEnv("OPERATOR_NAME"); found { diff --git a/internal/metrics/metrics.go b/internal/metrics/metrics.go new file mode 100644 index 00000000..2f0754d2 --- /dev/null +++ b/internal/metrics/metrics.go @@ -0,0 +1,45 @@ +// 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" + + helmVersion "github.com/joelanford/helm-operator/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": helmVersion.GitCommit, + "version": helmVersion.GitVersion, + }, + }, + ) +) + +//RegisterBuildInfo registers buildInfo Collector to be included in metrics collection +func RegisterBuildInfo(r prometheus.Registerer) { + buildInfo.Set(1) + r.MustRegister(buildInfo) +} From cbbf05e8afb5ca2e1fc7441f999a841fa44b50a6 Mon Sep 17 00:00:00 2001 From: Joe Lanford Date: Tue, 12 Jan 2021 10:44:07 -0500 Subject: [PATCH 2/2] Update internal/metrics/metrics.go --- internal/metrics/metrics.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/metrics/metrics.go b/internal/metrics/metrics.go index 2f0754d2..7c6761aa 100644 --- a/internal/metrics/metrics.go +++ b/internal/metrics/metrics.go @@ -1,4 +1,4 @@ -// Copyright 2020 The Operator-SDK Authors +// Copyright 2021 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.