Skip to content

Commit 16ddc79

Browse files
anmol372joelanford
andauthored
add build info metric (#67)
Co-authored-by: Joe Lanford <[email protected]>
1 parent 06e5a4f commit 16ddc79

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/onsi/ginkgo v1.14.1
1111
github.com/onsi/gomega v1.10.2
1212
github.com/operator-framework/operator-lib v0.3.0
13+
github.com/prometheus/client_golang v1.7.1
1314
github.com/sirupsen/logrus v1.7.0
1415
github.com/spf13/afero v1.2.2
1516
github.com/spf13/cobra v1.1.1

internal/cmd/run/cmd.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ import (
2929
"sigs.k8s.io/controller-runtime/pkg/healthz"
3030
logf "sigs.k8s.io/controller-runtime/pkg/log"
3131
zapl "sigs.k8s.io/controller-runtime/pkg/log/zap"
32+
crmetrics "sigs.k8s.io/controller-runtime/pkg/metrics"
3233

34+
"github.com/joelanford/helm-operator/internal/metrics"
3335
"github.com/joelanford/helm-operator/internal/version"
3436
"github.com/joelanford/helm-operator/pkg/annotation"
3537
"github.com/joelanford/helm-operator/pkg/manager"
@@ -96,6 +98,8 @@ func printVersion() {
9698
func (r *run) run(cmd *cobra.Command) {
9799
printVersion()
98100

101+
metrics.RegisterBuildInfo(crmetrics.Registry)
102+
99103
// Deprecated: OPERATOR_NAME environment variable is an artifact of the legacy operator-sdk project scaffolding.
100104
// Flag `--leader-election-id` should be used instead.
101105
if operatorName, found := os.LookupEnv("OPERATOR_NAME"); found {

internal/metrics/metrics.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2021 The Operator-SDK Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package metrics
16+
17+
import (
18+
"github.com/prometheus/client_golang/prometheus"
19+
20+
helmVersion "github.com/joelanford/helm-operator/internal/version"
21+
)
22+
23+
const (
24+
subsystem = "helm_operator"
25+
)
26+
27+
var (
28+
buildInfo = prometheus.NewGauge(
29+
prometheus.GaugeOpts{
30+
Subsystem: subsystem,
31+
Name: "build_info",
32+
Help: "Build information for the helm-operator binary",
33+
ConstLabels: map[string]string{
34+
"commit": helmVersion.GitCommit,
35+
"version": helmVersion.GitVersion,
36+
},
37+
},
38+
)
39+
)
40+
41+
//RegisterBuildInfo registers buildInfo Collector to be included in metrics collection
42+
func RegisterBuildInfo(r prometheus.Registerer) {
43+
buildInfo.Set(1)
44+
r.MustRegister(buildInfo)
45+
}

0 commit comments

Comments
 (0)