Search in sources :

Example 1 with MetricNameTemplate

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

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 2 with MetricNameTemplate

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

the class FetcherTest method testFetcherMetricsTemplates.

@Test
public void testFetcherMetricsTemplates() throws Exception {
    metrics.close();
    Map<String, String> clientTags = Collections.singletonMap("client-id", "clientA");
    metrics = new Metrics(new MetricConfig().tags(clientTags));
    metricsRegistry = new FetcherMetricsRegistry(clientTags.keySet(), "consumer" + groupId);
    fetcher.close();
    fetcher = createFetcher(subscriptions, metrics);
    // Fetch from topic to generate topic metrics
    subscriptions.assignFromUser(singleton(tp0));
    subscriptions.seek(tp0, 0);
    assertEquals(1, fetcher.sendFetches());
    client.prepareResponse(fullFetchResponse(tp0, this.records, Errors.NONE, 100L, 0));
    consumerClient.poll(0);
    assertTrue(fetcher.hasCompletedFetches());
    Map<TopicPartition, List<ConsumerRecord<byte[], byte[]>>> partitionRecords = fetcher.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) MetricNameTemplate(org.apache.kafka.common.MetricNameTemplate) MetricName(org.apache.kafka.common.MetricName) Metrics(org.apache.kafka.common.metrics.Metrics) TopicPartition(org.apache.kafka.common.TopicPartition) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with MetricNameTemplate

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

the class SenderMetricsRegistry method createTemplate.

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

Example 4 with MetricNameTemplate

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

the class SenderMetricsRegistry method createTemplate.

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

Example 5 with MetricNameTemplate

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

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<>();
    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<>());
            }
            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)

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