Search in sources :

Example 56 with KafkaMetric

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);
        }
    };
}
Also used : MetricName(org.apache.kafka.common.MetricName) SensorMetric(io.confluent.ksql.metrics.TopicSensors.SensorMetric) KafkaMetric(org.apache.kafka.common.metrics.KafkaMetric) Sensor(org.apache.kafka.common.metrics.Sensor)

Example 57 with KafkaMetric

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)));
}
Also used : KafkaMetric(org.apache.kafka.common.metrics.KafkaMetric) Test(org.junit.Test)

Example 58 with KafkaMetric

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();
}
Also used : MetricConfig(org.apache.kafka.common.metrics.MetricConfig) MetricName(org.apache.kafka.common.MetricName) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) Value(org.apache.kafka.common.metrics.stats.Value) Metric(org.apache.kafka.common.Metric) KafkaMetric(org.apache.kafka.common.metrics.KafkaMetric) KafkaMetric(org.apache.kafka.common.metrics.KafkaMetric) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) Test(org.junit.jupiter.api.Test)

Example 59 with KafkaMetric

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));
}
Also used : MetricConfig(org.apache.kafka.common.metrics.MetricConfig) Measurement(io.micrometer.core.instrument.Measurement) MetricName(org.apache.kafka.common.MetricName) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) Value(org.apache.kafka.common.metrics.stats.Value) KafkaMetric(org.apache.kafka.common.metrics.KafkaMetric) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) Issue(io.micrometer.core.Issue) Test(org.junit.jupiter.api.Test)

Example 60 with KafkaMetric

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);
}
Also used : MetricConfig(org.apache.kafka.common.metrics.MetricConfig) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) KafkaMetric(org.apache.kafka.common.metrics.KafkaMetric) LinkedHashMap(java.util.LinkedHashMap) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) MetricName(org.apache.kafka.common.MetricName) Value(org.apache.kafka.common.metrics.stats.Value) Metric(org.apache.kafka.common.Metric) KafkaMetric(org.apache.kafka.common.metrics.KafkaMetric) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Aggregations

KafkaMetric (org.apache.kafka.common.metrics.KafkaMetric)129 Test (org.junit.Test)84 MetricName (org.apache.kafka.common.MetricName)59 HashMap (java.util.HashMap)30 LinkedHashMap (java.util.LinkedHashMap)24 Test (org.junit.jupiter.api.Test)23 Map (java.util.Map)18 MetricConfig (org.apache.kafka.common.metrics.MetricConfig)16 Metric (org.apache.kafka.common.Metric)15 MemoryRecordsBuilder (org.apache.kafka.common.record.MemoryRecordsBuilder)14 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)13 MeterRegistry (io.micrometer.core.instrument.MeterRegistry)12 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)12 Value (org.apache.kafka.common.metrics.stats.Value)12 Windowed (org.apache.kafka.streams.kstream.Windowed)12 Sensor (org.apache.kafka.common.metrics.Sensor)11 TopicPartition (org.apache.kafka.common.TopicPartition)10 Metrics (org.apache.kafka.common.metrics.Metrics)10 List (java.util.List)8 Cluster (org.apache.kafka.common.Cluster)8