Configuring built-in monitoring with Prometheus

This guide describes the built-in monitoring support provided by the Operator SDK using the Prometheus Operator and details usage for Operator authors.

Prometheus Operator support

Prometheus is an open-source systems monitoring and alerting toolkit. The Prometheus Operator creates, configures, and manages Prometheus clusters running on Kubernetes-based clusters, such as OKD.

Helper functions exist in the Operator SDK by default to automatically set up metrics in any generated Go-based Operator for use on clusters where the Prometheus Operator is deployed.

Metrics helper

In Go-based Operators generated using the Operator SDK, the following function exposes general metrics about the running program:

  1. func ExposeMetricsPort(ctx context.Context, port int32) (*v1.Service, error)

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 pod’s root owner is deleted.

The following example is present in the cmd/manager/main.go file in all Operators generated using the Operator SDK:

  1. import(
  2. "github.com/operator-framework/operator-sdk/pkg/metrics"
  3. "machine.openshift.io/controller-runtime/pkg/manager"
  4. )
  5. var (
  6. // Change the below variables to serve metrics on a different host or port.
  7. metricsHost = "0.0.0.0" (1)
  8. metricsPort int32 = 8383 (2)
  9. )
  10. ...
  11. func main() {
  12. ...
  13. // Pass metrics address to controller-runtime manager
  14. mgr, err := manager.New(cfg, manager.Options{
  15. Namespace: namespace,
  16. MetricsBindAddress: fmt.Sprintf("%s:%d", metricsHost, metricsPort),
  17. })
  18. ...
  19. // Create Service object to expose the metrics port.
  20. _, err = metrics.ExposeMetricsPort(ctx, metricsPort)
  21. if err != nil {
  22. // handle error
  23. log.Info(err.Error())
  24. }
  25. ...
  26. }
1The host that the metrics are exposed on.
2The port that the metrics are exposed on.

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

Procedure

  • In the cmd/manager/main.go file of the generated Operator, change the value of metricsPort in the following line:

    1. var metricsPort int32 = 8383

Service monitors

A ServiceMonitor is a custom resource provided by the Prometheus Operator that discovers the Endpoints in Service objects and configures Prometheus to monitor those pods.

In Go-based Operators generated using the Operator SDK, the GenerateServiceMonitor() helper function can take a Service object and generate a ServiceMonitor object based on it.

Additional resources

Creating service monitors

Operator authors can add service target discovery of created monitoring services using the metrics.CreateServiceMonitor() helper function, which accepts the newly created service.

Prerequisites

  • 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:

    1. import(
    2. "k8s.io/api/core/v1"
    3. "github.com/operator-framework/operator-sdk/pkg/metrics"
    4. "machine.openshift.io/controller-runtime/pkg/client/config"
    5. )
    6. func main() {
    7. ...
    8. // Populate below with the Service(s) for which you want to create ServiceMonitors.
    9. services := []*v1.Service{}
    10. // Create one ServiceMonitor per application per namespace.
    11. // Change the below value to name of the Namespace you want the ServiceMonitor to be created in.
    12. ns := "default"
    13. // restConfig is used for talking to the Kubernetes apiserver
    14. restConfig := config.GetConfig()
    15. // Pass the Service(s) to the helper function, which in turn returns the array of ServiceMonitor objects.
    16. serviceMonitors, err := metrics.CreateServiceMonitors(restConfig, ns, services)
    17. if err != nil {
    18. // Handle errors here.
    19. }
    20. ...
    21. }