Skip to content

Commit 1163edc

Browse files
Jay Boydpmorie
Jay Boyd
authored andcommitted
expose Prometheus metrics from Controller (openshift#677) (openshift#1608)
1 parent 2cd6554 commit 1163edc

File tree

5 files changed

+95
-0
lines changed

5 files changed

+95
-0
lines changed

Diff for: charts/catalog/templates/controller-manager-deployment.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ spec:
1414
app: {{ template "fullname" . }}-controller-manager
1515
template:
1616
metadata:
17+
annotations:
18+
prometheus.io/scrape: "{{ .Values.controllerManager.enablePrometheusScrape }}"
1719
labels:
1820
app: {{ template "fullname" . }}-controller-manager
1921
chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"

Diff for: charts/catalog/values.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ controllerManager:
8989
serviceAccount: service-catalog-controller-manager
9090
# Controls whether the API server's TLS verification should be skipped.
9191
apiserverSkipVerify: true
92+
# Whether the controller will expose metrics on /metrics
93+
enablePrometheusScrape: false
9294
# Whether the OriginatingIdentity alpha feature should be enabled
9395
originatingIdentityEnabled: false
9496
# Whether the AsyncBindingOperations alpha feature should be enabled

Diff for: cmd/controller-manager/app/controller_manager.go

+3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ import (
3838
"k8s.io/client-go/tools/record"
3939

4040
"github.com/kubernetes-incubator/service-catalog/pkg/kubernetes/pkg/util/configz"
41+
"github.com/kubernetes-incubator/service-catalog/pkg/metrics"
42+
4143
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
4244
"k8s.io/apimachinery/pkg/runtime/schema"
4345
"k8s.io/apimachinery/pkg/util/wait"
@@ -152,6 +154,7 @@ func Run(controllerManagerOptions *options.ControllerManagerServer) error {
152154
}
153155
healthz.InstallHandler(mux, healthz.PingHealthz, apiAvailableChecker)
154156
configz.InstallHandler(mux)
157+
metrics.RegisterMetricsAndInstallHandler(mux)
155158

156159
if controllerManagerOptions.EnableProfiling {
157160
mux.HandleFunc("/debug/pprof/", pprof.Index)

Diff for: pkg/controller/controller_broker.go

+9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
"github.com/kubernetes-incubator/service-catalog/pkg/api"
2727
"github.com/kubernetes-incubator/service-catalog/pkg/apis/servicecatalog/v1beta1"
28+
"github.com/kubernetes-incubator/service-catalog/pkg/metrics"
2829
"github.com/kubernetes-incubator/service-catalog/pkg/pretty"
2930
corev1 "k8s.io/api/core/v1"
3031
"k8s.io/apimachinery/pkg/api/errors"
@@ -408,6 +409,10 @@ func (c *controller) reconcileClusterServiceBroker(broker *v1beta1.ClusterServic
408409

409410
c.recorder.Event(broker, corev1.EventTypeNormal, successFetchedCatalogReason, successFetchedCatalogMessage)
410411

412+
// Update metrics with the number of serviceclass and serviceplans from this broker
413+
metrics.BrokerServiceClassCount.WithLabelValues(broker.Name).Set(float64(len(payloadServiceClasses)))
414+
metrics.BrokerServicePlanCount.WithLabelValues(broker.Name).Set(float64(len(payloadServicePlans)))
415+
411416
return nil
412417
}
413418

@@ -477,6 +482,10 @@ func (c *controller) reconcileClusterServiceBroker(broker *v1beta1.ClusterServic
477482

478483
c.recorder.Eventf(broker, corev1.EventTypeNormal, successClusterServiceBrokerDeletedReason, successClusterServiceBrokerDeletedMessage, broker.Name)
479484
glog.V(5).Info(pcb.Message("Successfully deleted"))
485+
486+
// delete the metrics associated with this broker
487+
metrics.BrokerServiceClassCount.DeleteLabelValues(broker.Name)
488+
metrics.BrokerServicePlanCount.DeleteLabelValues(broker.Name)
480489
return nil
481490
}
482491

Diff for: pkg/metrics/metrics.go

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
Copyright 2017 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package metrics creates and registers metrics objects with Prometheus
18+
// and sets the Prometheus HTTP handler for /metrics
19+
package metrics
20+
21+
import (
22+
"net/http"
23+
"sync"
24+
25+
"github.com/golang/glog"
26+
"github.com/prometheus/client_golang/prometheus"
27+
"github.com/prometheus/client_golang/prometheus/promhttp"
28+
)
29+
30+
var registerMetrics sync.Once
31+
32+
const (
33+
catalogNamespace = "servicecatalog" // Prometheus namespace (nothing to do with k8s namespace)
34+
)
35+
36+
var (
37+
// Metrics are identified in Prometheus by concatinating Namespace,
38+
// Subsystem and Name while omitting any nulls and separating each key with
39+
// an underscore. Note that in this context, Namespace is the Prometheus
40+
// Namespace and there is no correlation with Kubernetes Namespace.
41+
42+
// BrokerServiceClassCount exposes the number of Service Classes registered
43+
// per broker.
44+
BrokerServiceClassCount = prometheus.NewGaugeVec(
45+
prometheus.GaugeOpts{
46+
Namespace: catalogNamespace,
47+
Name: "broker_service_class_count",
48+
Help: "Number of services classes by Broker.",
49+
},
50+
[]string{"broker"},
51+
)
52+
53+
// BrokerServicePlanCount exposes the number of Service Plans registered
54+
// per broker.
55+
BrokerServicePlanCount = prometheus.NewGaugeVec(
56+
prometheus.GaugeOpts{
57+
Namespace: catalogNamespace,
58+
Name: "broker_service_plan_count",
59+
Help: "Number of services classes by Broker.",
60+
},
61+
[]string{"broker"},
62+
)
63+
)
64+
65+
func register() {
66+
registerMetrics.Do(func() {
67+
prometheus.MustRegister(BrokerServiceClassCount)
68+
prometheus.MustRegister(BrokerServicePlanCount)
69+
})
70+
}
71+
72+
// RegisterMetricsAndInstallHandler registers the Service Catalog metrics
73+
// objects with Prometheus and installs the Prometheus http handler at the
74+
// default context.
75+
func RegisterMetricsAndInstallHandler(m *http.ServeMux) {
76+
register()
77+
m.Handle("/metrics", promhttp.Handler())
78+
glog.V(4).Info("Registered /metrics with promhttp")
79+
}

0 commit comments

Comments
 (0)