|
4 | 4 | import java.util.ArrayList;
|
5 | 5 | import java.util.Arrays;
|
6 | 6 | import java.util.Collection;
|
| 7 | +import java.util.concurrent.atomic.AtomicInteger; |
| 8 | +import java.util.concurrent.atomic.AtomicLong; |
7 | 9 | import java.util.HashMap;
|
8 | 10 | import java.util.LinkedHashMap;
|
9 | 11 | import java.util.LinkedList;
|
10 | 12 | import java.util.List;
|
11 | 13 | import java.util.Map;
|
12 |
| -import java.util.concurrent.atomic.AtomicInteger; |
13 |
| -import java.util.concurrent.atomic.AtomicLong; |
| 14 | +import java.util.regex.Matcher; |
14 | 15 | import java.util.regex.Pattern;
|
15 | 16 |
|
16 | 17 | import javax.management.AttributeNotFoundException;
|
|
24 | 25 |
|
25 | 26 | public abstract class JMXAttribute {
|
26 | 27 |
|
| 28 | + protected static final String ALIAS = "alias"; |
| 29 | + protected static final String METRIC_TYPE = "metric_type"; |
27 | 30 | private final static Logger LOGGER = Logger.getLogger(JMXAttribute.class.getName());
|
28 | 31 | private static final List<String> EXCLUDED_BEAN_PARAMS = Arrays.asList("domain", "domain_regex", "bean_name", "bean", "bean_regex", "attribute");
|
29 | 32 | private static final String FIRST_CAP_PATTERN = "(.)([A-Z][a-z]+)";
|
30 | 33 | private static final String ALL_CAP_PATTERN = "([a-z0-9])([A-Z])";
|
31 | 34 | private static final String METRIC_REPLACEMENT = "([^a-zA-Z0-9_.]+)|(^[^a-zA-Z]+)";
|
32 | 35 | private static final String DOT_UNDERSCORE = "_*\\._*";
|
33 | 36 | protected static final String CASSANDRA_DOMAIN = "org.apache.cassandra.metrics";
|
| 37 | + |
34 | 38 | private MBeanAttributeInfo attribute;
|
35 | 39 | private Connection connection;
|
36 | 40 | private ObjectName beanName;
|
@@ -355,6 +359,85 @@ public ObjectName getBeanName() {
|
355 | 359 | return beanName;
|
356 | 360 | }
|
357 | 361 |
|
| 362 | + /** |
| 363 | + * Get attribute alias. |
| 364 | + * |
| 365 | + * In order, tries to: |
| 366 | + * * Use `alias_match` to generate an alias with a regular expression |
| 367 | + * * Use `alias` directly |
| 368 | + * * Create an generic alias prefixed with user's `metric_prefix` preference or default to `jmx` |
| 369 | + * |
| 370 | + * Argument(s): |
| 371 | + * * (Optional) `field` |
| 372 | + * `Null` for `JMXSimpleAttribute`. |
| 373 | + */ |
| 374 | + protected String getAlias(String field) { |
| 375 | + String alias = null; |
| 376 | + |
| 377 | + Filter include = getMatchingConf().getInclude(); |
| 378 | + LinkedHashMap<String, Object> conf = getMatchingConf().getConf(); |
| 379 | + |
| 380 | + String fullAttributeName =(field!=null)?(getAttribute().getName() + "." + field):(getAttribute().getName()); |
| 381 | + |
| 382 | + if (include.getAttribute() instanceof LinkedHashMap<?, ?>) { |
| 383 | + LinkedHashMap<String, LinkedHashMap<String, String>> attribute = (LinkedHashMap<String, LinkedHashMap<String, String>>) (include.getAttribute()); |
| 384 | + if (attribute.get(fullAttributeName).get("alias_match") != null) { |
| 385 | + Pattern p = Pattern.compile(attribute.get(fullAttributeName).get("alias_match")); |
| 386 | + Matcher m = p.matcher(getBeanName().getCanonicalName() + "." + fullAttributeName); |
| 387 | + if (m.find()) { |
| 388 | + alias = m.replaceFirst(attribute.get(fullAttributeName).get(ALIAS)); |
| 389 | + } |
| 390 | + } else { |
| 391 | + alias = attribute.get(fullAttributeName).get(ALIAS); |
| 392 | + } |
| 393 | + } else if (conf.get("metric_prefix") != null) { |
| 394 | + alias = conf.get("metric_prefix") + "." + getDomain() + "." + fullAttributeName; |
| 395 | + } else if (getDomain().startsWith("org.apache.cassandra")) { |
| 396 | + alias = getCassandraAlias(); |
| 397 | + } |
| 398 | + |
| 399 | + //If still null - generate generic alias, |
| 400 | + if (alias == null) { |
| 401 | + alias = "jmx." + getDomain() + "." + fullAttributeName; |
| 402 | + } |
| 403 | + alias = convertMetricName(alias); |
| 404 | + return alias; |
| 405 | + } |
| 406 | + |
| 407 | + /** |
| 408 | + * Metric name aliasing specific to Cassandra. |
| 409 | + * |
| 410 | + * * (Default) `cassandra_aliasing` == False. |
| 411 | + * Legacy aliasing: drop `org.apache` prefix. |
| 412 | + * * `cassandra_aliasing` == True |
| 413 | + * Comply with CASSANDRA-4009 |
| 414 | + * |
| 415 | + * More information: https://issues.apache.org/jira/browse/CASSANDRA-4009 |
| 416 | + */ |
| 417 | + private String getCassandraAlias() { |
| 418 | + if (renameCassandraMetrics()) { |
| 419 | + Map<String, String> beanParameters = getBeanParameters(); |
| 420 | + String metricName = beanParameters.get("name"); |
| 421 | + String attributeName = getAttributeName(); |
| 422 | + if (attributeName.equals("Value")) { |
| 423 | + return "cassandra." + metricName; |
| 424 | + } |
| 425 | + return "cassandra." + metricName + "." + attributeName; |
| 426 | + } |
| 427 | + //Deprecated Cassandra metric. Remove domain prefix. |
| 428 | + return getDomain().replace("org.apache.", "") + "." + getAttributeName(); |
| 429 | + } |
| 430 | + |
| 431 | + /** |
| 432 | + * Overload `getAlias` method. |
| 433 | + * |
| 434 | + * Note: used for `JMXSimpleAttribute` only, as `field` is null. |
| 435 | + */ |
| 436 | + protected String getAlias(){ |
| 437 | + return getAlias(null); |
| 438 | + } |
| 439 | + |
| 440 | + |
358 | 441 | @SuppressWarnings("unchecked")
|
359 | 442 | protected String[] getTags() {
|
360 | 443 | if(tags != null) {
|
|
0 commit comments