Skip to content

Commit 3002d0d

Browse files
authored
Merge pull request #1139 from IABTechLab/ian-UID2-4408-add-old-js-sdk-info-back
temporarily add back use metrics for the operator-served SDKs
2 parents 81646aa + bfde65c commit 3002d0d

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.uid2.operator.vertx;
2+
3+
import com.uid2.shared.Const;
4+
import io.micrometer.core.instrument.Counter;
5+
import io.micrometer.core.instrument.Metrics;
6+
import io.vertx.core.Handler;
7+
import io.vertx.ext.web.RoutingContext;
8+
9+
import java.io.IOException;
10+
import java.nio.file.DirectoryStream;
11+
import java.nio.file.Files;
12+
import java.nio.file.Path;
13+
import java.nio.file.Paths;
14+
import java.util.HashMap;
15+
import java.util.Map;
16+
17+
public class ClientVersionCapturingHandler implements Handler<RoutingContext> {
18+
private final Map<String, Counter> _clientVersionCounters = new HashMap<>();
19+
20+
public ClientVersionCapturingHandler(String dir, String whitelistGlob) throws IOException {
21+
try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(Paths.get(dir), whitelistGlob)) {
22+
dirStream.forEach(path -> {
23+
final String version = getFileNameWithoutExtension(path);
24+
final Counter counter = Counter
25+
.builder("uid2.client_sdk_versions")
26+
.description("counter for how many http requests are processed per each client sdk version")
27+
.tags("client_version", version)
28+
.register(Metrics.globalRegistry);
29+
_clientVersionCounters.put(version, counter);
30+
});
31+
}
32+
}
33+
@Override
34+
public void handle(RoutingContext context) {
35+
String clientVersion = context.request().headers().get(Const.Http.ClientVersionHeader);
36+
if (clientVersion == null) {
37+
clientVersion = !context.queryParam("client").isEmpty() ? context.queryParam("client").get(0) : null;
38+
}
39+
if (clientVersion != null) {
40+
final Counter counter = _clientVersionCounters.get(clientVersion);
41+
if (counter != null) {
42+
counter.increment();
43+
}
44+
}
45+
context.next();
46+
}
47+
48+
private static String getFileNameWithoutExtension(Path path) {
49+
final String fileName = path.getFileName().toString();
50+
return fileName.indexOf(".") > 0 ? fileName.substring(0, fileName.lastIndexOf(".")) : fileName;
51+
}
52+
}

src/main/java/com/uid2/operator/vertx/UIDOperatorVerticle.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ private Router createRoutesSetup() throws IOException {
221221

222222
router.allowForward(AllowForwardHeaders.X_FORWARD);
223223
router.route().handler(new RequestCapturingHandler());
224+
router.route().handler(new ClientVersionCapturingHandler("static/js", "*.js"));
224225
router.route().handler(CorsHandler.create()
225226
.addRelativeOrigin(".*.")
226227
.allowedMethod(io.vertx.core.http.HttpMethod.GET)

0 commit comments

Comments
 (0)