Skip to content

Commit 63ab0a8

Browse files
committed
Update RequestCapturingHandler.java
1 parent 3a7056c commit 63ab0a8

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

src/main/java/com/uid2/shared/vertx/RequestCapturingHandler.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import com.uid2.shared.auth.IAuthorizable;
55
import com.uid2.shared.jmx.AdminApi;
66
import com.uid2.shared.middleware.AuthMiddleware;
7+
import com.uid2.shared.model.Site;
8+
import com.uid2.shared.store.ISiteStore;
79
import io.micrometer.core.instrument.Counter;
810
import io.micrometer.core.instrument.Metrics;
911
import io.vertx.core.Handler;
@@ -32,11 +34,17 @@ public class RequestCapturingHandler implements Handler<RoutingContext> {
3234
private Queue<String> _capturedRequests = null;
3335
private final Map<String, Counter> _apiMetricCounters = new HashMap<>();
3436
private final Map<String, Counter> _clientAppVersionCounters = new HashMap<>();
37+
private final ISiteStore siteStore;
3538

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

43+
public RequestCapturingHandler(ISiteStore siteStore)
44+
{
45+
this.siteStore = siteStore;
46+
}
47+
4048
@Override
4149
public void handle(RoutingContext context) {
4250
if (!AdminApi.instance.getCaptureRequests() && !AdminApi.instance.getPublishApiMetrics()) {
@@ -105,7 +113,17 @@ private void capture(RoutingContext context, long timestamp, String remoteClient
105113
}
106114

107115
final Integer siteId = getSiteId(context);
108-
incrementMetricCounter(apiContact, siteId, host, status, method, path);
116+
117+
String siteName = "unknown";
118+
if (siteId != null) {
119+
Site site = siteStore.getSite(siteId);
120+
if (site != null)
121+
{
122+
siteName = site.getName();
123+
}
124+
}
125+
126+
incrementMetricCounter(apiContact, siteId, siteName, host, status, method, path);
109127

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

199-
private void incrementMetricCounter(String apiContact, Integer siteId, String host, int status, HttpMethod method, String path) {
217+
private void incrementMetricCounter(String apiContact, Integer siteId, String siteName,
218+
String host, int status, HttpMethod method, String path) {
200219
assert apiContact != null;
201-
String key = apiContact + "|" + siteId + "|" + host + "|" + status + "|" + method.name() + "|" + path;
220+
String key = apiContact + "|" + siteId + "|" + siteName + "|" + host + "|" + status + "|" + method.name() + "|" + path;
202221
if (!_apiMetricCounters.containsKey(key)) {
203222
Counter counter = Counter
204223
.builder("uid2.http_requests")
205224
.description("counter for how many http requests are processed per each api contact and status code")
206-
.tags("api_contact", apiContact, "site_id", String.valueOf(siteId), "host", host, "status", String.valueOf(status), "method", method.name(), "path", path)
225+
.tags("api_contact", apiContact, "site_id", String.valueOf(siteId), "site_name", siteName,
226+
"host", host, "status", String.valueOf(status), "method", method.name(), "path", path)
207227
.register(Metrics.globalRegistry);
208228
_apiMetricCounters.put(key, counter);
209229
}

0 commit comments

Comments
 (0)