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..7c6761aa --- /dev/null +++ b/internal/metrics/metrics.go @@ -0,0 +1,45 @@ +// 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. +// 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) +}