Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance bootup logging to include detailed rule information #1333

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
6 changes: 4 additions & 2 deletions core/src/main/java/com/predic8/membrane/core/Router.java
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,9 @@ public void start() {
synchronized (lock) {
running = true;
}
log.info(PRODUCT_NAME + " " + VERSION + " up and running!");

RuleDisplayInfo.logInfosAboutStartedProxies(ruleManager);
log.info(PRODUCT_NAME + " {} up and running!", VERSION);
}

private void startJmx() {
Expand Down Expand Up @@ -390,7 +392,7 @@ public void run() {
public void tryReinitialization() {
boolean stillFailing = false;
ArrayList<Rule> inactive = getInactiveRules();
if (inactive.size() > 0) {
if (!inactive.isEmpty()) {
log.info("Trying to activate all inactive rules.");
for (Rule rule : inactive) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@
import com.predic8.membrane.core.transport.ssl.SSLContext;
import com.predic8.membrane.core.transport.ssl.SSLProvider;
import com.predic8.membrane.core.transport.ssl.StaticSSLContext;
import org.apache.commons.lang3.StringUtils;

@MCElement(name="internalProxy")
public class InternalProxy extends AbstractProxy{
private AbstractServiceProxy.Target target = new AbstractServiceProxy.Target();
private SSLContext sslOutboundContext;

public InternalProxy() {
key = new AbstractRuleKey(-1,null){

key = new AbstractRuleKey(-1,null) {
@Override
public String toString() {
return StringUtils.defaultIfEmpty(getPath(), "") + ":" + port;
}
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package com.predic8.membrane.core.rules;

import com.predic8.membrane.core.*;
import com.predic8.membrane.core.openapi.serviceproxy.*;
import org.jetbrains.annotations.*;
import org.slf4j.*;

import java.util.*;

import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.joining;

/**
* Provides infos about a rule e.g. for startup infos
*/
public class RuleDisplayInfo {

private static final Logger log = LoggerFactory.getLogger(RuleDisplayInfo.class.getName());

public static void logInfosAboutStartedProxies(RuleManager manager) {
log.info("Started {} API{}:", manager.getRules().size(), (manager.getRules().size() > 1 ? "s" : ""));
manager.getRules().forEach(rule ->
log.info(" {} {}{}{}", ruleDisplayName(rule), ruleCustomName(rule), getRuleKeyDisplayName(rule), additionalRuleDisplayName(rule))
);
}

private static String getRuleKeyDisplayName(Rule rule) {
return String.format("%s:%d%s",
getHost(rule),
rule.getKey().getPort(),
getPath(rule));
}

private static @NotNull String getPath(Rule rule) {
String path = rule.getKey().getPath();
return path != null ? path : "";
}

private static @Nullable String getHost(Rule rule) {
String host = rule.getKey().getHost();
return Objects.equals(host, "*") ? getIP(rule) : host;
}

private static @NotNull String getIP(Rule rule) {
String ip = rule.getKey().getIp();
if (ip == null) {
return "0.0.0.0";
}
return ip;
}

private static String additionalRuleDisplayName(Rule rule) {
if (rule instanceof APIProxy a) {
Map<String,OpenAPIRecord> recs = a.getApiRecords();
if (!recs.isEmpty()) {
return " using OpenAPI " + formatLocationInfo(recs);
}
} else if (rule instanceof SOAPProxy s) {
return " using WSDL @ " + s.getWsdl();
}
return "";
}

private static String formatLocationInfo(Map<String, OpenAPIRecord> specs) {
return getSpecsByDir(specs).entrySet().stream()
.map(e -> formatDirGroup(e.getKey(), e.getValue()))
.collect(joining("\n"));
}

private static @NotNull Map<String, @NotNull List<Map.Entry<String, OpenAPIRecord>>> getSpecsByDir(Map<String, OpenAPIRecord> specs) {
return specs.entrySet().stream().collect(groupingBy(e ->
Optional.ofNullable(e.getValue().getSpec().getDir()).orElse("")
));
}

private static String formatDirGroup(String dir, List<Map.Entry<String, OpenAPIRecord>> entries) {
var specsInfo = entries.stream()
.map(e -> "\"%s\" @ %s".formatted(e.getKey(), e.getValue().getSpec().getLocation()))
.collect(joining("\n" + " ".repeat(67)));

return dir.isEmpty() ? specsInfo : ("Directory \"%s\":\n" + " ".repeat(67) + "%s").formatted(dir, specsInfo);
}

private static String ruleCustomName(Rule rule) {
if (Objects.equals(rule.getName(), rule.getKey().toString())) {
return "";
}
return "\"%s\" ".formatted(rule.getName());
}

private static String ruleDisplayName(Rule rule) {
if (rule instanceof APIProxy) {
return "API";
} else if (rule instanceof ServiceProxy) {
return "ServiceProxy";
} else if (rule instanceof SOAPProxy) {
return "SOAPProxy";
} else if (rule instanceof InternalProxy) {
return "InternalProxy";
}
return "Proxy";
}
}
25 changes: 24 additions & 1 deletion distribution/conf/proxies.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,36 @@

<router>

<api port="2000">
<api port="2000" name="hello">
<path>/shop/v2</path>
<target host="api.predic8.de">
<ssl/>
</target>
</api>

<api port="2000" name="hello2">
<openapi location="https://api.predic8.de/shop/v2/api-docs" />
</api>

<api port="2000" name="hello3">
<openapi dir="." />
</api>

<serviceProxy port="3000">
<target host="api.predic8.de">
<ssl/>
</target>
</serviceProxy>

<soapProxy port="4000" wsdl="https://www.predic8.de/city-service?wsdl">
</soapProxy>

<internalProxy name="wow">
<target host="api.predic8.de">
<ssl/>
</target>
</internalProxy>

</router>

</spring:beans>
Loading