Skip to content

Commit

Permalink
controllers: report StorageSystem metrics
Browse files Browse the repository at this point in the history
transforming StorageSystem spec into metrics so that we can
use Prometheus queries to understand object relationships.

Signed-off-by: Umanga Chapagain <[email protected]>
  • Loading branch information
umangachapagain committed Aug 10, 2021
1 parent 39593e9 commit 735d38e
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
3 changes: 3 additions & 0 deletions controllers/storagesystem_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (

operatorv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
odfv1alpha1 "github.com/red-hat-data-services/odf-operator/api/v1alpha1"
"github.com/red-hat-data-services/odf-operator/metrics"
"github.com/red-hat-data-services/odf-operator/pkg/util"
)

Expand Down Expand Up @@ -76,6 +77,8 @@ func (r *StorageSystemReconciler) Reconcile(ctx context.Context, req ctrl.Reques

logger.Info("storagesystem instance found")

metrics.ReportODFSystemMapMetrics(instance.Name, instance.Spec.Name, instance.Spec.Namespace, string(instance.Spec.Kind))

if err := r.validateStorageSystemSpec(instance, logger); err != nil {
logger.Error(err, "failed to validate storagesystem")
return reconcile.Result{}, err
Expand Down
44 changes: 44 additions & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Copyright 2021 Red Hat OpenShift Data Foundation.
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"
"sigs.k8s.io/controller-runtime/pkg/metrics"
)

var (
storageSystemMap = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "odf",
Subsystem: "",
Name: "system_map",
Help: "Map of ODF StorageSystems to their target Custom Resource",
}, []string{"storage_system", "target_name", "target_namespace", "target_kind"})
)

func init() {
metrics.Registry.MustRegister(storageSystemMap)
}

func ReportODFSystemMapMetrics(storageSystem, name, namespace, kind string) {
storageSystemMap.With(prometheus.Labels{
"storage_system": storageSystem,
"target_name": name,
"target_namespace": namespace,
"target_kind": kind,
}).Set(1)
}
68 changes: 68 additions & 0 deletions metrics/metrics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
Copyright 2021 Red Hat OpenShift Data Foundation.
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 (
"testing"

"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/assert"
"sigs.k8s.io/controller-runtime/pkg/metrics"
)

var defaultRegistry = metrics.Registry
var find string = "odf_system_map"

func TestReportODFSystemMapMetrics(t *testing.T) {
type args struct {
storageSystem, name, namespace, kind string
}
tests := []struct {
name string
args args
}{
{
name: "StorageSystem to StorageCluster",
args: args{
storageSystem: "StorageSystem1",
name: "StorageCluster1",
namespace: "Namespace1",
kind: "StorageCluster",
},
},
{
name: "StorageSystem to Flashsystem",
args: args{
storageSystem: "StorageSystem2",
name: "FlashSystem1",
namespace: "Namespace1",
kind: "Flashsystem",
},
},
}
for n, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ReportODFSystemMapMetrics(tt.args.storageSystem, tt.args.name, tt.args.namespace, tt.args.kind)
count, err := testutil.GatherAndCount(defaultRegistry, find)
assert.NoError(t, err)
assert.Equal(t, n+1, count)
problems, err := testutil.GatherAndLint(defaultRegistry, find)
assert.NoError(t, err)
assert.Equal(t, 0, len(problems))
})
}
}

0 comments on commit 735d38e

Please sign in to comment.