Search in sources :

Example 6 with MetricNameTemplate

use of org.apache.kafka.common.MetricNameTemplate in project apache-kafka-on-k8s by banzaicloud.

the class Metrics method toHtmlTable.

/**
 * Use the specified domain and metric name templates to generate an HTML table documenting the metrics. A separate table section
 * will be generated for each of the MBeans and the associated attributes. The MBean names are lexicographically sorted to
 * determine the order of these sections. This order is therefore dependent upon the order of the
 * tags in each {@link MetricNameTemplate}.
 *
 * @param domain the domain or prefix for the JMX MBean names; may not be null
 * @param allMetrics the collection of all {@link MetricNameTemplate} instances each describing one metric; may not be null
 * @return the string containing the HTML table; never null
 */
public static String toHtmlTable(String domain, Iterable<MetricNameTemplate> allMetrics) {
    Map<String, Map<String, String>> beansAndAttributes = new TreeMap<String, Map<String, String>>();
    try (Metrics metrics = new Metrics()) {
        for (MetricNameTemplate template : allMetrics) {
            Map<String, String> tags = new LinkedHashMap<>();
            for (String s : template.tags()) {
                tags.put(s, "{" + s + "}");
            }
            MetricName metricName = metrics.metricName(template.name(), template.group(), template.description(), tags);
            String mBeanName = JmxReporter.getMBeanName(domain, metricName);
            if (!beansAndAttributes.containsKey(mBeanName)) {
                beansAndAttributes.put(mBeanName, new TreeMap<String, String>());
            }
            Map<String, String> attrAndDesc = beansAndAttributes.get(mBeanName);
            if (!attrAndDesc.containsKey(template.name())) {
                attrAndDesc.put(template.name(), template.description());
            } else {
                throw new IllegalArgumentException("mBean '" + mBeanName + "' attribute '" + template.name() + "' is defined twice.");
            }
        }
    }
    StringBuilder b = new StringBuilder();
    b.append("<table class=\"data-table\"><tbody>\n");
    for (Entry<String, Map<String, String>> e : beansAndAttributes.entrySet()) {
        b.append("<tr>\n");
        b.append("<td colspan=3 class=\"mbeanName\" style=\"background-color:#ccc; font-weight: bold;\">");
        b.append(e.getKey());
        b.append("</td>");
        b.append("</tr>\n");
        b.append("<tr>\n");
        b.append("<th style=\"width: 90px\"></th>\n");
        b.append("<th>Attribute name</th>\n");
        b.append("<th>Description</th>\n");
        b.append("</tr>\n");
        for (Entry<String, String> e2 : e.getValue().entrySet()) {
            b.append("<tr>\n");
            b.append("<td></td>");
            b.append("<td>");
            b.append(e2.getKey());
            b.append("</td>");
            b.append("<td>");
            b.append(e2.getValue());
            b.append("</td>");
            b.append("</tr>\n");
        }
    }
    b.append("</tbody></table>");
    return b.toString();
}
Also used : TreeMap(java.util.TreeMap) MetricNameTemplate(org.apache.kafka.common.MetricNameTemplate) LinkedHashMap(java.util.LinkedHashMap) MetricName(org.apache.kafka.common.MetricName) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 7 with MetricNameTemplate

use of org.apache.kafka.common.MetricNameTemplate in project apache-kafka-on-k8s by banzaicloud.

the class SenderTest method testSenderMetricsTemplates.

@Test
public void testSenderMetricsTemplates() throws Exception {
    metrics.close();
    Map<String, String> clientTags = Collections.singletonMap("client-id", "clientA");
    metrics = new Metrics(new MetricConfig().tags(clientTags));
    SenderMetricsRegistry metricsRegistry = new SenderMetricsRegistry(metrics);
    Sender sender = new Sender(logContext, client, metadata, this.accumulator, false, MAX_REQUEST_SIZE, ACKS_ALL, 1, metricsRegistry, time, REQUEST_TIMEOUT, 50, null, apiVersions);
    // Append a message so that topic metrics are created
    accumulator.append(tp0, 0L, "key".getBytes(), "value".getBytes(), null, null, MAX_BLOCK_TIMEOUT);
    // connect
    sender.run(time.milliseconds());
    // send produce request
    sender.run(time.milliseconds());
    client.respond(produceResponse(tp0, 0, Errors.NONE, 0));
    sender.run(time.milliseconds());
    // Create throttle time metrics
    Sender.throttleTimeSensor(metricsRegistry);
    // Verify that all metrics except metrics-count have registered templates
    Set<MetricNameTemplate> allMetrics = new HashSet<>();
    for (MetricName n : metrics.metrics().keySet()) {
        if (!n.group().equals("kafka-metrics-count"))
            allMetrics.add(new MetricNameTemplate(n.name(), n.group(), "", n.tags().keySet()));
    }
    TestUtils.checkEquals(allMetrics, new HashSet<>(metricsRegistry.allTemplates()), "metrics", "templates");
}
Also used : MetricConfig(org.apache.kafka.common.metrics.MetricConfig) MetricName(org.apache.kafka.common.MetricName) Metrics(org.apache.kafka.common.metrics.Metrics) MetricNameTemplate(org.apache.kafka.common.MetricNameTemplate) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 8 with MetricNameTemplate

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

the class ConnectMetricsRegistry method createTemplate.

private MetricNameTemplate createTemplate(String name, String group, String doc, Set<String> tags) {
    MetricNameTemplate template = new MetricNameTemplate(name, group, doc, tags);
    allTemplates.add(template);
    return template;
}
Also used : MetricNameTemplate(org.apache.kafka.common.MetricNameTemplate)

Example 9 with MetricNameTemplate

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

the class FetcherTest method testFetcherMetricsTemplates.

@Test
public void testFetcherMetricsTemplates() {
    Map<String, String> clientTags = Collections.singletonMap("client-id", "clientA");
    buildFetcher(new MetricConfig().tags(clientTags), OffsetResetStrategy.EARLIEST, new ByteArrayDeserializer(), new ByteArrayDeserializer(), Integer.MAX_VALUE, IsolationLevel.READ_UNCOMMITTED);
    // Fetch from topic to generate topic metrics
    assignFromUser(singleton(tp0));
    subscriptions.seek(tp0, 0);
    assertEquals(1, fetcher.sendFetches());
    client.prepareResponse(fullFetchResponse(tidp0, this.records, Errors.NONE, 100L, 0));
    consumerClient.poll(time.timer(0));
    assertTrue(fetcher.hasCompletedFetches());
    Map<TopicPartition, List<ConsumerRecord<byte[], byte[]>>> partitionRecords = fetchedRecords();
    assertTrue(partitionRecords.containsKey(tp0));
    // Create throttle metrics
    Fetcher.throttleTimeSensor(metrics, metricsRegistry);
    // Verify that all metrics except metrics-count have registered templates
    Set<MetricNameTemplate> allMetrics = new HashSet<>();
    for (MetricName n : metrics.metrics().keySet()) {
        String name = n.name().replaceAll(tp0.toString(), "{topic}-{partition}");
        if (!n.group().equals("kafka-metrics-count"))
            allMetrics.add(new MetricNameTemplate(name, n.group(), "", n.tags().keySet()));
    }
    TestUtils.checkEquals(allMetrics, new HashSet<>(metricsRegistry.getAllTemplates()), "metrics", "templates");
}
Also used : MetricConfig(org.apache.kafka.common.metrics.MetricConfig) MetricName(org.apache.kafka.common.MetricName) TopicPartition(org.apache.kafka.common.TopicPartition) Collections.singletonList(java.util.Collections.singletonList) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) ByteArrayDeserializer(org.apache.kafka.common.serialization.ByteArrayDeserializer) MetricNameTemplate(org.apache.kafka.common.MetricNameTemplate) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 10 with MetricNameTemplate

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

the class SenderTest method testSenderMetricsTemplates.

@Test
public void testSenderMetricsTemplates() throws Exception {
    metrics.close();
    Map<String, String> clientTags = Collections.singletonMap("client-id", "clientA");
    metrics = new Metrics(new MetricConfig().tags(clientTags));
    SenderMetricsRegistry metricsRegistry = new SenderMetricsRegistry(metrics);
    Sender sender = new Sender(logContext, client, metadata, this.accumulator, false, MAX_REQUEST_SIZE, ACKS_ALL, 1, metricsRegistry, time, REQUEST_TIMEOUT, RETRY_BACKOFF_MS, null, apiVersions);
    // Append a message so that topic metrics are created
    appendToAccumulator(tp0, 0L, "key", "value");
    // connect
    sender.runOnce();
    // send produce request
    sender.runOnce();
    client.respond(produceResponse(tp0, 0, Errors.NONE, 0));
    sender.runOnce();
    // Create throttle time metrics
    Sender.throttleTimeSensor(metricsRegistry);
    // Verify that all metrics except metrics-count have registered templates
    Set<MetricNameTemplate> allMetrics = new HashSet<>();
    for (MetricName n : metrics.metrics().keySet()) {
        if (!n.group().equals("kafka-metrics-count"))
            allMetrics.add(new MetricNameTemplate(n.name(), n.group(), "", n.tags().keySet()));
    }
    TestUtils.checkEquals(allMetrics, new HashSet<>(metricsRegistry.allTemplates()), "metrics", "templates");
}
Also used : MetricConfig(org.apache.kafka.common.metrics.MetricConfig) MetricName(org.apache.kafka.common.MetricName) Metrics(org.apache.kafka.common.metrics.Metrics) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MetricNameTemplate(org.apache.kafka.common.MetricNameTemplate) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

MetricNameTemplate (org.apache.kafka.common.MetricNameTemplate)10 MetricName (org.apache.kafka.common.MetricName)6 HashSet (java.util.HashSet)4 MetricConfig (org.apache.kafka.common.metrics.MetricConfig)4 Metrics (org.apache.kafka.common.metrics.Metrics)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 TopicPartition (org.apache.kafka.common.TopicPartition)2 Test (org.junit.Test)2 Test (org.junit.jupiter.api.Test)2 Arrays.asList (java.util.Arrays.asList)1 Collections.emptyList (java.util.Collections.emptyList)1 Collections.singletonList (java.util.Collections.singletonList)1 ByteArrayDeserializer (org.apache.kafka.common.serialization.ByteArrayDeserializer)1