Skip to content

Commit

Permalink
remove unused metricsConfig; refactor config objects to use private f…
Browse files Browse the repository at this point in the history
…ields and be immutable (#31)
  • Loading branch information
eemhu authored Nov 8, 2024
1 parent 09ec612 commit 9034651
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 122 deletions.
12 changes: 6 additions & 6 deletions src/main/java/com/teragrep/aer_02/DefaultOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,14 @@ final class DefaultOutput implements Output {
Reservoir sendReservoir,
Reservoir connectReservoir
) {
this.relpAddress = relpConfig.destinationAddress;
this.relpPort = relpConfig.destinationPort;
this.reconnectInterval = relpConfig.reconnectInterval;
this.relpAddress = relpConfig.relpAddress();
this.relpPort = relpConfig.relpPort();
this.reconnectInterval = relpConfig.reconnectInterval();

this.relpConnection = relpConnection;
this.relpConnection.setConnectionTimeout(relpConfig.connectionTimeout);
this.relpConnection.setReadTimeout(relpConfig.readTimeout);
this.relpConnection.setWriteTimeout(relpConfig.writeTimeout);
this.relpConnection.setConnectionTimeout(relpConfig.connectTimeout());
this.relpConnection.setReadTimeout(relpConfig.readTimeout());
this.relpConnection.setWriteTimeout(relpConfig.writeTimeout());

this.records = metricRegistry.counter(name(DefaultOutput.class, "<[" + name + "]>", "records"));
this.bytes = metricRegistry.counter(name(DefaultOutput.class, "<[" + name + "]>", "bytes"));
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/com/teragrep/aer_02/EventDataConsumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@
*/
package com.teragrep.aer_02;

import com.codahale.metrics.*;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Slf4jReporter;
import com.codahale.metrics.jmx.JmxReporter;
import com.teragrep.aer_02.config.RelpConfig;
import com.teragrep.aer_02.config.SyslogConfig;
Expand Down Expand Up @@ -181,8 +183,8 @@ public Long getValue() {
.withSeverity(Severity.INFORMATIONAL)
.withFacility(Facility.LOCAL0)
.withTimestamp(enqueuedTime.toInstant())
.withHostname(syslogConfig.hostname)
.withAppName(syslogConfig.appName)
.withHostname(syslogConfig.hostName())
.withAppName(syslogConfig.appName())
.withSDElement(sdId)
.withSDElement(sdPartition)
.withSDElement(sdEvent)
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/com/teragrep/aer_02/SyslogBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,21 @@
*/
package com.teragrep.aer_02;

import com.microsoft.azure.functions.annotation.*;
import com.microsoft.azure.functions.*;
import com.microsoft.azure.functions.annotation.*;
import com.teragrep.aer_02.config.source.EnvironmentSource;
import com.teragrep.aer_02.config.source.Sourceable;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.exporter.common.TextFormat;

import java.io.*;
import java.io.IOException;
import java.io.StringWriter;
import java.io.UncheckedIOException;
import java.io.Writer;
import java.time.ZonedDateTime;
import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public class SyslogBridge {

Expand Down
60 changes: 0 additions & 60 deletions src/main/java/com/teragrep/aer_02/config/MetricsConfig.java

This file was deleted.

76 changes: 43 additions & 33 deletions src/main/java/com/teragrep/aer_02/config/RelpConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,71 +47,81 @@

import com.teragrep.aer_02.config.source.Sourceable;

// copy from snw_01 with fixes
public final class RelpConfig {

public final Sourceable configSource;
public final int connectionTimeout;
public final int readTimeout;
public final int writeTimeout;
public final int reconnectInterval;
public final int destinationPort;
public final String destinationAddress;
private final int connectTimeout;
private final int readTimeout;
private final int writeTimeout;
private final int reconnectInterval;
private final int port;
private final String address;

public RelpConfig(Sourceable configSource) {
this.configSource = configSource;
this.connectionTimeout = getConnectTimeout();
this.readTimeout = getReadTimeout();
this.writeTimeout = getWriteTimeout();
this.reconnectInterval = getReconnectInterval();
this.destinationPort = getRelpPort();
this.destinationAddress = getRelpAddress();
public RelpConfig(final Sourceable configSource) {
this(
Integer.parseInt(configSource.source("relp.connection.timeout", "5000")),
Integer.parseInt(configSource.source("relp.transaction.read.timeout", "5000")),
Integer.parseInt(configSource.source("relp.transaction.write.timeout", "5000")),
Integer.parseInt(configSource.source("relp.connection.retry.interval", "5000")),
Integer.parseInt(configSource.source("relp.connection.port", "601")),
configSource.source("relp.connection.address", "localhost")
);
}

public RelpConfig(
final int connectTimeout,
final int readTimeout,
final int writeTimeout,
final int reconnectInt,
final int port,
final String addr
) {
this.connectTimeout = connectTimeout;
this.readTimeout = readTimeout;
this.writeTimeout = writeTimeout;
this.reconnectInterval = reconnectInt;
this.port = port;
this.address = addr;
}

/**
* @return relp.connection.timeout
*/
private int getConnectTimeout() {
String connectTimeout = configSource.source("relp.connection.timeout", "5000");
return Integer.parseInt(connectTimeout);
public int connectTimeout() {
return connectTimeout;
}

/**
* @return relp.transaction.read.timeout
*/
private int getReadTimeout() {
String rto = configSource.source("relp.transaction.read.timeout", "5000");
return Integer.parseInt(rto);
public int readTimeout() {
return readTimeout;
}

/**
* @return relp.transaction.write.timeout
*/
private int getWriteTimeout() {
String wto = configSource.source("relp.transaction.write.timeout", "5000");
return Integer.parseInt(wto);
public int writeTimeout() {
return writeTimeout;
}

/**
* @return relp.connection.retry.interval
*/
private int getReconnectInterval() {
String reconnectString = configSource.source("relp.connection.retry.interval", "5000");
return Integer.parseInt(reconnectString);
public int reconnectInterval() {
return reconnectInterval;
}

/**
* @return relp.connection.port
*/
private int getRelpPort() {
String relpPort = configSource.source("relp.connection.port", "601");
return Integer.parseInt(relpPort);
public int relpPort() {
return port;
}

/**
* @return relp.connection.address
*/
private String getRelpAddress() {
return configSource.source("relp.connection.address", "localhost");
public String relpAddress() {
return address;
}
}
28 changes: 16 additions & 12 deletions src/main/java/com/teragrep/aer_02/config/SyslogConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,32 @@

public final class SyslogConfig {

// TODO this is a copy from snw_01, with configSource
public final Sourceable configSource;
public final String hostname;
public final String appName;
private final String appName;
private final String hostName;

public SyslogConfig(Sourceable configSource) {
this.configSource = configSource;
this.hostname = getHostname();
this.appName = getAppName();
public SyslogConfig(final Sourceable configSource) {
this(
configSource.source("syslog.appname", "aer-02"),
configSource.source("syslog.hostname", "localhost.localdomain")
);
}

public SyslogConfig(final String appName, final String hostName) {
this.appName = appName;
this.hostName = hostName;
}

/**
* @return syslog.appname
*/
private String getAppName() {
return configSource.source("syslog.appname", "aer-02");
public String appName() {
return appName;
}

/**
* @return syslog.hostname
*/
private String getHostname() {
return configSource.source("syslog.hostname", "localhost.localdomain");
public String hostName() {
return hostName;
}
}
4 changes: 2 additions & 2 deletions src/test/java/com/teragrep/aer_02/ConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ public void testConfigFromProperty() {
String expected = "testing.hostname.example.com";
System.setProperty("syslog.hostname", expected);
SyslogConfig syslogConfig = new SyslogConfig(new PropertySource());
Assertions.assertEquals(expected, syslogConfig.hostname, "Expected to get config from property");
Assertions.assertEquals(expected, syslogConfig.hostName(), "Expected to get config from property");
System.clearProperty("syslog.hostname");
}

@Test
public void testConfigFallback() {
RelpConfig relpConfig = new RelpConfig(new EnvironmentSource());
Assertions.assertEquals(1601, relpConfig.destinationPort, "Expected to get fallback value");
Assertions.assertEquals(1601, relpConfig.relpPort(), "Expected to get fallback value");
}
}
4 changes: 3 additions & 1 deletion src/test/java/com/teragrep/aer_02/DefaultOutputTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@
import com.teragrep.rlo_14.Severity;
import com.teragrep.rlo_14.SyslogMessage;
import com.teragrep.rlp_01.RelpConnection;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;

import java.nio.charset.StandardCharsets;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
import com.codahale.metrics.MetricRegistry;
import com.teragrep.aer_02.config.source.PropertySource;
import com.teragrep.aer_02.config.source.Sourceable;
import com.teragrep.aer_02.fakes.*;
import com.teragrep.aer_02.fakes.OutputFake;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
Expand Down
5 changes: 4 additions & 1 deletion src/test/java/com/teragrep/aer_02/SyslogBridgeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@
import com.teragrep.rlp_03.frame.delegate.DefaultFrameDelegate;
import com.teragrep.rlp_03.frame.delegate.FrameContext;
import com.teragrep.rlp_03.frame.delegate.FrameDelegate;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
Expand Down

0 comments on commit 9034651

Please sign in to comment.