Skip to content

Commit

Permalink
Merge pull request #54 from NoamShaish/master
Browse files Browse the repository at this point in the history
Added support for https connection to InfluxDB
  • Loading branch information
dossett authored Jun 10, 2019
2 parents 081478d + 3010543 commit 7c348c5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ username | The username with which to connect to InfluxDB (required)
password | The password with which to connect to InfluxDB (required)
database | The database to which to write metrics (required)
tagMapping | A mapping of tag names from the metric prefix (optional, defaults to no mapping)
useHttps | A flag indicating if https connecition should be used (optional, defaults to false)

##### Tag Mapping
InfluxDB 0.9 supports tagging measurements and querying based on those tags. statsd-jvm-profilers uses these tags to support richer querying of the produced data. For compatibility with other metric backends, the tags are extracted from the metric prefix.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,29 @@

import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Reporter that sends data to InfluxDB
*
* @author Andrew Johnson
*/
public class InfluxDBReporter extends Reporter<InfluxDB> {
private static final Logger LOGGER = Logger.getLogger(InfluxDBReporter.class.getName());

public static final String VALUE_COLUMN = "value";
public static final String USERNAME_ARG = "username";
public static final String PASSWORD_ARG = "password";
public static final String DATABASE_ARG = "database";
public static final String TAG_MAPPING_ARG = "tagMapping";
public static final String USE_HTTPS_ARG = "useHttps";

private String username;
private String password;
private String database;
private String tagMapping;
private Boolean useHttps;
private final Map<String, String> tags;

public InfluxDBReporter(Arguments arguments) {
Expand Down Expand Up @@ -94,7 +100,21 @@ public boolean emitBounds() {
*/
@Override
protected InfluxDB createClient(String server, int port, String prefix) {
return InfluxDBFactory.connect(String.format("http://%s:%d", server, port), username, password);
String url = resolveUrl(server, port);
logInfo(String.format("Connecting to influxDB at %s", url));

return InfluxDBFactory.connect(url, username, password);
}

public String resolveUrl(String server, int port) {
String protocol = useHttps ? "https" : "http";
return String.format("%s://%s:%d", protocol, server, port);
}

private void logInfo(String message) {
if (LOGGER.isLoggable(Level.INFO)) {
LOGGER.info(message);
}
}

/**
Expand All @@ -108,6 +128,13 @@ protected void handleArguments(Arguments arguments) {
password = arguments.remainingArgs.get(PASSWORD_ARG);
database = arguments.remainingArgs.get(DATABASE_ARG);
tagMapping = arguments.remainingArgs.get(TAG_MAPPING_ARG);
if (arguments.remainingArgs.containsKey(USE_HTTPS_ARG)) {
useHttps = Boolean.parseBoolean(arguments.remainingArgs.get(USE_HTTPS_ARG));
} else {
useHttps = false;
}

logInfo(String.format("Received arguments: username = %s, password = XXXXX, database = %s, tagMapping= %s, useHttps = %s", username, database, tagMapping, useHttps));

Preconditions.checkNotNull(username);
Preconditions.checkNotNull(password);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,22 @@ public void testRecordGaugeValue() {
Mockito.doAnswer(answer).when(client).write(Matchers.any(BatchPoints.class));
reporter.recordGaugeValue("fake", 100L);
}

@Test
public void testHttpsUrlResolution() {
Arguments arguments = MockArguments.createArgs("localhost", 443, "influxdb.reporter.test",
ImmutableMap.of("username", "user", "password", "password", "database", "database", "useHttps", "true"));
InfluxDBReporter reporter = new InfluxDBReporter(arguments);

assertEquals(reporter.resolveUrl("localhost", 443), "https://localhost:443");
}

@Test
public void testHttpUrlResolution() {
Arguments arguments = MockArguments.createArgs("localhost", 8888, "influxdb.reporter.test",
ImmutableMap.of("username", "user", "password", "password", "database", "database", "useHttps", "false"));
InfluxDBReporter reporter = new InfluxDBReporter(arguments);

assertEquals(reporter.resolveUrl("localhost", 8888), "http://localhost:8888");
}
}

0 comments on commit 7c348c5

Please sign in to comment.