Search in sources :

Example 1 with MetricName

use of org.apache.kafka.common.MetricName in project kafka by apache.

the class FetcherTest method testQuotaMetrics.

/*
     * Send multiple requests. Verify that the client side quota metrics have the right values
     */
@Test
public void testQuotaMetrics() throws Exception {
    subscriptions.assignFromUser(singleton(tp));
    subscriptions.seek(tp, 0);
    // normal fetch
    for (int i = 1; i < 4; i++) {
        // We need to make sure the message offset grows. Otherwise they will be considered as already consumed
        // and filtered out by consumer.
        MemoryRecordsBuilder builder = MemoryRecords.builder(ByteBuffer.allocate(1024), CompressionType.NONE, TimestampType.CREATE_TIME);
        for (int v = 0; v < 3; v++) builder.appendWithOffset((long) i * 3 + v, Record.NO_TIMESTAMP, "key".getBytes(), String.format("value-%d", v).getBytes());
        List<ConsumerRecord<byte[], byte[]>> records = fetchRecords(builder.build(), Errors.NONE, 100L, 100 * i).get(tp);
        assertEquals(3, records.size());
    }
    Map<MetricName, KafkaMetric> allMetrics = metrics.metrics();
    KafkaMetric avgMetric = allMetrics.get(metrics.metricName("fetch-throttle-time-avg", metricGroup, ""));
    KafkaMetric maxMetric = allMetrics.get(metrics.metricName("fetch-throttle-time-max", metricGroup, ""));
    assertEquals(200, avgMetric.value(), EPSILON);
    assertEquals(300, maxMetric.value(), EPSILON);
}
Also used : MetricName(org.apache.kafka.common.MetricName) MemoryRecordsBuilder(org.apache.kafka.common.record.MemoryRecordsBuilder) KafkaMetric(org.apache.kafka.common.metrics.KafkaMetric) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) Test(org.junit.Test)

Example 2 with MetricName

use of org.apache.kafka.common.MetricName in project kafka by apache.

the class StreamsMetricsImplTest method testLatencyMetrics.

@Test
public void testLatencyMetrics() {
    String groupName = "doesNotMatter";
    String scope = "scope";
    String entity = "entity";
    String operation = "put";
    Map<String, String> tags = new HashMap<>();
    StreamsMetricsImpl streamsMetrics = new StreamsMetricsImpl(new Metrics(), groupName, tags);
    Sensor sensor1 = streamsMetrics.addLatencyAndThroughputSensor(scope, entity, operation, Sensor.RecordingLevel.DEBUG);
    Map<MetricName, ? extends Metric> metrics = streamsMetrics.metrics();
    // 6 metrics plus a common metric that keeps track of total registered metrics in Metrics() constructor
    assertEquals(metrics.size(), 7);
    streamsMetrics.removeSensor(sensor1);
    metrics = streamsMetrics.metrics();
    assertEquals(metrics.size(), 1);
}
Also used : MetricName(org.apache.kafka.common.MetricName) Metrics(org.apache.kafka.common.metrics.Metrics) HashMap(java.util.HashMap) Sensor(org.apache.kafka.common.metrics.Sensor) Test(org.junit.Test)

Example 3 with MetricName

use of org.apache.kafka.common.MetricName in project kafka by apache.

the class RecordAccumulator method registerMetrics.

private void registerMetrics(Metrics metrics, String metricGrpName) {
    MetricName metricName = metrics.metricName("waiting-threads", metricGrpName, "The number of user threads blocked waiting for buffer memory to enqueue their records");
    Measurable waitingThreads = new Measurable() {

        public double measure(MetricConfig config, long now) {
            return free.queued();
        }
    };
    metrics.addMetric(metricName, waitingThreads);
    metricName = metrics.metricName("buffer-total-bytes", metricGrpName, "The maximum amount of buffer memory the client can use (whether or not it is currently used).");
    Measurable totalBytes = new Measurable() {

        public double measure(MetricConfig config, long now) {
            return free.totalMemory();
        }
    };
    metrics.addMetric(metricName, totalBytes);
    metricName = metrics.metricName("buffer-available-bytes", metricGrpName, "The total amount of buffer memory that is not being used (either unallocated or in the free list).");
    Measurable availableBytes = new Measurable() {

        public double measure(MetricConfig config, long now) {
            return free.availableMemory();
        }
    };
    metrics.addMetric(metricName, availableBytes);
    Sensor bufferExhaustedRecordSensor = metrics.sensor("buffer-exhausted-records");
    metricName = metrics.metricName("buffer-exhausted-rate", metricGrpName, "The average per-second number of record sends that are dropped due to buffer exhaustion");
    bufferExhaustedRecordSensor.add(metricName, new Rate());
}
Also used : MetricConfig(org.apache.kafka.common.metrics.MetricConfig) MetricName(org.apache.kafka.common.MetricName) Measurable(org.apache.kafka.common.metrics.Measurable) Rate(org.apache.kafka.common.metrics.stats.Rate) Sensor(org.apache.kafka.common.metrics.Sensor)

Example 4 with MetricName

use of org.apache.kafka.common.MetricName in project kafka by apache.

the class JmxReporter method removeAttribute.

private KafkaMbean removeAttribute(KafkaMetric metric) {
    MetricName metricName = metric.metricName();
    String mBeanName = getMBeanName(metricName);
    KafkaMbean mbean = this.mbeans.get(mBeanName);
    if (mbean != null)
        mbean.removeAttribute(metricName.name());
    return mbean;
}
Also used : MetricName(org.apache.kafka.common.MetricName)

Example 5 with MetricName

use of org.apache.kafka.common.MetricName in project kafka by apache.

the class MetricsTest method testMetricName.

@Test
public void testMetricName() {
    MetricName n1 = metrics.metricName("name", "group", "description", "key1", "value1", "key2", "value2");
    Map<String, String> tags = new HashMap<String, String>();
    tags.put("key1", "value1");
    tags.put("key2", "value2");
    MetricName n2 = metrics.metricName("name", "group", "description", tags);
    assertEquals("metric names created in two different ways should be equal", n1, n2);
    try {
        metrics.metricName("name", "group", "description", "key1");
        fail("Creating MetricName with an odd number of keyValue should fail");
    } catch (IllegalArgumentException e) {
    // this is expected
    }
}
Also used : MetricName(org.apache.kafka.common.MetricName) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

MetricName (org.apache.kafka.common.MetricName)13 Test (org.junit.Test)6 Sensor (org.apache.kafka.common.metrics.Sensor)4 HashMap (java.util.HashMap)3 KafkaMetric (org.apache.kafka.common.metrics.KafkaMetric)3 Metrics (org.apache.kafka.common.metrics.Metrics)3 Map (java.util.Map)2 KafkaMetricWrapper (org.apache.flink.streaming.connectors.kafka.internals.metrics.KafkaMetricWrapper)2 MemoryRecordsBuilder (org.apache.kafka.common.record.MemoryRecordsBuilder)2 ArrayList (java.util.ArrayList)1 JMException (javax.management.JMException)1 RuntimeContext (org.apache.flink.api.common.functions.RuntimeContext)1 MetricGroup (org.apache.flink.metrics.MetricGroup)1 StreamingRuntimeContext (org.apache.flink.streaming.api.operators.StreamingRuntimeContext)1 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)1 OffsetAndMetadata (org.apache.kafka.clients.consumer.OffsetAndMetadata)1 OffsetCommitCallback (org.apache.kafka.clients.consumer.OffsetCommitCallback)1 Callback (org.apache.kafka.clients.producer.Callback)1 RecordMetadata (org.apache.kafka.clients.producer.RecordMetadata)1 KafkaException (org.apache.kafka.common.KafkaException)1