Skip to content

Commit 1654c1f

Browse files
committed
doc/user/metrics: Add metrics docs
1 parent 534f051 commit 1654c1f

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

doc/user/metrics/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Monitoring with Prometheus
2+
3+
[Prometheus](https://prometheus.io/) 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 operators.
4+
5+
## Metrics in operator-sdk
6+
7+
The `metrics.ExposeMetricsPort(metricsAddress)` is a helper function that exposes general metrics about the running program. The metrics are by default served on `:8383/metrics`. A [Service](https://kubernetes.io/docs/concepts/services-networking/service/) object is created with the metrics port exposed.
8+
9+
To modify the port the metrics are exposed on, change the `var metricsAddress string = ":8383"` variable in the `cmd/manager/main.go` file of the generated operator.
10+
11+
### Usage:
12+
13+
```go
14+
import "github.com/operator-framework/operator-sdk/pkg/metrics"
15+
16+
// Change metricsAddress to serve metrics on different host and port.
17+
var metricsAddress string = ":8383"
18+
19+
// Pass metricsAddress when initiliazing the manager.
20+
mgr, err := manager.New(cfg, manager.Options{Namespace: namespace, MetricsBindAddress: metricsAddress})
21+
22+
// Expose metrics by creating a Service object
23+
s, err := metrics.ExposeMetricsPort(metricsAddress)
24+
if err != nil {
25+
// handle error
26+
}
27+
client := mgr.GetClient()
28+
if err := client.Create(context.TODO(), s); err != nil && !apierrors.IsAlreadyExists(err) {
29+
// handle error
30+
}
31+
32+
```
33+
34+
Note: The above example is already present in `cmd/manager/main.go` in all the operators generated with operator-sdk.

doc/user/metrics/service_monitor.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
## Using the ServiceMonitor prometheus-operator CRD
2+
3+
[prometheus-operator](https://github.com/coreos/prometheus-operator) is an operator that creates, configures and manages Prometheus clusters atop Kubernetes.
4+
5+
`ServiceMonitor` is a [CR](https://kubernetes.io/docs/concepts/extend-kubernetes/api-extension/custom-resources/) 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](https://github.com/coreos/prometheus-operator/tree/master/contrib/kube-prometheus#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+

0 commit comments

Comments
 (0)