Skip to content

Commit e56ad8a

Browse files
authored
internal/cmd: add build info metric in helm/ansible operators (#4220)
1 parent 93347d0 commit e56ad8a

File tree

5 files changed

+77
-0
lines changed

5 files changed

+77
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
entries:
2+
- description: >
3+
In Ansible-based operators, added the `ansible_operator_build_info`
4+
metric to instrument commit and version information.
5+
kind: "addition"
6+
- description: >
7+
In Helm-based operators, added the `helm_operator_build_info`
8+
metric to instrument commit and version information.
9+
kind: "addition"

internal/ansible/metrics/metrics.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,26 @@ import (
2020
"github.com/prometheus/client_golang/prometheus"
2121
logf "sigs.k8s.io/controller-runtime/pkg/log"
2222
"sigs.k8s.io/controller-runtime/pkg/metrics"
23+
24+
sdkVersion "github.com/operator-framework/operator-sdk/internal/version"
2325
)
2426

2527
const (
2628
subsystem = "ansible_operator"
2729
)
2830

2931
var (
32+
buildInfo = prometheus.NewGauge(
33+
prometheus.GaugeOpts{
34+
Subsystem: subsystem,
35+
Name: "build_info",
36+
Help: "Build information for the ansible-operator binary",
37+
ConstLabels: map[string]string{
38+
"commit": sdkVersion.GitCommit,
39+
"version": sdkVersion.Version,
40+
},
41+
})
42+
3043
reconcileResults = prometheus.NewGaugeVec(
3144
prometheus.GaugeOpts{
3245
Subsystem: subsystem,
@@ -64,6 +77,11 @@ func recoverMetricPanic() {
6477
}
6578
}
6679

80+
func RegisterBuildInfo(r prometheus.Registerer) {
81+
buildInfo.Set(1)
82+
r.MustRegister(buildInfo)
83+
}
84+
6785
func ReconcileSucceeded(gvk string) {
6886
defer recoverMetricPanic()
6987
reconcileResults.WithLabelValues(gvk, "succeeded").Inc()

internal/cmd/ansible-operator/run/cmd.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ import (
3333
zapf "sigs.k8s.io/controller-runtime/pkg/log/zap"
3434
"sigs.k8s.io/controller-runtime/pkg/manager"
3535
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
36+
crmetrics "sigs.k8s.io/controller-runtime/pkg/metrics"
3637

3738
"github.com/operator-framework/operator-sdk/internal/ansible/controller"
3839
"github.com/operator-framework/operator-sdk/internal/ansible/flags"
40+
"github.com/operator-framework/operator-sdk/internal/ansible/metrics"
3941
"github.com/operator-framework/operator-sdk/internal/ansible/proxy"
4042
"github.com/operator-framework/operator-sdk/internal/ansible/proxy/controllermap"
4143
"github.com/operator-framework/operator-sdk/internal/ansible/runner"
@@ -81,6 +83,7 @@ func NewCmd() *cobra.Command {
8183

8284
func run(cmd *cobra.Command, f *flags.Flags) {
8385
printVersion()
86+
metrics.RegisterBuildInfo(crmetrics.Registry)
8487

8588
cfg, err := config.GetConfig()
8689
if err != nil {

internal/cmd/helm-operator/run/cmd.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ import (
3131
zapf "sigs.k8s.io/controller-runtime/pkg/log/zap"
3232
"sigs.k8s.io/controller-runtime/pkg/manager"
3333
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
34+
crmetrics "sigs.k8s.io/controller-runtime/pkg/metrics"
3435

3536
"github.com/operator-framework/operator-sdk/internal/helm/controller"
3637
"github.com/operator-framework/operator-sdk/internal/helm/flags"
38+
"github.com/operator-framework/operator-sdk/internal/helm/metrics"
3739
"github.com/operator-framework/operator-sdk/internal/helm/release"
3840
"github.com/operator-framework/operator-sdk/internal/helm/watches"
3941
"github.com/operator-framework/operator-sdk/internal/util/k8sutil"
@@ -73,6 +75,7 @@ func NewCmd() *cobra.Command {
7375

7476
func run(cmd *cobra.Command, f *flags.Flags) {
7577
printVersion()
78+
metrics.RegisterBuildInfo(crmetrics.Registry)
7679

7780
cfg, err := config.GetConfig()
7881
if err != nil {

internal/helm/metrics/metrics.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2020 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+
sdkVersion "github.com/operator-framework/operator-sdk/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": sdkVersion.GitCommit,
35+
"version": sdkVersion.Version,
36+
},
37+
},
38+
)
39+
)
40+
41+
func RegisterBuildInfo(r prometheus.Registerer) {
42+
buildInfo.Set(1)
43+
r.MustRegister(buildInfo)
44+
}

0 commit comments

Comments
 (0)