|
| 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)` function exposes general metrics about the running program. These metrics are inherited from controller-runtime. This helper function creates a [Service][service] object with the metrics port exposed, which can then be accessed by Prometheus. The Service object is [garbage collected][gc] when the leader pod's root owner is deleted. |
| 8 | + |
| 9 | +By default, the metrics are served on `0.0.0.0:8383/metrics`. To modify the port the metrics are exposed on, change the `var metricsPort int32 = 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 | + func main() { |
| 20 | + |
| 21 | + ... |
| 22 | + |
| 23 | + // Change the below variables to serve metrics on different host or port. |
| 24 | + var metricsHost = "0.0.0.0" |
| 25 | + var metricsPort int32 = 8383 |
| 26 | + |
| 27 | + // Pass metrics address to controller-runtime manager |
| 28 | + mgr, err := manager.New(cfg, manager.Options{ |
| 29 | + Namespace: namespace, |
| 30 | + MetricsBindAddress: fmt.Sprintf("%s:%d", metricsHost, metricsPort), |
| 31 | + }) |
| 32 | + |
| 33 | + ... |
| 34 | + |
| 35 | + // Create Service object to expose the metrics port. |
| 36 | + _, err = metrics.ExposeMetricsPort(ctx, metricsPort) |
| 37 | + if err != nil { |
| 38 | + // handle error |
| 39 | + log.Info(err.Error()) |
| 40 | + } |
| 41 | + |
| 42 | + ... |
| 43 | + } |
| 44 | +``` |
| 45 | + |
| 46 | +*Note:* The above example is already present in `cmd/manager/main.go` in all the operators generated with operator-sdk. |
| 47 | + |
| 48 | +[prometheus]: https://prometheus.io/ |
| 49 | +[service]: https://kubernetes.io/docs/concepts/services-networking/service/ |
| 50 | +[gc]: https://kubernetes.io/docs/concepts/workloads/controllers/garbage-collection/#owners-and-dependents |
| 51 | + |
0 commit comments