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 @@ -21,6 +21,9 @@
import org.hypertrace.traceenricher.enrichment.enrichers.BackendType;

public class HttpBackendProvider implements BackendProvider {
private static final String COLON = ":";
private static final int DEFAULT_HTTP_PORT = 80;
private static final int DEFAULT_HTTPS_PORT = 443;
private Supplier<Protocol> protocolSupplier;

@Override
Expand All @@ -42,7 +45,18 @@ public BackendType getBackendType(Event event) {

@Override
public Optional<String> getBackendUri(Event event, StructuredTraceGraph structuredTraceGraph) {
return HttpSemanticConventionUtils.getHttpHost(event);
Optional<String> httpHost = HttpSemanticConventionUtils.getHttpHost(event);
// since http protocol has default ports for http and https protocol,
// Removing default port if available as suffix in httpHost based on protocol
if (httpHost.isPresent()) {
String[] hostAndPort = httpHost.get().split(COLON);
String host = hostAndPort[0];
int port = hostAndPort.length == 2 ? Integer.valueOf(hostAndPort[1]) : getDefaultPort();
if (port == getDefaultPort()) {
return Optional.of(host);
}
}
return httpHost;
}

@Override
Expand Down Expand Up @@ -78,4 +92,8 @@ public Optional<String> getBackendDestination(Event event) {
private Protocol getProtocol() {
return this.protocolSupplier.get();
}

private int getDefaultPort() {
return getProtocol() == Protocol.PROTOCOL_HTTP ? DEFAULT_HTTP_PORT : DEFAULT_HTTPS_PORT;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,109 @@ public void checkBackendEntityGeneratedFromHttpEventType3() {
backendEntity.getAttributesMap().get("http.request.method").getValue().getString(), "GET");
}

@Test
public void checkBackendEntityGeneratedFromHttpEventType4() {
Event e =
Event.newBuilder()
.setCustomerId("__default")
.setEventId(ByteBuffer.wrap("bdf03dfabf5c70f8".getBytes()))
.setEntityIdList(Arrays.asList("4bfca8f7-4974-36a4-9385-dd76bf5c8824"))
.setEnrichedAttributes(
Attributes.newBuilder()
.setAttributeMap(
Map.of(
"SPAN_TYPE",
AttributeValue.newBuilder().setValue("EXIT").build(),
"PROTOCOL",
AttributeValue.newBuilder().setValue("HTTP").build()))
.build())
.setAttributes(
Attributes.newBuilder()
.setAttributeMap(
Map.of(
RawSpanConstants.getValue(HTTP_URL),
AttributeValue.newBuilder()
.setValue(
"http://dataservice:80/userreview?productId=5d644175551847d7408760b4")
.build(),
RawSpanConstants.getValue(HTTP_HOST),
AttributeValue.newBuilder().setValue("dataservice:80").build(),
RawSpanConstants.getValue(HTTP_PATH),
AttributeValue.newBuilder().setValue("/userreview").build(),
RawSpanConstants.getValue(HTTP_REQUEST_QUERY_STRING),
AttributeValue.newBuilder()
.setValue("productId=5d644175551847d7408760b4")
.build(),
"http.request.method",
AttributeValue.newBuilder().setValue("GET").build(),
"FLAGS",
AttributeValue.newBuilder().setValue("OK").build(),
"http.request.url",
AttributeValue.newBuilder()
.setValue(
"http://dataservice:80/userreview?productId=5d644175551847d7408760b4")
.build()))
.build())
.setEventName("jaxrs.client.exit")
.setStartTimeMillis(1566869077746L)
.setEndTimeMillis(1566869077750L)
.setMetrics(
Metrics.newBuilder()
.setMetricMap(
Map.of("Duration", MetricValue.newBuilder().setValue(4.0).build()))
.build())
.setEventRefList(
Arrays.asList(
EventRef.newBuilder()
.setTraceId(ByteBuffer.wrap("random_trace_id".getBytes()))
.setEventId(ByteBuffer.wrap("random_event_id".getBytes()))
.setRefType(EventRefType.CHILD_OF)
.build()))
.build();

final Entity backendEntity =
backendEntityEnricher.resolve(e, structuredTrace, structuredTraceGraph).get().getEntity();
assertEquals("dataservice", backendEntity.getEntityName());
assertEquals(3, backendEntity.getIdentifyingAttributesCount());
assertEquals(
BackendType.HTTP.name(),
backendEntity
.getIdentifyingAttributesMap()
.get(Constants.getEntityConstant(BackendAttribute.BACKEND_ATTRIBUTE_PROTOCOL))
.getValue()
.getString());
assertEquals(
"dataservice",
backendEntity
.getIdentifyingAttributesMap()
.get(Constants.getEntityConstant(BackendAttribute.BACKEND_ATTRIBUTE_HOST))
.getValue()
.getString());
assertEquals(
backendEntity
.getIdentifyingAttributesMap()
.get(Constants.getEntityConstant(BackendAttribute.BACKEND_ATTRIBUTE_PORT))
.getValue()
.getString(),
"-1");
assertEquals(
backendEntity
.getAttributesMap()
.get(Constants.getEnrichedSpanConstant(Backend.BACKEND_FROM_EVENT))
.getValue()
.getString(),
"jaxrs.client.exit");
assertEquals(
backendEntity
.getAttributesMap()
.get(Constants.getEnrichedSpanConstant(Backend.BACKEND_FROM_EVENT_ID))
.getValue()
.getString(),
"62646630336466616266356337306638");
assertEquals(
backendEntity.getAttributesMap().get("http.request.method").getValue().getString(), "GET");
}

@Test
public void checkBackendEntityGeneratedFromHttpsEvent() {
Event e =
Expand Down Expand Up @@ -513,6 +616,118 @@ public void checkBackendEntityGeneratedFromHttpsEvent() {
"GET");
}

@Test
public void checkBackendEntityGeneratedFromHttpsEvent2() {
Event e =
Event.newBuilder()
.setCustomerId("__default")
.setEventId(ByteBuffer.wrap("bdf03dfabf5c707f".getBytes()))
.setEntityIdList(Arrays.asList("4bfca8f7-4974-36a4-9385-dd76bf5c8865"))
.setEnrichedAttributes(
Attributes.newBuilder()
.setAttributeMap(
Map.of(
"SPAN_TYPE",
AttributeValue.newBuilder().setValue("EXIT").build(),
"PROTOCOL",
AttributeValue.newBuilder().setValue("HTTPS").build()))
.build())
.setAttributes(
Attributes.newBuilder()
.setAttributeMap(
Map.of(
"http.status_code",
AttributeValue.newBuilder().setValue("200").build(),
"http.user_agent",
AttributeValue.newBuilder().setValue("").build(),
"http.path",
AttributeValue.newBuilder()
.setValue("/product/5d644175551847d7408760b1")
.build(),
"FLAGS",
AttributeValue.newBuilder().setValue("OK").build(),
"status.message",
AttributeValue.newBuilder().setValue("200").build(),
Constants.getRawSpanConstant(Http.HTTP_METHOD),
AttributeValue.newBuilder().setValue("GET").build(),
"http.host",
AttributeValue.newBuilder().setValue("dataservice:443").build(),
"status.code",
AttributeValue.newBuilder().setValue("0").build()))
.build())
.setEventName("Sent./product/5d644175551847d7408760b1")
.setStartTimeMillis(1566869077746L)
.setEndTimeMillis(1566869077750L)
.setMetrics(
Metrics.newBuilder()
.setMetricMap(
Map.of("Duration", MetricValue.newBuilder().setValue(4.0).build()))
.build())
.setEventRefList(
Arrays.asList(
EventRef.newBuilder()
.setTraceId(ByteBuffer.wrap("random_trace_id".getBytes()))
.setEventId(ByteBuffer.wrap("random_event_id".getBytes()))
.setRefType(EventRefType.CHILD_OF)
.build()))
.setHttp(
org.hypertrace.core.datamodel.eventfields.http.Http.newBuilder()
.setRequest(
Request.newBuilder()
.setHost("dataservice:443")
.setPath("/product/5d644175551847d7408760b1")
.build())
.build())
.build();

final Entity backendEntity =
backendEntityEnricher.resolve(e, structuredTrace, structuredTraceGraph).get().getEntity();
assertEquals(backendEntity.getEntityName(), "dataservice");
assertEquals(3, backendEntity.getIdentifyingAttributesCount());
assertEquals(
BackendType.HTTPS.name(),
backendEntity
.getIdentifyingAttributesMap()
.get(Constants.getEntityConstant(BackendAttribute.BACKEND_ATTRIBUTE_PROTOCOL))
.getValue()
.getString());
assertEquals(
backendEntity
.getIdentifyingAttributesMap()
.get(Constants.getEntityConstant(BackendAttribute.BACKEND_ATTRIBUTE_HOST))
.getValue()
.getString(),
"dataservice");
assertEquals(
backendEntity
.getIdentifyingAttributesMap()
.get(Constants.getEntityConstant(BackendAttribute.BACKEND_ATTRIBUTE_PORT))
.getValue()
.getString(),
"-1");
assertEquals(
backendEntity
.getAttributesMap()
.get(Constants.getEnrichedSpanConstant(Backend.BACKEND_FROM_EVENT))
.getValue()
.getString(),
"Sent./product/5d644175551847d7408760b1");
assertEquals(
backendEntity
.getAttributesMap()
.get(Constants.getEnrichedSpanConstant(Backend.BACKEND_FROM_EVENT_ID))
.getValue()
.getString(),
"62646630336466616266356337303766");
assertEquals(
backendEntity
.getAttributesMap()
.get(Constants.getRawSpanConstant(Http.HTTP_METHOD))
.getValue()
.getString(),
"GET");
}

@Test
public void checkBackendEntityGeneratedFromHttpEventUrlWithIllegalQueryCharacter() {
Event e =
Expand Down