Skip to content

Commit 340af01

Browse files
committed
feat(system-metrics): track api usage by user, client, api
1 parent f507e2c commit 340af01

File tree

4 files changed

+24
-14
lines changed

4 files changed

+24
-14
lines changed

metadata-io/build.gradle

-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ dependencies {
3131

3232
implementation externalDependency.guava
3333
implementation externalDependency.reflections
34-
// https://mvnrepository.com/artifact/nl.basjes.parse.useragent/yauaa
35-
implementation 'nl.basjes.parse.useragent:yauaa:7.27.0'
3634

3735
api(externalDependency.dgraph4j) {
3836
exclude group: 'com.google.guava', module: 'guava'

metadata-io/src/main/java/com/linkedin/metadata/dao/throttle/APIThrottle.java

+2-12
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,9 @@
1212
import java.util.stream.Collectors;
1313
import javax.annotation.Nonnull;
1414
import javax.annotation.Nullable;
15-
import nl.basjes.parse.useragent.UserAgent;
16-
import nl.basjes.parse.useragent.UserAgentAnalyzer;
1715

1816
public class APIThrottle {
1917
private static final Set<String> AGENT_EXEMPTIONS = Set.of("Browser");
20-
private static final UserAgentAnalyzer UAA =
21-
UserAgentAnalyzer.newBuilder()
22-
.hideMatcherLoadStats()
23-
.withField(UserAgent.AGENT_CLASS)
24-
.withCache(1000)
25-
.build();
2618

2719
private APIThrottle() {}
2820

@@ -56,13 +48,11 @@ public static void evaluate(
5648
private static boolean isExempt(@Nullable RequestContext requestContext) {
5749
// Exclude internal calls
5850
if (requestContext == null
59-
|| requestContext.getUserAgent() == null
51+
|| requestContext.getAgentClass() == null
6052
|| requestContext.getUserAgent().isEmpty()) {
6153
return true;
6254
}
63-
64-
UserAgent ua = UAA.parse(requestContext.getUserAgent());
65-
return AGENT_EXEMPTIONS.contains(ua.get(UserAgent.AGENT_CLASS).getValue());
55+
return AGENT_EXEMPTIONS.contains(requestContext.getAgentClass());
6656
}
6757

6858
private static Set<Long> eventMatchMaxWaitMs(

metadata-io/src/test/java/com/linkedin/metadata/dao/throttle/APIThrottleTest.java

+18
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.List;
1313
import java.util.Map;
1414
import java.util.Set;
15+
import nl.basjes.parse.useragent.UserAgent;
1516
import org.testng.Assert;
1617
import org.testng.annotations.BeforeMethod;
1718
import org.testng.annotations.Test;
@@ -61,6 +62,7 @@ public void testExemptions() {
6162

6263
for (ThrottleEvent event : ALL_EVENTS) {
6364
when(mockRequestContext.getUserAgent()).thenReturn(null);
65+
when(mockRequestContext.getAgentClass()).thenReturn(null);
6466
try {
6567
APIThrottle.evaluate(opContext, Set.of(event), false);
6668
} catch (Exception ex) {
@@ -76,12 +78,16 @@ public void testExemptions() {
7678
for (String ua : exemptions) {
7779
try {
7880
when(mockRequestContext.getUserAgent()).thenReturn(ua);
81+
when(mockRequestContext.getAgentClass())
82+
.thenReturn(RequestContext.UAA.parse(ua).get(UserAgent.AGENT_CLASS).getValue());
7983
APIThrottle.evaluate(opContext, Set.of(event), true);
8084
} catch (Exception ex) {
8185
Assert.fail("Exception was thrown and NOT expected! " + event);
8286
}
8387
try {
8488
when(mockRequestContext.getUserAgent()).thenReturn(ua);
89+
when(mockRequestContext.getAgentClass())
90+
.thenReturn(RequestContext.UAA.parse(ua).get(UserAgent.AGENT_CLASS).getValue());
8591
APIThrottle.evaluate(opContext, Set.of(event), false);
8692
} catch (Exception ex) {
8793
Assert.fail("Exception was thrown and NOT expected! " + event);
@@ -106,6 +112,8 @@ public void testThrottleException() {
106112
&& !event.getActiveThrottles().contains(MANUAL)) {
107113
try {
108114
when(mockRequestContext.getUserAgent()).thenReturn(ua);
115+
when(mockRequestContext.getAgentClass())
116+
.thenReturn(RequestContext.UAA.parse(ua).get(UserAgent.AGENT_CLASS).getValue());
109117
APIThrottle.evaluate(opContext, Set.of(event), true);
110118
Assert.fail(String.format("Exception WAS expected! %s %s", ua, event));
111119
} catch (Exception ignored) {
@@ -115,6 +123,8 @@ public void testThrottleException() {
115123
&& !event.getActiveThrottles().contains(MANUAL)) {
116124
try {
117125
when(mockRequestContext.getUserAgent()).thenReturn(ua);
126+
when(mockRequestContext.getAgentClass())
127+
.thenReturn(RequestContext.UAA.parse(ua).get(UserAgent.AGENT_CLASS).getValue());
118128
APIThrottle.evaluate(opContext, Set.of(event), true);
119129
} catch (Exception ex) {
120130
Assert.fail(String.format("Exception was thrown and NOT expected! %s %s", ua, event));
@@ -126,6 +136,8 @@ public void testThrottleException() {
126136
&& !event.getActiveThrottles().contains(MANUAL)) {
127137
try {
128138
when(mockRequestContext.getUserAgent()).thenReturn(ua);
139+
when(mockRequestContext.getAgentClass())
140+
.thenReturn(RequestContext.UAA.parse(ua).get(UserAgent.AGENT_CLASS).getValue());
129141
APIThrottle.evaluate(opContext, Set.of(event), false);
130142
Assert.fail(String.format("Exception WAS expected! %s %s", ua, event));
131143
} catch (Exception ignored) {
@@ -135,6 +147,8 @@ public void testThrottleException() {
135147
&& !event.getActiveThrottles().contains(MANUAL)) {
136148
try {
137149
when(mockRequestContext.getUserAgent()).thenReturn(ua);
150+
when(mockRequestContext.getAgentClass())
151+
.thenReturn(RequestContext.UAA.parse(ua).get(UserAgent.AGENT_CLASS).getValue());
138152
APIThrottle.evaluate(opContext, Set.of(event), false);
139153
} catch (Exception ex) {
140154
Assert.fail(String.format("Exception was thrown and NOT expected! %s %s", ua, event));
@@ -145,12 +159,16 @@ public void testThrottleException() {
145159
if (event.getActiveThrottles().contains(MANUAL)) {
146160
try {
147161
when(mockRequestContext.getUserAgent()).thenReturn(ua);
162+
when(mockRequestContext.getAgentClass())
163+
.thenReturn(RequestContext.UAA.parse(ua).get(UserAgent.AGENT_CLASS).getValue());
148164
APIThrottle.evaluate(opContext, Set.of(event), true);
149165
Assert.fail(String.format("Exception WAS expected! %s %s", ua, event));
150166
} catch (Exception ignored) {
151167
}
152168
try {
153169
when(mockRequestContext.getUserAgent()).thenReturn(ua);
170+
when(mockRequestContext.getAgentClass())
171+
.thenReturn(RequestContext.UAA.parse(ua).get(UserAgent.AGENT_CLASS).getValue());
154172
APIThrottle.evaluate(opContext, Set.of(event), false);
155173
Assert.fail(String.format("Exception WAS expected! %s %s", ua, event));
156174
} catch (Exception ignored) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package io.datahubproject.metadata.context;
2+
3+
public class RequestContextTest {
4+
}

0 commit comments

Comments
 (0)