Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port "KAFKA-1723; make the metrics name in new producer more standard; patched by Manikumar Reddy; reviewed by Jay Kreps and Jun Rao" to common-metrics. #11

Merged
merged 1 commit into from
Jan 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,19 @@ public interface CompoundStat extends Stat {

public static class NamedMeasurable {

private final String name;
private final String description;
private final MetricName name;
private final Measurable stat;

public NamedMeasurable(String name, String description, Measurable stat) {
public NamedMeasurable(MetricName name, Measurable stat) {
super();
this.name = name;
this.description = description;
this.stat = stat;
}

public String name() {
public MetricName name() {
return name;
}

public String description() {
return description;
}

public Measurable stat() {
return stat;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,40 @@ public void metricChange(KafkaMetric metric) {

private KafkaMbean addAttribute(KafkaMetric metric) {
try {
String[] names = split(prefix + metric.name());
String qualifiedName = names[0] + "." + names[1];
if (!this.mbeans.containsKey(qualifiedName)) {
mbeans.put(qualifiedName, new KafkaMbean(names[0], names[1]));
}
KafkaMbean mbean = this.mbeans.get(qualifiedName);
mbean.setAttribute(names[2], metric);
MetricName metricName = metric.metricName();
String mBeanName = getMBeanName(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 MetricsException("Error creating mbean attribute " + metric.name(), e);
throw new MetricsException(
"Error creating mbean attribute for metricName :" + metric.metricName(), e);
}
}

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

public void close() {
synchronized (lock) {
for (KafkaMbean mbean : this.mbeans.values()) {
Expand Down Expand Up @@ -124,32 +145,14 @@ private void reregister(KafkaMbean mbean) {
}
}

private String[] split(String name) {
int attributeStart = name.lastIndexOf('.');
if (attributeStart < 0) {
throw new IllegalArgumentException("No MBean name in metric name: " + name);
}
String attributeName = name.substring(attributeStart + 1, name.length());
String remainder = name.substring(0, attributeStart);
int beanStart = remainder.lastIndexOf('.');
if (beanStart < 0) {
return new String[]{"", remainder, attributeName};
}
String packageName = remainder.substring(0, beanStart);
String beanName = remainder.substring(beanStart + 1, remainder.length());
return new String[]{packageName, beanName, attributeName};
}

private static class KafkaMbean implements DynamicMBean {

private final String beanName;
private final ObjectName objectName;
private final Map<String, KafkaMetric> metrics;

public KafkaMbean(String packageName, String beanName) throws MalformedObjectNameException {
this.beanName = beanName;
public KafkaMbean(String mbeanName) throws MalformedObjectNameException {
this.metrics = new HashMap<String, KafkaMetric>();
this.objectName = new ObjectName(packageName + ":type=" + beanName);
this.objectName = new ObjectName(mbeanName);
}

public ObjectName name() {
Expand Down Expand Up @@ -192,11 +195,11 @@ public MBeanInfo getMBeanInfo() {
String attribute = entry.getKey();
KafkaMetric metric = entry.getValue();
attrs[i] =
new MBeanAttributeInfo(attribute, double.class.getName(), metric.description(), true,
false, false);
new MBeanAttributeInfo(attribute, double.class.getName(), metric.metricName()
.description(), true, false, false);
i += 1;
}
return new MBeanInfo(beanName, "", attrs, null, null, null);
return new MBeanInfo(this.getClass().getName(), "", attrs, null, null, null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,16 @@

public final class KafkaMetric implements Metric {

private final String name;
private final String description;
private MetricName metricName;
private final Object lock;
private final Time time;
private final Measurable measurable;
private MetricConfig config;

KafkaMetric(Object lock, String name, String description, Measurable measurable,
MetricConfig config, Time time) {
KafkaMetric(Object lock, MetricName metricName, Measurable measurable, MetricConfig config,
Time time) {
super();
this.name = name;
this.description = description;
this.metricName = metricName;
this.lock = lock;
this.measurable = measurable;
this.config = config;
Expand All @@ -43,13 +41,8 @@ MetricConfig config() {
}

@Override
public String name() {
return this.name;
}

@Override
public String description() {
return this.description;
public MetricName metricName() {
return this.metricName;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,9 @@
public interface Metric {

/**
* A unique name for this metric
* A name for this metric
*/
public String name();

/**
* A description of what is measured...this will be "" if no description was given
*/
public String description();
public MetricName metricName();

/**
* The value of the metric
Expand Down
191 changes: 191 additions & 0 deletions metrics/src/main/java/io/confluent/common/metrics/MetricName.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
* file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
* to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will need to change the header.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have #1 filed for that since the rest of the files didn't have it changed. I think we're actually required to maintain the existing notice and then just layer ours on top.

package io.confluent.common.metrics;

import java.util.HashMap;
import java.util.Map;

import io.confluent.common.utils.Utils;

/**
* The <code>MetricName</code> class encapsulates a metric's name, logical group and its related attributes
* <p>
* This class captures the following parameters
* <pre>
* <b>name</b> The name of the metric
* <b>group</b> logical group name of the metrics to which this metric belongs.
* <b>description</b> A human-readable description to include in the metric. This is optional.
* <b>tags</b> additional key/value attributes of the metric. This is optional.
* </pre>
* group, tags parameters can be used to create unique metric names while reporting in JMX or any custom reporting.
* <p>
* Ex: standard JMX MBean can be constructed like <b>domainName:type=group,key1=val1,key2=val2</b>
* <p>
* Usage looks something like this:
* <pre>{@code
* // set up metrics:
* Metrics metrics = new Metrics(); // this is the global repository of metrics and sensors
* Sensor sensor = metrics.sensor(&quot;message-sizes&quot;);
* Map<String, String> metricTags = new LinkedHashMap<String, String>();
* metricTags.put("client-id", "producer-1");
* metricTags.put("topic", "topic");
* MetricName metricName = new MetricName(&quot;message-size-avg&quot;, &quot;producer-metrics&quot;, "average message size", metricTags);
* sensor.add(metricName, new Avg());
* metricName = new MetricName(&quot;message-size-max&quot;, &quot;producer-metrics&quot;,metricTags);
* sensor.add(metricName, new Max());
*
* // as messages are sent we record the sizes
* sensor.record(messageSize);
* }</pre>
*/
public final class MetricName {

private final String name;
private final String group;
private final String description;
private Map<String, String> tags;
private int hash = 0;

/**
* @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(String name, String group, String description, Map<String, String> tags) {
this.name = Utils.notNull(name);
this.group = Utils.notNull(group);
this.description = Utils.notNull(description);
this.tags = Utils.notNull(tags);
}

/**
* @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 keyValue additional key/value attributes of the metric (must come in pairs)
*/
public MetricName(String name, String group, String description, String... keyValue) {
this(name, group, description, getTags(keyValue));
}

private static Map<String, String> getTags(String... keyValue) {
if ((keyValue.length % 2) != 0) {
throw new IllegalArgumentException("keyValue needs to be specified in paris");
}
Map<String, String> tags = new HashMap<String, String>();

for (int i = 0; i < (keyValue.length / 2); i++) {
tags.put(keyValue[i], keyValue[i + 1]);
}
return tags;
}

/**
* @param name The name of the metric
* @param group logical group name of the metrics to which this metric belongs
* @param tags key/value attributes of the metric
*/
public MetricName(String name, String group, Map<String, String> tags) {
this(name, group, "", tags);
}

/**
* @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
*/
public MetricName(String name, String group, String description) {
this(name, group, description, new HashMap<String, String>());
}

/**
* @param name The name of the metric
* @param group logical group name of the metrics to which this metric belongs
*/
public MetricName(String name, String group) {
this(name, group, "", new HashMap<String, String>());
}

public String name() {
return this.name;
}

public String group() {
return this.group;
}

public Map<String, String> tags() {
return this.tags;
}

public String description() {
return this.description;
}

@Override
public int hashCode() {
if (hash != 0) {
return hash;
}
final int prime = 31;
int result = 1;
result = prime * result + ((group == null) ? 0 : group.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((tags == null) ? 0 : tags.hashCode());
this.hash = result;
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
MetricName other = (MetricName) obj;
if (group == null) {
if (other.group != null) {
return false;
}
} else if (!group.equals(other.group)) {
return false;
}
if (name == null) {
if (other.name != null) {
return false;
}
} else if (!name.equals(other.name)) {
return false;
}
if (tags == null) {
if (other.tags != null) {
return false;
}
} else if (!tags.equals(other.tags)) {
return false;
}
return true;
}

@Override
public String toString() {
return "MetricName [name=" + name + ", group=" + group + ", description="
+ description + ", tags=" + tags + "]";
}
}
Loading