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