Skip to content

Commit

Permalink
Codeless EventDataConsumer ctor (#32)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
eemhu authored Nov 8, 2024
1 parent 9cae3b0 commit ad22238
Show file tree
Hide file tree
Showing 11 changed files with 607 additions and 55 deletions.
56 changes: 9 additions & 47 deletions src/main/java/com/teragrep/aer_02/EventDataConsumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,19 @@

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;
import com.teragrep.rlo_14.Facility;
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;

Expand All @@ -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(
Expand Down Expand Up @@ -198,7 +162,5 @@ public Long getValue() {
@Override
public void close() throws Exception {
output.close();
slf4jReporter.close();
jmxReporter.close();
}
}
92 changes: 92 additions & 0 deletions src/main/java/com/teragrep/aer_02/Hostname.java
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/teragrep/teragrep/blob/main/LICENSE>.
*
*
* 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);
}
}
28 changes: 25 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,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;
Expand All @@ -64,6 +69,8 @@
public class SyslogBridge {

private EventDataConsumer consumer = null;
private Report report = null;
private MetricRegistry metricRegistry = null;

@FunctionName("metrics")
public HttpResponseMessage metrics(
Expand Down Expand Up @@ -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++) {
Expand Down
76 changes: 76 additions & 0 deletions src/main/java/com/teragrep/aer_02/metrics/JmxReport.java
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/teragrep/teragrep/blob/main/LICENSE>.
*
*
* 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();
}
}
68 changes: 68 additions & 0 deletions src/main/java/com/teragrep/aer_02/metrics/PrometheusReport.java
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/teragrep/teragrep/blob/main/LICENSE>.
*
*
* 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);
}
}
Loading

0 comments on commit ad22238

Please sign in to comment.