org.influxdb.InfluxDB Java Examples

The following examples show how to use org.influxdb.InfluxDB. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: InfluxDBImpl.java    From influxdb-java with MIT License 6 votes vote down vote up
private InfluxDB enableBatch(final int actions, final int flushDuration, final int jitterDuration,
                             final TimeUnit durationTimeUnit, final ThreadFactory threadFactory,
                             final BiConsumer<Iterable<Point>, Throwable> exceptionHandler) {
  if (this.batchEnabled.get()) {
    throw new IllegalStateException("BatchProcessing is already enabled.");
  }
  this.batchProcessor = BatchProcessor
          .builder(this)
          .actions(actions)
          .exceptionHandler(exceptionHandler)
          .interval(flushDuration, jitterDuration, durationTimeUnit)
          .threadFactory(threadFactory)
          .consistencyLevel(consistency)
          .build();
  this.batchEnabled.set(true);
  return this;
}
 
Example #2
Source File: InfluxDbSourceTest.java    From hazelcast-jet-contrib with Apache License 2.0 6 votes vote down vote up
@Test
public void test_stream_influxDbSource_withPojoResultMapper() {
    InfluxDB db = influxdbContainer.getNewInfluxDB();
    fillCpuData(db);

    Pipeline p = Pipeline.create();

    p.readFrom(
            InfluxDbSources.influxDb("SELECT * FROM test_db..cpu",
                    DATABASE_NAME,
                    influxdbContainer.getUrl(),
                    USERNAME,
                    PASSWORD,
                    Cpu.class))
     .addTimestamps(cpu -> cpu.time.toEpochMilli(), 0)
     .writeTo(Sinks.list("results"));

    jet.newJob(p).join();

    assertEquals(VALUE_COUNT, jet.getList("results").size());
}
 
Example #3
Source File: TestInfluxTarget.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@Test
public void testNoAutoCreateMissingDb() throws Exception {
  InfluxConfigBean conf = getConfigBean();
  conf.autoCreate = false;
  conf.dbName = "abcdef";

  InfluxTarget target = PowerMockito.spy(new InfluxTarget(conf));
  TargetRunner runner = new TargetRunner.Builder(InfluxDTarget.class, target).build();

  InfluxDB client = mock(InfluxDB.class);
  when(client.describeDatabases()).thenReturn(ImmutableList.of("test"));

  PowerMockito.doReturn(client).when(target, "getClient", any(InfluxConfigBean.class));

  List<Stage.ConfigIssue> issues = runner.runValidateConfigs();
  assertEquals(1, issues.size());
}
 
Example #4
Source File: TestInfluxTarget.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@Test
public void testAutoCreateMissingDb() throws Exception {
  InfluxConfigBean conf = getConfigBean();
  conf.autoCreate = true;
  conf.dbName = "abcdef";

  InfluxTarget target = PowerMockito.spy(new InfluxTarget(conf));
  TargetRunner runner = new TargetRunner.Builder(InfluxDTarget.class, target).build();

  InfluxDB client = mock(InfluxDB.class);
  when(client.describeDatabases()).thenReturn(ImmutableList.of("test"));

  PowerMockito.doReturn(client).when(target, "getClient", any(InfluxConfigBean.class));

  runner.runInit();
  verify(client, times(1)).createDatabase(conf.dbName);
}
 
Example #5
Source File: InfluxLogger.java    From Okra with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    String dbName = "centos_test_db";

    InfluxDB influxDB = InfluxDBFactory.connect("http://192.168.0.71:18086", "influxdbUser", "influxdbPsw");

    // Flush every 2000 Points, at least every 100ms
    influxDB.enableBatch(2000, 100, TimeUnit.MILLISECONDS);

    for (int i = 0; i < 50; i++) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Point point2 = Point.measurement("disk")
                .time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
                .addField("used", Math.random() * 80L)
                .addField("free", Math.random() * 30L)
                .build();
        influxDB.write(dbName, "autogen", point2);
    }

    System.out.println();
}
 
Example #6
Source File: InfluxDbSources.java    From hazelcast-jet-contrib with Apache License 2.0 6 votes vote down vote up
InfluxDbSourceContext(
        @Nonnull String query,
        @Nonnull SupplierEx<InfluxDB> connectionSupplier,
        @Nullable Class<T> pojoClass,
        @Nullable MeasurementProjection<T> measurementProjection
) {
    assert pojoClass != null ^ measurementProjection != null;
    this.pojoClass = pojoClass;
    this.resultMapper = pojoClass != null ? new InfluxDBResultMapper() : null;
    this.measurementProjection = measurementProjection;
    db = connectionSupplier.get();
    db.query(new Query(query),
            DEFAULT_CHUNK_SIZE,
            e -> {
                try {
                    queue.put(e);
                } catch (InterruptedException ex) {
                    Thread.currentThread().interrupt();
                }
            },
            () -> finished = true
    );
}
 
Example #7
Source File: InfluxDBConnectionLiveTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void whenDatabaseCreatedDatabaseChecksOk() {

    InfluxDB connection = connectDatabase();

    // Create "baeldung" and check for it
    connection.createDatabase("baeldung");
    assertTrue(connection.databaseExists("baeldung"));

    // Verify that nonsense databases are not there
    assertFalse(connection.databaseExists("foobar"));

    // Drop "baeldung" and check again
    connection.deleteDatabase("baeldung");
    assertFalse(connection.databaseExists("baeldung"));
}
 
Example #8
Source File: InfluxDBMethodInterceptorTest.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    // write
    writeArguments = new Object[] {
        "sw8", "auto_gen", InfluxDB.ConsistencyLevel.ALL, TimeUnit.SECONDS,
        "weather,location=us-midwest temperature=82 1465839830100400200"
    };
    writeArgumentTypes = new Class[] {
        String.class, String.class, InfluxDB.ConsistencyLevel.class, TimeUnit.class, String.class
    };

    // query
    queryArguments = new Object[] {
        new Query("select * from weather limit 1", "sw8")
    };
    queryArgumentTypes = new Class[] {
        Query.class
    };

    interceptor = new InfluxDBMethodInterceptor();
    when(enhancedInstance.getSkyWalkingDynamicField()).thenReturn("http://127.0.0.1:8086");
}
 
Example #9
Source File: AbstractInfluxDBProcessor.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Helper method to create InfluxDB instance
 * @return InfluxDB instance
 */
protected synchronized InfluxDB getInfluxDB(ProcessContext context) {
    if ( influxDB.get() == null ) {
        String username = context.getProperty(USERNAME).evaluateAttributeExpressions().getValue();
        String password = context.getProperty(PASSWORD).evaluateAttributeExpressions().getValue();
        long connectionTimeout = context.getProperty(INFLUX_DB_CONNECTION_TIMEOUT).asTimePeriod(TimeUnit.SECONDS);
        String influxDbUrl = context.getProperty(INFLUX_DB_URL).evaluateAttributeExpressions().getValue();
        try {
            influxDB.set(makeConnection(username, password, influxDbUrl, connectionTimeout));
        } catch(Exception e) {
            getLogger().error("Error while getting connection {}", new Object[] { e.getLocalizedMessage() },e);
            throw new RuntimeException("Error while getting connection " + e.getLocalizedMessage(),e);
        }
        getLogger().info("InfluxDB connection created for host {}",
                new Object[] {influxDbUrl});
    }
    return influxDB.get();
}
 
Example #10
Source File: ITExecuteInfluxDBQuery.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testValidSameTwoPoints() {
    String message = "water,country=US,city=nyc rain=1,humidity=0.6 1501002274856668652" +
        System.lineSeparator() +
        "water,country=US,city=nyc rain=1,humidity=0.6 1501002274856668652";
    influxDB.write(dbName, DEFAULT_RETENTION_POLICY, InfluxDB.ConsistencyLevel.ONE, message);
    String query = "select * from water";
    byte [] bytes = query.getBytes();
    runner.enqueue(bytes);
    runner.run(1,true,true);
    runner.assertAllFlowFilesTransferred(ExecuteInfluxDBQuery.REL_SUCCESS, 1);
    List<MockFlowFile> flowFiles = runner.getFlowFilesForRelationship(ExecuteInfluxDBQuery.REL_SUCCESS);
    assertEquals("Value should be equal", 1, flowFiles.size());
    assertNull("Value should be null", flowFiles.get(0).getAttribute(ExecuteInfluxDBQuery.INFLUX_DB_ERROR_MESSAGE));
    assertEquals("Value should be equal",query, flowFiles.get(0).getAttribute(ExecuteInfluxDBQuery.INFLUX_DB_EXECUTED_QUERY));

    QueryResult queryResult = gson.fromJson(new StringReader(new String(flowFiles.get(0).toByteArray())), QueryResult.class);
    assertNotNull("QueryResult should not be null", queryResult.getResults());
    assertEquals("Result size should be same", 1, queryResult.getResults().size());
    Series series = queryResult.getResults().get(0).getSeries().get(0);
    validateSeries(series.getName(), series.getColumns(), series.getValues().get(0),"nyc",1.0);
}
 
Example #11
Source File: InfluxDBContainerWithUserTest.java    From testcontainers-java with MIT License 6 votes vote down vote up
@Test
public void queryForWriteAndRead() {
    InfluxDB influxDB = influxDBContainer.getNewInfluxDB();

    Point point = Point.measurement("cpu")
        .time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
        .addField("idle", 90L)
        .addField("user", 9L)
        .addField("system", 1L)
        .build();
    influxDB.write(point);

    Query query = new Query("SELECT idle FROM cpu", DATABASE);
    QueryResult actual = influxDB.query(query);

    assertThat(actual, notNullValue());
    assertThat(actual.getError(), nullValue());
    assertThat(actual.getResults(), notNullValue());
    assertThat(actual.getResults().size(), is(1));

}
 
Example #12
Source File: InfluxdbGateway.java    From redis-rdb-cli with Apache License 2.0 6 votes vote down vote up
protected InfluxDB create() {
    //
    final OkHttpClient.Builder http = new OkHttpClient.Builder();
    Dispatcher dispatcher = new Dispatcher();
    dispatcher.setMaxRequests(2); dispatcher.setMaxRequestsPerHost(2);
    http.dispatcher(dispatcher);
    http.connectionPool(new ConnectionPool(threads, 5, TimeUnit.MINUTES));
    
    //
    final InfluxDB r = InfluxDBFactory.connect(url, user, password, http);
    BatchOptions opt = DEFAULTS;
    opt = opt.consistency(consistency).jitterDuration(jitter);
    opt = opt.actions(this.actions).threadFactory(new XThreadFactory("influxdb"));
    opt = opt.exceptionHandler(new ExceptionHandler()).bufferLimit(capacity).flushDuration(interval);
    r.setDatabase(this.database).setRetentionPolicy(retention).enableBatch((opt)).enableGzip();
    return r;
}
 
Example #13
Source File: BatchProcessorTest.java    From influxdb-java with MIT License 6 votes vote down vote up
@Test
public void testSchedulerExceptionHandlingCallback() throws InterruptedException, IOException {
  InfluxDB mockInfluxDB = mock(InfluxDBImpl.class);
  BiConsumer<Iterable<Point>, Throwable> mockHandler = mock(BiConsumer.class);
  BatchProcessor batchProcessor = BatchProcessor.builder(mockInfluxDB).actions(Integer.MAX_VALUE)
      .interval(1, TimeUnit.NANOSECONDS).exceptionHandler(mockHandler).build();

  doThrow(new RuntimeException()).when(mockInfluxDB).write(any(BatchPoints.class));

  Point point = Point.measurement("cpu").field("6", "").build();
  BatchProcessor.HttpBatchEntry batchEntry1 = new BatchProcessor.HttpBatchEntry(point, "db1", "");
  BatchProcessor.HttpBatchEntry batchEntry2 = new BatchProcessor.HttpBatchEntry(point, "db2", "");

  batchProcessor.put(batchEntry1);
  Thread.sleep(200); // wait for scheduler

  verify(mockHandler, times(1)).accept(argThat(Matchers.hasItems(point, point)), any(RuntimeException.class));
}
 
Example #14
Source File: InfluxDBGenericRecordSinkTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@BeforeMethod
public void setUp() throws Exception {
    influxSink = new InfluxDBGenericRecordSink();

    configMap.put("influxdbUrl", "http://localhost:8086");
    configMap.put("database", "testDB");
    configMap.put("consistencyLevel", "ONE");
    configMap.put("logLevel", "NONE");
    configMap.put("retentionPolicy", "autogen");
    configMap.put("gzipEnable", "false");
    configMap.put("batchTimeMs", "200");
    configMap.put("batchSize", "1");

    mockSinkContext = mock(SinkContext.class);
    influxSink.influxDBBuilder = mock(InfluxDBBuilder.class);
    influxDB = mock(InfluxDB.class);

    when(influxSink.influxDBBuilder.build(any())).thenReturn(influxDB);
}
 
Example #15
Source File: InfluxDBImpl.java    From influxdb-java with MIT License 6 votes vote down vote up
@Override
public InfluxDB enableBatch(final BatchOptions batchOptions) {

  if (this.batchEnabled.get()) {
    throw new IllegalStateException("BatchProcessing is already enabled.");
  }
  this.batchProcessor = BatchProcessor
          .builder(this)
          .actions(batchOptions.getActions())
          .exceptionHandler(batchOptions.getExceptionHandler())
          .interval(batchOptions.getFlushDuration(), batchOptions.getJitterDuration(), TimeUnit.MILLISECONDS)
          .threadFactory(batchOptions.getThreadFactory())
          .bufferLimit(batchOptions.getBufferLimit())
          .consistencyLevel(batchOptions.getConsistency())
          .precision(batchOptions.getPrecision())
          .build();
  this.batchEnabled.set(true);
  return this;
}
 
Example #16
Source File: StatsCollector.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
/**
 * Sends metrics to influxdb host. This method supports both VM and Host metrics
 */
protected void sendMetricsToInfluxdb(Map<Object, Object> metrics) {
    InfluxDB influxDbConnection = createInfluxDbConnection();

    Pong response = influxDbConnection.ping();
    if (response.getVersion().equalsIgnoreCase("unknown")) {
        throw new CloudRuntimeException(String.format("Cannot ping influxdb host %s:%s.", externalStatsHost, externalStatsPort));
    }

    Collection<Object> metricsObjects = metrics.values();
    List<Point> points = new ArrayList<>();

    s_logger.debug(String.format("Sending stats to %s host %s:%s", externalStatsType, externalStatsHost, externalStatsPort));

    for (Object metricsObject : metricsObjects) {
        Point vmPoint = creteInfluxDbPoint(metricsObject);
        points.add(vmPoint);
    }
    writeBatches(influxDbConnection, databaseName, points);
}
 
Example #17
Source File: ITExecuteInfluxDBQuery.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testEmptyFlowFileQueryWithScheduledQueryEL() {
    String message = "water,country=US,city=newark rain=1,humidity=0.6 1501002274856668652";
    influxDB.write(dbName, DEFAULT_RETENTION_POLICY, InfluxDB.ConsistencyLevel.ONE, message);

    String query = "select * from ${measurement}";
    runner.setProperty(ExecuteInfluxDBQuery.INFLUX_DB_QUERY, query);

    byte [] bytes = new byte [] {};
    Map<String,String> properties = new HashMap<>();
    properties.put("measurement","water");
    runner.enqueue(bytes, properties);
    runner.run(1,true,true);
    runner.assertAllFlowFilesTransferred(ExecuteInfluxDBQuery.REL_SUCCESS, 1);

    List<MockFlowFile> flowFiles = runner.getFlowFilesForRelationship(ExecuteInfluxDBQuery.REL_SUCCESS);
    assertEquals("Value should be equal", 1, flowFiles.size());
    assertNull("Value should be null",flowFiles.get(0).getAttribute(ExecuteInfluxDBQuery.INFLUX_DB_ERROR_MESSAGE));
    assertEquals("Value should be equal",query.replace("${measurement}", "water"), flowFiles.get(0).getAttribute(ExecuteInfluxDBQuery.INFLUX_DB_EXECUTED_QUERY));

    QueryResult queryResult = gson.fromJson(new StringReader(new String(flowFiles.get(0).toByteArray())), QueryResult.class);
    assertNotNull("QueryResult should not be null", queryResult.getResults());
    assertEquals("results array should be same size", 1, queryResult.getResults().size());
    Series series = queryResult.getResults().get(0).getSeries().get(0);
    validateSeries(series.getName(), series.getColumns(), series.getValues().get(0),"newark",1.0);
}
 
Example #18
Source File: InfluxDBAbstractSink.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public void open(Map<String, Object> config, SinkContext sinkContext) throws Exception {
    InfluxDBSinkConfig influxDBSinkConfig = InfluxDBSinkConfig.load(config);
    influxDBSinkConfig.validate();

    super.init(influxDBSinkConfig.getBatchTimeMs(), influxDBSinkConfig.getBatchSize());

    try {
        consistencyLevel = InfluxDB.ConsistencyLevel.valueOf(influxDBSinkConfig.getConsistencyLevel().toUpperCase());
    } catch (IllegalArgumentException e) {
        throw new IllegalArgumentException("Illegal Consistency Level, valid values are: "
            + Arrays.asList(InfluxDB.ConsistencyLevel.values()));
    }

    influxDatabase = influxDBSinkConfig.getDatabase();
    retentionPolicy = influxDBSinkConfig.getRetentionPolicy();

    influxDB = influxDBBuilder.build(influxDBSinkConfig);

    // create the database if not exists
    List<String> databases = influxDB.describeDatabases();
    if (!databases.contains(influxDatabase)) {
        influxDB.createDatabase(influxDatabase);
    }
}
 
Example #19
Source File: InfluxDBConnectionLiveTest.java    From tutorials with MIT License 6 votes vote down vote up
private boolean pingServer(InfluxDB influxDB) {
    try {
        // Ping and check for version string
        Pong response = influxDB.ping();
        if (response.getVersion().equalsIgnoreCase("unknown")) {
            log.error("Error pinging server.");
            return false;
        } else {
            log.info("Database version: {}", response.getVersion());
            return true;
        }
    } catch (InfluxDBIOException idbo) {
        log.error("Exception while pinging database: ", idbo);
        return false;
    }
}
 
Example #20
Source File: InfluxDBImpl.java    From influxdb-java with MIT License 6 votes vote down vote up
@Override
public InfluxDB setLogLevel(final LogLevel logLevel) {
  switch (logLevel) {
  case NONE:
    this.loggingInterceptor.setLevel(Level.NONE);
    break;
  case BASIC:
    this.loggingInterceptor.setLevel(Level.BASIC);
    break;
  case HEADERS:
    this.loggingInterceptor.setLevel(Level.HEADERS);
    break;
  case FULL:
    this.loggingInterceptor.setLevel(Level.BODY);
    break;
  default:
    break;
  }
  this.logLevel = logLevel;
  return this;
}
 
Example #21
Source File: InfluxDBContainerWithUserTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Test
public void checkVersion() {
    InfluxDB actual = influxDBContainer.getNewInfluxDB();

    assertThat(actual, notNullValue());

    assertThat(actual.ping(), notNullValue());
    assertThat(actual.ping().getVersion(), is(TEST_VERSION));

    assertThat(actual.version(), is(TEST_VERSION));
}
 
Example #22
Source File: BatchProcessorTest.java    From influxdb-java with MIT License 5 votes vote down vote up
@Test
public void testIntervalIsZero() throws InterruptedException, IOException {
    InfluxDB mockInfluxDB = mock(InfluxDBImpl.class);
    Assertions.assertThrows(IllegalArgumentException.class, () -> {
      BatchProcessor.builder(mockInfluxDB).actions(1)
          .interval(0, TimeUnit.NANOSECONDS).build();
    });
}
 
Example #23
Source File: InfluxClient.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public void connect() {
    influx = InfluxDBFactory.connect(config.getUrl(), config.getUser(), config.getPassword(),
                                     new OkHttpClient.Builder().readTimeout(3, TimeUnit.MINUTES)
                                                               .writeTimeout(3, TimeUnit.MINUTES),
                                     InfluxDB.ResponseFormat.MSGPACK
    );
    influx.query(new Query("CREATE DATABASE " + database));
    influx.enableGzip();

    influx.enableBatch(config.getActions(), config.getDuration(), TimeUnit.MILLISECONDS);
    influx.setDatabase(database);
}
 
Example #24
Source File: AbstractInfluxDBProcessor.java    From nifi with Apache License 2.0 5 votes vote down vote up
protected InfluxDB makeConnection(String username, String password, String influxDbUrl, long connectionTimeout) {
    Builder builder = new OkHttpClient.Builder().connectTimeout(connectionTimeout, TimeUnit.SECONDS);
    if ( StringUtils.isBlank(username) || StringUtils.isBlank(password) ) {
        return InfluxDBFactory.connect(influxDbUrl, builder);
    } else {
        return InfluxDBFactory.connect(influxDbUrl, username, password, builder);
    }
}
 
Example #25
Source File: InfluxDbTelemetryManager.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public void stopAll() {
    if (producers != null) {
        producers.values().forEach(InfluxDB::close);
    }

    log.info("InfluxDB producer has stopped");
}
 
Example #26
Source File: InfluxDBReporter.java    From statsd-jvm-profiler with MIT License 5 votes vote down vote up
/**
 *
 * @param server The server to which to report data
 * @param port The port on which the server is running
 * @param prefix The prefix for metrics
 * @return An InfluxDB client
 */
@Override
protected InfluxDB createClient(String server, int port, String prefix) {
    String url = resolveUrl(server, port);
    logInfo(String.format("Connecting to influxDB at %s", url));

    return InfluxDBFactory.connect(url, username, password);
}
 
Example #27
Source File: InfluxDBBuilderImpl.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public InfluxDB build(InfluxDBSinkConfig config) {
    InfluxDB influxDB;

    boolean enableAuth = !Strings.isNullOrEmpty(config.getUsername());
    if (enableAuth) {
        log.info("Authenticating to {} as {}", config.getInfluxdbUrl(), config.getUsername());
        influxDB = InfluxDBFactory.connect(config.getInfluxdbUrl(), config.getUsername(), config.getPassword());
    } else {
        log.info("Connecting to {}", config.getInfluxdbUrl());
        influxDB = InfluxDBFactory.connect(config.getInfluxdbUrl());
    }

    if (config.isGzipEnable()) {
        influxDB.enableGzip();
    }

    InfluxDB.LogLevel logLevel;
    try {
        logLevel = InfluxDB.LogLevel.valueOf(config.getLogLevel().toUpperCase());
    } catch (IllegalArgumentException e) {
        throw new IllegalArgumentException("Illegal Log Level, valid values are: "
            + Arrays.asList(InfluxDB.LogLevel.values()));
    }
    influxDB.setLogLevel(logLevel);

    return influxDB;
}
 
Example #28
Source File: BatchProcessorTest.java    From influxdb-java with MIT License 5 votes vote down vote up
@Test
public void testActionsIsZero() throws InterruptedException, IOException {
    InfluxDB mockInfluxDB = mock(InfluxDBImpl.class);
    Assertions.assertThrows(IllegalArgumentException.class, () -> {
      BatchProcessor.builder(mockInfluxDB).actions(0)
          .interval(1, TimeUnit.NANOSECONDS).build();
    });
}
 
Example #29
Source File: InfluxdbReporterTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testMetricReporting() throws Exception {
	String retentionPolicy = "one_hour";
	InfluxDB.ConsistencyLevel consistencyLevel = InfluxDB.ConsistencyLevel.ANY;
	MetricRegistryImpl metricRegistry = createMetricRegistry(retentionPolicy, consistencyLevel);
	try {
		String metricName = "TestCounter";
		Counter counter = registerTestMetric(metricName, metricRegistry);
		counter.inc(42);

		stubFor(post(urlPathEqualTo("/write"))
			.willReturn(aResponse()
				.withStatus(200)));

		InfluxdbReporter reporter = (InfluxdbReporter) metricRegistry.getReporters().get(0);
		reporter.report();

		verify(postRequestedFor(urlPathEqualTo("/write"))
			.withQueryParam("db", equalTo(TEST_INFLUXDB_DB))
			.withQueryParam("rp", equalTo(retentionPolicy))
			.withQueryParam("consistency", equalTo(consistencyLevel.name().toLowerCase()))
			.withHeader("Content-Type", containing("text/plain"))
			.withRequestBody(containing("taskmanager_" + metricName + ",host=" + METRIC_HOSTNAME + ",tm_id=" + METRIC_TM_ID + " count=42i")));
	} finally {
		metricRegistry.shutdown().get();
	}
}
 
Example #30
Source File: InfluxdbReporterTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private MetricRegistryImpl createMetricRegistry(String retentionPolicy, InfluxDB.ConsistencyLevel consistencyLevel) {
	MetricConfig metricConfig = new MetricConfig();
	metricConfig.setProperty(InfluxdbReporterOptions.HOST.key(), "localhost");
	metricConfig.setProperty(InfluxdbReporterOptions.PORT.key(), String.valueOf(wireMockRule.port()));
	metricConfig.setProperty(InfluxdbReporterOptions.DB.key(), TEST_INFLUXDB_DB);
	metricConfig.setProperty(InfluxdbReporterOptions.RETENTION_POLICY.key(), retentionPolicy);
	metricConfig.setProperty(InfluxdbReporterOptions.CONSISTENCY.key(), consistencyLevel.name());

	return new MetricRegistryImpl(
		MetricRegistryConfiguration.defaultMetricRegistryConfiguration(),
		Collections.singletonList(ReporterSetup.forReporter("test", metricConfig, new InfluxdbReporter())));
}