ProducerMetrics implements Metrics { @Override public void collect(Stats stats) { if (requestRate != null) stats.put(statName("request_rate"), (Double) requestRate.metricValue()); if (requestSizeAvg != null) { Double value = (Double) requestSizeAvg.metricValue(); if (!value.isNaN()) stats.put(statName("request_size_avg"), value); } if (outgoingByteRate != null) stats.put(statName("outgoing_byte_rate"), (Double) outgoingByteRate.metricValue()); } ProducerMetrics(String name); @Override void collect(Stats stats); void set(Map<MetricName, ? extends Metric> kafkaMetrics);  }
@Test void collect() { when(requestSizeAvg.metricValue()).thenReturn(10.0); var stats = new Stats(); metrics.collect(stats); assertThat(stats.stats).containsEntry("kafka_producer_request_size_avg", 10.0); }
@Test void collectWithoutRequestSizeAvg() { when(requestSizeAvg.metricValue()).thenReturn(Double.NaN); var stats = new Stats(); metrics.collect(stats); assertThat(stats.stats).doesNotContainKeys("kafka_producer_request_size_avg"); }