Skip to content

Commit

Permalink
Merge pull request #45690 from edeandrea/fix-lgtm-devservice-verbose-…
Browse files Browse the repository at this point in the history
…logging

Observability LGTM dev service filling up logs with services starting
  • Loading branch information
brunobat authored Jan 20, 2025
2 parents af4449f + 6957299 commit 34eb48e
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,6 +23,8 @@ public class JBossLoggingConsumer extends BaseConsumer<JBossLoggingConsumer> {

private String prefix = "";

private Predicate<OutputFrame> outputPredicate = f -> true;

public JBossLoggingConsumer(Logger logger) {
this(logger, false);
}
Expand Down Expand Up @@ -50,39 +54,48 @@ public JBossLoggingConsumer withSeparateOutputStreams() {
return this;
}

public JBossLoggingConsumer withLoggingFilter(Predicate<OutputFrame> 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<String, Object> 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<String, Object> 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);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -95,6 +97,11 @@ protected String prefix() {
return "LGTM";
}

@Override
protected Predicate<OutputFrame> getLoggingFilter() {
return new LgtmLoggingFilter();
}

public String getOtlpProtocol() {
return config.otlpProtocol();
}
Expand Down Expand Up @@ -143,4 +150,12 @@ public String otlpProtocol() {
return ContainerConstants.OTEL_HTTP_PROTOCOL;
}
}

protected static class LgtmLoggingFilter implements Predicate<OutputFrame> {
@Override
public boolean test(OutputFrame outputFrame) {
final var line = outputFrame.getUtf8StringWithoutLineEnding();
return !(line.startsWith("Waiting for") && line.endsWith("to start up..."));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -37,8 +38,14 @@ public ObservabilityContainer(C config) {

protected abstract String prefix();

protected Predicate<OutputFrame> getLoggingFilter() {
return f -> true;
}

protected Consumer<OutputFrame> frameConsumer() {
return new JBossLoggingConsumer(log).withPrefix(prefix());
return new JBossLoggingConsumer(log)
.withPrefix(prefix())
.withLoggingFilter(getLoggingFilter());
}

protected byte[] getResourceAsBytes(String resource) {
Expand Down

0 comments on commit 34eb48e

Please sign in to comment.