diff --git a/hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/main/java/org/hypertrace/traceenricher/enrichment/enrichers/backend/AbstractBackendEntityEnricher.java b/hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/main/java/org/hypertrace/traceenricher/enrichment/enrichers/backend/AbstractBackendEntityEnricher.java index d35814bea..19c1eccac 100644 --- a/hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/main/java/org/hypertrace/traceenricher/enrichment/enrichers/backend/AbstractBackendEntityEnricher.java +++ b/hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/main/java/org/hypertrace/traceenricher/enrichment/enrichers/backend/AbstractBackendEntityEnricher.java @@ -27,6 +27,7 @@ import org.hypertrace.entity.data.service.v1.Entity.Builder; import org.hypertrace.entity.service.constants.EntityConstants; import org.hypertrace.entity.v1.entitytype.EntityType; +import org.hypertrace.semantic.convention.utils.http.HttpSemanticConventionUtils; import org.hypertrace.semantic.convention.utils.span.SpanSemanticConventionUtils; import org.hypertrace.traceenricher.enrichedspan.constants.BackendType; import org.hypertrace.traceenricher.enrichedspan.constants.EnrichedSpanConstants; @@ -64,10 +65,12 @@ public abstract class AbstractBackendEntityEnricher extends AbstractTraceEnriche EnrichedSpanConstants.getValue(Backend.BACKEND_OPERATION); private static final String BACKEND_DESTINATION_ATTR = EnrichedSpanConstants.getValue(Backend.BACKEND_DESTINATION); + private static final String BACKEND_EXCLUDED_DOMAINS = "backend.excluded.domains"; private EdsClient edsClient; private EntityCache entityCache; private FqnResolver fqnResolver; + private List backendExcludedDomains; @Override public void init(Config enricherConfig, ClientRegistry clientRegistry) { @@ -76,6 +79,10 @@ public void init(Config enricherConfig, ClientRegistry clientRegistry) { this.entityCache = clientRegistry.getEntityCache(); setup(enricherConfig, clientRegistry); this.fqnResolver = getFqnResolver(); + this.backendExcludedDomains = + enricherConfig.hasPath(BACKEND_EXCLUDED_DOMAINS) + ? enricherConfig.getStringList(BACKEND_EXCLUDED_DOMAINS) + : Collections.emptyList(); } public abstract void setup(Config enricherConfig, ClientRegistry clientRegistry); @@ -122,18 +129,6 @@ protected boolean canResolveBackend(StructuredTraceGraph structuredTraceGraph, E return true; } - /** - * Method to check if backend uri is valid or not. This will enable any custom logic to be - * inserted in the implementing classes. - * - * @param backendURI backend URI information - * @return true if backend uri resolution is allowed - */ - protected boolean isValidBackendUri(String backendURI) { - // by default allow the backend uri to proceed - return true; - } - /** Checks if the candidateEntity is indeed a backend Entity */ private boolean isValidBackendEntity( StructuredTrace trace, Event backendSpan, BackendInfo candidateInfo) { @@ -323,7 +318,7 @@ public Optional resolve( } String backendUri = maybeBackendUri.get(); - if (!isValidBackendUri(backendUri)) { + if (isDomainExcluded(event, backendUri)) { return Optional.empty(); } @@ -349,6 +344,13 @@ public Optional resolve( return Optional.empty(); } + private boolean isDomainExcluded(Event event, String backendUri) { + String[] hostAndPort = backendUri.split(COLON); + String host = fqnResolver.resolve(hostAndPort[0], event); + String domain = HttpSemanticConventionUtils.getPrimaryDomain(host); + return backendExcludedDomains.contains(domain); + } + private Builder getEntityBuilder( StructuredTrace trace, Event event, BackendType type, String backendUri) { try { diff --git a/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/http/HttpSemanticConventionUtils.java b/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/http/HttpSemanticConventionUtils.java index 5440c0cd7..c7a677a7b 100644 --- a/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/http/HttpSemanticConventionUtils.java +++ b/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/http/HttpSemanticConventionUtils.java @@ -44,6 +44,7 @@ import com.google.common.base.Splitter; import com.google.common.collect.Lists; import com.google.common.collect.Sets; +import com.google.common.net.InternetDomainName; import io.micrometer.core.instrument.util.StringUtils; import java.net.HttpCookie; import java.net.MalformedURLException; @@ -230,6 +231,15 @@ public static Optional getEnvironmentForSpan(Event event) { event, OTelDeploymentSemanticConventions.DEPLOYMENT_ENVIRONMENT.getValue())); } + public static String getPrimaryDomain(String host) { + try { + return InternetDomainName.from(host).topPrivateDomain().toString(); + } catch (Exception exception) { + LOGGER.error("Error while extracting the primary domain from the host {} ", host, exception); + return host; + } + } + /** * OTel mandates one of the following set to be present for http server span - http.scheme, * http.host, http.target - http.scheme, http.server_name, net.host.port, http.target - diff --git a/semantic-convention-utils/src/test/java/org/hypertrace/semantic/convention/utils/http/HttpSemanticConventionUtilsTest.java b/semantic-convention-utils/src/test/java/org/hypertrace/semantic/convention/utils/http/HttpSemanticConventionUtilsTest.java index f2e76b437..9e7d3f8e5 100644 --- a/semantic-convention-utils/src/test/java/org/hypertrace/semantic/convention/utils/http/HttpSemanticConventionUtilsTest.java +++ b/semantic-convention-utils/src/test/java/org/hypertrace/semantic/convention/utils/http/HttpSemanticConventionUtilsTest.java @@ -591,4 +591,15 @@ public void testGetPathFromUrl() { "/api/v1/gatekeeper/check?url=%2Fpixel%2Factivities%3Fadvertisable%3DTRHRT&method=GET&service=pixel"); Assertions.assertEquals(path.get(), "/api/v1/gatekeeper/check"); } + + @Test + public void testGetPrimaryDomain() { + Assertions.assertEquals(HttpSemanticConventionUtils.getPrimaryDomain("www.xyz.com"), "xyz.com"); + Assertions.assertEquals( + HttpSemanticConventionUtils.getPrimaryDomain("www.abc.xyz.com"), "xyz.com"); + Assertions.assertEquals(HttpSemanticConventionUtils.getPrimaryDomain("abc.xyz.com"), "xyz.com"); + Assertions.assertEquals(HttpSemanticConventionUtils.getPrimaryDomain("xyz.com"), "xyz.com"); + + Assertions.assertEquals(HttpSemanticConventionUtils.getPrimaryDomain("10.0.0.0"), "10.0.0.0"); + } }