44
55import datadog .trace .api .Config ;
66import java .util .ArrayList ;
7+ import java .util .HashSet ;
78import java .util .List ;
89import java .util .Locale ;
10+ import java .util .Set ;
911import java .util .concurrent .ConcurrentHashMap ;
1012import javax .annotation .Nullable ;
1113import 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