use of com.wavefront.common.TaggedMetricName in project java by wavefrontHQ.
the class DataDogPortUnificationHandler method processMetadataAndSystemMetrics.
private HttpResponseStatus processMetadataAndSystemMetrics(final JsonNode metrics, boolean reportSystemMetrics, @Nullable final AtomicInteger pointCounter, Consumer<String> outputConsumer) {
if (metrics == null || !metrics.isObject()) {
error("Empty or malformed /intake payload", outputConsumer);
return HttpResponseStatus.BAD_REQUEST;
}
if (!metrics.has("internalHostname")) {
error("Payload missing 'internalHostname' field, ignoring", outputConsumer);
return HttpResponseStatus.ACCEPTED;
}
// Some /api/v1/intake requests only contain host-tag metadata so process it first
String hostName = metrics.get("internalHostname").textValue().toLowerCase();
HashMap<String, String> systemTags = new HashMap<>();
if (metrics.has("host-tags") && metrics.get("host-tags").get("system") != null) {
extractTags(metrics.get("host-tags").get("system"), systemTags);
// cache even if map is empty so we know how many unique hosts report metrics.
tagsCache.put(hostName, systemTags);
if (logger.isLoggable(Level.FINE)) {
logger.fine("Cached system tags for " + hostName + ": " + systemTags.toString());
}
} else {
Map<String, String> cachedTags = tagsCache.getIfPresent(hostName);
if (cachedTags != null) {
systemTags.clear();
systemTags.putAll(cachedTags);
}
}
if (!reportSystemMetrics) {
Metrics.newCounter(new TaggedMetricName("listeners", "http-requests.ignored", "port", handle)).inc();
return HttpResponseStatus.ACCEPTED;
}
if (metrics.has("collection_timestamp")) {
long timestamp = metrics.get("collection_timestamp").asLong() * 1000;
// Report "system.io." metrics
JsonNode ioStats = metrics.get("ioStats");
if (ioStats != null && ioStats.isObject()) {
ioStats.fields().forEachRemaining(entry -> {
Map<String, String> deviceTags = ImmutableMap.<String, String>builder().putAll(systemTags).put("device", entry.getKey()).build();
if (entry.getValue() != null && entry.getValue().isObject()) {
entry.getValue().fields().forEachRemaining(metricEntry -> {
String metric = "system.io." + metricEntry.getKey().replace('%', ' ').replace('/', '_').trim();
reportValue(metric, hostName, deviceTags, metricEntry.getValue(), timestamp, pointCounter);
});
}
});
}
// Report all metrics that already start with "system."
metrics.fields().forEachRemaining(entry -> {
if (entry.getKey().startsWith("system.")) {
reportValue(entry.getKey(), hostName, systemTags, entry.getValue(), timestamp, pointCounter);
}
});
// Report CPU and memory metrics
SYSTEM_METRICS.forEach((key, value) -> reportValue(key, hostName, systemTags, metrics.get(value), timestamp, pointCounter));
}
return HttpResponseStatus.ACCEPTED;
}
use of com.wavefront.common.TaggedMetricName in project java by wavefrontHQ.
the class JsonMetricsGenerator method writeRegularMetrics.
public static void writeRegularMetrics(Processor processor, JsonGenerator json, MetricsRegistry registry, boolean showFullSamples, @Nullable Map<String, String> pointTags, @Nullable MetricTranslator metricTranslator) throws IOException {
for (Map.Entry<String, SortedMap<MetricName, Metric>> entry : registry.groupedMetrics().entrySet()) {
for (Map.Entry<MetricName, Metric> subEntry : entry.getValue().entrySet()) {
MetricName key = subEntry.getKey();
Metric value = subEntry.getValue();
if (metricTranslator != null) {
Pair<MetricName, Metric> pair = metricTranslator.apply(Pair.of(key, value));
if (pair == null)
continue;
key = pair._1;
value = pair._2;
}
boolean closeObjectRequired = false;
if (key instanceof TaggedMetricName || pointTags != null) {
closeObjectRequired = true;
// write the hashcode since we need to support metrics with the same name but with different tags.
// the server will remove the suffix.
json.writeFieldName(sanitize(key) + "$" + subEntry.hashCode());
// write out the tags separately
// instead of metricName: {...}
// we write
// metricName_hashCode: {
// tags: {
// tagK: tagV,...
// },
// value: {...}
// }
//
json.writeStartObject();
json.writeFieldName("tags");
json.writeStartObject();
Map<String, String> tags = new HashMap<>();
if (pointTags != null) {
tags.putAll(pointTags);
}
if (key instanceof TaggedMetricName) {
tags.putAll(((TaggedMetricName) key).getTags());
}
for (Map.Entry<String, String> tagEntry : tags.entrySet()) {
json.writeStringField(tagEntry.getKey(), tagEntry.getValue());
}
json.writeEndObject();
json.writeFieldName("value");
} else {
json.writeFieldName(sanitize(key));
}
try {
value.processWith(processor, key, new Context(json, showFullSamples));
} catch (Exception e) {
e.printStackTrace();
}
// need to close the object as well.
if (closeObjectRequired) {
json.writeEndObject();
}
}
}
}
use of com.wavefront.common.TaggedMetricName in project java by wavefrontHQ.
the class AbstractAgent method initPreprocessors.
private void initPreprocessors() throws IOException {
// blacklistRegex and whitelistRegex are applied to pushListenerPorts, graphitePorts and picklePorts
if (whitelistRegex != null || blacklistRegex != null) {
String allPorts = StringUtils.join(new String[] { pushListenerPorts == null ? "" : pushListenerPorts, graphitePorts == null ? "" : graphitePorts, picklePorts == null ? "" : picklePorts }, ",");
Iterable<String> ports = Splitter.on(",").omitEmptyStrings().trimResults().split(allPorts);
for (String strPort : ports) {
if (blacklistRegex != null) {
preprocessors.forPort(strPort).forPointLine().addFilter(new PointLineBlacklistRegexFilter(blacklistRegex, Metrics.newCounter(new TaggedMetricName("validationRegex", "points-rejected", "port", strPort))));
}
if (whitelistRegex != null) {
preprocessors.forPort(strPort).forPointLine().addFilter(new PointLineWhitelistRegexFilter(whitelistRegex, Metrics.newCounter(new TaggedMetricName("validationRegex", "points-rejected", "port", strPort))));
}
}
}
// opentsdbBlacklistRegex and opentsdbWhitelistRegex are applied to opentsdbPorts only
if (opentsdbPorts != null && (opentsdbWhitelistRegex != null || opentsdbBlacklistRegex != null)) {
Iterable<String> ports = Splitter.on(",").omitEmptyStrings().trimResults().split(opentsdbPorts);
for (String strPort : ports) {
if (opentsdbBlacklistRegex != null) {
preprocessors.forPort(strPort).forPointLine().addFilter(new PointLineBlacklistRegexFilter(opentsdbBlacklistRegex, Metrics.newCounter(new TaggedMetricName("validationRegex", "points-rejected", "port", strPort))));
}
if (opentsdbWhitelistRegex != null) {
preprocessors.forPort(strPort).forPointLine().addFilter(new PointLineWhitelistRegexFilter(opentsdbWhitelistRegex, Metrics.newCounter(new TaggedMetricName("validationRegex", "points-rejected", "port", strPort))));
}
}
}
if (preprocessorConfigFile != null) {
FileInputStream stream = new FileInputStream(preprocessorConfigFile);
preprocessors.loadFromStream(stream);
logger.info("Preprocessor configuration loaded from " + preprocessorConfigFile);
}
}
use of com.wavefront.common.TaggedMetricName in project java by wavefrontHQ.
the class SocketMetricsProcessor method tagsForMetricName.
/**
* @return " k1=v1 k2=v2 ..." if metricName is an instance of TaggedMetricName. "" otherwise.
*/
private String tagsForMetricName(MetricName metricName) {
if (metricName instanceof TaggedMetricName) {
TaggedMetricName taggedMetricName = (TaggedMetricName) metricName;
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String> entry : taggedMetricName.getTags().entrySet()) {
sb.append(" ").append(entry.getKey()).append("=\"").append(entry.getValue()).append("\"");
}
return sb.toString();
} else {
return "";
}
}
use of com.wavefront.common.TaggedMetricName in project java by wavefrontHQ.
the class WavefrontYammerMetricsReporterTest method testWavefrontHistogram.
@Test(timeout = 1000)
public void testWavefrontHistogram() throws Exception {
AtomicLong clock = new AtomicLong(System.currentTimeMillis());
WavefrontHistogram wavefrontHistogram = WavefrontHistogram.get(metricsRegistry, new TaggedMetricName("group", "myhisto", "tag1", "value1", "tag2", "value2"), clock::get);
for (int i = 0; i < 101; i++) {
wavefrontHistogram.update(i);
}
// Advance the clock by 1 min ...
clock.addAndGet(60000L + 1);
wavefrontYammerMetricsReporter.run();
assertThat(receiveFromSocket(1, fromHistograms), contains(equalTo("!M 1485224035 #101 50.0 \"myhisto\" tag1=\"value1\" tag2=\"value2\"")));
}
Aggregations