// Module included in the following assemblies:
//
// metrics/assembly-metrics.adoc

[id='con-metrics-cruise-control-{context}']
= Monitoring Cruise Control operations

[role="_abstract"]
Cruise Control monitors Kafka brokers in order to track the utilization of brokers, topics, and partitions.
Cruise Control also provides a set of metrics for monitoring its own performance.

The Cruise Control metrics reporter collects raw metrics data from Kafka brokers.
The data is produced to topics that are automatically created by Cruise Control.
The metrics are used to link:{BookURLUsing}#proc-generating-optimization-proposals-str[generate optimization proposals for Kafka clusters^].

Cruise Control metrics are available for real-time monitoring of Cruise Control operations.
For example, you can use Cruise Control metrics to monitor the status of rebalancing operations that are running or provide alerts on any anomalies that are detected in an operation's performance.

You expose Cruise Control metrics by enabling the {JMXExporter} in the Cruise Control configuration.

NOTE: For a full list of available Cruise Control metrics, which are known as _sensors_, see the {CruiseControlSensorsDocs}.

== Exposing Cruise Control metrics

If you want to expose metrics on Cruise Control operations, configure the `Kafka` resource xref:proc-metrics-kafka-deploy-options-{context}[to deploy Cruise Control and enable Prometheus metrics in the deployment].
You can use your own configuration or use the example `kafka-cruise-control-metrics.yaml` file provided by Strimzi.

You add the configuration to the `metricsConfig` of the `CruiseControl` property in the `Kafka` resource.
The configuration enables the {JMXExporter} to expose Cruise Control metrics through an HTTP endpoint.
The HTTP endpoint is scraped by the Prometheus server.

.Example metrics configuration for Cruise Control
[source,yaml,subs="+quotes,attributes"]
----
  apiVersion: {KafkaApiVersion}
  kind: Kafka
  metadata:
    name: my-cluster
  Spec:
    # ...
    cruiseControl:
      # ...
      metricsConfig:
        type: jmxPrometheusExporter
        valueFrom:
          configMapKeyRef:
            name: cruise-control-metrics
            key: metrics-config.yml
  ---
  kind: ConfigMap
  apiVersion: v1
  metadata:
    name: cruise-control-metrics
    labels:
      app: strimzi
  data:
    metrics-config.yml: |
    # _metrics configuration..._
----

== Viewing Cruise Control metrics

After you expose the Cruise Control metrics, you can use Prometheus or another suitable monitoring system to view information on the metrics data.
Strimzi provides an xref:assembly-metrics-config-files-str[example Grafana dashboard] to display visualizations of Cruise Control metrics.
The dashboard is a JSON file called `strimzi-cruise-control.json`.
The exposed metrics provide the monitoring data when you xref:proc-metrics-grafana-dashboard-str[enable the Grafana dashboard].

=== Monitoring balancedness scores

Cruise Control metrics include a balancedness score.
Balancedness is the measure of how evenly a workload is distributed in a Kafka cluster.

The Cruise Control metric for balancedness score (`balancedness-score`) might differ from the balancedness score in the `KafkaRebalance` resource.
Cruise Control calculates each score using `anomaly.detection.goals` which might not be the same as the `default.goals` used in the `KafkaRebalance` resource.
The `anomaly.detection.goals` are specified in the `spec.cruiseControl.config` of the `Kafka` custom resource.

[NOTE]
====
Refreshing the `KafkaRebalance` resource fetches an optimization proposal.
The latest cached optimization proposal is fetched if one of the following conditions applies:

* KafkaRebalance `goals` match the goals configured in the `default.goals` section of the `Kafka` resource
* KafkaRebalance `goals` are not specified

Otherwise, Cruise Control generates a new optimization proposal based on KafkaRebalance `goals`. If new proposals are generated with each refresh, this can impact performance monitoring.
====

=== Alerts on anomaly detection

Cruise control's _anomaly detector_ provides metrics data for conditions that block the generation of optimization goals, such as broker failures.
If you want more visibility, you can use the metrics provided by the anomaly detector to set up alerts and send out notifications.
You can set up Cruise Control’s _anomaly notifier_ to route alerts based on these metrics through a specified notification channel.
Alternatively, you can set up Prometheus to scrape the metrics data provided by the anomaly detector and generate alerts.
Prometheus Alertmanager can then route the alerts generated by Prometheus.

The {CruiseControlConfigDocs} provides information on `AnomalyDetector` metrics and the anomaly notifier.
