Skip to content

Commit

Permalink
Shorten host property in network statsbeat (#2087)
Browse files Browse the repository at this point in the history
* Shorten host

* Use regex

* Fix spotless

* Strict protocol

* Remove sout debugging

* Remove escape .

* Send endpointUrl when region is not found
  • Loading branch information
heyams authored Feb 7, 2022
1 parent bf48695 commit 52437d3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.checkerframework.checker.lock.qual.GuardedBy;

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

private static final Pattern hostPattern = Pattern.compile("^https?://(?:www\\.)?([^/.]+)");

private final Object lock = new Object();
private final Cache<String, String> ikeyEndpointMap;

Expand Down Expand Up @@ -241,25 +245,16 @@ private double getRequestDurationAvg() {

/**
* e.g. endpointUrl 'https://westus-0.in.applicationinsights.azure.com/v2.1/track' host will
* return 'westus-0.in.applicationinsights.azure.com'
* return 'westus-0'
*/
static String getHost(String endpointUrl) {
assert (endpointUrl != null && !endpointUrl.isEmpty());
int start = endpointUrl.indexOf("://");
if (start != -1) {
int end = endpointUrl.indexOf("/", start + 3);
if (end != -1) {
return endpointUrl.substring(start + 3, end);
}

return endpointUrl.substring(start + 3);
}
Matcher matcher = hostPattern.matcher(endpointUrl);

int end = endpointUrl.indexOf("/");
if (end != -1) {
return endpointUrl.substring(0, end);
if (matcher.find()) {
return matcher.group(1);
}

// it's better to send bad endpointUrl to Statsbeat for troubleshooting.
return endpointUrl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,22 +112,28 @@ public void run() {

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

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

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

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

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

url = "http://fake-host.com/";
assertThat(NetworkStatsbeat.getHost(url)).isEqualTo("fake-host.com");
url = "https://www.fakehost-5.com";
assertThat(NetworkStatsbeat.getHost(url)).isEqualTo("fakehost-5");

url = "https://fakehost.com";
assertThat(NetworkStatsbeat.getHost(url)).isEqualTo("fakehost");

url = "http://fakehost-5/";
assertThat(NetworkStatsbeat.getHost(url)).isEqualTo("fakehost-5");
}
}

0 comments on commit 52437d3

Please sign in to comment.