Skip to content

Commit

Permalink
Metrics for SyncSet and SelectorSyncSets
Browse files Browse the repository at this point in the history
  • Loading branch information
rhamitarora committed Sep 10, 2024
1 parent 352b0a5 commit b5ac73b
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 27 deletions.
65 changes: 38 additions & 27 deletions pkg/monitor/cluster/syncsetstatus.go
Original file line number Diff line number Diff line change
@@ -1,43 +1,54 @@
package cluster

// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.

import (
"context"
)

func (mon *Monitor) emitSyncSetStatus(ctx context.Context) error {
cs, error := mon.hiveClusterManager.GetSyncSetResources(ctx, mon.doc)
if error != nil {
return nil
clusterSync, err := mon.hiveClusterManager.GetSyncSetResources(ctx, mon.doc)
if err != nil {
return err
}
if cs.Status.SyncSets != nil {
mon.emitGauge("syncsets.count", int64(len(cs.Status.SyncSets)), nil)

for _, s := range cs.Status.SyncSets {
mon.emitGauge("hive.syncsets", 1, map[string]string{
"name": s.Name,
"result": string(s.Result),
"firstSuccessTime": s.FirstSuccessTime.String(),
"lastTransitionTime": s.LastTransitionTime.String(),
"failureMessage": s.FailureMessage,
})
if clusterSync != nil {
clustersyncLabels := make(map[string]string)

if clusterSync.Status.SyncSets != nil {
for _, s := range clusterSync.Status.SyncSets {
labels := map[string]string{
"name": s.Name,
"result": string(s.Result),
"firstSuccessTime": s.FirstSuccessTime.String(),
"lastTransitionTime": s.LastTransitionTime.String(),
"failureMessage": s.FailureMessage,
}
mon.emitGauge("hive.syncsets", 1, labels)
for k, v := range labels {
clustersyncLabels[k] = v
}
}
}
}

if cs.Status.SelectorSyncSets != nil {
mon.emitGauge("selectorsyncsets.count", int64(len(cs.Status.SelectorSyncSets)), nil)
if clusterSync.Status.SelectorSyncSets != nil {
for _, s := range clusterSync.Status.SelectorSyncSets {
labels := map[string]string{
"name": s.Name,
"result": string(s.Result),
"firstSuccessTime": s.FirstSuccessTime.String(),
"lastTransitionTime": s.LastTransitionTime.String(),
"failureMessage": s.FailureMessage,
}
mon.emitGauge("hive.selectorsyncsets", 1, labels)
for k, v := range labels {
clustersyncLabels[k] = v
}
}
}

for _, s := range cs.Status.SelectorSyncSets {
mon.emitGauge("hive.selectorsyncsets", 1, map[string]string{
"name": s.Name,
"result": string(s.Result),
"firstSuccessTime": s.FirstSuccessTime.String(),
"lastTransitionTime": s.LastTransitionTime.String(),
"failureMessage": s.FailureMessage,
})
if len(clustersyncLabels) > 0 {
mon.emitGauge("hive.clustersync", 1, clustersyncLabels)
}
}

return nil
}
101 changes: 101 additions & 0 deletions pkg/monitor/cluster/syncsetstatus_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package cluster

import (
"context"
"errors"
"testing"
"time"

// "github.com/Azure/ARO-RP/pkg/operator/clientset/versioned/scheme"
mock_hive "github.com/Azure/ARO-RP/pkg/util/mocks/hive"
mock_metrics "github.com/Azure/ARO-RP/pkg/util/mocks/metrics"
"github.com/golang/mock/gomock"
hivev1alpha1 "github.com/openshift/hive/apis/hiveinternal/v1alpha1"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/scheme"
)

func init() {
// Register the ClusterSync type with the scheme
hivev1alpha1.AddToScheme(scheme.Scheme)
}

func TestEmitSyncSetStatus(t *testing.T) {
for _, tt := range []struct {
name string
clusterSync *hivev1alpha1.ClusterSync
getClusterSyncErr error
expectedError error
expectedGauges map[string]int64
expectedLabels map[string]string
}{
{
name: "SyncSets has elements",
clusterSync: &hivev1alpha1.ClusterSync{
Status: hivev1alpha1.ClusterSyncStatus{
SyncSets: []hivev1alpha1.SyncStatus{
{
Name: "syncset1",
Result: "Success",
FirstSuccessTime: &metav1.Time{Time: time.Now()},
LastTransitionTime: metav1.Time{Time: time.Now()},
FailureMessage: "",
},
},
},
},
expectedError: nil,
expectedGauges: map[string]int64{
"syncsets.count": 1,
},
expectedLabels: map[string]string{
"name": "syncset1",
"result": "Success",
"firstSuccessTime": "2024-09-09T14:44:45Z",
"lastTransitionTime": "2024-09-09T14:44:45Z",
"failureMessage": "",
},
},
{
name: "SelectorSyncSets is nil",
clusterSync: &hivev1alpha1.ClusterSync{
Status: hivev1alpha1.ClusterSyncStatus{
SelectorSyncSets: nil,
},
},
expectedError: nil,
expectedGauges: map[string]int64{"selectorsyncsets.count": 0},
},
{
name: "GetClusterSyncforClusterDeployment returns error",
getClusterSyncErr: errors.New("some error"),
expectedError: nil,
},
} {
t.Run(tt.name, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockHiveClusterManager := mock_hive.NewMockClusterManager(ctrl)

m := mock_metrics.NewMockEmitter(ctrl)
mockMonitor := &Monitor{
hiveClusterManager: mockHiveClusterManager,
m: m,
}

ctx := context.Background()

mockHiveClusterManager.EXPECT().GetSyncSetResources(ctx, mockMonitor.doc).Return(tt.clusterSync, tt.getClusterSyncErr).AnyTimes()

if tt.expectedGauges != nil {
for gauge, value := range tt.expectedGauges {
m.EXPECT().EmitGauge(gauge, value, tt.expectedLabels).Times(1)
}
}
err := mockMonitor.emitSyncSetStatus(ctx)
assert.Equal(t, tt.expectedError, err)
})
}
}

0 comments on commit b5ac73b

Please sign in to comment.