Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • Adding a new function above to the Metrics API. As part of this change, the registerMetric  method's return type would be changed from void  to KafkaMetric . It would return a KafkaMetric object if the requested metric already exists or return null if not after creating/registering the metric. The already existing addMetric  would throw an IllegalArgumentException  if registerMetric  returns a non-null value while metricOrElseCreate  would return the metric when non-For backward compatibility reasons, any place currently which relied on IllegalArgumentException would now instead check the output of registerMetric  and throw an IllegalArguementException  when the returned value of registerMetric  is non-null. This change would happen in => Metrics.addMetric , 2 Sensor.add  methods. On the other hand, metricOrElseCreate  method would simply return the object returned by registerMetric if not null.

    Code Block
    languagejava
    /**
         * Register a metric if not present or return an already existing metric otherwise.
         * When a metric is newly registered, this method returns null
         *
         * @param metric The KafkaMetric to register
         * @return KafkaMetric if the metric already exists, null otherwise
         */
    synchronized KafkaMetric registerMetric(KafkaMetric metric) {
            MetricName metricName = metric.metricName();
            if (this.metrics.containsKey(metricName)) {
                return this.metrics.get(metricName);
            }
            this.metrics.put(metricName, metric);
            for (MetricsReporter reporter : reporters) {
                try {
                    reporter.metricChange(metric);
                } catch (Exception e) {
                    log.error("Error when registering metric on " + reporter.getClass().getName(), e);
                }
            }
            log.trace("Registered metric named {}", metricName);
            return null;
        }


...