|
| 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 | + |
| 6 | +## operator-sdk specific metrics |
| 7 | + |
| 8 | +The `metrics.ExposeMetricsPort` is a helper function that creates and registers go and process collector metrics in operator-sdk. It also serves those metrics on `:6000/metrics`. A [Service](https://kubernetes.io/docs/concepts/services-networking/service/) object is created with the port exposed. |
| 9 | + |
| 10 | +### Usage: |
| 11 | + |
| 12 | +```go |
| 13 | +import "github.com/operator-framework/operator-sdk/pkg/metrics" |
| 14 | + |
| 15 | +// Returns the Service object and exposes metrics on port `6000/metrics`. |
| 16 | +s, err := metrics.ExposeMetricsPort() |
| 17 | +if err != nil { |
| 18 | + log.Println(err) |
| 19 | +} |
| 20 | +client := mgr.GetClient() |
| 21 | + |
| 22 | +// Create the operator-sdk Service object. |
| 23 | +client.Create(context.TODO(), s) |
| 24 | +``` |
| 25 | + |
| 26 | +### Usage: |
| 27 | + |
| 28 | + |
| 29 | +## Using ServiceMonitor prometheus-operator CRD |
| 30 | + |
| 31 | +[prometheus-operator](https://github.com/coreos/prometheus-operator) is an operator that creates, configures and manages Prometheus clusters atop Kubernetes. |
| 32 | + |
| 33 | +`ServiceMonitor` is a CRD 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. |
| 34 | +To add `Service` target discover of your created monitoring `Service`s you can use the `GenerateServiceMonitor` helper function, which accepts the newly created `Service`. |
| 35 | + |
| 36 | +### Prerequisites: |
| 37 | + |
| 38 | +- [prometheus-operator](https://github.com/coreos/prometheus-operator/tree/master/contrib/kube-prometheus#quickstart) needs to be deployed already in the cluster. |
| 39 | + |
| 40 | +### Usage example: |
| 41 | + |
| 42 | +```go |
| 43 | +import "github.com/operator-framework/operator-sdk/pkg/metrics" |
| 44 | + |
| 45 | +// Pass the Service to the helper function, which in turn returns the `ServiceMonitor` object. |
| 46 | +sm := metrics.GenerateServiceMonitor(service) |
| 47 | + |
| 48 | +// Create the `ServiceMonitor` resource in the cluster. |
| 49 | +client.Create(context.TODO(), s) |
| 50 | +sdk.Create(sm) |
| 51 | +``` |
| 52 | + |
| 53 | +*Note:* Create one `ServiceMonitor` per application and per `Namespace`. |
| 54 | + |
0 commit comments