From 5dc9040dd18855c8b3ceb76b795b98bf858aeb30 Mon Sep 17 00:00:00 2001 From: Alex Wood Date: Thu, 26 Sep 2024 11:14:10 -0400 Subject: [PATCH] Add Splunk HEC error counter to all quarkus projects --- .gitignore | 2 + settings.gradle | 1 + swatch-billable-usage/build.gradle | 1 + swatch-common-splunk/build.gradle | 12 +++ .../splunk/CountingSplunkErrorCallback.java | 5 +- swatch-contracts/build.gradle | 1 + swatch-metrics/build.gradle | 1 + swatch-producer-aws/build.gradle | 3 +- .../aws/splunk/QuarkusHecErrorCallback.java | 90 ------------------- swatch-producer-azure/build.gradle | 1 + 10 files changed, 23 insertions(+), 94 deletions(-) create mode 100644 swatch-common-splunk/build.gradle rename {swatch-producer-aws/src/main/java/com/redhat/swatch/aws => swatch-common-splunk/src/main/java/com/redhat/swatch}/splunk/CountingSplunkErrorCallback.java (94%) delete mode 100644 swatch-producer-aws/src/main/java/com/redhat/swatch/aws/splunk/QuarkusHecErrorCallback.java diff --git a/.gitignore b/.gitignore index 49a4040659..a08418c472 100644 --- a/.gitignore +++ b/.gitignore @@ -42,6 +42,8 @@ config/export-service/tmp .settings /**/bin/* !bin/import-events.py +!bin/import-features.sh +!bin/liquibase-new-changeset.sh !bin/build-images.sh !bin/insert-mock-hosts .vscode diff --git a/settings.gradle b/settings.gradle index d51aebcf0e..19cfe026a6 100644 --- a/settings.gradle +++ b/settings.gradle @@ -50,6 +50,7 @@ include ':swatch-common-health' include ':swatch-common-resteasy-client' include ':swatch-common-panache' include ':swatch-common-smallrye-fault-tolerance' +include ':swatch-common-splunk' include ':swatch-common-platform-spring-boot' include ':swatch-common-platform-quarkus' include ':swatch-common-clock' diff --git a/swatch-billable-usage/build.gradle b/swatch-billable-usage/build.gradle index daba9babee..7ad1d0be5d 100644 --- a/swatch-billable-usage/build.gradle +++ b/swatch-billable-usage/build.gradle @@ -23,6 +23,7 @@ dependencies { implementation project(':swatch-common-kafka') implementation project(':swatch-common-smallrye-fault-tolerance') implementation project(':swatch-common-trace-response') + implementation project(':swatch-common-splunk') implementation libraries["clowder-quarkus-config-source"] implementation libraries["quarkus-logging-splunk"] implementation libraries["splunk-library-javalogging"] diff --git a/swatch-common-splunk/build.gradle b/swatch-common-splunk/build.gradle new file mode 100644 index 0000000000..57011ec43e --- /dev/null +++ b/swatch-common-splunk/build.gradle @@ -0,0 +1,12 @@ +plugins { + id 'swatch.java-library-conventions' +} + +dependencies { + implementation enforcedPlatform(libraries["quarkus-bom"]) + implementation "io.micrometer:micrometer-core" + implementation libraries["quarkus-logging-splunk"] + implementation libraries["splunk-library-javalogging"] +} + +description = 'Shared Splunk classes for swatch services' diff --git a/swatch-producer-aws/src/main/java/com/redhat/swatch/aws/splunk/CountingSplunkErrorCallback.java b/swatch-common-splunk/src/main/java/com/redhat/swatch/splunk/CountingSplunkErrorCallback.java similarity index 94% rename from swatch-producer-aws/src/main/java/com/redhat/swatch/aws/splunk/CountingSplunkErrorCallback.java rename to swatch-common-splunk/src/main/java/com/redhat/swatch/splunk/CountingSplunkErrorCallback.java index 61eaffb097..c65e676801 100644 --- a/swatch-producer-aws/src/main/java/com/redhat/swatch/aws/splunk/CountingSplunkErrorCallback.java +++ b/swatch-common-splunk/src/main/java/com/redhat/swatch/splunk/CountingSplunkErrorCallback.java @@ -18,13 +18,14 @@ * granted to use or replicate Red Hat trademarks that are incorporated * in this software or its documentation. */ -package com.redhat.swatch.aws.splunk; +package com.redhat.swatch.splunk; import com.splunk.logging.HttpEventCollectorErrorHandler; import com.splunk.logging.HttpEventCollectorErrorHandler.ErrorCallback; import com.splunk.logging.HttpEventCollectorEventInfo; import io.micrometer.core.instrument.Counter; import io.micrometer.core.instrument.MeterRegistry; +import io.quarkiverse.logging.splunk.SplunkErrorCallback; import io.quarkus.runtime.StartupEvent; import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.event.Observes; @@ -47,7 +48,7 @@ public CountingSplunkErrorCallback(MeterRegistry meterRegistry) { // prints the formatted error to stderr, so we'll delegate invocations of our ErrorCallback // to the default callback so that it can handle creating a human-readable record of the // issue. - this.delegatedCallback = new QuarkusHecErrorCallback(); + this.delegatedCallback = new SplunkErrorCallback(); this.failureCounter = meterRegistry.counter("splunk.hec.message.failure.total"); } diff --git a/swatch-contracts/build.gradle b/swatch-contracts/build.gradle index adc33868e3..7c8fc2066a 100644 --- a/swatch-contracts/build.gradle +++ b/swatch-contracts/build.gradle @@ -41,6 +41,7 @@ dependencies { implementation project(':swatch-common-resteasy') implementation project(':swatch-common-panache') implementation project(':swatch-common-smallrye-fault-tolerance') + implementation project(':swatch-common-splunk') implementation project(':clients:quarkus:rbac-client') implementation project(':clients:quarkus:product-client') implementation project(':swatch-product-configuration') diff --git a/swatch-metrics/build.gradle b/swatch-metrics/build.gradle index 1d7bdb2be2..ba41ee6cdf 100644 --- a/swatch-metrics/build.gradle +++ b/swatch-metrics/build.gradle @@ -21,6 +21,7 @@ dependencies { implementation project(':swatch-common-config-workaround') implementation project(':swatch-common-resteasy') implementation project(':swatch-common-clock') + implementation project(':swatch-common-splunk') implementation project(':swatch-model-events') implementation project(':clients:quarkus:prometheus-client') implementation project(':swatch-common-trace-response') diff --git a/swatch-producer-aws/build.gradle b/swatch-producer-aws/build.gradle index 988a8f8e60..9f075126a4 100644 --- a/swatch-producer-aws/build.gradle +++ b/swatch-producer-aws/build.gradle @@ -19,8 +19,6 @@ dependencies { implementation 'software.amazon.awssdk:marketplacemetering' implementation 'software.amazon.awssdk:sts' implementation libraries["clowder-quarkus-config-source"] - implementation libraries["quarkus-logging-splunk"] - implementation libraries["splunk-library-javalogging"] implementation project(":clients:quarkus:contracts-client") implementation project(":swatch-model-billable-usage") implementation project(":swatch-common-resteasy") @@ -29,6 +27,7 @@ dependencies { implementation project(":swatch-common-resteasy-client") implementation project(':swatch-common-trace-response') implementation project(':swatch-common-smallrye-fault-tolerance') + implementation project(':swatch-common-splunk') testImplementation 'io.rest-assured:rest-assured' testImplementation 'org.mockito:mockito-junit-jupiter' testImplementation 'org.testcontainers:junit-jupiter' diff --git a/swatch-producer-aws/src/main/java/com/redhat/swatch/aws/splunk/QuarkusHecErrorCallback.java b/swatch-producer-aws/src/main/java/com/redhat/swatch/aws/splunk/QuarkusHecErrorCallback.java deleted file mode 100644 index 2ae0f103fb..0000000000 --- a/swatch-producer-aws/src/main/java/com/redhat/swatch/aws/splunk/QuarkusHecErrorCallback.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright Red Hat, Inc. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - * Red Hat trademarks are not licensed under GPLv3. No permission is - * granted to use or replicate Red Hat trademarks that are incorporated - * in this software or its documentation. - */ -package com.redhat.swatch.aws.splunk; - -import com.splunk.logging.HttpEventCollectorErrorHandler.ErrorCallback; -import com.splunk.logging.HttpEventCollectorEventInfo; -import io.quarkus.bootstrap.logging.InitialConfigurator; -import java.io.PrintStream; -import java.io.PrintWriter; -import java.io.StringWriter; -import java.util.Arrays; -import java.util.List; -import java.util.logging.ConsoleHandler; -import java.util.logging.Handler; -import java.util.logging.Level; -import org.jboss.logmanager.ExtHandler; - -// This is a copy because quarkus's stuff is package protected. Delete once -// https://github.com/quarkiverse/quarkus-logging-splunk/pull/211 is merged -public class QuarkusHecErrorCallback implements ErrorCallback { - Boolean consoleEnabled; - - PrintStream stdout; - - PrintStream stderr; - - QuarkusHecErrorCallback() { - this(System.out, System.err); // NOSONAR - } - - /** For unit tests */ - QuarkusHecErrorCallback(PrintStream stdout, PrintStream stderr) { - this.stdout = stdout; - this.stderr = stderr; - } - - /** - * Logs the original event to stdout (if console handler is disabled). Logs the error to stderr. - */ - @Override - public void error(List list, Exception e) { - final StringWriter stringWriter = new StringWriter(); - stringWriter.append("Error while sending events to Splunk HEC: "); - stringWriter.append(e.getMessage()).append(System.lineSeparator()); - e.printStackTrace(new PrintWriter(stringWriter)); - this.stderr.println(stringWriter.toString()); - - if (!isConsoleHandlerEnabled()) { - for (HttpEventCollectorEventInfo logEvent : list) { - this.stdout.println(logEvent.getMessage()); - } - } - } - - /** - * This has to be determined lazily, as handlers are not yet registered when splunk is - * initialized. An alternative was to check for config "quarkus.log.console.enable", but adds a - * microprofile-config dependency. - */ - private boolean isConsoleHandlerEnabled() { - if (consoleEnabled == null) { - ExtHandler delayedHandler = InitialConfigurator.DELAYED_HANDLER; - Handler consoleHandler = - Arrays.stream(delayedHandler.getHandlers()) - .filter(ConsoleHandler.class::isInstance) - .findFirst() - .orElse(null); - consoleEnabled = (consoleHandler != null && !consoleHandler.getLevel().equals(Level.OFF)); - } - return consoleEnabled; - } -} diff --git a/swatch-producer-azure/build.gradle b/swatch-producer-azure/build.gradle index a791c6e85f..6a81da4b36 100644 --- a/swatch-producer-azure/build.gradle +++ b/swatch-producer-azure/build.gradle @@ -24,6 +24,7 @@ dependencies { implementation project(':swatch-product-configuration') implementation project(':swatch-common-kafka') implementation project(':swatch-common-resteasy-client') + implementation project(':swatch-common-splunk') implementation project(":clients:azure-marketplace-client") implementation project(':swatch-model-billable-usage') implementation project(':swatch-common-trace-response')