diff --git a/modules/osdk-monitoring-prometheus-metrics-helper-modifying-port.adoc b/modules/osdk-monitoring-prometheus-metrics-helper-modifying-port.adoc index b7db1e45169c..e9918cc0e7c8 100644 --- a/modules/osdk-monitoring-prometheus-metrics-helper-modifying-port.adoc +++ b/modules/osdk-monitoring-prometheus-metrics-helper-modifying-port.adoc @@ -3,16 +3,16 @@ // * applications/operator_sdk/osdk-monitoring-prometheus.adoc [id="osdk-monitoring-prometheus-metrics-helper-modifying-port-{context}"] -= Modifying metrics port += Modifying the metrics port Operator authors can modify the port that metrics are exposed on. .Prerequisites -- Go-based Operator generated using the Operator SDK -- Kubernetes-based cluster with the Prometheus Operator deployed +* Go-based Operator generated using the Operator SDK +* Kubernetes-based cluster with the Prometheus Operator deployed .Procedure -. In the generated Operator's `cmd/manager/main.go` file, change the `var -metricsPort int32 = 8383` variable. +* In the generated Operator's `cmd/manager/main.go` file, change the value of `metricsPort` in the line `var +metricsPort int32 = 8383`. diff --git a/modules/osdk-monitoring-prometheus-metrics-helper.adoc b/modules/osdk-monitoring-prometheus-metrics-helper.adoc index 73f7dd747396..1f79e8523ecd 100644 --- a/modules/osdk-monitoring-prometheus-metrics-helper.adoc +++ b/modules/osdk-monitoring-prometheus-metrics-helper.adoc @@ -5,7 +5,7 @@ [id="osdk-monitoring-prometheus-metrics-helper-{context}"] = Metrics helper -In Go-based Operators generated using the Operator SDK, the following helper +In Go-based Operators generated using the Operator SDK, the following function exposes general metrics about the running program: [source,go] @@ -13,8 +13,7 @@ function exposes general metrics about the running program: func ExposeMetricsPort(ctx context.Context, port int32) (*v1.Service, error) ---- -These metrics are inherited from the `controller-runtime` library API. The -metrics are by default served on `:8383/metrics`. +These metrics are inherited from the `controller-runtime` library API. By default, the metrics are served on `0.0.0.0:8383/metrics`. A Service object is created with the metrics port exposed, which can be then accessed by Prometheus. The Service object is garbage collected when the leader @@ -25,29 +24,34 @@ Operators generated using the Operator SDK: [source,go] ---- - import( - "github.com/operator-framework/operator-sdk/pkg/metrics" - "sigs.k8s.io/controller-runtime/pkg/manager" - ) - - // Change the below variables to serve metrics on different host or port. - var metricsHost = "0.0.0.0" <1> - var metricsPort int32 = 8383 <2> - +import( + "github.com/operator-framework/operator-sdk/pkg/metrics" + "sigs.k8s.io/controller-runtime/pkg/manager" +) + +var ( + // Change the below variables to serve metrics on a different host or port. + metricsHost = "0.0.0.0" <1> + metricsPort int32 = 8383 <2> +) +... +func main() { + ... // Pass metrics address to controller-runtime manager - mgr, err := manager.New(cfg, manager.Options{ - Namespace: namespace, - MetricsBindAddress: fmt.Sprintf("%s:%d", metricsHost, metricsPort), - }) + mgr, err := manager.New(cfg, manager.Options{ + Namespace: namespace, + MetricsBindAddress: fmt.Sprintf("%s:%d", metricsHost, metricsPort), + }) ... - - // Create Service object to expose the metrics port. - _, err = metrics.ExposeMetricsPort(ctx, metricsPort) - if err != nil { + // Create Service object to expose the metrics port. + _, err = metrics.ExposeMetricsPort(ctx, metricsPort) + if err != nil { // handle error - log.Info(err.Error()) - } + log.Info(err.Error()) + } + ... +} ---- -<1> Host the metrics are exposed on. -<2> Port the metrics are exposed on. +<1> The host that the metrics are exposed on. +<2> The port that the metrics are exposed on. diff --git a/modules/osdk-monitoring-prometheus-servicemonitor-creating.adoc b/modules/osdk-monitoring-prometheus-servicemonitor-creating.adoc index 49f1ef634954..17d35e3f3c02 100644 --- a/modules/osdk-monitoring-prometheus-servicemonitor-creating.adoc +++ b/modules/osdk-monitoring-prometheus-servicemonitor-creating.adoc @@ -11,22 +11,36 @@ newly created Service. .Prerequisites -- Go-based Operator generated using the Operator SDK -- Kubernetes-based cluster with the Prometheus Operator deployed +* Go-based Operator generated using the Operator SDK +* Kubernetes-based cluster with the Prometheus Operator deployed .Procedure -. Add the `metrics.CreateServiceMonitor()` helper function to your Operator code, -creating only one ServiceMonitor per application and namespace: +* Add the `metrics.CreateServiceMonitor()` helper function to your Operator code: + [source,go] ---- - import "github.com/operator-framework/operator-sdk/pkg/metrics" +import( + "k8s.io/api/core/v1" + "github.com/operator-framework/operator-sdk/pkg/metrics" + "sigs.k8s.io/controller-runtime/pkg/client/config" +) +func main() { - serviceMonitors, err := metrics.CreateServiceMonitors(config *rest.Config, ns string, services []*v1.Service) <1> + ... + // Populate below with the Service(s) for which you want to create ServiceMonitors. + services := []*v1.Service{} + // Create one ServiceMonitor per application per namespace. + // Change the below value to name of the Namespace you want the ServiceMonitor to be created in. + ns := "default" + // restConfig is used for talking to the Kubernetes apiserver + restConfig := config.GetConfig() + + // Pass the Service(s) to the helper function, which in turn returns the array of ServiceMonitor objects. + serviceMonitors, err := metrics.CreateServiceMonitors(restConfig, ns, services) if err != nil { - <2> + // Handle errors here. } + ... +} ---- -<1> Pass the Service(s) to the helper function, which in turn returns the ServiceMonitor object. -<2> Handle errors here. diff --git a/modules/osdk-monitoring-prometheus-servicemonitor.adoc b/modules/osdk-monitoring-prometheus-servicemonitor.adoc index 77fc6d2592fe..8bf8cc0cbad8 100644 --- a/modules/osdk-monitoring-prometheus-servicemonitor.adoc +++ b/modules/osdk-monitoring-prometheus-servicemonitor.adoc @@ -15,6 +15,6 @@ generate a ServiceMonitor Custom Resource (CR) based on it. .Additional resources -- See the +* See the link:https://github.com/coreos/prometheus-operator/blob/7a25bf6b6bb2347dacb235659b73bc210117acc7/Documentation/design.md#servicemonitor[Prometheus Operator documentation] -for more about the ServiceMonitor CRD. +for more information about the ServiceMonitor CRD.