From 6957299cf3625163e3f170fdfcbb05cda20f380c Mon Sep 17 00:00:00 2001 From: Eric Deandrea Date: Fri, 17 Jan 2025 16:43:34 -0500 Subject: [PATCH] Observability LGTM dev service filling up logs with services starting --- .../common/JBossLoggingConsumer.java | 73 +++++++++++-------- .../testcontainers/GrafanaContainer.java | 18 +++-- .../testcontainers/LgtmContainer.java | 15 ++++ .../ObservabilityContainer.java | 9 ++- 4 files changed, 78 insertions(+), 37 deletions(-) diff --git a/extensions/devservices/common/src/main/java/io/quarkus/devservices/common/JBossLoggingConsumer.java b/extensions/devservices/common/src/main/java/io/quarkus/devservices/common/JBossLoggingConsumer.java index e578fd8106c5b..992916b3e619e 100644 --- a/extensions/devservices/common/src/main/java/io/quarkus/devservices/common/JBossLoggingConsumer.java +++ b/extensions/devservices/common/src/main/java/io/quarkus/devservices/common/JBossLoggingConsumer.java @@ -2,6 +2,8 @@ import java.util.HashMap; import java.util.Map; +import java.util.Objects; +import java.util.function.Predicate; import org.jboss.logging.Logger; import org.jboss.logging.MDC; @@ -21,6 +23,8 @@ public class JBossLoggingConsumer extends BaseConsumer { private String prefix = ""; + private Predicate outputPredicate = f -> true; + public JBossLoggingConsumer(Logger logger) { this(logger, false); } @@ -50,39 +54,48 @@ public JBossLoggingConsumer withSeparateOutputStreams() { return this; } + public JBossLoggingConsumer withLoggingFilter(Predicate outputPredicate) { + Objects.requireNonNull(outputPredicate); + this.outputPredicate = outputPredicate; + return this; + } + @Override public void accept(OutputFrame outputFrame) { - final OutputFrame.OutputType outputType = outputFrame.getType(); - final String utf8String = outputFrame.getUtf8StringWithoutLineEnding(); - - final Map originalMdc = MDC.getMap(); - MDC.clear(); - MDC.getMap().putAll(mdc); - try { - switch (outputType) { - case END: - break; - case STDOUT: - if (separateOutputStreams) { - logger.infof("%s%s", prefix.isEmpty() ? "" : (prefix + ": "), utf8String); - } else { - logger.infof("%s%s: %s", prefix, outputType, utf8String); - } - break; - case STDERR: - if (separateOutputStreams) { - logger.errorf("%s%s", prefix.isEmpty() ? "" : (prefix + ": "), utf8String); - } else { - logger.infof("%s%s: %s", prefix, outputType, utf8String); - } - break; - default: - throw new IllegalArgumentException("Unexpected outputType " + outputType); - } - } finally { + if (this.outputPredicate.test(outputFrame)) { + final OutputFrame.OutputType outputType = outputFrame.getType(); + final String utf8String = outputFrame.getUtf8StringWithoutLineEnding(); + + final Map originalMdc = MDC.getMap(); MDC.clear(); - if (originalMdc != null) { - MDC.getMap().putAll(originalMdc); + MDC.getMap().putAll(mdc); + + try { + switch (outputType) { + case END: + break; + case STDOUT: + if (separateOutputStreams) { + logger.infof("%s%s", prefix.isEmpty() ? "" : (prefix + ": "), utf8String); + } else { + logger.infof("%s%s: %s", prefix, outputType, utf8String); + } + break; + case STDERR: + if (separateOutputStreams) { + logger.errorf("%s%s", prefix.isEmpty() ? "" : (prefix + ": "), utf8String); + } else { + logger.infof("%s%s: %s", prefix, outputType, utf8String); + } + break; + default: + throw new IllegalArgumentException("Unexpected outputType " + outputType); + } + } finally { + MDC.clear(); + if (originalMdc != null) { + MDC.getMap().putAll(originalMdc); + } } } } diff --git a/extensions/observability-devservices/testcontainers/src/main/java/io/quarkus/observability/testcontainers/GrafanaContainer.java b/extensions/observability-devservices/testcontainers/src/main/java/io/quarkus/observability/testcontainers/GrafanaContainer.java index 0218c6911b3cf..e7b101500af87 100644 --- a/extensions/observability-devservices/testcontainers/src/main/java/io/quarkus/observability/testcontainers/GrafanaContainer.java +++ b/extensions/observability-devservices/testcontainers/src/main/java/io/quarkus/observability/testcontainers/GrafanaContainer.java @@ -1,6 +1,7 @@ package io.quarkus.observability.testcontainers; -import org.testcontainers.containers.wait.strategy.HttpWaitStrategy; +import org.testcontainers.containers.wait.strategy.Wait; +import org.testcontainers.containers.wait.strategy.WaitAllStrategy; import org.testcontainers.containers.wait.strategy.WaitStrategy; import io.quarkus.observability.common.config.GrafanaConfig; @@ -26,10 +27,15 @@ public int getGrafanaPort() { } private WaitStrategy grafanaWaitStrategy() { - return new HttpWaitStrategy() - .forPath("/") - .forPort(config.grafanaPort()) - .forStatusCode(200) - .withStartupTimeout(config.timeout()); + return new WaitAllStrategy() + .withStartupTimeout(config.timeout()) + .withStrategy( + Wait.forHttp("/") + .forPort(config.grafanaPort()) + .forStatusCode(200) + .withStartupTimeout(config.timeout())) + .withStrategy( + Wait.forLogMessage(".*The OpenTelemetry collector and the Grafana LGTM stack are up and running.*", 1) + .withStartupTimeout(config.timeout())); } } diff --git a/extensions/observability-devservices/testcontainers/src/main/java/io/quarkus/observability/testcontainers/LgtmContainer.java b/extensions/observability-devservices/testcontainers/src/main/java/io/quarkus/observability/testcontainers/LgtmContainer.java index 1f9fdc301c3a0..6541307bc292e 100644 --- a/extensions/observability-devservices/testcontainers/src/main/java/io/quarkus/observability/testcontainers/LgtmContainer.java +++ b/extensions/observability-devservices/testcontainers/src/main/java/io/quarkus/observability/testcontainers/LgtmContainer.java @@ -2,9 +2,11 @@ import java.util.Optional; import java.util.Set; +import java.util.function.Predicate; import org.eclipse.microprofile.config.Config; import org.eclipse.microprofile.config.ConfigProvider; +import org.testcontainers.containers.output.OutputFrame; import org.testcontainers.utility.MountableFile; import io.quarkus.observability.common.ContainerConstants; @@ -95,6 +97,11 @@ protected String prefix() { return "LGTM"; } + @Override + protected Predicate getLoggingFilter() { + return new LgtmLoggingFilter(); + } + public String getOtlpProtocol() { return config.otlpProtocol(); } @@ -143,4 +150,12 @@ public String otlpProtocol() { return ContainerConstants.OTEL_HTTP_PROTOCOL; } } + + protected static class LgtmLoggingFilter implements Predicate { + @Override + public boolean test(OutputFrame outputFrame) { + final var line = outputFrame.getUtf8StringWithoutLineEnding(); + return !(line.startsWith("Waiting for") && line.endsWith("to start up...")); + } + } } diff --git a/extensions/observability-devservices/testcontainers/src/main/java/io/quarkus/observability/testcontainers/ObservabilityContainer.java b/extensions/observability-devservices/testcontainers/src/main/java/io/quarkus/observability/testcontainers/ObservabilityContainer.java index a3e6c6610890f..8714c8c55bfc3 100644 --- a/extensions/observability-devservices/testcontainers/src/main/java/io/quarkus/observability/testcontainers/ObservabilityContainer.java +++ b/extensions/observability-devservices/testcontainers/src/main/java/io/quarkus/observability/testcontainers/ObservabilityContainer.java @@ -7,6 +7,7 @@ import java.util.Optional; import java.util.Set; import java.util.function.Consumer; +import java.util.function.Predicate; import org.jboss.logging.Logger; import org.testcontainers.containers.GenericContainer; @@ -37,8 +38,14 @@ public ObservabilityContainer(C config) { protected abstract String prefix(); + protected Predicate getLoggingFilter() { + return f -> true; + } + protected Consumer frameConsumer() { - return new JBossLoggingConsumer(log).withPrefix(prefix()); + return new JBossLoggingConsumer(log) + .withPrefix(prefix()) + .withLoggingFilter(getLoggingFilter()); } protected byte[] getResourceAsBytes(String resource) {