use of org.hypertrace.entity.data.service.v1.Entity.Builder in project hypertrace-ingester by hypertrace.
the class AbstractBackendEntityEnricher method resolve.
@VisibleForTesting
public Optional<BackendInfo> resolve(Event event, StructuredTrace trace, StructuredTraceGraph structuredTraceGraph) {
for (BackendProvider backendProvider : getBackendProviders()) {
backendProvider.init(event);
if (!backendProvider.isValidBackend(event)) {
continue;
}
BackendType type = backendProvider.getBackendType(event);
Optional<String> maybeBackendUri = backendProvider.getBackendUri(event, structuredTraceGraph);
if (maybeBackendUri.isEmpty() || StringUtils.isEmpty(maybeBackendUri.get())) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Unable to infer backend uri from event {} for backend type {}", HexUtils.getHex(event.getEventId()), type);
}
continue;
}
String backendUri = maybeBackendUri.get();
final Builder entityBuilder = getBackendEntityBuilder(type, backendUri, event, trace);
backendProvider.getEntityAttributes(event).forEach(entityBuilder::putAttributes);
Map<String, org.hypertrace.core.datamodel.AttributeValue> enrichedAttributes = new HashMap<>();
Optional<String> backendOperation = backendProvider.getBackendOperation(event);
Optional<String> backendDestination = backendProvider.getBackendDestination(event);
backendOperation.ifPresent(operation -> enrichedAttributes.put(BACKEND_OPERATION_ATTR, AttributeValueCreator.create(operation)));
backendDestination.ifPresent(destination -> enrichedAttributes.put(BACKEND_DESTINATION_ATTR, AttributeValueCreator.create(destination)));
return Optional.of(new BackendInfo(entityBuilder.build(), Collections.unmodifiableMap(enrichedAttributes)));
}
return Optional.empty();
}
use of org.hypertrace.entity.data.service.v1.Entity.Builder in project hypertrace-ingester by hypertrace.
the class AbstractBackendEntityEnricher method getBackendEntityBuilder.
protected Builder getBackendEntityBuilder(BackendType type, String backendURI, Event event, StructuredTrace trace) {
String[] hostAndPort = backendURI.split(COLON);
String host = hostAndPort[0];
String port = hostAndPort.length == 2 ? hostAndPort[1] : DEFAULT_PORT;
String fqn = fqnResolver.resolve(host, event);
String entityName = port.equals(DEFAULT_PORT) ? fqn : COLON_JOINER.join(fqn, port);
final Builder entityBuilder = Entity.newBuilder().setEntityType(EntityType.BACKEND.name()).setTenantId(event.getCustomerId()).setEntityName(entityName).putIdentifyingAttributes(BACKEND_PROTOCOL_ATTR_NAME, EnricherUtil.createAttributeValue(type.name())).putIdentifyingAttributes(BACKEND_HOST_ATTR_NAME, EnricherUtil.createAttributeValue(fqn)).putIdentifyingAttributes(BACKEND_PORT_ATTR_NAME, EnricherUtil.createAttributeValue(port));
entityBuilder.putAttributes(EnrichedSpanConstants.getValue(Backend.BACKEND_FROM_EVENT), EnricherUtil.createAttributeValue(event.getEventName()));
entityBuilder.putAttributes(EnrichedSpanConstants.getValue(Backend.BACKEND_FROM_EVENT_ID), EnricherUtil.createAttributeValue(HexUtils.getHex(event.getEventId())));
return entityBuilder;
}
use of org.hypertrace.entity.data.service.v1.Entity.Builder in project hypertrace-ingester by hypertrace.
the class GrpcBackendProviderTest method checkBackendEntityGeneratedFromGrpcEvent.
@Test
public void checkBackendEntityGeneratedFromGrpcEvent() {
Map<String, AttributeValue> attributeMap = ImmutableMap.<String, AttributeValue>builder().put("grpc.method", AttributeValue.newBuilder().setValue("/hipstershop.ProductCatalogService/ListProducts").build()).put("span.kind", AttributeValue.newBuilder().setValue("client").build()).put("component", AttributeValue.newBuilder().setValue("grpc").build()).put("k8s.pod_id", AttributeValue.newBuilder().setValue("55636196-c840-11e9-a417-42010a8a0064").build()).put("docker.container_id", AttributeValue.newBuilder().setValue("ee85cf2cfc3b24613a3da411fdbd2f3eabbe729a5c86c5262971c8d8c29dad0f").build()).put("FLAGS", AttributeValue.newBuilder().setValue("0").build()).put("grpc.host_port", AttributeValue.newBuilder().setValue("productcatalogservice:3550").build()).put("grpc.response.body", AttributeValue.newBuilder().setValue("products {\\n id: \\\"5d644175551847d7408760b5\\\"\\n name: \\\"Vintage Record Player\\\"\\n description: \\\"It still works.\\\"\\n picture: \\\"/static").build()).put("grpc.request.body", AttributeValue.newBuilder().setValue("").build()).build();
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(), "rpc.service", AttributeValue.newBuilder().setValue("myservice.EchoService").build(), "PROTOCOL", AttributeValue.newBuilder().setValue("GRPC").build())).build()).setAttributes(Attributes.newBuilder().setAttributeMap(attributeMap).build()).setEventName("Sent.hipstershop.ProductCatalogService.ListProducts").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 BackendInfo backendInfo = backendEntityEnricher.resolve(e, structuredTrace, structuredTraceGraph).get();
final Entity backendEntity = backendInfo.getEntity();
assertEquals("productcatalogservice:3550", backendEntity.getEntityName());
assertEquals(3, backendEntity.getIdentifyingAttributesCount());
assertEquals(backendEntity.getIdentifyingAttributesMap().get(Constants.getEntityConstant(BackendAttribute.BACKEND_ATTRIBUTE_PROTOCOL)).getValue().getString(), "GRPC");
assertEquals(backendEntity.getIdentifyingAttributesMap().get(Constants.getEntityConstant(BackendAttribute.BACKEND_ATTRIBUTE_HOST)).getValue().getString(), "productcatalogservice");
assertEquals(backendEntity.getIdentifyingAttributesMap().get(Constants.getEntityConstant(BackendAttribute.BACKEND_ATTRIBUTE_PORT)).getValue().getString(), "3550");
assertEquals(backendEntity.getAttributesMap().get(Constants.getEnrichedSpanConstant(Backend.BACKEND_FROM_EVENT)).getValue().getString(), "Sent.hipstershop.ProductCatalogService.ListProducts");
assertEquals(backendEntity.getAttributesMap().get(Constants.getEnrichedSpanConstant(Backend.BACKEND_FROM_EVENT_ID)).getValue().getString(), "62646630336466616266356337306638");
assertEquals(backendEntity.getAttributesMap().get(Constants.getRawSpanConstant(Grpc.GRPC_METHOD)).getValue().getString(), "/hipstershop.ProductCatalogService/ListProducts");
Map<String, AttributeValue> attributes = backendInfo.getAttributes();
assertEquals(Map.of("BACKEND_DESTINATION", AttributeValueCreator.create("myservice.EchoService"), "BACKEND_OPERATION", AttributeValueCreator.create("/hipstershop.ProductCatalogService/ListProducts")), attributes);
}
use of org.hypertrace.entity.data.service.v1.Entity.Builder in project entity-service by hypertrace.
the class EntityQueryConverter method convertToAttributeFilter.
private AttributeFilter convertToAttributeFilter(RequestContext requestContext, Query.Builder queryBuilder, Filter filter) {
if (filter == null || filter.equals(Filter.getDefaultInstance())) {
return null;
}
AttributeFilter.Builder builder = null;
if (filter.getChildFilterCount() == 0) {
// Copy the lhs and rhs from the filter.
AttributeColumnInformation attributeData = getAttributeColumnInfo(requestContext, filter.getLhs());
String edsColumnName = attributeData.getColumnName();
org.hypertrace.entity.query.service.v1.Value rhsValue = filter.getRhs().getLiteral().getValue();
if (edsColumnName.equals(EntityServiceConstants.ENTITY_ID)) {
if (rhsValue.getValueType() == ValueType.STRING) {
queryBuilder.addEntityId(rhsValue.getString());
} else if (rhsValue.getValueType() == ValueType.STRING_ARRAY) {
queryBuilder.addAllEntityId(rhsValue.getStringArrayList());
}
} else if (edsColumnName.equals(EntityServiceConstants.ENTITY_NAME)) {
queryBuilder.setEntityName(rhsValue.getString());
} else {
builder = AttributeFilter.newBuilder();
builder.setOperator(convertOperator(filter.getOperator()));
builder.setName(edsColumnName);
builder.setIsMultiValued(attributeData.isMultiValued());
builder.setAttributeValue(convertToAttributeValue(filter.getRhs()));
}
} else {
builder = AttributeFilter.newBuilder();
builder.setOperator(convertOperator(filter.getOperator()));
for (Filter child : filter.getChildFilterList()) {
AttributeFilter attributeFilter = convertToAttributeFilter(requestContext, queryBuilder, child);
if (null != attributeFilter) {
builder.addChildFilter(attributeFilter);
}
}
}
return (null != builder) ? builder.build() : null;
}
use of org.hypertrace.entity.data.service.v1.Entity.Builder in project hypertrace-ingester by hypertrace.
the class EnricherUtilTest method testSetAttributeForFirstExistingKey.
@Test
public void testSetAttributeForFirstExistingKey() {
Event e = mock(Event.class);
Attributes attributes = Attributes.newBuilder().setAttributeMap(Map.of("a", TestUtil.buildAttributeValue("a-value"), "b", TestUtil.buildAttributeValue("b-value"))).build();
when(e.getAttributes()).thenReturn(attributes);
Builder entityBuilder = Entity.newBuilder();
EnricherUtil.setAttributeForFirstExistingKey(e, entityBuilder, Arrays.asList("a", "b", "c"));
Assertions.assertTrue(entityBuilder.getAttributesMap().containsKey("a"));
}
Aggregations