All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.googlecode.jmxtrans.model.output.InfluxDbWriter Maven / Gradle / Ivy

There is a newer version: 272
Show newest version
/**
 * The MIT License
 * Copyright (c) 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.base.Predicate;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
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 org.influxdb.InfluxDB;
import org.influxdb.InfluxDB.ConsistencyLevel;
import org.influxdb.dto.BatchPoints;
import org.influxdb.dto.Point;

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 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 {

	public static final String TAG_HOSTNAME = "hostname";

	@Nonnull private final InfluxDB influxDB;
	@Nonnull private final String database;
	@Nonnull private final ConsistencyLevel writeConsistency;
	@Nonnull private final String retentionPolicy;

	/**
	 * 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;

	public InfluxDbWriter(
			@Nonnull InfluxDB influxDB,
			@Nonnull String database,
			@Nonnull ConsistencyLevel writeConsistency,
			@Nonnull String retentionPolicy,
			@Nonnull ImmutableSet resultAttributesToWriteAsTags,
			boolean createDatabase) {
		this.database = database;
		this.writeConsistency = writeConsistency;
		this.retentionPolicy = retentionPolicy;
		this.influxDB = influxDB;
		this.resultAttributesToWriteAsTags = resultAttributesToWriteAsTags;
		this.createDatabase = createDatabase;
	}

	Predicate isNotNaN = new Predicate() {
		@Override
		public boolean apply(Object input) {
			return !input.toString().equals("NaN");
		}
	};

	/**
	 * 

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

* */ @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 batchPoints = BatchPoints.database(database).retentionPolicy(retentionPolicy) .tag(TAG_HOSTNAME, server.getSource()).consistency(writeConsistency).build(); for (Result result : results) { HashMap filteredValues = newHashMap(Maps.filterValues(result.getValues(), isNotNaN)); // send the point if filteredValues isn't empty if (!filteredValues.isEmpty()) { filteredValues.put("_jmx_port", Integer.parseInt(server.getPort())); Map resultTagsToApply = buildResultTagMap(result); Point point = Point.measurement(result.getKeyAlias()).time(result.getEpoch(), MILLISECONDS) .tag(resultTagsToApply).fields(filteredValues).build(); batchPoints.point(point); } } influxDB.write(batchPoints); } private Map buildResultTagMap(Result result) throws Exception { Map resultTagMap = new TreeMap(); for (ResultAttribute resultAttribute : resultAttributesToWriteAsTags) { resultAttribute.addAttribute(resultTagMap, result); } return resultTagMap; } }