Skip to content

Commit a73a77d

Browse files
committed
doc/user/metrics: Add metrics docs
1 parent 0e004f7 commit a73a77d

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

doc/user/metrics/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Monitoring with Prometheus
2+
3+
[Prometheus][prometheus] is an open-source systems monitoring and alerting toolkit. Below is the overview of the different helpers that exist in operator-sdk to help setup metrics in the generated operator.
4+
5+
## Metrics in operator-sdk
6+
7+
The `func ExposeMetricsPort(ctx context.Context, port int32) (*v1.Service, error)` is a helper function that exposes general metrics about the running program. These metrics are inherited from controller-runtime. The metrics are by default served on `:8383/metrics`. A [Service][service] object is created with the metrics port exposed, which can be then accessed by Prometheus. The Service object is [garbage collected][gc] by the owner, which is determined based on the pods owner in which the leader is running in.
8+
9+
To modify the port the metrics are exposed on, change the variable `metricsHost string = ":8383"` variable in the `cmd/manager/main.go` file of the generated operator.
10+
11+
### Usage:
12+
13+
```go
14+
import(
15+
"github.com/operator-framework/operator-sdk/pkg/metrics"
16+
"sigs.k8s.io/controller-runtime/pkg/manager"
17+
)
18+
19+
// Change the below variables to serve metrics on different host or port.
20+
var metricsHost = "0.0.0.0"
21+
var metricsPort int32 = 8383
22+
23+
// Pass metrics address to controller-runtime manager
24+
mgr, err := manager.New(cfg, manager.Options{
25+
Namespace: namespace,
26+
MetricsBindAddress: fmt.Sprintf("%s:%d", metricsHost, metricsPort),
27+
})
28+
29+
...
30+
31+
// Create Service object to expose the metrics port.
32+
_, err = metrics.ExposeMetricsPort(ctx, metricsPort)
33+
if err != nil {
34+
// handle error
35+
log.Info(err.Error())
36+
}
37+
```
38+
39+
Note: The above example is already present in `cmd/manager/main.go` in all the operators generated with operator-sdk.
40+
41+
[prometheus]: https://prometheus.io/
42+
[service]: https://kubernetes.io/docs/concepts/services-networking/service/
43+
[gc]: https://kubernetes.io/docs/concepts/workloads/controllers/garbage-collection/#owners-and-dependents
44+

doc/user/metrics/service_monitor.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## Using the ServiceMonitor prometheus-operator CRD
2+
3+
[prometheus-operator][prom-operator] an operator that creates, configures and manages Prometheus clusters atop Kubernetes.
4+
5+
`ServiceMonitor` is a [CR][cr] of the prometheus-operator, which discovers the `Endpoints` in `Service` objects and configures Prometheus to monitor those `Pod`s. The `GenerateServiceMonitor` takes a `Service` object and generates a `ServiceMonitor` resource based on it.
6+
To add `Service` target discovery of your created monitoring `Service`s you can use the `metrics.CreateServiceMonitor()` helper function, which accepts the newly created `Service`.
7+
8+
### Prerequisites:
9+
10+
- [prometheus-operator](prom-quickstart) needs to be deployed already in the cluster.
11+
12+
### Usage example:
13+
14+
```go
15+
import "github.com/operator-framework/operator-sdk/pkg/metrics"
16+
17+
// Pass the Service(s) to the helper function, which in turn returns the `ServiceMonitor` object.
18+
serviceMonitors, err := metrics.CreateServiceMonitors(restConfig, "namespace", services)
19+
if err != nil {
20+
// handle error here
21+
}
22+
```
23+
24+
*Note:* Create one `ServiceMonitor` per application and per `Namespace`.
25+
26+
[prom-operator]: https://github.com/coreos/prometheus-operator
27+
[cr]: https://kubernetes.io/docs/concepts/extend-kubernetes/api-extension/custom-resources/
28+
[prom-quickstart]: https://github.com/coreos/prometheus-operator/tree/master/contrib/kube-prometheus#quickstart

0 commit comments

Comments
 (0)