Skip to content

Commit 52437d3

Browse files
authored
Shorten host property in network statsbeat (#2087)
* Shorten host * Use regex * Fix spotless * Strict protocol * Remove sout debugging * Remove escape . * Send endpointUrl when region is not found
1 parent bf48695 commit 52437d3

File tree

2 files changed

+27
-26
lines changed

2 files changed

+27
-26
lines changed

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/statsbeat/NetworkStatsbeat.java

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import java.util.Map;
3131
import java.util.concurrent.atomic.AtomicLong;
3232
import java.util.function.Consumer;
33+
import java.util.regex.Matcher;
34+
import java.util.regex.Pattern;
3335
import org.checkerframework.checker.lock.qual.GuardedBy;
3436

3537
public class NetworkStatsbeat extends BaseStatsbeat {
@@ -42,6 +44,8 @@ public class NetworkStatsbeat extends BaseStatsbeat {
4244
private static final String EXCEPTION_COUNT_METRIC_NAME = "Exception Count";
4345
private static final String BREEZE_ENDPOINT = "breeze";
4446

47+
private static final Pattern hostPattern = Pattern.compile("^https?://(?:www\\.)?([^/.]+)");
48+
4549
private final Object lock = new Object();
4650
private final Cache<String, String> ikeyEndpointMap;
4751

@@ -241,25 +245,16 @@ private double getRequestDurationAvg() {
241245

242246
/**
243247
* e.g. endpointUrl 'https://westus-0.in.applicationinsights.azure.com/v2.1/track' host will
244-
* return 'westus-0.in.applicationinsights.azure.com'
248+
* return 'westus-0'
245249
*/
246250
static String getHost(String endpointUrl) {
247-
assert (endpointUrl != null && !endpointUrl.isEmpty());
248-
int start = endpointUrl.indexOf("://");
249-
if (start != -1) {
250-
int end = endpointUrl.indexOf("/", start + 3);
251-
if (end != -1) {
252-
return endpointUrl.substring(start + 3, end);
253-
}
254-
255-
return endpointUrl.substring(start + 3);
256-
}
251+
Matcher matcher = hostPattern.matcher(endpointUrl);
257252

258-
int end = endpointUrl.indexOf("/");
259-
if (end != -1) {
260-
return endpointUrl.substring(0, end);
253+
if (matcher.find()) {
254+
return matcher.group(1);
261255
}
262256

257+
// it's better to send bad endpointUrl to Statsbeat for troubleshooting.
263258
return endpointUrl;
264259
}
265260
}

agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/statsbeat/NetworkStatsbeatTest.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,22 +112,28 @@ public void run() {
112112

113113
@Test
114114
public void testGetHost() {
115-
String url = "https://fake-host.applicationinsights.azure.com/v2.1/track";
116-
assertThat(NetworkStatsbeat.getHost(url)).isEqualTo("fake-host.applicationinsights.azure.com");
115+
String url = "https://fakehost-1.example.com/";
116+
assertThat(NetworkStatsbeat.getHost(url)).isEqualTo("fakehost-1");
117117

118-
url = "http://fake-host.example.com/v2/track";
119-
assertThat(NetworkStatsbeat.getHost(url)).isEqualTo("fake-host.example.com");
118+
url = "https://fakehost-2.example.com/";
119+
assertThat(NetworkStatsbeat.getHost(url)).isEqualTo("fakehost-2");
120120

121-
url = "http://www.fake-host.com/v2/track";
122-
assertThat(NetworkStatsbeat.getHost(url)).isEqualTo("www.fake-host.com");
121+
url = "http://www.fakehost-3.example.com/";
122+
assertThat(NetworkStatsbeat.getHost(url)).isEqualTo("fakehost-3");
123123

124-
url = "www.fake-host.com/";
125-
assertThat(NetworkStatsbeat.getHost(url)).isEqualTo("www.fake-host.com");
124+
url = "http://www.fakehost.com/v2/track";
125+
assertThat(NetworkStatsbeat.getHost(url)).isEqualTo("fakehost");
126126

127-
url = "http://fake-host.com";
128-
assertThat(NetworkStatsbeat.getHost(url)).isEqualTo("fake-host.com");
127+
url = "https://www.fakehost0-4.com/";
128+
assertThat(NetworkStatsbeat.getHost(url)).isEqualTo("fakehost0-4");
129129

130-
url = "http://fake-host.com/";
131-
assertThat(NetworkStatsbeat.getHost(url)).isEqualTo("fake-host.com");
130+
url = "https://www.fakehost-5.com";
131+
assertThat(NetworkStatsbeat.getHost(url)).isEqualTo("fakehost-5");
132+
133+
url = "https://fakehost.com";
134+
assertThat(NetworkStatsbeat.getHost(url)).isEqualTo("fakehost");
135+
136+
url = "http://fakehost-5/";
137+
assertThat(NetworkStatsbeat.getHost(url)).isEqualTo("fakehost-5");
132138
}
133139
}

0 commit comments

Comments
 (0)