/** * Check if a kafkaMetric is an interested metric. */ public static boolean isInterested(org.apache.kafka.common.MetricName metricName) { String group = metricName.group(); String name = metricName.name(); String type = metricName.tags().get(TYPE_KEY); return isInterested(group, name, type, metricName.tags()); }
/** * Create a MetricName with the given name, group, description and tags, plus default tags specified in the metric * configuration. Tag in tags takes precedence if the same tag key is specified in the default metric configuration. * * @param name The name of the metric * @param group logical group name of the metrics to which this metric belongs * @param description A human-readable description to include in the metric * @param tags additional key/value attributes of the metric */ public MetricName metricName(String name, String group, String description, Map<String, String> tags) { Map<String, String> combinedTag = new LinkedHashMap<>(config.tags()); combinedTag.putAll(tags); return new MetricName(name, group, description, combinedTag); }
@Override public MBeanInfo getMBeanInfo() { MBeanAttributeInfo[] attrs = new MBeanAttributeInfo[metrics.size()]; int i = 0; for (Map.Entry<String, KafkaMetric> entry : this.metrics.entrySet()) { String attribute = entry.getKey(); KafkaMetric metric = entry.getValue(); attrs[i] = new MBeanAttributeInfo(attribute, double.class.getName(), metric.metricName().description(), true, false, false); i += 1; } return new MBeanInfo(this.getClass().getName(), "", attrs, null, null, null); }
/** * @param metricName * @return standard JMX MBean name in the following format domainName:type=metricType,key1=val1,key2=val2 */ static String getMBeanName(String prefix, MetricName metricName) { StringBuilder mBeanName = new StringBuilder(); mBeanName.append(prefix); mBeanName.append(":type="); mBeanName.append(metricName.group()); for (Map.Entry<String, String> entry : metricName.tags().entrySet()) { if (entry.getKey().length() <= 0 || entry.getValue().length() <= 0) continue; mBeanName.append(","); mBeanName.append(entry.getKey()); mBeanName.append("="); mBeanName.append(Sanitizer.jmxSanitize(entry.getValue())); } return mBeanName.toString(); }
/** * Convert a KafkaMetric to a CruiseControlMetric */ public static CruiseControlMetric toCruiseControlMetric(KafkaMetric kafkaMetric, long now, int brokerId) { org.apache.kafka.common.MetricName metricName = kafkaMetric.metricName(); CruiseControlMetric ccm = toCruiseControlMetric(now, brokerId, metricName.name(), metricName.tags(), kafkaMetric.value()); if (ccm == null) { throw new IllegalArgumentException(String.format("Cannot convert KafkaMetric %s to a Cruise Control metric for " + "broker %d at time %d", kafkaMetric.metricName(), brokerId, now)); } return ccm; }
private KafkaMbean removeAttribute(KafkaMetric metric, String mBeanName) { MetricName metricName = metric.metricName(); KafkaMbean mbean = this.mbeans.get(mBeanName); if (mbean != null) mbean.removeAttribute(metricName.name()); return mbean; }
public void mirrorFrom(final ConcurrentMap<MetricName, KafkaMetric> kafkaMetrics, final Predicate<String> metricFilter, final Function<MetricName, String> metricNameAdjuster) { for (final Map.Entry<MetricName, KafkaMetric> entry : filter(kafkaMetrics, metricFilter)) { try { final MetricName metricName = entry.getKey(); final String realMetricName = metricNameAdjuster.apply(metricName); metricsFactory.createGauge( (metricName.group() + "_" + realMetricName).replaceAll("(-|\\.)", "_"), Strings.isNullOrEmpty(metricName.description()) ? "No description" : metricName.description(), () -> entry.getValue().value() ); } catch (final Exception e) { log.error("Could not process metrics", e); } } } }
MetricName inheritedMetric = inherited.metricInstance(SampleMetrics.METRIC_WITH_INHERITED_TAGS, childTagsWithValues); Map<String, String> filledOutTags = inheritedMetric.tags(); assertEquals("parent-tag should be set properly", filledOutTags.get("parent-tag"), "parent-tag-value"); assertEquals("child-tag should be set properly", filledOutTags.get("child-tag"), "child-tag-value");
private KafkaMetric getMetric(String name) throws Exception { Optional<Map.Entry<MetricName, KafkaMetric>> metric = metrics.metrics().entrySet().stream() .filter(entry -> entry.getKey().name().equals(name)) .findFirst(); if (!metric.isPresent()) throw new Exception(String.format("Could not find metric called %s", name)); return metric.get().getValue(); } }
private synchronized void checkAssignments() { logger.debug("Checking partition assignments"); try { KafkaConsumer<String, String> consumer = consumers.iterator().next(); Map<MetricName, ? extends Metric> metrics = consumer.metrics(); for (MetricName name : metrics.keySet()) { if ("assigned-partitions".equals(name.name())) { Metric metric = metrics.get(name); Map<String, String> tags = name.tags(); String clientId = tags.get("client-id"); int partitionCount = ((Double)metric.metricValue()).intValue(); processDataPoint(clientId, partitionCount, Instant.now()); } } } catch (NoSuchElementException ex) { } }
@Override public KeyValueStats getStats(Map<String, Object> stats) { super.getStats(stats); if (isOpen()) { Map<MetricName, ? extends Metric> kMetrics = producer.metrics(); Set<MetricName> keys = kMetrics.keySet(); for (MetricName kMetric : keys) { stats.put(Utils.qualify(this, kMetric.group() + "/" + kMetric.name()), kMetrics.get(kMetric).metricValue()); } } return this; }
/** * Determine if the supplied metric name is part of this group identifier. * * @param metricName the metric name * @return true if the metric name's group and tags match this group identifier, or false otherwise */ public boolean includes(MetricName metricName) { return metricName != null && groupName.equals(metricName.group()) && tags.equals(metricName.tags()); }
@Override public Map<String, Map<MetricName, ? extends Metric>> metrics() { ListenerConsumer listenerConsumerForMetrics = this.listenerConsumer; if (listenerConsumerForMetrics != null) { Map<MetricName, ? extends Metric> metrics = listenerConsumerForMetrics.consumer.metrics(); Iterator<MetricName> metricIterator = metrics.keySet().iterator(); if (metricIterator.hasNext()) { String clientId = metricIterator.next().tags().get("client-id"); return Collections.singletonMap(clientId, metrics); } } return Collections.emptyMap(); }
@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); sender.run(time.milliseconds()); // connect sender.run(time.milliseconds()); // send produce request 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"); }
private KafkaMbean addAttribute(KafkaMetric metric) { try { MetricName metricName = metric.metricName(); String mBeanName = getMBeanName(prefix, metricName); if (!this.mbeans.containsKey(mBeanName)) mbeans.put(mBeanName, new KafkaMbean(mBeanName)); KafkaMbean mbean = this.mbeans.get(mBeanName); mbean.setAttribute(metricName.name(), metric); return mbean; } catch (JMException e) { throw new KafkaException("Error creating mbean attribute for metricName :" + metric.metricName(), e); } }
public boolean matches(MetricName name, KafkaMetric metric) { Boolean excluded = pattern.matcher(name.name()).matches(); // Collect client aggregated metrics only. So we need to exclude topic-level or broker-level metrics, including: // 1. exclude metrics whose `tags` contain "topic" and "node-id". // 2. exclude metrics whose MetricName format is '{topic}-{partition}.xxx', like test-1.records-lag-avg. Boolean topicLevelMetrics = name.tags().containsKey("topic") || name.tags().containsKey("node-id") || name.name().contains("."); return !excluded && !topicLevelMetrics; } }
protected MetricName name(String metricName) { return new MetricName(metricName, "group-id", "desc", Collections.<String, String>emptyMap()); }
/** * @param metricName * @return standard JMX MBean name in the following format * domainName:type=metricType,key1=val1,key2=val2 */ private String getMBeanName(MetricName metricName) { StringBuilder mBeanName = new StringBuilder(); mBeanName.append(prefix); mBeanName.append(":type="); mBeanName.append(metricName.group()); for (Map.Entry<String, String> entry : metricName.tags().entrySet()) { if(entry.getKey().length() <= 0 || entry.getValue().length() <= 0) continue; mBeanName.append(","); mBeanName.append(entry.getKey()); mBeanName.append("="); mBeanName.append(entry.getValue()); } return mBeanName.toString(); }