From 1f808f7915bd45c50b59b51a709ff12319c50c5c Mon Sep 17 00:00:00 2001 From: Aline Abler Date: Thu, 7 Dec 2023 13:48:13 +0100 Subject: [PATCH] Add appuio_control_organization_info metric --- controller.go | 4 +++ controllers/org_info_metric.go | 54 +++++++++++++++++++++++++++++ controllers/org_info_metric_test.go | 52 +++++++++++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 controllers/org_info_metric.go create mode 100644 controllers/org_info_metric_test.go diff --git a/controller.go b/controller.go index d65e43fc..e7c581f0 100644 --- a/controller.go +++ b/controller.go @@ -257,6 +257,10 @@ func setupManager( &controllers.OrgBillingRefLinkMetric{ Client: mgr.GetClient(), }) + metrics.Registry.MustRegister( + &controllers.OrgInfoMetric{ + Client: mgr.GetClient(), + }) metrics.Registry.MustRegister( &controllers.EmailPendingMetric{ Client: mgr.GetClient(), diff --git a/controllers/org_info_metric.go b/controllers/org_info_metric.go new file mode 100644 index 00000000..dad7f3be --- /dev/null +++ b/controllers/org_info_metric.go @@ -0,0 +1,54 @@ +package controllers + +import ( + "context" + + "github.com/prometheus/client_golang/prometheus" + "sigs.k8s.io/controller-runtime/pkg/client" + + orgv1 "github.com/appuio/control-api/apis/organization/v1" +) + +//+kubebuilder:rbac:groups="rbac.appuio.io",resources=organizations,verbs=get;list;watch +//+kubebuilder:rbac:groups="organization.appuio.io",resources=organizations,verbs=get;list;watch + +var orgInfoMetricDesc = prometheus.NewDesc( + "appuio_control_organization_info", + "Information about APPUiO Cloud organizations", + []string{"organization", "billing_entity", "sales_order"}, + nil, +) + +// OrgInfoMetric is a Prometheus collector that exposes the link between an organization and a billing entity. +type OrgInfoMetric struct { + client.Client +} + +var _ prometheus.Collector = &OrgInfoMetric{} + +// Describe implements prometheus.Collector. +// Sends the static description of the metric to the provided channel. +func (o *OrgInfoMetric) Describe(ch chan<- *prometheus.Desc) { + ch <- orgInfoMetricDesc +} + +// Collect implements prometheus.Collector. +// Sends a metric for each organization and its billing entity to the provided channel. +func (o *OrgInfoMetric) Collect(ch chan<- prometheus.Metric) { + orgs := &orgv1.OrganizationList{} + if err := o.List(context.Background(), orgs); err != nil { + ch <- prometheus.NewInvalidMetric(desc, err) + return + } + + for _, org := range orgs.Items { + ch <- prometheus.MustNewConstMetric( + orgInfoMetricDesc, + prometheus.GaugeValue, + 1, + org.Name, + org.Spec.BillingEntityRef, + org.Status.SaleOrderName, + ) + } +} diff --git a/controllers/org_info_metric_test.go b/controllers/org_info_metric_test.go new file mode 100644 index 00000000..02789e58 --- /dev/null +++ b/controllers/org_info_metric_test.go @@ -0,0 +1,52 @@ +package controllers_test + +import ( + "strings" + "testing" + + "github.com/prometheus/client_golang/prometheus/testutil" + "github.com/stretchr/testify/require" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + orgv1 "github.com/appuio/control-api/apis/organization/v1" + "github.com/appuio/control-api/controllers" +) + +func TestOrgInfoMetric(t *testing.T) { + c := prepareTest(t, &orgv1.Organization{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-org", + }, + Spec: orgv1.OrganizationSpec{ + BillingEntityRef: "test-billing-entity", + }, + }, &orgv1.Organization{ + ObjectMeta: metav1.ObjectMeta{ + Name: "blub-org", + }, + Spec: orgv1.OrganizationSpec{ + BillingEntityRef: "be-1734", + }, + }, &orgv1.Organization{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo-org", + }, + Spec: orgv1.OrganizationSpec{ + BillingEntityRef: "be-234", + }, + Status: orgv1.OrganizationStatus{ + SaleOrderName: "SO9999", + }, + }) + + require.NoError(t, + testutil.CollectAndCompare(&controllers.OrgInfoMetric{c}, strings.NewReader(` +# HELP appuio_control_organization_info Information about APPUiO Cloud organizations +# TYPE appuio_control_organization_info gauge +appuio_control_organization_info{billing_entity="be-1734",organization="blub-org",sales_order=""} 1 +appuio_control_organization_info{billing_entity="be-234",organization="foo-org",sales_order="SO9999"} 1 +appuio_control_organization_info{billing_entity="test-billing-entity",organization="test-org",sales_order=""} 1 +`), + "appuio_control_organization_info"), + ) +}