Skip to content

Commit 1f9af43

Browse files
Merge pull request #183 from appuio/feat/info-metric
Add appuio_control_organization_info metric
2 parents 5c79acf + 1f808f7 commit 1f9af43

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

controller.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@ func setupManager(
257257
&controllers.OrgBillingRefLinkMetric{
258258
Client: mgr.GetClient(),
259259
})
260+
metrics.Registry.MustRegister(
261+
&controllers.OrgInfoMetric{
262+
Client: mgr.GetClient(),
263+
})
260264
metrics.Registry.MustRegister(
261265
&controllers.EmailPendingMetric{
262266
Client: mgr.GetClient(),

controllers/org_info_metric.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package controllers
2+
3+
import (
4+
"context"
5+
6+
"github.com/prometheus/client_golang/prometheus"
7+
"sigs.k8s.io/controller-runtime/pkg/client"
8+
9+
orgv1 "github.com/appuio/control-api/apis/organization/v1"
10+
)
11+
12+
//+kubebuilder:rbac:groups="rbac.appuio.io",resources=organizations,verbs=get;list;watch
13+
//+kubebuilder:rbac:groups="organization.appuio.io",resources=organizations,verbs=get;list;watch
14+
15+
var orgInfoMetricDesc = prometheus.NewDesc(
16+
"appuio_control_organization_info",
17+
"Information about APPUiO Cloud organizations",
18+
[]string{"organization", "billing_entity", "sales_order"},
19+
nil,
20+
)
21+
22+
// OrgInfoMetric is a Prometheus collector that exposes the link between an organization and a billing entity.
23+
type OrgInfoMetric struct {
24+
client.Client
25+
}
26+
27+
var _ prometheus.Collector = &OrgInfoMetric{}
28+
29+
// Describe implements prometheus.Collector.
30+
// Sends the static description of the metric to the provided channel.
31+
func (o *OrgInfoMetric) Describe(ch chan<- *prometheus.Desc) {
32+
ch <- orgInfoMetricDesc
33+
}
34+
35+
// Collect implements prometheus.Collector.
36+
// Sends a metric for each organization and its billing entity to the provided channel.
37+
func (o *OrgInfoMetric) Collect(ch chan<- prometheus.Metric) {
38+
orgs := &orgv1.OrganizationList{}
39+
if err := o.List(context.Background(), orgs); err != nil {
40+
ch <- prometheus.NewInvalidMetric(desc, err)
41+
return
42+
}
43+
44+
for _, org := range orgs.Items {
45+
ch <- prometheus.MustNewConstMetric(
46+
orgInfoMetricDesc,
47+
prometheus.GaugeValue,
48+
1,
49+
org.Name,
50+
org.Spec.BillingEntityRef,
51+
org.Status.SaleOrderName,
52+
)
53+
}
54+
}

controllers/org_info_metric_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package controllers_test
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/prometheus/client_golang/prometheus/testutil"
8+
"github.com/stretchr/testify/require"
9+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10+
11+
orgv1 "github.com/appuio/control-api/apis/organization/v1"
12+
"github.com/appuio/control-api/controllers"
13+
)
14+
15+
func TestOrgInfoMetric(t *testing.T) {
16+
c := prepareTest(t, &orgv1.Organization{
17+
ObjectMeta: metav1.ObjectMeta{
18+
Name: "test-org",
19+
},
20+
Spec: orgv1.OrganizationSpec{
21+
BillingEntityRef: "test-billing-entity",
22+
},
23+
}, &orgv1.Organization{
24+
ObjectMeta: metav1.ObjectMeta{
25+
Name: "blub-org",
26+
},
27+
Spec: orgv1.OrganizationSpec{
28+
BillingEntityRef: "be-1734",
29+
},
30+
}, &orgv1.Organization{
31+
ObjectMeta: metav1.ObjectMeta{
32+
Name: "foo-org",
33+
},
34+
Spec: orgv1.OrganizationSpec{
35+
BillingEntityRef: "be-234",
36+
},
37+
Status: orgv1.OrganizationStatus{
38+
SaleOrderName: "SO9999",
39+
},
40+
})
41+
42+
require.NoError(t,
43+
testutil.CollectAndCompare(&controllers.OrgInfoMetric{c}, strings.NewReader(`
44+
# HELP appuio_control_organization_info Information about APPUiO Cloud organizations
45+
# TYPE appuio_control_organization_info gauge
46+
appuio_control_organization_info{billing_entity="be-1734",organization="blub-org",sales_order=""} 1
47+
appuio_control_organization_info{billing_entity="be-234",organization="foo-org",sales_order="SO9999"} 1
48+
appuio_control_organization_info{billing_entity="test-billing-entity",organization="test-org",sales_order=""} 1
49+
`),
50+
"appuio_control_organization_info"),
51+
)
52+
}

0 commit comments

Comments
 (0)