Skip to content

Commit

Permalink
Merge pull request #30 from zeebe-io/24-env-variables
Browse files Browse the repository at this point in the history
chore(exporter): configure JDBC property via environment variables
  • Loading branch information
Zelldon committed Nov 14, 2018
2 parents 469f515 + fcf82b1 commit 2ddbb32
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 10 deletions.
11 changes: 9 additions & 2 deletions exporter/README.md
Expand Up @@ -37,8 +37,8 @@ className = "io.zeebe.monitor.SimpleMonitorExporter"

### Configure the Exporter

The jdbc uses some default properties which can be configured as well.
To do this you can add the following. All values are commeted out and have the default values. To change them remove the `#` and change the value.
The JDBC uses some default properties which can be configured as well.
To do this you can add the following. All values are commented out and have the default values. To change them remove the `#` and change the value.

```
[[exporters]]
Expand Down Expand Up @@ -70,6 +70,13 @@ className = "io.zeebe.monitor.SimpleMonitorExporter"
#batchTimerMilli = 1000
```

The JDBC properties can also be set by the following environment variables:

* `SIMPLE_MONITOR_EXPORTER_JDBC_URL`
* `SIMPLE_MONITOR_EXPORTER_JDBC_DRIVER`
* `SIMPLE_MONITOR_EXPORTER_JDBC_USER`
* `SIMPLE_MONITOR_EXPORTER_JDBC_PASSWORD`

### Start the broker

After configure the broker and also maybe configuring the exporter you are done and can start the broker.
Expand Down
36 changes: 28 additions & 8 deletions exporter/src/main/java/io/zeebe/monitor/SimpleMonitorExporter.java
Expand Up @@ -31,6 +31,8 @@
import io.zeebe.protocol.intent.DeploymentIntent;
import io.zeebe.protocol.intent.Intent;
import io.zeebe.protocol.intent.WorkflowInstanceIntent;
import org.slf4j.Logger;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
Expand All @@ -39,17 +41,18 @@
import java.sql.SQLException;
import java.sql.Statement;
import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import org.slf4j.Logger;

public class SimpleMonitorExporter implements Exporter {

private static final String ENV_PREFIX = "SIMPLE_MONITOR_EXPORTER_";
private static final String ENV_JDBC_URL = ENV_PREFIX + "JDBC_URL";
private static final String ENV_JDBC_DRIVER = ENV_PREFIX + "JDBC_DRIVER";
private static final String ENV_JDBC_USER = ENV_PREFIX + "JDBC_USER";
private static final String ENV_JDBC_PASSWORD = ENV_PREFIX + "JDBC_PASSWORD";

private static final String INSERT_WORKFLOW =
"INSERT INTO WORKFLOW (ID_, KEY_, BPMN_PROCESS_ID_, VERSION_, RESOURCE_, TIMESTAMP_) VALUES ('%s', %d, '%s', %d, '%s', %d);";

Expand Down Expand Up @@ -102,6 +105,9 @@ public void configure(final Context context) {
log = context.getLogger();
configuration =
context.getConfiguration().instantiate(SimpleMonitorExporterConfiguration.class);

applyEnvironmentVariables(configuration);

batchSize = configuration.batchSize;
batchTimerMilli = configuration.batchTimerMilli;

Expand All @@ -113,6 +119,19 @@ public void configure(final Context context) {
}
}

private void applyEnvironmentVariables(final SimpleMonitorExporterConfiguration configuration) {
final Map<String, String> environment = System.getenv();

Optional.ofNullable(environment.get(ENV_JDBC_URL))
.ifPresent(url -> configuration.jdbcUrl = url);
Optional.ofNullable(environment.get(ENV_JDBC_DRIVER))
.ifPresent(driver -> configuration.driverName = driver);
Optional.ofNullable(environment.get(ENV_JDBC_USER))
.ifPresent(user -> configuration.userName = user);
Optional.ofNullable(environment.get(ENV_JDBC_PASSWORD))
.ifPresent(password -> configuration.password = password);
}

@Override
public void open(final Controller controller) {
try {
Expand All @@ -121,7 +140,8 @@ public void open(final Controller controller) {
configuration.jdbcUrl, configuration.userName, configuration.password);
connection.setAutoCommit(true);
} catch (final SQLException e) {
throw new RuntimeException("Error on opening database.", e);
throw new RuntimeException(
String.format("Error on opening database with configuration %s.", configuration), e);
}

createTables();
Expand Down Expand Up @@ -201,7 +221,7 @@ private void exportDeploymentRecord(final Record record) {
final RecordMetadata metadata = record.getMetadata();
if (metadata.getIntent() != DeploymentIntent.CREATED
|| metadata.getPartitionId() != Protocol.DEPLOYMENT_PARTITION) {
// ignore deployment event on other partitions to avoid duplicates
// ignore deployment event on other partitions to avoid duplicates
return;
}
final long timestamp = record.getTimestamp().toEpochMilli();
Expand Down
Expand Up @@ -30,4 +30,20 @@ public class SimpleMonitorExporterConfiguration {
* <p>If the value is less then one, then no timer will be scheduled.
*/
int batchTimerMilli = 1000;

@Override
public String toString() {
return "SimpleMonitorExporterConfiguration{"
+ "jdbcUrl='"
+ jdbcUrl
+ '\''
+ ", driverName='"
+ driverName
+ '\''
+ ", batchSize="
+ batchSize
+ ", batchTimerMilli="
+ batchTimerMilli
+ '}';
}
}

0 comments on commit 2ddbb32

Please sign in to comment.