/
org_info_metric.go
53 lines (43 loc) · 1.47 KB
/
org_info_metric.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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", "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.Status.SalesOrderName,
)
}
}