use of org.hypertrace.core.datamodel.StructuredTrace in project hypertrace-ingester by hypertrace.
the class AbstractBackendEntityEnricher method enrichTrace.
// At trace level, based on the next span to identify if a backend entity is actually a service
// entity.
@Override
public void enrichTrace(StructuredTrace trace) {
try {
StructuredTraceGraph structuredTraceGraph = buildGraph(trace);
trace.getEventList().stream().filter(event -> EnrichedSpanUtils.isExitSpan(event) && SpanAttributeUtils.isLeafSpan(structuredTraceGraph, event) && canResolveBackend(structuredTraceGraph, event)).map(event -> Pair.of(event, resolve(event, trace, structuredTraceGraph))).filter(pair -> pair.getRight().isPresent()).filter(pair -> isValidBackendEntity(trace, pair.getLeft(), pair.getRight().get())).forEach(pair -> decorateWithBackendEntity(pair.getRight().get(), pair.getLeft(), trace));
} catch (Exception ex) {
LOGGER.error("An error occurred while enriching backend", ex);
}
}
use of org.hypertrace.core.datamodel.StructuredTrace in project hypertrace-ingester by hypertrace.
the class StructuredTraceGraphBuilder method buildGraph.
public static StructuredTraceGraph buildGraph(StructuredTrace trace) {
StructuredTrace cachedTrace = cachedTraceThreadLocal.get();
StructuredTraceGraph cachedGraph = cachedGraphThreadLocal.get();
boolean shouldRebuildTraceEventsGraph = GraphBuilderUtil.isTraceEventsChanged(cachedTrace, trace);
boolean shouldRebuildTraceEntitiesGraph = GraphBuilderUtil.isTraceEntitiesChanged(cachedTrace, trace);
if (null == cachedGraph || GraphBuilderUtil.isDifferentTrace(cachedTrace, trace) || (shouldRebuildTraceEventsGraph && shouldRebuildTraceEntitiesGraph)) {
Instant start = Instant.now();
StructuredTraceGraph graph = new StructuredTraceGraph(trace);
if (LOG.isDebugEnabled()) {
LOG.debug("Time taken in building StructuredTraceGraph, duration_millis:{} for tenantId:{}", Duration.between(start, Instant.now()).toMillis(), trace.getCustomerId());
}
cachedTraceThreadLocal.set(StructuredTrace.newBuilder(trace).build());
cachedGraphThreadLocal.set(graph);
debugGraph("Case: Rebuilding the graph.", graph, trace);
return graph;
}
if (shouldRebuildTraceEventsGraph || shouldRebuildTraceEntitiesGraph) {
Instant start = Instant.now();
if (shouldRebuildTraceEventsGraph) {
cachedGraph.reCreateTraceEventsGraph(trace);
} else {
cachedGraph.reCreateTraceEntitiesGraph(trace);
}
if (LOG.isDebugEnabled()) {
LOG.debug("Time taken in building TraceEventsGraph, duration_millis:{} for tenantId:{}", Duration.between(start, Instant.now()).toMillis(), trace.getCustomerId());
}
cachedTraceThreadLocal.set(StructuredTrace.newBuilder(trace).build());
cachedGraphThreadLocal.set(cachedGraph);
debugGraph("Case: Partially building the graph.", cachedGraph, trace);
return cachedGraph;
}
debugGraph("Case: Not building the graph.", cachedGraphThreadLocal.get(), trace);
return cachedGraph;
}
use of org.hypertrace.core.datamodel.StructuredTrace in project hypertrace-ingester by hypertrace.
the class GraphBuilderUtilTest method testIsStructuredTraceChangedForSizeCondition.
@Test
public void testIsStructuredTraceChangedForSizeCondition() {
Entity entity = mock(Entity.class);
Event parent = mock(Event.class);
Event child = mock(Event.class);
Edge eventEdge = mock(Edge.class);
// same size
StructuredTrace cachedTrace = mock(StructuredTrace.class);
when(cachedTrace.getCustomerId()).thenReturn("__defaultTenant");
when(cachedTrace.getTraceId()).thenReturn(ByteBuffer.wrap("2ebbc19b6428510f".getBytes()));
when(cachedTrace.getEntityList()).thenReturn(List.of(entity));
when(cachedTrace.getEventList()).thenReturn(List.of(parent, child));
when(cachedTrace.getEntityEdgeList()).thenReturn(List.of());
when(cachedTrace.getEntityEventEdgeList()).thenReturn(List.of());
when(cachedTrace.getEventEdgeList()).thenReturn(List.of(eventEdge));
StructuredTrace underTestTrace = mock(StructuredTrace.class);
when(underTestTrace.getCustomerId()).thenReturn("__defaultTenant");
when(underTestTrace.getTraceId()).thenReturn(ByteBuffer.wrap("2ebbc19b6428510f".getBytes()));
when(underTestTrace.getEntityList()).thenReturn(List.of(entity));
when(underTestTrace.getEventList()).thenReturn(List.of(parent, child));
when(underTestTrace.getEntityEdgeList()).thenReturn(List.of());
when(underTestTrace.getEntityEventEdgeList()).thenReturn(List.of());
when(underTestTrace.getEventEdgeList()).thenReturn(List.of(eventEdge));
boolean result = GraphBuilderUtil.isStructuredTraceChanged(cachedTrace, underTestTrace);
Assertions.assertFalse(result);
}
use of org.hypertrace.core.datamodel.StructuredTrace in project hypertrace-ingester by hypertrace.
the class GraphBuilderUtilTest method testIsDifferentTraceForNullInputs.
@Test
public void testIsDifferentTraceForNullInputs() {
StructuredTrace cachedTrace = mock(StructuredTrace.class);
StructuredTrace underTestTrace = mock(StructuredTrace.class);
boolean result = GraphBuilderUtil.isDifferentTrace(null, null);
Assertions.assertTrue(result);
result = GraphBuilderUtil.isDifferentTrace(null, underTestTrace);
Assertions.assertTrue(result);
result = GraphBuilderUtil.isDifferentTrace(cachedTrace, null);
Assertions.assertTrue(result);
}
use of org.hypertrace.core.datamodel.StructuredTrace in project hypertrace-ingester by hypertrace.
the class GraphBuilderUtilTest method testIsDifferentTraceForTenantAndTraceCondition.
@Test
public void testIsDifferentTraceForTenantAndTraceCondition() {
// different tenant id
StructuredTrace cachedTrace = mock(StructuredTrace.class);
when(cachedTrace.getCustomerId()).thenReturn("__defaultTenant");
when(cachedTrace.getTraceId()).thenReturn(ByteBuffer.wrap("2ebbc19b6428510f".getBytes()));
StructuredTrace underTestTrace = mock(StructuredTrace.class);
when(underTestTrace.getCustomerId()).thenReturn("__defaultTenantUnderTest");
when(underTestTrace.getTraceId()).thenReturn(ByteBuffer.wrap("2ebbc19b6428510f".getBytes()));
boolean result = GraphBuilderUtil.isDifferentTrace(cachedTrace, underTestTrace);
Assertions.assertTrue(result);
// different trace ids
cachedTrace = mock(StructuredTrace.class);
when(cachedTrace.getCustomerId()).thenReturn("__defaultTenant");
when(cachedTrace.getTraceId()).thenReturn(ByteBuffer.wrap("2ebbc19b6428510f".getBytes()));
underTestTrace = mock(StructuredTrace.class);
when(underTestTrace.getCustomerId()).thenReturn("__defaultTenant");
when(underTestTrace.getTraceId()).thenReturn(ByteBuffer.wrap("2ebbc19b6428511f".getBytes()));
result = GraphBuilderUtil.isDifferentTrace(cachedTrace, underTestTrace);
Assertions.assertTrue(result);
}
Aggregations