From f088dc320a744275a5143b883d0ce8e745630571 Mon Sep 17 00:00:00 2001 From: Morven Cao Date: Tue, 19 Nov 2024 14:43:26 +0800 Subject: [PATCH] register cloud events metrics. (#217) * register metrics. Signed-off-by: morvencao * add testing. Signed-off-by: morvencao --------- Signed-off-by: morvencao --- cmd/maestro/agent/cmd.go | 7 +++++++ go.mod | 2 +- go.sum | 4 ++-- pkg/client/cloudevents/source_client.go | 3 ++- test/integration/resource_test.go | 20 ++++++++++++++++++++ 5 files changed, 32 insertions(+), 4 deletions(-) diff --git a/cmd/maestro/agent/cmd.go b/cmd/maestro/agent/cmd.go index bde78ba6..4e23a36e 100644 --- a/cmd/maestro/agent/cmd.go +++ b/cmd/maestro/agent/cmd.go @@ -8,11 +8,13 @@ import ( "github.com/spf13/pflag" utilruntime "k8s.io/apimachinery/pkg/util/runtime" utilflag "k8s.io/component-base/cli/flag" + "k8s.io/component-base/metrics/legacyregistry" "k8s.io/component-base/version" ocmfeature "open-cluster-management.io/api/feature" commonoptions "open-cluster-management.io/ocm/pkg/common/options" "open-cluster-management.io/ocm/pkg/features" "open-cluster-management.io/ocm/pkg/work/spoke" + "open-cluster-management.io/sdk-go/pkg/cloudevents/generic" ) var ( @@ -20,6 +22,11 @@ var ( agentOption = spoke.NewWorkloadAgentOptions() ) +func init() { + // register the cloud events metrics for the agent + generic.RegisterCloudEventsMetrics(legacyregistry.Registerer()) +} + // by default uses 1M as the limit for state feedback const maxJSONRawLength int32 = 1024 * 1024 diff --git a/go.mod b/go.mod index b6dfd565..2f01877a 100755 --- a/go.mod +++ b/go.mod @@ -53,7 +53,7 @@ require ( k8s.io/klog/v2 v2.130.1 open-cluster-management.io/api v0.15.0 open-cluster-management.io/ocm v0.15.0 - open-cluster-management.io/sdk-go v0.15.1-0.20241031061311-f50d6e83dae3 + open-cluster-management.io/sdk-go v0.15.1-0.20241118094717-4b0d20455f47 sigs.k8s.io/yaml v1.4.0 ) diff --git a/go.sum b/go.sum index 7d38707b..7bb2a03b 100644 --- a/go.sum +++ b/go.sum @@ -846,8 +846,8 @@ open-cluster-management.io/api v0.15.0 h1:lRee1KOlGHZb2scTA7ff9E9Fxt2hJc7jpkHnaC open-cluster-management.io/api v0.15.0/go.mod h1:9erZEWEn4bEqh0nIX2wA7f/s3KCuFycQdBrPrRzi0QM= open-cluster-management.io/ocm v0.15.0 h1:anXQzvQUhM/DT8FcKVi4n8AY97IA5DVI0mb8R1wsvbs= open-cluster-management.io/ocm v0.15.0/go.mod h1:d6ubRiBaouiQ+yV+wFAmarpU7I77nXhkJnQJf8gLZC4= -open-cluster-management.io/sdk-go v0.15.1-0.20241031061311-f50d6e83dae3 h1:Dw10oC4N54TJDYt9c8SyRD0du80aS3MSvvBUFWlZyTI= -open-cluster-management.io/sdk-go v0.15.1-0.20241031061311-f50d6e83dae3/go.mod h1:fi5WBsbC5K3txKb8eRLuP0Sim/Oqz/PHX18skAEyjiA= +open-cluster-management.io/sdk-go v0.15.1-0.20241118094717-4b0d20455f47 h1:1gNvY3quZ6CWeXnwCLAXuEoNjGGZL+U4uS79vuo8API= +open-cluster-management.io/sdk-go v0.15.1-0.20241118094717-4b0d20455f47/go.mod h1:fi5WBsbC5K3txKb8eRLuP0Sim/Oqz/PHX18skAEyjiA= sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.29.0 h1:/U5vjBbQn3RChhv7P11uhYvCSm5G2GaIi5AIGBS6r4c= sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.29.0/go.mod h1:z7+wmGM2dfIiLRfrC6jb5kV2Mq/sK1ZP303cxzkV5Y4= sigs.k8s.io/controller-runtime v0.18.5 h1:nTHio/W+Q4aBlQMgbnC5hZb4IjIidyrizMai9P6n4Rk= diff --git a/pkg/client/cloudevents/source_client.go b/pkg/client/cloudevents/source_client.go index 0c0bff28..0b61f8f5 100644 --- a/pkg/client/cloudevents/source_client.go +++ b/pkg/client/cloudevents/source_client.go @@ -9,6 +9,7 @@ import ( "github.com/openshift-online/maestro/pkg/api" "github.com/openshift-online/maestro/pkg/logger" "github.com/openshift-online/maestro/pkg/services" + "github.com/prometheus/client_golang/prometheus" cegeneric "open-cluster-management.io/sdk-go/pkg/cloudevents/generic" ceoptions "open-cluster-management.io/sdk-go/pkg/cloudevents/generic/options" cetypes "open-cluster-management.io/sdk-go/pkg/cloudevents/generic/types" @@ -42,7 +43,7 @@ func NewSourceClient(sourceOptions *ceoptions.CloudEventsSourceOptions, resource } // register resource resync metrics for cloud event source client - cegeneric.RegisterResourceResyncMetrics() + cegeneric.RegisterCloudEventsMetrics(prometheus.DefaultRegisterer) return &SourceClientImpl{ Codec: codec, diff --git a/test/integration/resource_test.go b/test/integration/resource_test.go index 08a9e613..a66f07a8 100755 --- a/test/integration/resource_test.go +++ b/test/integration/resource_test.go @@ -186,6 +186,17 @@ func TestResourcePost(t *testing.T) { Expect(contentStatus["availableReplicas"]).To(Equal(float64(1))) Expect(contentStatus["readyReplicas"]).To(Equal(float64(1))) Expect(contentStatus["updatedReplicas"]).To(Equal(float64(1))) + + // check the metrics + time.Sleep(1 * time.Second) + families := getServerMetrics(t, "http://localhost:8080/metrics") + labels := []*prommodel.LabelPair{ + {Name: strPtr("source"), Value: strPtr("maestro")}, + {Name: strPtr("cluster"), Value: strPtr(clusterName)}, + {Name: strPtr("type"), Value: strPtr("io.open-cluster-management.works.v1alpha1.manifests")}, + } + checkServerCounterMetric(t, families, "cloudevents_sent_total", labels, 3.0) + checkServerCounterMetric(t, families, "cloudevents_received_total", labels, 3.0) } func TestResourcePostWithoutName(t *testing.T) { @@ -777,6 +788,7 @@ func TestResourceFromGRPC(t *testing.T) { }, 10*time.Second, 1*time.Second).Should(Succeed()) // check the metrics + time.Sleep(1 * time.Second) families := getServerMetrics(t, "http://localhost:8080/metrics") labels := []*prommodel.LabelPair{ {Name: strPtr("type"), Value: strPtr("Publish")}, @@ -807,6 +819,14 @@ func TestResourceFromGRPC(t *testing.T) { {Name: strPtr("code"), Value: strPtr("OK")}, } checkServerCounterMetric(t, families, "grpc_server_processed_total", labels, 0.0) + + labels = []*prommodel.LabelPair{ + {Name: strPtr("source"), Value: strPtr("maestro")}, + {Name: strPtr("cluster"), Value: strPtr(clusterName)}, + {Name: strPtr("type"), Value: strPtr("io.open-cluster-management.works.v1alpha1.manifestbundles")}, + } + checkServerCounterMetric(t, families, "cloudevents_sent_total", labels, 3.0) + checkServerCounterMetric(t, families, "cloudevents_received_total", labels, 3.0) } func TestResourceBundleFromGRPC(t *testing.T) {