github.skymusic.top/operator-framework/operator-sdk@v0.8.2/doc/user/metrics/service-monitor.md (about) 1 ## Using the ServiceMonitor prometheus-operator CRD 2 3 [prometheus-operator][prom-operator] is an operator that creates, configures, and manages Prometheus clusters atop Kubernetes. 4 5 `ServiceMonitor` is a CustomResource of the prometheus-operator, which discovers the `Endpoints` in `Service` objects and configures Prometheus to monitor those pods. See the prometheus-operator [documentation][service-monitor] to learn more about `ServiceMonitor`. 6 7 The `GenerateServiceMonitor` function takes a `Service` object and generates a `ServiceMonitor` resource based on it. To add `Service` target discovery of your created monitoring `Service` you can use the `metrics.CreateServiceMonitor()` helper function, which accepts the newly created `Service`. 8 9 ### Prerequisites: 10 11 - [prometheus-operator][prom-quickstart] needs to be deployed in the cluster. 12 13 ### Usage example: 14 15 ```go 16 import( 17 "k8s.io/api/core/v1" 18 "github.com/operator-framework/operator-sdk/pkg/metrics" 19 ) 20 21 func main() { 22 23 ... 24 25 // Populate below with the Service(s) for which you want to create ServiceMonitors. 26 services := []*v1.Service{} 27 28 // Create one `ServiceMonitor` per application per namespace. 29 // Change below value to name of the Namespace you want the `ServiceMonitor` to be created in. 30 ns := "default" 31 32 // Pass the Service(s) to the helper function, which in turn returns the array of `ServiceMonitor` objects. 33 serviceMonitors, err := metrics.CreateServiceMonitors(restConfig, ns, services) 34 if err != nil { 35 // handle error here 36 } 37 38 ... 39 } 40 ``` 41 42 [prom-operator]: https://github.com/coreos/prometheus-operator 43 [service-monitor]: https://github.com/coreos/prometheus-operator/blob/7a25bf6b6bb2347dacb235659b73bc210117acc7/Documentation/design.md#servicemonitor 44 [prom-quickstart]: https://github.com/coreos/prometheus-operator/tree/master/contrib/kube-prometheus#quickstart