From 9034651f6e0d729274707c7a713d9e340556640e Mon Sep 17 00:00:00 2001 From: eemhu <125959687+eemhu@users.noreply.github.com> Date: Fri, 8 Nov 2024 12:08:25 +0200 Subject: [PATCH] remove unused metricsConfig; refactor config objects to use private fields and be immutable (#31) --- .../com/teragrep/aer_02/DefaultOutput.java | 12 +-- .../teragrep/aer_02/EventDataConsumer.java | 8 +- .../com/teragrep/aer_02/SyslogBridge.java | 11 ++- .../teragrep/aer_02/config/MetricsConfig.java | 60 --------------- .../teragrep/aer_02/config/RelpConfig.java | 76 +++++++++++-------- .../teragrep/aer_02/config/SyslogConfig.java | 28 ++++--- .../java/com/teragrep/aer_02/ConfigTest.java | 4 +- .../teragrep/aer_02/DefaultOutputTest.java | 4 +- .../aer_02/EventContextConsumerTest.java | 2 +- .../com/teragrep/aer_02/SyslogBridgeTest.java | 5 +- 10 files changed, 88 insertions(+), 122 deletions(-) delete mode 100644 src/main/java/com/teragrep/aer_02/config/MetricsConfig.java diff --git a/src/main/java/com/teragrep/aer_02/DefaultOutput.java b/src/main/java/com/teragrep/aer_02/DefaultOutput.java index 79a8d53..e40ae7a 100644 --- a/src/main/java/com/teragrep/aer_02/DefaultOutput.java +++ b/src/main/java/com/teragrep/aer_02/DefaultOutput.java @@ -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")); diff --git a/src/main/java/com/teragrep/aer_02/EventDataConsumer.java b/src/main/java/com/teragrep/aer_02/EventDataConsumer.java index 34fba35..e07a6f5 100644 --- a/src/main/java/com/teragrep/aer_02/EventDataConsumer.java +++ b/src/main/java/com/teragrep/aer_02/EventDataConsumer.java @@ -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; @@ -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) diff --git a/src/main/java/com/teragrep/aer_02/SyslogBridge.java b/src/main/java/com/teragrep/aer_02/SyslogBridge.java index 3f2bfb2..446dc63 100644 --- a/src/main/java/com/teragrep/aer_02/SyslogBridge.java +++ b/src/main/java/com/teragrep/aer_02/SyslogBridge.java @@ -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 { diff --git a/src/main/java/com/teragrep/aer_02/config/MetricsConfig.java b/src/main/java/com/teragrep/aer_02/config/MetricsConfig.java deleted file mode 100644 index 94a7473..0000000 --- a/src/main/java/com/teragrep/aer_02/config/MetricsConfig.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Teragrep Eventhub Reader as an Azure Function - * Copyright (C) 2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.aer_02.config; - -import com.teragrep.aer_02.config.source.Sourceable; - -public class MetricsConfig { - - public final Sourceable configSource; - public final int prometheusPort; - - public MetricsConfig(Sourceable configSource) { - this.configSource = configSource; - this.prometheusPort = Integer.parseInt(configSource.source("metrics.prometheusPort", "1234")); - } - -} diff --git a/src/main/java/com/teragrep/aer_02/config/RelpConfig.java b/src/main/java/com/teragrep/aer_02/config/RelpConfig.java index 54cc65c..e80b11d 100644 --- a/src/main/java/com/teragrep/aer_02/config/RelpConfig.java +++ b/src/main/java/com/teragrep/aer_02/config/RelpConfig.java @@ -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; } } diff --git a/src/main/java/com/teragrep/aer_02/config/SyslogConfig.java b/src/main/java/com/teragrep/aer_02/config/SyslogConfig.java index a2bac7a..cf5367d 100644 --- a/src/main/java/com/teragrep/aer_02/config/SyslogConfig.java +++ b/src/main/java/com/teragrep/aer_02/config/SyslogConfig.java @@ -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; } } diff --git a/src/test/java/com/teragrep/aer_02/ConfigTest.java b/src/test/java/com/teragrep/aer_02/ConfigTest.java index 7311a2c..6ad5a5c 100644 --- a/src/test/java/com/teragrep/aer_02/ConfigTest.java +++ b/src/test/java/com/teragrep/aer_02/ConfigTest.java @@ -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"); } } diff --git a/src/test/java/com/teragrep/aer_02/DefaultOutputTest.java b/src/test/java/com/teragrep/aer_02/DefaultOutputTest.java index 0d92526..1a4200d 100644 --- a/src/test/java/com/teragrep/aer_02/DefaultOutputTest.java +++ b/src/test/java/com/teragrep/aer_02/DefaultOutputTest.java @@ -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; diff --git a/src/test/java/com/teragrep/aer_02/EventContextConsumerTest.java b/src/test/java/com/teragrep/aer_02/EventContextConsumerTest.java index 0bc2e2e..72e23ea 100644 --- a/src/test/java/com/teragrep/aer_02/EventContextConsumerTest.java +++ b/src/test/java/com/teragrep/aer_02/EventContextConsumerTest.java @@ -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; diff --git a/src/test/java/com/teragrep/aer_02/SyslogBridgeTest.java b/src/test/java/com/teragrep/aer_02/SyslogBridgeTest.java index 0007465..1d6edfe 100644 --- a/src/test/java/com/teragrep/aer_02/SyslogBridgeTest.java +++ b/src/test/java/com/teragrep/aer_02/SyslogBridgeTest.java @@ -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;