Skip to content

Option to name the field for "value" depending on the type. #49

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

Open
wants to merge 3 commits 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ incomingRequestsMeter.mark(1);
* `index()`: The name of the index to write to, defaults to `metrics`
* `indexDateFormat()`: The date format to make sure to rotate to a new index, defaults to `yyyy-MM`
* `timestampFieldname()`: The field name of the timestamp, defaults to `@timestamp`, which makes it easy to use with kibana
* `dynamicValueFieldname()`: Whether the fieldname for `value` should depend on the value type or not. Defaults to `false`, so `value` is used as fieldname. When set to `true`, the fieldname will be one of `['value_string', 'value_long', 'value_double', 'value_boolean', 'value']`. Fallback is `value` when the type is not one of `String`, `Long|Integer`, `Double|Float`, or `Boolean`. Only used for meters of type gauge.

### Mapping

Expand Down
30 changes: 26 additions & 4 deletions src/main/java/org/elasticsearch/metrics/ElasticsearchReporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public static class Builder {
private int timeout = 1000;
private String timestampFieldname = "@timestamp";
private Map<String, Object> additionalFields;
private boolean dynamicValueFieldname = false;

private Builder(MetricRegistry registry) {
this.registry = registry;
Expand Down Expand Up @@ -196,7 +197,18 @@ public Builder additionalFields(Map<String, Object> additionalFields) {
return this;
}

public ElasticsearchReporter build() throws IOException {
/**
* Whether the fieldname for "value" should depend on the value type or not. Defaults to <code>false</code>.
* When set to <code>true</code>, the fieldname for value will be "value_string", "value_long" etc.
* @param dynamicValueFieldname
* @return
*/
public Builder dynamicValueFieldname(boolean dynamicValueFieldname) {
this.dynamicValueFieldname = dynamicValueFieldname;
return this;
}

public ElasticsearchReporter build() throws IOException {
return new ElasticsearchReporter(registry,
hosts,
timeout,
Expand All @@ -211,7 +223,8 @@ public ElasticsearchReporter build() throws IOException {
percolationFilter,
percolationNotifier,
timestampFieldname,
additionalFields);
additionalFields,
dynamicValueFieldname);
}
}

Expand All @@ -233,7 +246,7 @@ public ElasticsearchReporter build() throws IOException {

public ElasticsearchReporter(MetricRegistry registry, String[] hosts, int timeout,
String index, String indexDateFormat, int bulkSize, Clock clock, String prefix, TimeUnit rateUnit, TimeUnit durationUnit,
MetricFilter filter, MetricFilter percolationFilter, Notifier percolationNotifier, String timestampFieldname, Map<String, Object> additionalFields) throws MalformedURLException {
MetricFilter filter, MetricFilter percolationFilter, Notifier percolationNotifier, String timestampFieldname, Map<String, Object> additionalFields, boolean dynamicValueFieldname) throws MalformedURLException {
super(registry, "elasticsearch-reporter", filter, rateUnit, durationUnit);
this.hosts = hosts;
this.index = index;
Expand All @@ -259,7 +272,7 @@ public ElasticsearchReporter(MetricRegistry registry, String[] hosts, int timeou
objectMapper.configure(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT, false);
objectMapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
objectMapper.registerModule(new AfterburnerModule());
objectMapper.registerModule(new MetricsElasticsearchModule(rateUnit, durationUnit, timestampFieldname, additionalFields));
objectMapper.registerModule(new MetricsElasticsearchModule(rateUnit, durationUnit, timestampFieldname, additionalFields, dynamicValueFieldname));
writer = objectMapper.writer();
checkForIndexTemplate();
}
Expand Down Expand Up @@ -495,6 +508,15 @@ private void checkForIndexTemplate() {
json.writeEndObject();
json.writeEndObject();
json.writeEndObject();

json.writeObjectFieldStart("gauge");
json.writeObjectFieldStart("properties");
json.writeObjectFieldStart("value_string");
json.writeObjectField("type", "string");
json.writeObjectField("index", "not_analyzed");
json.writeEndObject();
json.writeEndObject();
json.writeEndObject();

json.writeEndObject();
json.writeEndObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,51 @@ public class MetricsElasticsearchModule extends Module {

public static final Version VERSION = new Version(3, 0, 0, "", "metrics-elasticsearch-reporter", "metrics-elasticsearch-reporter");

public final static String VALUE_FIELDNAME_DEFAULT = "value";
public final static String VALUE_FIELDNAME_STRING = "value_string";
public final static String VALUE_FIELDNAME_LONG = "value_long";
public final static String VALUE_FIELDNAME_DOUBLE = "value_double";
public final static String VALUE_FIELDNAME_BOOLEAN = "value_boolean";

private static void writeAdditionalFields(final Map<String, Object> additionalFields, final JsonGenerator json) throws IOException {
if (additionalFields != null) {
for (final Map.Entry<String, Object> field : additionalFields.entrySet()) {
json.writeObjectField(field.getKey(), field.getValue());
}
}
}

/**
* Get the fieldname for the "value" field depending on the type of value.
* @param value
* @return
*/
public static String getValueFieldname(Object value) {
String fieldName = VALUE_FIELDNAME_DEFAULT;
if (value instanceof String) {
fieldName = VALUE_FIELDNAME_STRING;
} else if (value instanceof Long || value instanceof Integer) {
fieldName = VALUE_FIELDNAME_LONG;
} else if (value instanceof Double || value instanceof Float) {
fieldName = VALUE_FIELDNAME_DOUBLE;
} else if (value instanceof Boolean) {
fieldName = VALUE_FIELDNAME_BOOLEAN;
} else {
fieldName = VALUE_FIELDNAME_DEFAULT;
}
return fieldName;
}

private static class GaugeSerializer extends StdSerializer<JsonGauge> {
private final String timestampFieldname;
private final Map<String, Object> additionalFields;
private final boolean dynamicValueFieldname;

private GaugeSerializer(String timestampFieldname, Map<String, Object> additionalFields) {
private GaugeSerializer(String timestampFieldname, Map<String, Object> additionalFields, boolean dynamicValueFieldname) {
super(JsonGauge.class);
this.timestampFieldname = timestampFieldname;
this.additionalFields = additionalFields;
this.dynamicValueFieldname = dynamicValueFieldname;
}

@Override
Expand All @@ -74,7 +103,8 @@ public void serialize(JsonGauge gauge,
final Object value;
try {
value = gauge.value().getValue();
json.writeObjectField("value", value);
String valueFieldname = dynamicValueFieldname ? getValueFieldname(value) : VALUE_FIELDNAME_DEFAULT;
json.writeObjectField(valueFieldname, value);
} catch (RuntimeException e) {
json.writeObjectField("error", e.toString());
}
Expand Down Expand Up @@ -278,12 +308,14 @@ public BulkIndexOperationHeader(String index, String type) {
private final TimeUnit durationUnit;
private final String timestampFieldname;
private final Map<String, Object> additionalFields;
private final boolean dynamicValueFieldname;

public MetricsElasticsearchModule(TimeUnit rateUnit, TimeUnit durationUnit, String timestampFieldname, Map<String, Object> additionalFields) {
public MetricsElasticsearchModule(TimeUnit rateUnit, TimeUnit durationUnit, String timestampFieldname, Map<String, Object> additionalFields, boolean dynamicValueFieldname) {
this.rateUnit = rateUnit;
this.durationUnit = durationUnit;
this.timestampFieldname = timestampFieldname;
this.additionalFields = additionalFields;
this.dynamicValueFieldname = dynamicValueFieldname;
}

@Override
Expand All @@ -299,7 +331,7 @@ public Version version() {
@Override
public void setupModule(SetupContext context) {
context.addSerializers(new SimpleSerializers(Arrays.<JsonSerializer<?>>asList(
new GaugeSerializer(timestampFieldname, additionalFields),
new GaugeSerializer(timestampFieldname, additionalFields, dynamicValueFieldname),
new CounterSerializer(timestampFieldname, additionalFields),
new HistogramSerializer(timestampFieldname, additionalFields),
new MeterSerializer(rateUnit, timestampFieldname, additionalFields),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public void testThatTemplateIsAdded() throws Exception {
IndexTemplateMetaData templateData = response.getIndexTemplates().get(0);
assertThat(templateData.order(), is(0));
assertThat(templateData.getMappings().get("_default_"), is(notNullValue()));
assertThat(templateData.getMappings().get("gauge"), is(notNullValue()));
}

@Test
Expand Down
124 changes: 124 additions & 0 deletions src/test/java/org/elasticsearch/metrics/ValueFieldnameTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
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 the fieldname for value gets correctly named when dynamicValueFieldname() is set to <code>true</code>.
* @author static-max
*
*/
public class ValueFieldnameTest {

@Test
public void test() throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new MetricsElasticsearchModule(TimeUnit.MINUTES, TimeUnit.MINUTES, "@timestamp", null, true));

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;
}
};

Gauge<Long> gaugeLong = new Gauge<Long>() {

@Override
public Long getValue() {
return Long.MAX_VALUE;
}
};

Gauge<Integer> gaugeInteger = new Gauge<Integer>() {

@Override
public Integer getValue() {
return 123;
}
};

Gauge<Double> gaugeDouble = new Gauge<Double>() {

@Override
public Double getValue() {
return 1.23d;
}
};

Gauge<Float> gaugeFloat = new Gauge<Float>() {

@Override
public Float getValue() {
return 321.0f;
}
};

Gauge<Boolean> gaugeBoolean = new Gauge<Boolean>() {

@Override
public Boolean getValue() {
return true;
}
};

JsonMetric<?> jsonMetricString = new JsonGauge("string", Long.MAX_VALUE, gaugeString);
JsonMetric<?> jsonMetricLong = new JsonGauge("long", Long.MAX_VALUE, gaugeLong);
JsonMetric<?> jsonMetricInteger = new JsonGauge("integer", Long.MAX_VALUE, gaugeInteger);
JsonMetric<?> jsonMetricDouble = new JsonGauge("double", Long.MAX_VALUE, gaugeDouble);
JsonMetric<?> jsonMetricFloat = new JsonGauge("float", Long.MAX_VALUE, gaugeFloat);
JsonMetric<?> jsonMetricBoolean = new JsonGauge("boolean", Long.MAX_VALUE, gaugeBoolean);

JsonNode stringNode = objectMapper.valueToTree(jsonMetricString);
assertTrue(stringNode.get("value_string").isTextual());
assertFalse(stringNode.get("value_string").isNumber());

JsonNode longNode = objectMapper.valueToTree(jsonMetricLong);
assertTrue(longNode.get("value_long").isNumber());
assertFalse(longNode.get("value_long").isTextual());

JsonNode integerNode = objectMapper.valueToTree(jsonMetricInteger);
assertTrue(integerNode.get("value_long").isNumber());
assertFalse(integerNode.get("value_long").isTextual());

JsonNode doubleNode = objectMapper.valueToTree(jsonMetricDouble);
assertTrue(doubleNode.get("value_double").isNumber());
assertTrue(doubleNode.get("value_double").isDouble());

JsonNode floatNode = objectMapper.valueToTree(jsonMetricFloat);
assertTrue(floatNode.get("value_double").isNumber());
assertTrue(floatNode.get("value_double").isDouble());

JsonNode booleanNode = objectMapper.valueToTree(jsonMetricBoolean);
assertTrue(booleanNode.get("value_boolean").isBoolean());
assertFalse(booleanNode.get("value_boolean").isTextual());
}

}