Skip to content

Commit

Permalink
Update RequestCapturingHandler.java
Browse files Browse the repository at this point in the history
  • Loading branch information
clarkxuyang committed Nov 26, 2024
1 parent 3a7056c commit 63ab0a8
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/main/java/com/uid2/shared/vertx/RequestCapturingHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.uid2.shared.auth.IAuthorizable;
import com.uid2.shared.jmx.AdminApi;
import com.uid2.shared.middleware.AuthMiddleware;
import com.uid2.shared.model.Site;
import com.uid2.shared.store.ISiteStore;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Metrics;
import io.vertx.core.Handler;
Expand Down Expand Up @@ -32,11 +34,17 @@ public class RequestCapturingHandler implements Handler<RoutingContext> {
private Queue<String> _capturedRequests = null;
private final Map<String, Counter> _apiMetricCounters = new HashMap<>();
private final Map<String, Counter> _clientAppVersionCounters = new HashMap<>();
private final ISiteStore siteStore;

private static String formatRFC1123DateTime(long time) {
return DateTimeFormatter.RFC_1123_DATE_TIME.format(Instant.ofEpochMilli(time).atZone(ZONE_GMT));
}

public RequestCapturingHandler(ISiteStore siteStore)
{
this.siteStore = siteStore;
}

@Override
public void handle(RoutingContext context) {
if (!AdminApi.instance.getCaptureRequests() && !AdminApi.instance.getPublishApiMetrics()) {
Expand Down Expand Up @@ -105,7 +113,17 @@ private void capture(RoutingContext context, long timestamp, String remoteClient
}

final Integer siteId = getSiteId(context);
incrementMetricCounter(apiContact, siteId, host, status, method, path);

String siteName = "unknown";
if (siteId != null) {
Site site = siteStore.getSite(siteId);
if (site != null)
{
siteName = site.getName();
}
}

incrementMetricCounter(apiContact, siteId, siteName, host, status, method, path);

if (request.headers().contains(Const.Http.AppVersionHeader)) {
incrementAppVersionCounter(apiContact, request.headers().get(Const.Http.AppVersionHeader));
Expand Down Expand Up @@ -196,14 +214,16 @@ private static Integer getSiteId(RoutingContext context) {
return null;
}

private void incrementMetricCounter(String apiContact, Integer siteId, String host, int status, HttpMethod method, String path) {
private void incrementMetricCounter(String apiContact, Integer siteId, String siteName,
String host, int status, HttpMethod method, String path) {
assert apiContact != null;
String key = apiContact + "|" + siteId + "|" + host + "|" + status + "|" + method.name() + "|" + path;
String key = apiContact + "|" + siteId + "|" + siteName + "|" + host + "|" + status + "|" + method.name() + "|" + path;
if (!_apiMetricCounters.containsKey(key)) {
Counter counter = Counter
.builder("uid2.http_requests")
.description("counter for how many http requests are processed per each api contact and status code")
.tags("api_contact", apiContact, "site_id", String.valueOf(siteId), "host", host, "status", String.valueOf(status), "method", method.name(), "path", path)
.tags("api_contact", apiContact, "site_id", String.valueOf(siteId), "site_name", siteName,
"host", host, "status", String.valueOf(status), "method", method.name(), "path", path)
.register(Metrics.globalRegistry);
_apiMetricCounters.put(key, counter);
}
Expand Down

0 comments on commit 63ab0a8

Please sign in to comment.