use of org.apache.kafka.common.metrics.KafkaMetric in project ksql by confluentinc.
the class StreamsErrorCollector method buildSensor.
private SensorMetric<Object> buildSensor(final String key, final String metricNameString, final MeasurableStat stat, final Function<Object, Double> recordValue) {
final String name = "sec-" + key + "-" + metricNameString + "-" + id;
final MetricName metricName = new MetricName(metricNameString, "consumer-metrics", "consumer-" + name, ImmutableMap.of("key", key, "id", id));
final Sensor sensor = metrics.sensor(name);
sensor.add(metricName, stat);
final KafkaMetric metric = metrics.metrics().get(metricName);
return new TopicSensors.SensorMetric<Object>(sensor, metric, time, true) {
void record(final Object o) {
sensor.record(recordValue.apply(o));
super.record(o);
}
};
}
use of org.apache.kafka.common.metrics.KafkaMetric in project ksql by confluentinc.
the class UdfMetricProducerTest method shouldRecordMetrics.
@Test
public void shouldRecordMetrics() {
final UdfMetricProducer metricProducer = new UdfMetricProducer(sensor, args -> {
time.sleep(100);
return null;
}, time);
metricProducer.evaluate("foo");
final KafkaMetric metric = metrics.metric(metricName);
final Double actual = (Double) metric.metricValue();
assertThat(actual.longValue(), equalTo(TimeUnit.MILLISECONDS.toNanos(100)));
}
use of org.apache.kafka.common.metrics.KafkaMetric in project micrometer by micrometer-metrics.
the class KafkaMetricsTest method closeShouldRemoveAllMeters.
@Test
void closeShouldRemoveAllMeters() {
// Given
Supplier<Map<MetricName, ? extends Metric>> supplier = () -> {
MetricName metricName = new MetricName("a", "b", "c", new LinkedHashMap<>());
KafkaMetric metric = new KafkaMetric(this, metricName, new Value(), new MetricConfig(), Time.SYSTEM);
return Collections.singletonMap(metricName, metric);
};
kafkaMetrics = new KafkaMetrics(supplier);
MeterRegistry registry = new SimpleMeterRegistry();
kafkaMetrics.bindTo(registry);
assertThat(registry.getMeters()).hasSize(1);
kafkaMetrics.close();
assertThat(registry.getMeters()).isEmpty();
}
use of org.apache.kafka.common.metrics.KafkaMetric in project micrometer by micrometer-metrics.
the class KafkaMetricsTest method shouldUseMetricFromSupplierIfInstanceChanges.
@Issue("#2726")
@Test
void shouldUseMetricFromSupplierIfInstanceChanges() {
MetricName metricName = new MetricName("a0", "b0", "c0", new LinkedHashMap<>());
Value oldValue = new Value();
oldValue.record(new MetricConfig(), 1.0, System.currentTimeMillis());
KafkaMetric oldMetricInstance = new KafkaMetric(this, metricName, oldValue, new MetricConfig(), Time.SYSTEM);
Map<MetricName, KafkaMetric> metrics = new HashMap<>();
metrics.put(metricName, oldMetricInstance);
kafkaMetrics = new KafkaMetrics(() -> metrics);
MeterRegistry registry = new SimpleMeterRegistry();
kafkaMetrics.bindTo(registry);
assertThat(registry.getMeters()).hasSize(1);
Value newValue = new Value();
newValue.record(new MetricConfig(), 2.0, System.currentTimeMillis());
KafkaMetric newMetricInstance = new KafkaMetric(this, metricName, newValue, new MetricConfig(), Time.SYSTEM);
metrics.put(metricName, newMetricInstance);
kafkaMetrics.checkAndBindMetrics(registry);
assertThat(registry.getMeters()).singleElement().extracting(Meter::measure).satisfies(measurements -> assertThat(measurements).singleElement().extracting(Measurement::getValue).isEqualTo(2.0));
}
use of org.apache.kafka.common.metrics.KafkaMetric in project micrometer by micrometer-metrics.
the class KafkaMetricsTest method shouldRemoveOlderMeterWithLessTags.
@Test
void shouldRemoveOlderMeterWithLessTags() {
Map<String, String> tags = new LinkedHashMap<>();
Supplier<Map<MetricName, ? extends Metric>> supplier = () -> {
MetricName metricName = new MetricName("a", "b", "c", tags);
KafkaMetric metric = new KafkaMetric(this, metricName, new Value(), new MetricConfig(), Time.SYSTEM);
return Collections.singletonMap(metricName, metric);
};
kafkaMetrics = new KafkaMetrics(supplier);
MeterRegistry registry = new SimpleMeterRegistry();
kafkaMetrics.bindTo(registry);
assertThat(registry.getMeters()).hasSize(1);
// only version
assertThat(registry.getMeters().get(0).getId().getTags()).hasSize(1);
tags.put("key0", "value0");
kafkaMetrics.checkAndBindMetrics(registry);
assertThat(registry.getMeters()).hasSize(1);
assertThat(registry.getMeters().get(0).getId().getTags()).hasSize(2);
}
Aggregations