Search in sources :

Example 21 with BatchPoints

use of org.influxdb.dto.BatchPoints in project openems by OpenEMS.

the class InfluxdbPersistence method forever.

@Override
protected void forever() {
    // Prepare DB connection
    Optional<InfluxDB> _influxdb = getInfluxDB();
    if (!_influxdb.isPresent()) {
        synchronized (queue) {
            // Clear queue if we don't have a valid influxdb connection. This is necessary to avoid filling the
            // memory in case of no available DB connection
            queue.clear();
        }
    }
    InfluxDB influxDB = _influxdb.get();
    /*
		 * Convert FieldVales in queue to Points
		 */
    BatchPoints batchPoints = // 
    BatchPoints.database(database.valueOptional().orElse("db")).tag("fems", // 
    String.valueOf(fems.valueOptional().get())).build();
    synchronized (queue) {
        queue.asMap().forEach((timestamp, fieldValues) -> {
            Builder builder = // 
            Point.measurement("data").time(timestamp, TimeUnit.MILLISECONDS);
            fieldValues.forEach(fieldValue -> {
                if (fieldValue instanceof NumberFieldValue) {
                    builder.addField(fieldValue.field, ((NumberFieldValue) fieldValue).value);
                } else if (fieldValue instanceof StringFieldValue) {
                    builder.addField(fieldValue.field, ((StringFieldValue) fieldValue).value);
                }
            });
            batchPoints.point(builder.build());
        });
        queue.clear();
    }
    // write to DB
    try {
        influxDB.write(batchPoints);
        log.debug("Wrote [" + batchPoints.getPoints().size() + "] points to InfluxDB");
    } catch (RuntimeException e) {
        log.error("Error writing to InfluxDB: " + e);
    }
}
Also used : BatchPoints(org.influxdb.dto.BatchPoints) InfluxDB(org.influxdb.InfluxDB) Builder(org.influxdb.dto.Point.Builder)

Example 22 with BatchPoints

use of org.influxdb.dto.BatchPoints in project flink by apache.

the class InfluxdbReporter method buildReport.

@Nullable
private BatchPoints buildReport() {
    Instant timestamp = Instant.now();
    BatchPoints.Builder report = BatchPoints.database(database);
    report.retentionPolicy(retentionPolicy);
    report.consistency(consistency);
    try {
        for (Map.Entry<Gauge<?>, MeasurementInfo> entry : gauges.entrySet()) {
            report.point(MetricMapper.map(entry.getValue(), timestamp, entry.getKey()));
        }
        for (Map.Entry<Counter, MeasurementInfo> entry : counters.entrySet()) {
            report.point(MetricMapper.map(entry.getValue(), timestamp, entry.getKey()));
        }
        for (Map.Entry<Histogram, MeasurementInfo> entry : histograms.entrySet()) {
            report.point(MetricMapper.map(entry.getValue(), timestamp, entry.getKey()));
        }
        for (Map.Entry<Meter, MeasurementInfo> entry : meters.entrySet()) {
            report.point(MetricMapper.map(entry.getValue(), timestamp, entry.getKey()));
        }
    } catch (ConcurrentModificationException | NoSuchElementException e) {
        // report next time
        return null;
    }
    return report.build();
}
Also used : Histogram(org.apache.flink.metrics.Histogram) ConcurrentModificationException(java.util.ConcurrentModificationException) BatchPoints(org.influxdb.dto.BatchPoints) Meter(org.apache.flink.metrics.Meter) Instant(java.time.Instant) Gauge(org.apache.flink.metrics.Gauge) Counter(org.apache.flink.metrics.Counter) Map(java.util.Map) NoSuchElementException(java.util.NoSuchElementException) Nullable(javax.annotation.Nullable)

Example 23 with BatchPoints

use of org.influxdb.dto.BatchPoints in project jmxtrans by jmxtrans.

the class InfluxDbWriter method doWrite.

/**
 * <p>
 * Each {@link Result} is written as a {@link Point} to InfluxDB
 * </p>
 *
 * <p>
 * The measurement for the {@link Point} is to {@link Result#getKeyAlias()}
 * <p>
 * <a href=
 * "https://influxdb.com/docs/v0.9/concepts/key_concepts.html#retention-policy">
 * The retention policy</a> for the measurement is set to "default" unless
 * overridden in settings:
 * </p>
 *
 * <p>
 * The write consistency level defaults to "ALL" unless overridden in
 * settings:
 *
 * <ul>
 * <li>ALL = Write succeeds only if write reached all cluster members.</li>
 * <li>ANY = Write succeeds if write reached any cluster members.</li>
 * <li>ONE = Write succeeds if write reached at least one cluster members.
 * </li>
 * <li>QUORUM = Write succeeds only if write reached a quorum of cluster
 * members.</li>
 * </ul>
 *
 * <p>
 * The time key for the {@link Point} is set to {@link Result#getEpoch()}
 * </p>
 *
 * <p>
 * All {@link Result#getValues()} are written as fields to the {@link Point}
 * </p>
 *
 * <p>
 * The following properties from {@link Result} are written as tags to the
 * {@link Point} unless overriden in settings:
 *
 * <ul>
 * <li>{@link Result#getAttributeName()}</li>
 * <li>{@link Result#getClassName()}</li>
 * <li>{@link Result#getObjDomain()}</li>
 * <li>{@link Result#getTypeName()}</li>
 * </ul>
 * <p>
 * {@link Server#getHost()} is set as a tag on every {@link Point}
 * </p>
 * <p>
 * {@link Server#getPort()} is written as a field, unless {@link #reportJmxPortAsTag} is set to {@code true}
 * </p>
 */
@Override
public void doWrite(Server server, Query query, Iterable<Result> 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<String, String> tag : tags.entrySet()) {
        batchPointsBuilder.tag(tag.getKey(), tag.getValue());
    }
    BatchPoints batchPoints = batchPointsBuilder.consistency(writeConsistency).build();
    ImmutableList<String> 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<String, Object> 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<String, String> 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);
}
Also used : BatchPoints(org.influxdb.dto.BatchPoints) Point(org.influxdb.dto.Point) ImmutableMap(com.google.common.collect.ImmutableMap) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) Result(com.googlecode.jmxtrans.model.Result)

Example 24 with BatchPoints

use of org.influxdb.dto.BatchPoints in project jmxtrans by jmxtrans.

the class InfluxDbWriterTests method jmxPortIsReportedAsFieldByDefault.

@Test
public void jmxPortIsReportedAsFieldByDefault() throws Exception {
    BatchPoints batchPoints = writeToInfluxDb(getTestInfluxDbWriterWithDefaultSettings());
    verifyJMXPortOnlyInToken(batchPoints.getPoints().get(0).lineProtocol(), 1);
}
Also used : BatchPoints(org.influxdb.dto.BatchPoints) Test(org.junit.Test)

Example 25 with BatchPoints

use of org.influxdb.dto.BatchPoints in project jmxtrans by jmxtrans.

the class InfluxDbWriterTests method customTagsAreWrittenToDb.

@Test
public void customTagsAreWrittenToDb() throws Exception {
    ImmutableMap<String, String> tags = ImmutableMap.of("customTag", "customValue");
    BatchPoints batchPoints = writeToInfluxDb(getTestInfluxDbWriterWithCustomTags(tags));
    assertThat(batchPoints.getPoints().get(0).lineProtocol()).contains("customTag=customValue");
}
Also used : BatchPoints(org.influxdb.dto.BatchPoints) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Aggregations

BatchPoints (org.influxdb.dto.BatchPoints)26 Test (org.junit.Test)12 Point (org.influxdb.dto.Point)11 Map (java.util.Map)6 HashMap (java.util.HashMap)5 TreeMap (java.util.TreeMap)5 InfluxDB (org.influxdb.InfluxDB)5 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5 Builder (org.influxdb.dto.Point.Builder)3 BgReading (com.eveningoutpost.dexdrip.Models.BgReading)2 Calibration (com.eveningoutpost.dexdrip.Models.Calibration)2 IOException (java.io.IOException)2 ConsistencyLevel (org.influxdb.InfluxDB.ConsistencyLevel)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Maps.newHashMap (com.google.common.collect.Maps.newHashMap)1 JsonObject (com.google.gson.JsonObject)1 Result (com.googlecode.jmxtrans.model.Result)1 ResultAttribute (com.googlecode.jmxtrans.model.ResultAttribute)1 InterruptedIOException (java.io.InterruptedIOException)1 SocketException (java.net.SocketException)1