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

Support array (multi valued) gauges. #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -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;
Expand All @@ -53,7 +54,7 @@ private static void writeAdditionalFields(final Map<String, Object> additionalFi
}
}
}

private static class GaugeSerializer extends StdSerializer<JsonGauge> {
private final String timestampFieldname;
private final Map<String, Object> additionalFields;
Expand All @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> gaugeString = new Gauge<String>() {
@Override
public String getValue() {
return "STRING VALUE";
}
};

/**
* Used for deadlocks in metrics-jvm ThreadStatesGaugeSet.class.
*/
Gauge<Set<String>> gaugeStringSet = new Gauge<Set<String>>() {
@Override
public Set<String> getValue() {
HashSet<String> 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());
}

}