Skip to content

Commit 74acfe6

Browse files
committed
Add custom prometheus metrics documentation
1 parent b2c4917 commit 74acfe6

File tree

3 files changed

+72
-10
lines changed

3 files changed

+72
-10
lines changed

docs/book/src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
- [controller-gen CLI](./reference/controller-gen.md)
7070
- [Artifacts](./reference/artifacts.md)
7171
- [Writing controller tests](./reference/writing-tests.md)
72+
- [Metrics](./reference/metrics.md)
7273

7374
---
7475

docs/book/src/reference/metrics.md

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Metrics
2+
3+
By default, controller-runtime builds a global prometheus registry and
4+
publishes a collection of performance metrics for each controller.
5+
6+
## Publishing Additional Metrics
7+
8+
If you wish to publish additional metrics from your controllers, this
9+
can be easily achieved by using the global registry from
10+
`controller-runtime/pkg/metrics`.
11+
12+
One way to achieve this is to declare your collectors as global variables and then register them using `init()`.
13+
14+
For example:
15+
16+
```go
17+
import (
18+
"github.com/prometheus/client_golang/prometheus"
19+
"sigs.k8s.io/controller-runtime/pkg/metrics"
20+
)
21+
22+
var (
23+
goobers = prometheus.NewCounter(
24+
prometheus.CounterOpts{
25+
Name: "goobers_total",
26+
Help: "Number of goobers proccessed",
27+
},
28+
)
29+
gooberFailures = prometheus.NewCounter(
30+
prometheus.CounterOpts{
31+
Name: "goober_failures_total",
32+
Help: "Number of failed goobers",
33+
},
34+
)
35+
)
36+
37+
func init() {
38+
// Register custom metrics with the global prometheus registry
39+
metrics.Registry.MustRegister(goobers, gooberFailures)
40+
}
41+
```
42+
43+
You may then record metrics to those collectors from any part of your
44+
reconcile loop, and those metrics will be available for prometheus or
45+
other openmetrics systems to scrape.

docs/book/src/reference/reference.md

+26-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
11
# Reference
2-
3-
- [Using Finalizers](./using-finalizers.md): Finalizers are a mechanism to
4-
execute any custom logic related to a resource before it gets deleted from
5-
Kubernetes cluster.
6-
- [Webhooks](./webhook-overview.md): Webhooks are HTTP callbacks, there are 3
7-
types of webhooks in k8s: 1) admission webhook 2) CRD conversion webhook 3)
8-
authorization webhook
92

10-
- [Admission Webhook](./admission-webhook.md): Admission webhooks are HTTP
11-
callbacks for mutating or validating resources before the API server admit
12-
them.
3+
- [Generating CRDs](generating-crd.md)
4+
- [Using Finalizers](using-finalizers.md)
5+
Finalizers are a mechanism to
6+
execute any custom logic related to a resource before it gets deleted from
7+
Kubernetes cluster.
8+
- [Kind cluster](kind.md)
9+
- [What's a webhook?](webhook-overview.md)
10+
Webhooks are HTTP callbacks, there are 3
11+
types of webhooks in k8s: 1) admission webhook 2) CRD conversion webhook 3)
12+
authorization webhook
13+
- [Admission webhook](admission-webhook.md)
14+
Admission webhooks are HTTP
15+
callbacks for mutating or validating resources before the API server admit
16+
them.
17+
- [Markers for Config/Code Generation](markers.md)
18+
19+
- [CRD Generation](markers/crd.md)
20+
- [CRD Validation](markers/crd-validation.md)
21+
- [Webhook](markers/webhook.md)
22+
- [Object/DeepCopy](markers/object.md)
23+
- [RBAC](markers/rbac.md)
24+
25+
- [controller-gen CLI](controller-gen.md)
26+
- [Artifacts](artifacts.md)
27+
- [Writing controller tests](writing-tests.md)
28+
- [Metrics](metrics.md)

0 commit comments

Comments
 (0)