
com.googlecode.jmxtrans.model.output.InfluxDbWriter Maven / Gradle / Ivy
Show all versions of jmxtrans-output-influxdb Show documentation
/**
* The MIT License
* Copyright © 2010 JmxTrans team
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.googlecode.jmxtrans.model.output;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.googlecode.jmxtrans.model.OutputWriterAdapter;
import com.googlecode.jmxtrans.model.Query;
import com.googlecode.jmxtrans.model.Result;
import com.googlecode.jmxtrans.model.ResultAttribute;
import com.googlecode.jmxtrans.model.Server;
import com.googlecode.jmxtrans.model.naming.KeyUtils;
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDB.ConsistencyLevel;
import org.influxdb.dto.BatchPoints;
import org.influxdb.dto.Point;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.ThreadSafe;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import static com.google.common.collect.Maps.newHashMap;
import static com.googlecode.jmxtrans.util.NumberUtils.isValidNumber;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
/**
* {@link com.googlecode.jmxtrans.model.OutputWriter} for
* InfluxDB.
*
* @author Simon Hutchinson
* github.com/sihutch
*/
@ThreadSafe
public class InfluxDbWriter extends OutputWriterAdapter {
private static final Logger log = LoggerFactory.getLogger(InfluxDbWriter.class);
public static final String TAG_HOSTNAME = "hostname";
public static final String JMX_PORT_KEY = "_jmx_port";
@Nonnull private final InfluxDB influxDB;
@Nonnull private final String database;
@Nonnull private final ConsistencyLevel writeConsistency;
@Nonnull private final String retentionPolicy;
@Nonnull private final ImmutableMap tags;
@Nonnull ImmutableList typeNames;
/**
* The {@link ImmutableSet} of {@link ResultAttribute} attributes of
* {@link Result} that will be written as {@link Point} tags
*/
private final ImmutableSet resultAttributesToWriteAsTags;
private final boolean createDatabase;
private final boolean typeNamesAsTags;
private final boolean allowStringValues;
private final boolean reportJmxPortAsTag;
public InfluxDbWriter(
@Nonnull InfluxDB influxDB,
@Nonnull String database,
@Nonnull ConsistencyLevel writeConsistency,
@Nonnull String retentionPolicy,
@Nonnull ImmutableMap tags,
@Nonnull ImmutableSet resultAttributesToWriteAsTags,
@Nonnull ImmutableList typeNames,
boolean createDatabase,
boolean reportJmxPortAsTag,
boolean typeNamesAsTags,
boolean allowStringValues) {
this.typeNames = typeNames;
this.database = database;
this.writeConsistency = writeConsistency;
this.retentionPolicy = retentionPolicy;
this.influxDB = influxDB;
this.tags = tags;
this.resultAttributesToWriteAsTags = resultAttributesToWriteAsTags;
this.createDatabase = createDatabase;
this.reportJmxPortAsTag = reportJmxPortAsTag;
this.typeNamesAsTags = typeNamesAsTags;
this.allowStringValues = allowStringValues;
}
/**
*
* Each {@link Result} is written as a {@link Point} to InfluxDB
*
*
*
* The measurement for the {@link Point} is to {@link Result#getKeyAlias()}
*
*
* The retention policy for the measurement is set to "default" unless
* overridden in settings:
*
*
*
* The write consistency level defaults to "ALL" unless overridden in
* settings:
*
*
* - ALL = Write succeeds only if write reached all cluster members.
* - ANY = Write succeeds if write reached any cluster members.
* - ONE = Write succeeds if write reached at least one cluster members.
*
* - QUORUM = Write succeeds only if write reached a quorum of cluster
* members.
*
*
*
* The time key for the {@link Point} is set to {@link Result#getEpoch()}
*
*
*
* All {@link Result#getValues()} are written as fields to the {@link Point}
*
*
*
* The following properties from {@link Result} are written as tags to the
* {@link Point} unless overriden in settings:
*
*
* - {@link Result#getAttributeName()}
* - {@link Result#getClassName()}
* - {@link Result#getObjDomain()}
* - {@link Result#getTypeName()}
*
*
* {@link Server#getHost()} is set as a tag on every {@link Point}
*
*
* {@link Server#getPort()} is written as a field, unless {@link #reportJmxPortAsTag} is set to {@code true}
*
*
*/
@Override
public void doWrite(Server server, Query query, Iterable results) throws Exception {
// Creates only if it doesn't already exist
if (createDatabase) influxDB.createDatabase(database);
BatchPoints.Builder batchPointsBuilder = BatchPoints.database(database).retentionPolicy(retentionPolicy)
.tag(TAG_HOSTNAME, server.getSource());
for(Map.Entry tag : tags.entrySet()) {
batchPointsBuilder.tag(tag.getKey(),tag.getValue());
}
BatchPoints batchPoints = batchPointsBuilder.consistency(writeConsistency).build();
ImmutableList typeNamesParam = null;
// if not typeNamesAsTag, we concat typeName in values.
if (!typeNamesAsTags) {
typeNamesParam = this.typeNames;
}
for (Result result : results) {
log.debug("Query result: {}", result);
HashMap filteredValues = newHashMap();
Object value = result.getValue();
String key = KeyUtils.getPrefixedKeyString(query, result, typeNamesParam);
if (isValidNumber(value) || allowStringValues && value instanceof String) {
filteredValues.put(key, value);
}
// send the point if filteredValues isn't empty
if (!filteredValues.isEmpty()) {
Map resultTagsToApply = buildResultTagMap(result);
if (reportJmxPortAsTag) {
resultTagsToApply.put(JMX_PORT_KEY, server.getPort());
} else {
filteredValues.put(JMX_PORT_KEY, Integer.parseInt(server.getPort()));
}
Point point = Point.measurement(result.getKeyAlias()).time(result.getEpoch(), MILLISECONDS)
.tag(resultTagsToApply).fields(filteredValues).build();
log.debug("Point: {}", point);
batchPoints.point(point);
}
}
influxDB.write(batchPoints);
}
private Map buildResultTagMap(Result result) {
Map resultTagMap = new TreeMap<>();
for (ResultAttribute resultAttribute : resultAttributesToWriteAsTags) {
resultAttribute.addTo(resultTagMap, result);
}
if (typeNamesAsTags) {
Map typeNameValueMap = result.getTypeNameMap();
for (String typeToTag : this.typeNames) {
if (typeNameValueMap.containsKey(typeToTag)) {
resultTagMap.put(typeToTag, typeNameValueMap.get(typeToTag));
}
}
}
return resultTagMap;
}
}