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