From ad22238cc1d89042ce3ee91858aab8c725918820 Mon Sep 17 00:00:00 2001 From: eemhu <125959687+eemhu@users.noreply.github.com> Date: Fri, 8 Nov 2024 15:34:05 +0200 Subject: [PATCH] Codeless EventDataConsumer ctor (#32) * remove code from EventDataConsumer ctor, separate reporters to Report interface, separate getRealHostname into Hostname object * add http metrics integration test with syslogBridge * removed DummyReport usages as it was not really needed. --- .../teragrep/aer_02/EventDataConsumer.java | 56 ++------- .../java/com/teragrep/aer_02/Hostname.java | 92 ++++++++++++++ .../com/teragrep/aer_02/SyslogBridge.java | 28 ++++- .../teragrep/aer_02/metrics/JmxReport.java | 76 ++++++++++++ .../aer_02/metrics/PrometheusReport.java | 68 ++++++++++ .../com/teragrep/aer_02/metrics/Report.java | 51 ++++++++ .../teragrep/aer_02/metrics/Slf4jReport.java | 82 ++++++++++++ ...erTest.java => EventDataConsumerTest.java} | 9 +- .../com/teragrep/aer_02/HostnameTest.java | 75 +++++++++++ .../java/com/teragrep/aer_02/MetricsTest.java | 8 +- .../com/teragrep/aer_02/SyslogBridgeTest.java | 117 +++++++++++++++++- 11 files changed, 607 insertions(+), 55 deletions(-) create mode 100644 src/main/java/com/teragrep/aer_02/Hostname.java create mode 100644 src/main/java/com/teragrep/aer_02/metrics/JmxReport.java create mode 100644 src/main/java/com/teragrep/aer_02/metrics/PrometheusReport.java create mode 100644 src/main/java/com/teragrep/aer_02/metrics/Report.java create mode 100644 src/main/java/com/teragrep/aer_02/metrics/Slf4jReport.java rename src/test/java/com/teragrep/aer_02/{EventContextConsumerTest.java => EventDataConsumerTest.java} (95%) create mode 100644 src/test/java/com/teragrep/aer_02/HostnameTest.java diff --git a/src/main/java/com/teragrep/aer_02/EventDataConsumer.java b/src/main/java/com/teragrep/aer_02/EventDataConsumer.java index e07a6f5..e2d547d 100644 --- a/src/main/java/com/teragrep/aer_02/EventDataConsumer.java +++ b/src/main/java/com/teragrep/aer_02/EventDataConsumer.java @@ -47,8 +47,6 @@ 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; import com.teragrep.aer_02.config.source.Sourceable; @@ -56,18 +54,12 @@ import com.teragrep.rlo_14.SDElement; import com.teragrep.rlo_14.Severity; import com.teragrep.rlo_14.SyslogMessage; -import io.prometheus.client.CollectorRegistry; -import io.prometheus.client.dropwizard.DropwizardExports; -import org.slf4j.LoggerFactory; -import java.net.InetAddress; -import java.net.UnknownHostException; import java.nio.charset.StandardCharsets; import java.time.Instant; import java.time.ZonedDateTime; import java.util.Map; import java.util.UUID; -import java.util.concurrent.TimeUnit; import static com.codahale.metrics.MetricRegistry.name; @@ -78,54 +70,26 @@ final class EventDataConsumer implements AutoCloseable { private final String realHostName; private final SyslogConfig syslogConfig; private final MetricRegistry metricRegistry; - private final JmxReporter jmxReporter; - private final Slf4jReporter slf4jReporter; - EventDataConsumer(Sourceable configSource) { - this(configSource, new MetricRegistry()); - } - - EventDataConsumer(Sourceable configSource, MetricRegistry metricRegistry) { + EventDataConsumer(final Sourceable configSource, final String hostname, final MetricRegistry metricRegistry) { this( configSource, new DefaultOutput("defaultOutput", new RelpConfig(configSource), metricRegistry), + hostname, metricRegistry ); } - EventDataConsumer(Sourceable configSource, Output output, MetricRegistry metricRegistry) { + EventDataConsumer( + final Sourceable configSource, + final Output output, + final String hostname, + final MetricRegistry metricRegistry + ) { this.metricRegistry = metricRegistry; this.output = output; - this.realHostName = getRealHostName(); + this.realHostName = hostname; this.syslogConfig = new SyslogConfig(configSource); - - this.jmxReporter = JmxReporter.forRegistry(metricRegistry).build(); - this.slf4jReporter = Slf4jReporter - .forRegistry(metricRegistry) - .outputTo(LoggerFactory.getLogger(EventDataConsumer.class)) - .convertRatesTo(TimeUnit.SECONDS) - .convertDurationsTo(TimeUnit.MILLISECONDS) - .build(); - startMetrics(); - } - - private String getRealHostName() { - String hostname; - try { - hostname = InetAddress.getLocalHost().getHostName(); - } - catch (UnknownHostException e) { - hostname = "localhost"; - } - return hostname; - } - - private void startMetrics() { - this.jmxReporter.start(); - this.slf4jReporter.start(1, TimeUnit.MINUTES); - - // prometheus-exporter - CollectorRegistry.defaultRegistry.register(new DropwizardExports(metricRegistry)); } public void accept( @@ -198,7 +162,5 @@ public Long getValue() { @Override public void close() throws Exception { output.close(); - slf4jReporter.close(); - jmxReporter.close(); } } diff --git a/src/main/java/com/teragrep/aer_02/Hostname.java b/src/main/java/com/teragrep/aer_02/Hostname.java new file mode 100644 index 0000000..a389758 --- /dev/null +++ b/src/main/java/com/teragrep/aer_02/Hostname.java @@ -0,0 +1,92 @@ +/* + * 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; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.Objects; + +public final class Hostname { + + private final String defaultHostname; + private static final Logger LOGGER = LoggerFactory.getLogger(Hostname.class); + + public Hostname(final String defaultHostname) { + this.defaultHostname = defaultHostname; + } + + public String hostname() { + String rv; + try { + rv = InetAddress.getLocalHost().getHostName(); + } + catch (UnknownHostException e) { + rv = defaultHostname; + LOGGER.warn("Could not determine hostname, defaulting to <{}>", defaultHostname, e); + } + return rv; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Hostname hostname = (Hostname) o; + return Objects.equals(defaultHostname, hostname.defaultHostname); + } + + @Override + public int hashCode() { + return Objects.hashCode(defaultHostname); + } +} diff --git a/src/main/java/com/teragrep/aer_02/SyslogBridge.java b/src/main/java/com/teragrep/aer_02/SyslogBridge.java index 446dc63..0076ae8 100644 --- a/src/main/java/com/teragrep/aer_02/SyslogBridge.java +++ b/src/main/java/com/teragrep/aer_02/SyslogBridge.java @@ -45,11 +45,16 @@ */ package com.teragrep.aer_02; +import com.codahale.metrics.MetricRegistry; 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 com.teragrep.aer_02.metrics.JmxReport; +import com.teragrep.aer_02.metrics.PrometheusReport; +import com.teragrep.aer_02.metrics.Report; +import com.teragrep.aer_02.metrics.Slf4jReport; import io.prometheus.client.CollectorRegistry; +import io.prometheus.client.dropwizard.DropwizardExports; import io.prometheus.client.exporter.common.TextFormat; import java.io.IOException; @@ -64,6 +69,8 @@ public class SyslogBridge { private EventDataConsumer consumer = null; + private Report report = null; + private MetricRegistry metricRegistry = null; @FunctionName("metrics") public HttpResponseMessage metrics( @@ -113,9 +120,24 @@ public void eventHubTriggerToSyslog( context.getLogger().fine("eventHubTriggerToSyslog triggered"); context.getLogger().fine("Got events: " + events.length); + if (metricRegistry == null) { + metricRegistry = new MetricRegistry(); + } + + if (report == null) { + report = new JmxReport( + new Slf4jReport(new PrometheusReport(new DropwizardExports(metricRegistry)), metricRegistry), + metricRegistry + ); + report.start(); + } + if (consumer == null) { - final Sourceable configSource = new EnvironmentSource(); - consumer = new EventDataConsumer(configSource); + consumer = new EventDataConsumer( + new EnvironmentSource(), + new Hostname("localhost").hostname(), + metricRegistry + ); } for (int index = 0; index < events.length; index++) { diff --git a/src/main/java/com/teragrep/aer_02/metrics/JmxReport.java b/src/main/java/com/teragrep/aer_02/metrics/JmxReport.java new file mode 100644 index 0000000..4ddef54 --- /dev/null +++ b/src/main/java/com/teragrep/aer_02/metrics/JmxReport.java @@ -0,0 +1,76 @@ +/* + * 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.metrics; + +import com.codahale.metrics.MetricRegistry; +import com.codahale.metrics.jmx.JmxReporter; + +public final class JmxReport implements Report { + + private final Report report; + private final JmxReporter jmxReporter; + + public JmxReport(final Report report, final MetricRegistry metricRegistry) { + this(report, JmxReporter.forRegistry(metricRegistry).build()); + } + + public JmxReport(final Report report, final JmxReporter jmxReporter) { + this.report = report; + this.jmxReporter = jmxReporter; + } + + @Override + public void start() { + jmxReporter.start(); + report.start(); + } + + @Override + public void close() throws Exception { + report.close(); + jmxReporter.close(); + } +} diff --git a/src/main/java/com/teragrep/aer_02/metrics/PrometheusReport.java b/src/main/java/com/teragrep/aer_02/metrics/PrometheusReport.java new file mode 100644 index 0000000..9a3b531 --- /dev/null +++ b/src/main/java/com/teragrep/aer_02/metrics/PrometheusReport.java @@ -0,0 +1,68 @@ +/* + * 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.metrics; + +import io.prometheus.client.Collector; +import io.prometheus.client.CollectorRegistry; + +public final class PrometheusReport implements Report { + + private final Collector collector; + + public PrometheusReport(final Collector collector) { + this.collector = collector; + } + + @Override + public void start() { + CollectorRegistry.defaultRegistry.register(collector); + } + + @Override + public void close() { + CollectorRegistry.defaultRegistry.unregister(collector); + } +} diff --git a/src/main/java/com/teragrep/aer_02/metrics/Report.java b/src/main/java/com/teragrep/aer_02/metrics/Report.java new file mode 100644 index 0000000..5b21309 --- /dev/null +++ b/src/main/java/com/teragrep/aer_02/metrics/Report.java @@ -0,0 +1,51 @@ +/* + * 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.metrics; + +public interface Report extends AutoCloseable { + + public abstract void start(); +} diff --git a/src/main/java/com/teragrep/aer_02/metrics/Slf4jReport.java b/src/main/java/com/teragrep/aer_02/metrics/Slf4jReport.java new file mode 100644 index 0000000..12244ac --- /dev/null +++ b/src/main/java/com/teragrep/aer_02/metrics/Slf4jReport.java @@ -0,0 +1,82 @@ +/* + * 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.metrics; + +import com.codahale.metrics.MetricRegistry; +import com.codahale.metrics.Slf4jReporter; +import org.slf4j.LoggerFactory; + +import java.util.concurrent.TimeUnit; + +public final class Slf4jReport implements Report { + + private final Slf4jReporter slf4jReporter; + private final Report report; + + public Slf4jReport(final Report report, final MetricRegistry metricRegistry) { + this( + report, + Slf4jReporter.forRegistry(metricRegistry).outputTo(LoggerFactory.getLogger(Slf4jReport.class)).convertRatesTo(TimeUnit.SECONDS).convertDurationsTo(TimeUnit.MILLISECONDS).build() + ); + } + + public Slf4jReport(final Report report, final Slf4jReporter slf4jReporter) { + this.report = report; + this.slf4jReporter = slf4jReporter; + } + + @Override + public void start() { + slf4jReporter.start(1, TimeUnit.MINUTES); + report.start(); + } + + @Override + public void close() throws Exception { + report.close(); + slf4jReporter.close(); + } +} diff --git a/src/test/java/com/teragrep/aer_02/EventContextConsumerTest.java b/src/test/java/com/teragrep/aer_02/EventDataConsumerTest.java similarity index 95% rename from src/test/java/com/teragrep/aer_02/EventContextConsumerTest.java rename to src/test/java/com/teragrep/aer_02/EventDataConsumerTest.java index 72e23ea..8955e70 100644 --- a/src/test/java/com/teragrep/aer_02/EventContextConsumerTest.java +++ b/src/test/java/com/teragrep/aer_02/EventDataConsumerTest.java @@ -61,7 +61,7 @@ import static com.codahale.metrics.MetricRegistry.name; @TestInstance(TestInstance.Lifecycle.PER_CLASS) -public class EventContextConsumerTest { +public class EventDataConsumerTest { private final Sourceable configSource = new PropertySource(); @@ -76,7 +76,12 @@ public void testLatencyMetric() { Map systemProps = new HashMap<>(); systemProps.put("SequenceNumber", "1"); MetricRegistry metricRegistry = new MetricRegistry(); - EventDataConsumer eventDataConsumer = new EventDataConsumer(configSource, new OutputFake(), metricRegistry); + EventDataConsumer eventDataConsumer = new EventDataConsumer( + configSource, + new OutputFake(), + new Hostname("localhost").hostname(), + metricRegistry + ); final double records = 10; for (int i = 0; i < records; i++) { diff --git a/src/test/java/com/teragrep/aer_02/HostnameTest.java b/src/test/java/com/teragrep/aer_02/HostnameTest.java new file mode 100644 index 0000000..d01c67e --- /dev/null +++ b/src/test/java/com/teragrep/aer_02/HostnameTest.java @@ -0,0 +1,75 @@ +/* + * 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; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.net.InetAddress; + +public final class HostnameTest { + + @Test + void testHostname() { + final Hostname hostname = new Hostname("default"); + Assertions + .assertEquals(Assertions.assertDoesNotThrow(() -> InetAddress.getLocalHost().getHostName()), hostname.hostname()); + } + + @Test + void testEquals() { + final Hostname hostname1 = new Hostname("default"); + final Hostname hostname2 = new Hostname("default"); + Assertions.assertEquals(hostname1, hostname2); + } + + @Test + void testNotEquals() { + final Hostname hostname1 = new Hostname("default"); + final Hostname hostname2 = new Hostname("localhost"); + Assertions.assertNotEquals(hostname1, hostname2); + } +} diff --git a/src/test/java/com/teragrep/aer_02/MetricsTest.java b/src/test/java/com/teragrep/aer_02/MetricsTest.java index 57b5cb9..559f6c0 100644 --- a/src/test/java/com/teragrep/aer_02/MetricsTest.java +++ b/src/test/java/com/teragrep/aer_02/MetricsTest.java @@ -51,6 +51,7 @@ import com.teragrep.aer_02.fakes.HttpResponseMessageBuilderFake; import io.prometheus.client.CollectorRegistry; import io.prometheus.client.dropwizard.DropwizardExports; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -69,10 +70,15 @@ void setup() { registry.counter("test-counter").inc(123); } + @AfterEach + void teardown() { + CollectorRegistry.defaultRegistry.clear(); + } + @Test void testHttp() { SyslogBridge bridge = new SyslogBridge(); - HttpRequestMessage> req = new HttpRequestMessage>() { + HttpRequestMessage> req = new HttpRequestMessage<>() { @Override public URI getUri() { diff --git a/src/test/java/com/teragrep/aer_02/SyslogBridgeTest.java b/src/test/java/com/teragrep/aer_02/SyslogBridgeTest.java index 1d6edfe..25ecd9e 100644 --- a/src/test/java/com/teragrep/aer_02/SyslogBridgeTest.java +++ b/src/test/java/com/teragrep/aer_02/SyslogBridgeTest.java @@ -45,7 +45,9 @@ */ package com.teragrep.aer_02; +import com.microsoft.azure.functions.*; import com.teragrep.aer_02.fakes.ExecutionContextFake; +import com.teragrep.aer_02.fakes.HttpResponseMessageBuilderFake; import com.teragrep.aer_02.fakes.PartitionContextFake; import com.teragrep.aer_02.fakes.SystemPropsFake; import com.teragrep.net_01.channel.socket.PlainFactory; @@ -64,6 +66,7 @@ import org.junit.jupiter.api.Test; import java.io.ByteArrayInputStream; +import java.net.URI; import java.nio.charset.StandardCharsets; import java.util.*; import java.util.concurrent.ExecutorService; @@ -84,8 +87,9 @@ public final class SyslogBridgeTest { @BeforeEach void setup() { + messages.clear(); this.executorService = Executors.newFixedThreadPool(1); - Consumer syslogConsumer = new Consumer() { + Consumer syslogConsumer = new Consumer<>() { @Override public synchronized void accept(FrameContext frameContext) { @@ -116,7 +120,6 @@ void teardown() { Assertions.assertDoesNotThrow(() -> eventLoopThread.join()); executorService.shutdown(); Assertions.assertDoesNotThrow(() -> server.close()); - messages.clear(); } @Test @@ -150,4 +153,114 @@ void testSyslogBridge() { Assertions.assertEquals(3, loops); } + + @Test + void testSyslogBridgeMetrics() { + PartitionContextFake pcf = new PartitionContextFake("eventhub.123", "test1", "$Default", "0"); + Map props = new HashMap<>(); + final SyslogBridge bridge = new SyslogBridge(); + + bridge.eventHubTriggerToSyslog(new String[] { + "event0", "event1", "event2" + }, pcf.asMap(), new Map[] { + props, props, props + }, new Map[] { + new SystemPropsFake("0").asMap(), new SystemPropsFake("1").asMap(), new SystemPropsFake("2").asMap() + }, Arrays.asList("2010-01-01T00:00:00", "2010-01-02T00:00:00", "2010-01-03T00:00:00"), + Arrays.asList("0", "1", "2"), new ExecutionContextFake() + ); + + Assertions.assertEquals(3, messages.size()); + + HttpRequestMessage> req = new HttpRequestMessage<>() { + + @Override + public URI getUri() { + return null; + } + + @Override + public HttpMethod getHttpMethod() { + return HttpMethod.GET; + } + + @Override + public Map getHeaders() { + Map h = new HashMap<>(); + h.put("Accept", "text/plain"); + return h; + } + + @Override + public Map getQueryParameters() { + Map q = new HashMap<>(); + return q; + } + + @Override + public Optional getBody() { + return Optional.empty(); + } + + @Override + public HttpResponseMessage.Builder createResponseBuilder(HttpStatus httpStatus) { + return new HttpResponseMessageBuilderFake(); + } + + @Override + public HttpResponseMessage.Builder createResponseBuilder(HttpStatusType httpStatusType) { + return new HttpResponseMessageBuilderFake(); + } + }; + + HttpResponseMessage resp = bridge.metrics(req, new ExecutionContextFake()); + List responseLines = Arrays.asList(resp.getBody().toString().split("\n")); + + Assertions.assertEquals(36, responseLines.size()); + // Check all metrics are present + Assertions + .assertTrue( + responseLines + .contains("# TYPE com_teragrep_aer_02_DefaultOutput___defaultOutput___resends gauge") + ); + Assertions + .assertTrue( + responseLines + .contains( + "# TYPE com_teragrep_aer_02_DefaultOutput___defaultOutput___connectLatency summary" + ) + ); + Assertions + .assertTrue( + responseLines + .contains( + "# TYPE com_teragrep_aer_02_DefaultOutput___defaultOutput___retriedConnects gauge" + ) + ); + Assertions + .assertTrue( + responseLines + .contains( + "# TYPE com_teragrep_aer_02_DefaultOutput___defaultOutput___sendLatency summary" + ) + ); + Assertions + .assertTrue( + responseLines + .contains("# TYPE com_teragrep_aer_02_DefaultOutput___defaultOutput___records gauge") + ); + Assertions + .assertTrue( + responseLines.contains("# TYPE com_teragrep_aer_02_DefaultOutput___defaultOutput___bytes gauge") + ); + Assertions + .assertTrue( + responseLines + .contains("# TYPE com_teragrep_aer_02_DefaultOutput___defaultOutput___connects gauge") + ); + Assertions + .assertTrue( + responseLines.contains("# TYPE com_teragrep_aer_02_EventDataConsumer_latency_seconds_0 gauge") + ); + } }