Skip to content

Commit

Permalink
Merge pull request #57 from facebookincubator/hunter/webhook_logger
Browse files Browse the repository at this point in the history
add optional logging for all incoming webhooks
  • Loading branch information
hunterjackson authored Jul 11, 2024
2 parents 7a90bfa + 3a38470 commit be2ea48
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
18 changes: 18 additions & 0 deletions src/main/java/com/meta/cp4m/ServicesRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class ServicesRunner implements AutoCloseable {
private final Javalin app = Javalin.create();
private final Set<Service<?>> services = new LinkedHashSet<>();

private boolean logWebhooks = false;
private String heartbeatPath = "/heartbeat";
private boolean started = false;
private int port = 8080;
Expand Down Expand Up @@ -74,6 +75,17 @@ private void routeSelectorAndHandler(Context ctx, List<Route<?>> routes) {
return this;
}

if (logWebhooks) {
app.before(
ctx ->
LOGGER
.atInfo()
.addKeyValue("headers", ctx.headerMap())
.addKeyValue("body", ctx.body())
.addKeyValue("path", ctx.path())
.addKeyValue("request_type", ctx.handlerType())
.log("received webhook"));
}
app.addHttpHandler(HandlerType.GET, heartbeatPath, ctx -> {});
record RouteGroup(String path, HandlerType handlerType) {}
Map<RouteGroup, List<Route<?>>> routeGroups = new HashMap<>();
Expand Down Expand Up @@ -140,4 +152,10 @@ public int port() {
public void close() {
app.stop();
}

public @This ServicesRunner logWebhooks(boolean logWebhooks) {
Preconditions.checkState(!started, "cannot adjust logging, server already started");
this.logWebhooks = logWebhooks;
return this;
}
}
13 changes: 10 additions & 3 deletions src/main/java/com/meta/cp4m/configuration/RootConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class RootConfiguration {

private final int port;
private final String heartbeatPath;
private final boolean logWebhooks;

@JsonCreator
RootConfiguration(
Expand All @@ -43,9 +44,11 @@ public class RootConfiguration {
@JsonProperty("pre_processors") Collection<PreProcessorConfig> preProcessors,
@JsonProperty("services") Collection<ServiceConfiguration> services,
@JsonProperty("port") @Nullable Integer port,
@JsonProperty("heartbeat_path") @Nullable String heartbeatPath) {
@JsonProperty("heartbeat_path") @Nullable String heartbeatPath,
@JsonProperty("log_webhooks") @Nullable Boolean logWebhooks) {
this.port = port == null ? 8080 : port;
this.heartbeatPath = heartbeatPath == null ? "/heartbeat" : heartbeatPath;
this.logWebhooks = Objects.requireNonNullElse(logWebhooks, false);
stores = stores == null ? Collections.emptyList() : stores;
preProcessors = preProcessors == null ? Collections.emptyList() : preProcessors;
Preconditions.checkArgument(
Expand Down Expand Up @@ -155,7 +158,7 @@ private <T extends Message> Service<T> createService(

List<String> preProcessorNames = serviceConfig.preProcessors();
for (String i : preProcessorNames) {
// guaranteed to return a non null value due to check in constructor
// guaranteed to return a non-null value due to check in constructor
preProcessor = preProcessors.get(i).toPreProcessor();
preProcessorsList.add(preProcessor);
}
Expand All @@ -164,7 +167,11 @@ private <T extends Message> Service<T> createService(
}

public ServicesRunner toServicesRunner() {
ServicesRunner runner = ServicesRunner.newInstance().port(port).heartbeatPath(heartbeatPath);
ServicesRunner runner =
ServicesRunner.newInstance()
.port(port)
.heartbeatPath(heartbeatPath)
.logWebhooks(logWebhooks);
for (ServiceConfiguration service : services) {
MessageHandler<?> handler = handlers.get(service.handler()).toMessageHandler();
runner.service(createService(handler, service));
Expand Down

0 comments on commit be2ea48

Please sign in to comment.