Skip to content

Commit 428323f

Browse files
committed
Improves ServiceNameCollector
1 parent a19f73a commit 428323f

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

internal-api/src/main/java/datadog/trace/api/remoteconfig/ServiceNameCollector.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
import datadog.trace.api.Config;
66
import java.util.ArrayList;
7+
import java.util.HashSet;
78
import java.util.List;
89
import java.util.Locale;
10+
import java.util.Set;
911
import java.util.concurrent.ConcurrentHashMap;
1012
import javax.annotation.Nullable;
1113
import org.slf4j.Logger;
@@ -17,6 +19,9 @@ public class ServiceNameCollector {
1719

1820
private static final int MAX_EXTRA_SERVICE = Config.get().getRemoteConfigMaxExtraServices();
1921

22+
private static final String SERVICE_NAME_LOWERCASE =
23+
Config.get().getServiceName().toLowerCase(Locale.ROOT);
24+
2025
// This is not final to allow mocking it on tests
2126
private static ServiceNameCollector INSTANCE = new ServiceNameCollector();
2227

@@ -48,14 +53,30 @@ public void addService(final String serviceName) {
4853
}
4954
return;
5055
}
51-
if (!Config.get().getServiceName().equalsIgnoreCase(serviceName)) {
52-
services.put(serviceName.toLowerCase(Locale.ROOT), serviceName);
53-
}
56+
services.put(serviceName, serviceName);
5457
}
5558

59+
/**
60+
* Get the list of unique services deduplicated by case. There is no locking on the addService map
61+
* so, the method is not thread safe.
62+
*
63+
* @return
64+
*/
5665
@Nullable
5766
public List<String> getServices() {
58-
return services.isEmpty() ? null : new ArrayList<>(services.values());
67+
if (services.isEmpty()) {
68+
return null;
69+
}
70+
final Set<String> uniques = new HashSet<>(services.size());
71+
// avoids reiterating again to convert a set to a list
72+
final ArrayList<String> ret = new ArrayList<>(services.size());
73+
for (String current : services.keySet()) {
74+
String lowerCase = current.toLowerCase(Locale.ROOT);
75+
if (!SERVICE_NAME_LOWERCASE.equals(lowerCase) && uniques.add(lowerCase)) {
76+
ret.add(current);
77+
}
78+
}
79+
return ret.isEmpty() ? null : ret;
5980
}
6081

6182
public void clear() {

0 commit comments

Comments
 (0)