@Override public String get(Result result) { return result.getAttributeName(); } };
public static String getValueKey(Result result) { if (result.getValuePath().isEmpty()) { return result.getAttributeName(); } return result.getAttributeName() + "." + getValuePathString(result); }
@Override public String formatName(Result result) { StringBuilder formatted = new StringBuilder(); String attName = result.getAttributeName(); String className = result.getKeyAlias(); if (className == null) className = result.getClassName(); formatted.append(className); formatted.append(delimiter); formatted.append(attName); return formatted.toString(); } }
private void doGenerate(List<Result> results) throws Exception { if (isDebugEnabled() && generate) { StringBuilder sb = new StringBuilder("\n"); List<String> keys = new ArrayList<>(); for (Result res : results) { if (isNumeric(res.getValue())) { String key = getDataSourceName(getConcatedTypeNameValues(res.getTypeName()), res.getAttributeName(), res.getValuePath()); if (keys.contains(key)) { throw new Exception("Duplicate datasource name found: '" + key + "'. Please try to add more typeName keys to the writer to make the name more unique. " + res.toString()); } keys.add(key); sb.append("<datasource><!-- ").append(res.getTypeName()).append(":") .append(res.getAttributeName()).append(":").append(res.getValuePath()) .append(" --><name>").append(key) .append("</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>\n"); } } log.debug(sb.toString()); } }
resultIndex++; String bucketType = getBucketType(resultIndex); String attributeName = result.getAttributeName(); log.debug("Skipping message key[{}] with value: {}.", result.getAttributeName(), result.getValue()); continue;
private MetricDatum processResult(Result result) { // Sometimes the attribute name and the key of the value are the same MetricDatum metricDatum = new MetricDatum(); if (result.getValuePath().isEmpty()) { metricDatum.setMetricName(result.getAttributeName()); } else { metricDatum.setMetricName(result.getAttributeName() + "_" + KeyUtils.getValuePathString(result)); } metricDatum.setDimensions(dimensions); // Converts the Objects to Double-values for CloudWatch metricDatum.setValue(toDoubleConverter.apply(result.getValue())); metricDatum.setTimestamp(new Date()); return metricDatum; }
@Override public void internalWrite(Server server, Query query, ImmutableList<Result> results) throws Exception { RrdDef def = getDatabaseTemplateSpec(); List<String> dsNames = getDsNames(def.getDsDefs()); Map<String, String> dataMap = new TreeMap<>(); // go over all the results and look for datasource names that map to // keys from the result values for (Result res : results) { log.debug(res.toString()); String key = getDataSourceName(getConcatedTypeNameValues(res.getTypeName()), res.getAttributeName(), res.getValuePath()); if (isNumeric(res.getValue())) { log.debug("Generated DataSource name:value: {} : {}", key, res.getValue()); if (dsNames.contains(key)) { dataMap.put(key, res.getValue().toString()); } } } doGenerate(results); if (!dataMap.keySet().isEmpty() && !dataMap.values().isEmpty()) { rrdToolUpdate(StringUtils.join(dataMap.keySet(), ':'), StringUtils.join(dataMap.values(), ':')); } else { log.error("Nothing was logged for query: " + query); } }
@Override protected void internalWrite(Server server, Query query, ImmutableList<Result> results) throws Exception { for (Result result : results) { log.debug("Query result: [{}]", result); if (isNumeric(result.getValue())) { Map<String, Object> map = new HashMap<>(); map.put("serverAlias", server.getAlias()); map.put("server", server.getHost()); map.put("port", server.getPort()); map.put("objDomain", result.getObjDomain()); map.put("className", result.getClassName()); map.put("typeName", result.getTypeName()); map.put("attributeName", result.getAttributeName()); map.put("valuePath", Joiner.on('/').join(result.getValuePath())); map.put("keyAlias", result.getKeyAlias()); map.put("value", Double.parseDouble(result.getValue().toString())); map.put("timestamp", result.getEpoch()); log.debug("Insert into Elastic: Index: [{}] Type: [{}] Map: [{}]", indexName, ELASTIC_TYPE_NAME, map); Index index = new Index.Builder(map).index(indexName).type(ELASTIC_TYPE_NAME).build(); JestResult addToIndex = jestClient.execute(index); if (!addToIndex.isSucceeded()) { throw new ElasticWriterException(String.format("Unable to write entry to elastic: %s", addToIndex.getErrorMessage())); } } else { log.warn("Unable to submit non-numeric value to Elastic: [{}] from result [{}]", result.getValue(), result); } } }
private void logValue(Server server, Query query, List<String> typeNames, Result result) throws IOException { Object value = result.getValue(); String resultAsString = resultSerializer.serialize(server, query, result); if (resultAsString == null) { return; } Closer closer = Closer.create(); try { closer.register(MDC.putCloseable("server", computeAlias(server))); closer.register(MDC.putCloseable("metric", KeyUtils.getKeyString(server, query, result, typeNames, null))); closer.register(MDC.putCloseable("value", value.toString())); if (result.getKeyAlias() != null) { closer.register(MDC.putCloseable("resultAlias", result.getKeyAlias())); } closer.register(MDC.putCloseable("attributeName", result.getAttributeName())); closer.register(MDC.putCloseable("key", KeyUtils.getValueKey(result))); closer.register(MDC.putCloseable("epoch", valueOf(result.getEpoch()))); logger.info(resultAsString); } catch (Throwable t) { throw closer.rethrow(t); } finally { closer.close(); } }
@Nullable @Override public Result apply(@Nullable Result input) { if (input == null) { return null; } return new Result( input.getEpoch(), input.getAttributeName(), input.getClassName(), input.getObjDomain(), input.getKeyAlias(), input.getTypeName(), input.getValuePath(), valueTransformer.apply(input.getValue()) ); }
/** * Populate the context with values from the result. * * @param context - the expression context used when evaluating JEXL expressions. * @param result - the result of a JMX query. */ protected void populateContext(JexlContext context, Result result) { context.set(VAR_CLASSNAME, result.getClassName()); context.set(VAR_ATTRIBUTE_NAME, result.getAttributeName()); context.set(VAR_CLASSNAME_ALIAS, result.getKeyAlias()); Map<String, String> typeNameMap = TypeNameValue.extractMap(result.getTypeName()); context.set(VAR_TYPENAME, typeNameMap); String effectiveClassname = result.getKeyAlias(); if (effectiveClassname == null) { effectiveClassname = result.getClassName(); } context.set(VAR_EFFECTIVE_CLASSNAME, effectiveClassname); context.set(VAR_RESULT, result); } }
private KResult(Server server, Result result) { alias = server.getAlias(); pid = server.getPid(); host = server.getHost(); port = server.getPort(); source = server.getSource(); attributeName = result.getAttributeName(); className = result.getClassName(); objDomain = result.getObjDomain(); typeName = result.getTypeName(); typeNameMap = result.getTypeNameMap(); epoch = result.getEpoch(); keyAlias = result.getKeyAlias(); this.valuePath = result.getValuePath(); this.value = result.getValue(); } }
@Override public String get(Result result) { return result.getAttributeName(); } };
public static String getValueKey(Result result) { if (result.getValuePath().isEmpty()) { return result.getAttributeName(); } return result.getAttributeName() + "." + getValuePathString(result); }
@Override public String formatName(Result result) { StringBuilder formatted = new StringBuilder(); String attName = result.getAttributeName(); String className = result.getKeyAlias(); if (className == null) className = result.getClassName(); formatted.append(className); formatted.append(delimiter); formatted.append(attName); return formatted.toString(); } }
private MetricDatum processResult(Result result) { // Sometimes the attribute name and the key of the value are the same MetricDatum metricDatum = new MetricDatum(); if (result.getValuePath().isEmpty()) { metricDatum.setMetricName(result.getAttributeName()); } else { metricDatum.setMetricName(result.getAttributeName() + "_" + KeyUtils.getValuePathString(result)); } metricDatum.setDimensions(dimensions); // Converts the Objects to Double-values for CloudWatch metricDatum.setValue(toDoubleConverter.apply(result.getValue())); metricDatum.setTimestamp(new Date()); return metricDatum; }
@Override protected void internalWrite(Server server, Query query, ImmutableList<Result> results) throws Exception { for (Result result : results) { log.debug("Query result: [{}]", result); if (isNumeric(result.getValue())) { Map<String, Object> map = new HashMap<>(); map.put("serverAlias", server.getAlias()); map.put("server", server.getHost()); map.put("port", server.getPort()); map.put("objDomain", result.getObjDomain()); map.put("className", result.getClassName()); map.put("typeName", result.getTypeName()); map.put("attributeName", result.getAttributeName()); map.put("valuePath", Joiner.on('/').join(result.getValuePath())); map.put("keyAlias", result.getKeyAlias()); map.put("value", Double.parseDouble(result.getValue().toString())); map.put("timestamp", result.getEpoch()); log.debug("Insert into Elastic: Index: [{}] Type: [{}] Map: [{}]", indexName, ELASTIC_TYPE_NAME, map); Index index = new Index.Builder(map).index(indexName).type(ELASTIC_TYPE_NAME).build(); JestResult addToIndex = jestClient.execute(index); if (!addToIndex.isSucceeded()) { throw new ElasticWriterException(String.format("Unable to write entry to elastic: %s", addToIndex.getErrorMessage())); } } else { log.warn("Unable to submit non-numeric value to Elastic: [{}] from result [{}]", result.getValue(), result); } } }
@Nullable @Override public Result apply(@Nullable Result input) { if (input == null) { return null; } return new Result( input.getEpoch(), input.getAttributeName(), input.getClassName(), input.getObjDomain(), input.getKeyAlias(), input.getTypeName(), input.getValuePath(), valueTransformer.apply(input.getValue()) ); }
/** * Populate the context with values from the result. * * @param context - the expression context used when evaluating JEXL expressions. * @param result - the result of a JMX query. */ protected void populateContext(JexlContext context, Result result) { context.set(VAR_CLASSNAME, result.getClassName()); context.set(VAR_ATTRIBUTE_NAME, result.getAttributeName()); context.set(VAR_CLASSNAME_ALIAS, result.getKeyAlias()); Map<String, String> typeNameMap = TypeNameValue.extractMap(result.getTypeName()); context.set(VAR_TYPENAME, typeNameMap); String effectiveClassname = result.getKeyAlias(); if (effectiveClassname == null) { effectiveClassname = result.getClassName(); } context.set(VAR_EFFECTIVE_CLASSNAME, effectiveClassname); context.set(VAR_RESULT, result); } }
private KResult(Server server, Result result) { alias = server.getAlias(); pid = server.getPid(); host = server.getHost(); port = server.getPort(); source = server.getSource(); attributeName = result.getAttributeName(); className = result.getClassName(); objDomain = result.getObjDomain(); typeName = result.getTypeName(); typeNameMap = result.getTypeNameMap(); epoch = result.getEpoch(); keyAlias = result.getKeyAlias(); this.valuePath = result.getValuePath(); this.value = result.getValue(); } }