From 30bb13b44956dd7c7622034c6adeca9ed9e21396 Mon Sep 17 00:00:00 2001 From: Andrea Marziali Date: Fri, 20 Dec 2024 10:00:52 +0100 Subject: [PATCH] Improves ServiceNameCollector (#8109) * Improves ServiceNameCollector * Update internal-api/src/main/java/datadog/trace/api/remoteconfig/ServiceNameCollector.java Co-authored-by: Stuart McCulloch * apply suggestions --------- Co-authored-by: Stuart McCulloch --- .../remoteconfig/ServiceNameCollector.java | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/internal-api/src/main/java/datadog/trace/api/remoteconfig/ServiceNameCollector.java b/internal-api/src/main/java/datadog/trace/api/remoteconfig/ServiceNameCollector.java index abc036f3ddb..c4f78b22c58 100644 --- a/internal-api/src/main/java/datadog/trace/api/remoteconfig/ServiceNameCollector.java +++ b/internal-api/src/main/java/datadog/trace/api/remoteconfig/ServiceNameCollector.java @@ -5,7 +5,8 @@ import datadog.trace.api.Config; import java.util.ArrayList; import java.util.List; -import java.util.Locale; +import java.util.Set; +import java.util.TreeSet; import java.util.concurrent.ConcurrentHashMap; import javax.annotation.Nullable; import org.slf4j.Logger; @@ -48,14 +49,24 @@ public void addService(final String serviceName) { } return; } - if (!Config.get().getServiceName().equalsIgnoreCase(serviceName)) { - services.put(serviceName.toLowerCase(Locale.ROOT), serviceName); - } + services.putIfAbsent(serviceName, serviceName); } + /** + * Get the list of unique services deduplicated by case. There is no locking on the addService map + * so, the method is not thread safe. + * + * @return + */ @Nullable public List getServices() { - return services.isEmpty() ? null : new ArrayList<>(services.values()); + if (services.isEmpty()) { + return null; + } + final Set uniqueNames = new TreeSet<>(String.CASE_INSENSITIVE_ORDER); + uniqueNames.addAll(services.keySet()); + uniqueNames.remove(Config.get().getServiceName()); + return uniqueNames.isEmpty() ? null : new ArrayList<>(uniqueNames); } public void clear() {