From d0b7a77124f895d966e9cfae2522b30c287ed37f Mon Sep 17 00:00:00 2001 From: Max Kuklinski Date: Mon, 16 Jan 2017 15:49:43 +0100 Subject: [PATCH] Support array (multi valued) gauges. --- .../metrics/MetricsElasticsearchModule.java | 14 +++- .../MetricsElasticsearchModuleTest.java | 66 +++++++++++++++++++ 2 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 src/test/java/org/elasticsearch/metrics/MetricsElasticsearchModuleTest.java diff --git a/src/main/java/org/elasticsearch/metrics/MetricsElasticsearchModule.java b/src/main/java/org/elasticsearch/metrics/MetricsElasticsearchModule.java index 8055393..1529926 100644 --- a/src/main/java/org/elasticsearch/metrics/MetricsElasticsearchModule.java +++ b/src/main/java/org/elasticsearch/metrics/MetricsElasticsearchModule.java @@ -32,6 +32,7 @@ import java.io.IOException; import java.util.Arrays; +import java.util.Collection; import java.util.Locale; import java.util.Map; import java.util.concurrent.TimeUnit; @@ -53,7 +54,7 @@ private static void writeAdditionalFields(final Map additionalFi } } } - + private static class GaugeSerializer extends StdSerializer { private final String timestampFieldname; private final Map additionalFields; @@ -74,7 +75,16 @@ public void serialize(JsonGauge gauge, final Object value; try { value = gauge.value().getValue(); - json.writeObjectField("value", value); + if (value instanceof Iterable) { // Check if writing a single value or an array + Iterable iterable = (Iterable) value; + json.writeArrayFieldStart("value"); + for (Object valueItem : iterable) { + json.writeObject(valueItem); + } + json.writeEndArray(); + } else { + json.writeObjectField("value", value); + } } catch (RuntimeException e) { json.writeObjectField("error", e.toString()); } diff --git a/src/test/java/org/elasticsearch/metrics/MetricsElasticsearchModuleTest.java b/src/test/java/org/elasticsearch/metrics/MetricsElasticsearchModuleTest.java new file mode 100644 index 0000000..ddadb38 --- /dev/null +++ b/src/test/java/org/elasticsearch/metrics/MetricsElasticsearchModuleTest.java @@ -0,0 +1,66 @@ +package org.elasticsearch.metrics; + +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.TimeUnit; + +import org.elasticsearch.metrics.JsonMetrics.JsonGauge; +import org.elasticsearch.metrics.JsonMetrics.JsonMetric; +import org.junit.Test; + +import com.codahale.metrics.Gauge; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + +import static org.junit.Assert.*; + +/** + * Tests if value is an array. + * @author static-max + * + */ +public class ElasticsearchReporterTestTest { + + @Test + public void test() throws JsonProcessingException { + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.registerModule(new MetricsElasticsearchModule(TimeUnit.MINUTES, TimeUnit.MINUTES, "@timestamp", null)); + + Gauge gaugeString = new Gauge() { + @Override + public String getValue() { + return "STRING VALUE"; + } + }; + + /** + * Used for deadlocks in metrics-jvm ThreadStatesGaugeSet.class. + */ + Gauge> gaugeStringSet = new Gauge>() { + @Override + public Set getValue() { + HashSet testSet = new HashSet<>(); + testSet.add("1"); + testSet.add("2"); + testSet.add("3"); + return testSet; + } + }; + + + JsonMetric jsonMetricString = new JsonGauge("string", Long.MAX_VALUE, gaugeString); + JsonMetric jsonMetricStringSet = new JsonGauge("string", Long.MAX_VALUE, gaugeStringSet); + + JsonNode stringNode = objectMapper.valueToTree(jsonMetricString); + assertTrue(stringNode.get("value").isTextual()); + assertFalse(stringNode.get("value").isNumber()); + + JsonNode stringSetNode = objectMapper.valueToTree(jsonMetricStringSet); + assertTrue(stringSetNode.get("value").isArray()); + assertTrue(stringSetNode.get("value").get(0).isTextual()); + assertTrue(stringSetNode.get("value").get(1).isTextual()); + assertTrue(stringSetNode.get("value").get(2).isTextual()); + } + +} \ No newline at end of file