diff --git a/hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/main/java/org/hypertrace/traceenricher/enrichment/enrichers/endpoint/ApiEntityDao.java b/hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/main/java/org/hypertrace/traceenricher/enrichment/enrichers/endpoint/ApiEntityDao.java index 45c8f67e2..67064c8c3 100644 --- a/hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/main/java/org/hypertrace/traceenricher/enrichment/enrichers/endpoint/ApiEntityDao.java +++ b/hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/main/java/org/hypertrace/traceenricher/enrichment/enrichers/endpoint/ApiEntityDao.java @@ -34,6 +34,8 @@ public class ApiEntityDao { private static final String SERVICE_ID_ATTR = EntityConstants.getValue(ServiceAttribute.SERVICE_ATTRIBUTE_ID); + private static final String SERVICE_NAME_ATTR = + EntityConstants.getValue(ServiceAttribute.SERVICE_ATTRIBUTE_NAME); private static final String API_URL_PATTERN_ATTR = EntityConstants.getValue(ApiAttribute.API_ATTRIBUTE_URL_PATTERN); private static final String API_TYPE_ATTR = @@ -45,10 +47,12 @@ public ApiEntityDao(EdsClient client) { edsClient = client; } - public Entity upsertApiEntity(String tenantId, String serviceId, String apiType, String apiName) { + public Entity upsertApiEntity( + String tenantId, String serviceId, String serviceName, String apiType, String apiName) { Preconditions.checkNotNull(tenantId, "tenantId can't be empty"); Preconditions.checkNotNull(serviceId, "serviceId can't be empty"); + Preconditions.checkNotNull(serviceName, "serviceName can't be empty"); Preconditions.checkNotNull(apiType, "apiType can't be empty"); Preconditions.checkNotNull(apiName, "apiName can't be empty"); @@ -62,6 +66,7 @@ public Entity upsertApiEntity(String tenantId, String serviceId, String apiType, .putIdentifyingAttributes(API_URL_PATTERN_ATTR, createAttributeValue(pattern)) .putIdentifyingAttributes(SERVICE_ID_ATTR, createAttributeValue(serviceId)) .putIdentifyingAttributes(API_TYPE_ATTR, createAttributeValue(apiType)) + .putAttributes(SERVICE_NAME_ATTR, createAttributeValue(serviceName)) .putAttributes(API_DISCOVERY_FROM_ATTR, createAttributeValue(DISCOVERED_FROM)) .putAttributes(API_DISCOVERY_STATE_ATTR, createAttributeValue(DISCOVERED_STATE)) .putAttributes(API_NAME_ATTR, createAttributeValue(apiName)); diff --git a/hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/main/java/org/hypertrace/traceenricher/enrichment/enrichers/endpoint/EndpointEnricher.java b/hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/main/java/org/hypertrace/traceenricher/enrichment/enrichers/endpoint/EndpointEnricher.java index b5640c2f7..76f5c9cc7 100644 --- a/hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/main/java/org/hypertrace/traceenricher/enrichment/enrichers/endpoint/EndpointEnricher.java +++ b/hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/main/java/org/hypertrace/traceenricher/enrichment/enrichers/endpoint/EndpointEnricher.java @@ -70,6 +70,7 @@ public void enrichEvent(StructuredTrace trace, Event event) { // Lookup the service id from the span. If we can't find a service id, we can't really // associate API details with that span. String serviceId = EnrichedSpanUtils.getServiceId(event); + String serviceName = EnrichedSpanUtils.getServiceName(event); String customerId = trace.getCustomerId(); if (serviceId == null) { LOGGER.warn( @@ -84,7 +85,8 @@ public void enrichEvent(StructuredTrace trace, Event event) { Entity apiEntity = null; try { apiEntity = - getOperationNameBasedEndpointDiscoverer(customerId, serviceId).getApiEntity(event); + getOperationNameBasedEndpointDiscoverer(customerId, serviceId, serviceName) + .getApiEntity(event); } catch (Exception e) { LOGGER.error( "Unable to get apiEntity for tenantId {}, serviceId {} and event {}", @@ -142,10 +144,12 @@ void setApiEntityDao(ApiEntityDao apiEntityDao) { } private OperationNameBasedEndpointDiscoverer getOperationNameBasedEndpointDiscoverer( - String customerId, String serviceId) { + String customerId, String serviceId, String serviceName) { serviceIdToEndpointDiscoverer.computeIfAbsent( serviceId, - e -> new OperationNameBasedEndpointDiscoverer(customerId, serviceId, apiEntityDao)); + e -> + new OperationNameBasedEndpointDiscoverer( + customerId, serviceId, serviceName, apiEntityDao)); return serviceIdToEndpointDiscoverer.get(serviceId); } diff --git a/hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/main/java/org/hypertrace/traceenricher/enrichment/enrichers/endpoint/OperationNameBasedEndpointDiscoverer.java b/hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/main/java/org/hypertrace/traceenricher/enrichment/enrichers/endpoint/OperationNameBasedEndpointDiscoverer.java index 0a8ec60b6..aacf2cd9b 100644 --- a/hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/main/java/org/hypertrace/traceenricher/enrichment/enrichers/endpoint/OperationNameBasedEndpointDiscoverer.java +++ b/hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/main/java/org/hypertrace/traceenricher/enrichment/enrichers/endpoint/OperationNameBasedEndpointDiscoverer.java @@ -18,6 +18,7 @@ public class OperationNameBasedEndpointDiscoverer { private final String customerId; private final String serviceId; + private final String serviceName; private final ApiEntityDao apiEntityDao; private final LoadingCache patternToApiEntityCache = @@ -32,9 +33,10 @@ public Entity load(@Nonnull String pattern) { }); public OperationNameBasedEndpointDiscoverer( - String customerId, String serviceId, ApiEntityDao apiEntityDao) { + String customerId, String serviceId, String serviceName, ApiEntityDao apiEntityDao) { this.customerId = customerId; this.serviceId = serviceId; + this.serviceName = serviceName; this.apiEntityDao = apiEntityDao; } @@ -44,7 +46,8 @@ public Entity getApiEntity(Event spanEvent) throws ExecutionException { } private Entity getEntityForPattern(String pattern) { - return apiEntityDao.upsertApiEntity(customerId, serviceId, ApiEntityDao.API_TYPE, pattern); + return apiEntityDao.upsertApiEntity( + customerId, serviceId, serviceName, ApiEntityDao.API_TYPE, pattern); } @VisibleForTesting diff --git a/hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/test/java/org/hypertrace/traceenricher/enrichment/enrichers/endpoint/ApiEntityDaoTest.java b/hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/test/java/org/hypertrace/traceenricher/enrichment/enrichers/endpoint/ApiEntityDaoTest.java index 7902c32f5..c71909275 100644 --- a/hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/test/java/org/hypertrace/traceenricher/enrichment/enrichers/endpoint/ApiEntityDaoTest.java +++ b/hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/test/java/org/hypertrace/traceenricher/enrichment/enrichers/endpoint/ApiEntityDaoTest.java @@ -9,44 +9,53 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -public class ApiEntityDaoTest { +class ApiEntityDaoTest { private EdsClient edsClient; - private ApiEntityDao underTest; + private ApiEntityDao apiEntityDao; @BeforeEach - public void setup() { + void setup() { edsClient = mock(EdsClient.class); - underTest = new ApiEntityDao(edsClient); + apiEntityDao = new ApiEntityDao(edsClient); } @Test - public void whenValidDataIsPassedExpectDaoToBeCalled() { - underTest.upsertApiEntity("tenant-1", "service-1", "OPERATION_NAME", "Driver:;getCustomers"); + void whenValidDataIsPassedExpectDaoToBeCalled() { + apiEntityDao.upsertApiEntity( + "tenant-1", "service-1", "service1", "OPERATION_NAME", "Driver:;getCustomers"); verify(edsClient).upsert(any(org.hypertrace.entity.data.service.v1.Entity.class)); } @Test - public void whenParamsAreNullThenExpectNullPointerException() { + void whenParamsAreNullThenExpectNullPointerException() { Assertions.assertThrows( NullPointerException.class, () -> { - underTest.upsertApiEntity(null, "service-1", "OPERATION_NAME", "Driver:;getCustomers"); + apiEntityDao.upsertApiEntity( + null, "service-1", "service1", "OPERATION_NAME", "Driver:;getCustomers"); }); Assertions.assertThrows( NullPointerException.class, () -> { - underTest.upsertApiEntity("tenant-1", null, "OPERATION_NAME", "Driver:;getCustomers"); + apiEntityDao.upsertApiEntity( + "tenant-1", null, null, "OPERATION_NAME", "Driver:;getCustomers"); }); Assertions.assertThrows( NullPointerException.class, () -> { - underTest.upsertApiEntity("tenant-1", "service-1", null, "Driver:;getCustomers"); + apiEntityDao.upsertApiEntity("tenant-1", "service-1", null, null, "Driver:;getCustomers"); }); Assertions.assertThrows( NullPointerException.class, () -> { - underTest.upsertApiEntity("tenant-1", "service-1", "OPERATION_NAME", null); + apiEntityDao.upsertApiEntity( + "tenant-1", "service-1", "service1", null, "Driver:;getCustomers"); + }); + Assertions.assertThrows( + NullPointerException.class, + () -> { + apiEntityDao.upsertApiEntity("tenant-1", "service-1", "service1", "OPERATION_NAME", null); }); } } diff --git a/hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/test/java/org/hypertrace/traceenricher/enrichment/enrichers/endpoint/EndpointEnricherTest.java b/hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/test/java/org/hypertrace/traceenricher/enrichment/enrichers/endpoint/EndpointEnricherTest.java index 5784c5cb7..3e9e6d2bc 100644 --- a/hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/test/java/org/hypertrace/traceenricher/enrichment/enrichers/endpoint/EndpointEnricherTest.java +++ b/hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/test/java/org/hypertrace/traceenricher/enrichment/enrichers/endpoint/EndpointEnricherTest.java @@ -33,7 +33,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -public class EndpointEnricherTest extends AbstractAttributeEnricherTest { +class EndpointEnricherTest extends AbstractAttributeEnricherTest { private static final String API_NAME_ATTR = EntityConstants.getValue(ApiAttribute.API_ATTRIBUTE_NAME); @@ -51,14 +51,14 @@ public class EndpointEnricherTest extends AbstractAttributeEnricherTest { private ApiEntityDao dao; @BeforeEach - public void setup() { + void setup() { dao = mock(ApiEntityDao.class); endpointEnricher = new EndpointEnricher(); endpointEnricher.setApiEntityDao(dao); } @Test - public void whenEnrichedAttributesAreMissing_thenNoEnrichmentWillHappen() { + void whenEnrichedAttributesAreMissing_thenNoEnrichmentWillHappen() { Event event = Event.newBuilder() .setCustomerId(TENANT_ID) @@ -70,7 +70,7 @@ public void whenEnrichedAttributesAreMissing_thenNoEnrichmentWillHappen() { } @Test - public void whenServiceIdMissing_thenNoEnrichmentWillHappen() { + void whenServiceIdMissing_thenNoEnrichmentWillHappen() { Event event = createMockApiBoundaryEntryEvent(); StructuredTrace trace = getBigTrace(); endpointEnricher.enrichEvent(trace, event); @@ -79,7 +79,7 @@ public void whenServiceIdMissing_thenNoEnrichmentWillHappen() { } @Test - public void whenAllAttributesArePresentThenExpectEventToBeEnriched() { + void whenAllAttributesArePresentThenExpectEventToBeEnriched() { StructuredTrace trace = getBigTrace(); Event event = trace.getEventList().get(0); Entity entity = @@ -99,7 +99,11 @@ public void whenAllAttributesArePresentThenExpectEventToBeEnriched() { .build(); when(dao.upsertApiEntity( - trace.getCustomerId(), SERVICE_ID, ApiEntityDao.API_TYPE, event.getEventName())) + trace.getCustomerId(), + SERVICE_ID, + SERVICE_NAME, + ApiEntityDao.API_TYPE, + event.getEventName())) .thenReturn(entity); endpointEnricher.enrichEvent(trace, event); @@ -110,7 +114,7 @@ public void whenAllAttributesArePresentThenExpectEventToBeEnriched() { } @Test - public void testApiEnrichmentForIntermediateEvents() { + void testApiEnrichmentForIntermediateEvents() { StructuredTrace trace = getBigTrace(); Event exitEvent = trace.getEventList().get(2); endpointEnricher.enrichTrace(trace); diff --git a/hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/test/java/org/hypertrace/traceenricher/enrichment/enrichers/endpoint/OperationNameBasedEndpointDiscovererTest.java b/hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/test/java/org/hypertrace/traceenricher/enrichment/enrichers/endpoint/OperationNameBasedEndpointDiscovererTest.java index c280ce2a1..eda6935b1 100644 --- a/hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/test/java/org/hypertrace/traceenricher/enrichment/enrichers/endpoint/OperationNameBasedEndpointDiscovererTest.java +++ b/hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/test/java/org/hypertrace/traceenricher/enrichment/enrichers/endpoint/OperationNameBasedEndpointDiscovererTest.java @@ -14,21 +14,22 @@ public class OperationNameBasedEndpointDiscovererTest { - private OperationNameBasedEndpointDiscoverer underTest; - private ApiEntityDao dao; + private OperationNameBasedEndpointDiscoverer endpointDiscoverer; + private ApiEntityDao apiEntityDao; @BeforeEach public void setup() { - dao = mock(ApiEntityDao.class); - underTest = new OperationNameBasedEndpointDiscoverer("tenant-1", "service-1", dao); + apiEntityDao = mock(ApiEntityDao.class); + endpointDiscoverer = + new OperationNameBasedEndpointDiscoverer("tenant-1", "service-1", "service1", apiEntityDao); } @Test public void whenCacheIsEmptyExpectCacheToLoadAndReturnEntity() throws Exception { Entity entity = Entity.newBuilder().setEntityId("entity-id").setEntityName("entity-name").build(); - when(dao.upsertApiEntity( - "tenant-1", "service-1", ApiEntityDao.API_TYPE, "Driver::getCustomers")) + when(apiEntityDao.upsertApiEntity( + "tenant-1", "service-1", "service1", ApiEntityDao.API_TYPE, "Driver::getCustomers")) .thenReturn(entity); Event event = Event.newBuilder() @@ -36,16 +37,17 @@ public void whenCacheIsEmptyExpectCacheToLoadAndReturnEntity() throws Exception .setEventName("Driver::getCustomers") .setCustomerId("tenant-1") .build(); - Entity expected = underTest.getApiEntity(event); - assertEquals(expected, underTest.getPatternToApiEntityCache().get(event.getEventName())); + Entity expected = endpointDiscoverer.getApiEntity(event); + assertEquals( + expected, endpointDiscoverer.getPatternToApiEntityCache().get(event.getEventName())); } @Test public void whenCacheIsNotEmptyExpectCacheToReturnCachedEntity() throws Exception { Entity entity = Entity.newBuilder().setEntityId("entity-id").setEntityName("entity-name").build(); - when(dao.upsertApiEntity( - "tenant-1", "service-1", ApiEntityDao.API_TYPE, "Driver::getCustomers")) + when(apiEntityDao.upsertApiEntity( + "tenant-1", "service-1", "service1", ApiEntityDao.API_TYPE, "Driver::getCustomers")) .thenReturn(entity); Event event = Event.newBuilder() @@ -53,13 +55,14 @@ public void whenCacheIsNotEmptyExpectCacheToReturnCachedEntity() throws Exceptio .setEventName("Driver::getCustomers") .setCustomerId("tenant-1") .build(); - Entity expected = underTest.getApiEntity(event); + Entity expected = endpointDiscoverer.getApiEntity(event); // query again - underTest.getApiEntity(event); + endpointDiscoverer.getApiEntity(event); // make sure cache didn't trigger a load again. dao should have been called only once during // setup above - verify(dao, times(1)) - .upsertApiEntity("tenant-1", "service-1", ApiEntityDao.API_TYPE, event.getEventName()); + verify(apiEntityDao, times(1)) + .upsertApiEntity( + "tenant-1", "service-1", "service1", ApiEntityDao.API_TYPE, event.getEventName()); } }