From ce90764e7de52fce08e61c3ff02832280afe1052 Mon Sep 17 00:00:00 2001 From: saxenakshitiz Date: Fri, 23 Jul 2021 00:33:14 +0530 Subject: [PATCH 1/2] Remove the default port during http resolution based on protocol. This will help reduce number of resolved backend http entity. --- .../backend/provider/HttpBackendProvider.java | 21 +- .../provider/HttpBackendProviderTest.java | 215 ++++++++++++++++++ 2 files changed, 235 insertions(+), 1 deletion(-) diff --git a/hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/main/java/org/hypertrace/traceenricher/enrichment/enrichers/backend/provider/HttpBackendProvider.java b/hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/main/java/org/hypertrace/traceenricher/enrichment/enrichers/backend/provider/HttpBackendProvider.java index 643904d7e..ba9169cf2 100644 --- a/hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/main/java/org/hypertrace/traceenricher/enrichment/enrichers/backend/provider/HttpBackendProvider.java +++ b/hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/main/java/org/hypertrace/traceenricher/enrichment/enrichers/backend/provider/HttpBackendProvider.java @@ -21,6 +21,11 @@ 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 static final String DEFAULT_HTTP_PORT_SUFFIX = COLON + DEFAULT_HTTP_PORT; + private static final String DEFAULT_HTTPS_PORT_SUFFIX = COLON + DEFAULT_HTTPS_PORT; private Supplier protocolSupplier; @Override @@ -42,7 +47,21 @@ public BackendType getBackendType(Event event) { @Override public Optional getBackendUri(Event event, StructuredTraceGraph structuredTraceGraph) { - return HttpSemanticConventionUtils.getHttpHost(event); + Optional 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 httpHostStr = httpHost.get(); + int index = + getProtocol() == Protocol.PROTOCOL_HTTP + ? httpHostStr.indexOf(DEFAULT_HTTP_PORT_SUFFIX) + : httpHostStr.indexOf(DEFAULT_HTTPS_PORT_SUFFIX); + + if (index > -1) { + return Optional.of(httpHostStr.substring(0, index)); + } + } + return httpHost; } @Override diff --git a/hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/test/java/org/hypertrace/traceenricher/enrichment/enrichers/backend/provider/HttpBackendProviderTest.java b/hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/test/java/org/hypertrace/traceenricher/enrichment/enrichers/backend/provider/HttpBackendProviderTest.java index 1cfc407be..fbc9c8e38 100644 --- a/hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/test/java/org/hypertrace/traceenricher/enrichment/enrichers/backend/provider/HttpBackendProviderTest.java +++ b/hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/test/java/org/hypertrace/traceenricher/enrichment/enrichers/backend/provider/HttpBackendProviderTest.java @@ -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 = @@ -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 = From 46a8b030724f0310a9241d082c6e2b2ac48a4309 Mon Sep 17 00:00:00 2001 From: saxenakshitiz Date: Fri, 23 Jul 2021 11:32:10 +0530 Subject: [PATCH 2/2] Address review comments --- .../backend/provider/HttpBackendProvider.java | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/main/java/org/hypertrace/traceenricher/enrichment/enrichers/backend/provider/HttpBackendProvider.java b/hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/main/java/org/hypertrace/traceenricher/enrichment/enrichers/backend/provider/HttpBackendProvider.java index ba9169cf2..caab0c1d9 100644 --- a/hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/main/java/org/hypertrace/traceenricher/enrichment/enrichers/backend/provider/HttpBackendProvider.java +++ b/hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/main/java/org/hypertrace/traceenricher/enrichment/enrichers/backend/provider/HttpBackendProvider.java @@ -24,8 +24,6 @@ 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 static final String DEFAULT_HTTP_PORT_SUFFIX = COLON + DEFAULT_HTTP_PORT; - private static final String DEFAULT_HTTPS_PORT_SUFFIX = COLON + DEFAULT_HTTPS_PORT; private Supplier protocolSupplier; @Override @@ -51,14 +49,11 @@ public Optional getBackendUri(Event event, StructuredTraceGraph structur // 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 httpHostStr = httpHost.get(); - int index = - getProtocol() == Protocol.PROTOCOL_HTTP - ? httpHostStr.indexOf(DEFAULT_HTTP_PORT_SUFFIX) - : httpHostStr.indexOf(DEFAULT_HTTPS_PORT_SUFFIX); - - if (index > -1) { - return Optional.of(httpHostStr.substring(0, index)); + 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; @@ -97,4 +92,8 @@ public Optional getBackendDestination(Event event) { private Protocol getProtocol() { return this.protocolSupplier.get(); } + + private int getDefaultPort() { + return getProtocol() == Protocol.PROTOCOL_HTTP ? DEFAULT_HTTP_PORT : DEFAULT_HTTPS_PORT; + } }