Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> backendExcludedDomains;

@Override
public void init(Config enricherConfig, ClientRegistry clientRegistry) {
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -323,7 +318,7 @@ public Optional<BackendInfo> resolve(
}

String backendUri = maybeBackendUri.get();
if (!isValidBackendUri(backendUri)) {
if (isDomainExcluded(event, backendUri)) {
return Optional.empty();
}

Expand All @@ -349,6 +344,13 @@ public Optional<BackendInfo> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -230,6 +231,15 @@ public static Optional<String> 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 -
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}