Skip to content

Commit

Permalink
no sharing connections for now (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
kortemik authored Jan 23, 2025
1 parent 8faa8cf commit e449d68
Showing 1 changed file with 32 additions and 46 deletions.
78 changes: 32 additions & 46 deletions src/main/java/com/teragrep/aer_02/SyslogBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,9 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class SyslogBridge {

private final Lock initLock = new ReentrantLock();
private final MetricRegistry metricRegistry = new MetricRegistry();;
private DefaultOutput defaultOutput = null;
private boolean initialized = false;

public SyslogBridge() {

}
Expand Down Expand Up @@ -132,51 +125,41 @@ public void eventHubTriggerToSyslog(
final Sourceable configSource = new EnvironmentSource();
final String hostname = new Hostname("localhost").hostname();

try {
initLock.lock();
if (!initialized) {
context.getLogger().info("initializing at " + this);
final Report report = new JmxReport(
new Slf4jReport(new PrometheusReport(new DropwizardExports(metricRegistry)), metricRegistry), metricRegistry
);
report.start();

if (configSource.source("relp.tls.mode", "none").equals("keyVault")) {
context.getLogger().info("connection tls enabled");

defaultOutput = new DefaultOutput(
context.getLogger(),
"defaultOutput",
new RelpConnectionConfig(configSource),
metricRegistry,
new AzureSSLContextSupplier()
);
}
else {
context.getLogger().info("connection tls disabled");
defaultOutput = new DefaultOutput(
context.getLogger(),
"defaultOutput",
new RelpConnectionConfig(configSource),
metricRegistry
);
}
final MetricRegistry metricRegistry = new MetricRegistry();
;

final Thread shutdownHook = new Thread(() -> {
defaultOutput.close();
report.close();
});
context.getLogger().info("initializing at " + this);

Runtime.getRuntime().addShutdownHook(shutdownHook);
final Report report = new JmxReport(
new Slf4jReport(new PrometheusReport(new DropwizardExports(metricRegistry)), metricRegistry),
metricRegistry
);
report.start();

initialized = true;
context.getLogger().info("initialized at " + this);
}
DefaultOutput defaultOutput;
if (configSource.source("relp.tls.mode", "none").equals("keyVault")) {
context.getLogger().info("connection tls enabled");

defaultOutput = new DefaultOutput(
context.getLogger(),
"defaultOutput",
new RelpConnectionConfig(configSource),
metricRegistry,
new AzureSSLContextSupplier()
);
}
finally {
initLock.unlock();
else {
context.getLogger().info("connection tls disabled");
defaultOutput = new DefaultOutput(
context.getLogger(),
"defaultOutput",
new RelpConnectionConfig(configSource),
metricRegistry
);
}

context.getLogger().info("initialized at " + this);

EventDataConsumer consumer = new EventDataConsumer(configSource, defaultOutput, hostname, metricRegistry);

for (int index = 0; index < events.length; index++) {
Expand All @@ -193,6 +176,9 @@ public void eventHubTriggerToSyslog(
context.getLogger().warning("eventHubTriggerToSyslog event data is null");
}
}

// close connections to prevent resource leak
defaultOutput.close();
}
catch (Throwable t) {
context.getLogger().severe("exiting because caught Throwable: " + t);
Expand Down

0 comments on commit e449d68

Please sign in to comment.