Search in sources :

Example 1 with StructuredTrace

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);
    }
}
Also used : RequestContext(org.hypertrace.core.grpcutils.context.RequestContext) EnrichedSpanUtils(org.hypertrace.traceenricher.enrichedspan.constants.utils.EnrichedSpanUtils) EntityConstants(org.hypertrace.entity.service.constants.EntityConstants) BackendInfo(org.hypertrace.traceenricher.enrichment.enrichers.resolver.backend.BackendInfo) AttributeValueCreator(org.hypertrace.core.datamodel.shared.trace.AttributeValueCreator) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) StringUtils(org.apache.commons.lang3.StringUtils) LinkedHashMap(java.util.LinkedHashMap) StructuredTraceGraph(org.hypertrace.core.datamodel.shared.StructuredTraceGraph) Pair(org.apache.commons.lang3.tuple.Pair) BackendType(org.hypertrace.traceenricher.enrichment.enrichers.BackendType) AttributeValue(org.hypertrace.entity.data.service.v1.AttributeValue) Map(java.util.Map) BackendProvider(org.hypertrace.traceenricher.enrichment.enrichers.backend.provider.BackendProvider) EnrichedSpanConstants(org.hypertrace.traceenricher.enrichedspan.constants.EnrichedSpanConstants) Entity(org.hypertrace.entity.data.service.v1.Entity) HexUtils(org.hypertrace.core.datamodel.shared.HexUtils) SpanAttributeUtils(org.hypertrace.core.datamodel.shared.SpanAttributeUtils) Backend(org.hypertrace.traceenricher.enrichedspan.constants.v1.Backend) EntityType(org.hypertrace.entity.v1.entitytype.EntityType) Nullable(javax.annotation.Nullable) StructuredTrace(org.hypertrace.core.datamodel.StructuredTrace) Logger(org.slf4j.Logger) SpanSemanticConventionUtils(org.hypertrace.semantic.convention.utils.span.SpanSemanticConventionUtils) Config(com.typesafe.config.Config) AbstractTraceEnricher(org.hypertrace.traceenricher.enrichment.AbstractTraceEnricher) BackendAttribute(org.hypertrace.entity.constants.v1.BackendAttribute) EnricherUtil(org.hypertrace.traceenricher.util.EnricherUtil) Event(org.hypertrace.core.datamodel.Event) EdsClient(org.hypertrace.entity.data.service.client.EdsClient) ClientRegistry(org.hypertrace.traceenricher.enrichment.clients.ClientRegistry) EntityCache(org.hypertrace.traceenricher.enrichment.enrichers.cache.EntityCache) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) EntityAvroConverter(org.hypertrace.traceenricher.util.EntityAvroConverter) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) Joiner(com.google.common.base.Joiner) Builder(org.hypertrace.entity.data.service.v1.Entity.Builder) StructuredTraceGraph(org.hypertrace.core.datamodel.shared.StructuredTraceGraph) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with StructuredTrace

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;
}
Also used : StructuredTrace(org.hypertrace.core.datamodel.StructuredTrace) Instant(java.time.Instant) StructuredTraceGraph(org.hypertrace.core.datamodel.shared.StructuredTraceGraph)

Example 3 with StructuredTrace

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);
}
Also used : Entity(org.hypertrace.core.datamodel.Entity) StructuredTrace(org.hypertrace.core.datamodel.StructuredTrace) Event(org.hypertrace.core.datamodel.Event) Edge(org.hypertrace.core.datamodel.Edge) Test(org.junit.jupiter.api.Test)

Example 4 with StructuredTrace

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);
}
Also used : StructuredTrace(org.hypertrace.core.datamodel.StructuredTrace) Test(org.junit.jupiter.api.Test)

Example 5 with StructuredTrace

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);
}
Also used : StructuredTrace(org.hypertrace.core.datamodel.StructuredTrace) Test(org.junit.jupiter.api.Test)

Aggregations

StructuredTrace (org.hypertrace.core.datamodel.StructuredTrace)79 Test (org.junit.jupiter.api.Test)66 Event (org.hypertrace.core.datamodel.Event)47 HashMap (java.util.HashMap)15 AbstractAttributeEnricherTest (org.hypertrace.traceenricher.enrichment.enrichers.AbstractAttributeEnricherTest)15 Config (com.typesafe.config.Config)7 ByteBuffer (java.nio.ByteBuffer)7 Map (java.util.Map)7 Edge (org.hypertrace.core.datamodel.Edge)7 File (java.io.File)6 List (java.util.List)6 Entity (org.hypertrace.entity.data.service.v1.Entity)6 URL (java.net.URL)5 ArrayList (java.util.ArrayList)5 SpecificDatumReader (org.apache.avro.specific.SpecificDatumReader)5 RawSpan (org.hypertrace.core.datamodel.RawSpan)5 HexUtils (org.hypertrace.core.datamodel.shared.HexUtils)5 Counter (io.micrometer.core.instrument.Counter)4 Duration (java.time.Duration)4 Instant (java.time.Instant)4