Search in sources :

Example 1 with Metrics

use of org.hypertrace.core.datamodel.Metrics in project hypertrace-ingester by hypertrace.

the class ErrorsAndExceptionsEnricher method enrichTrace.

@Override
public void enrichTrace(StructuredTrace trace) {
    // TODO: There could be other cases where the client which is initiating this transaction
    // has errored out but the entry span in transaction might be fine (server responded but
    // client couldn't process it). Those cases should be handled in future.
    ApiTraceGraph apiTraceGraph = ApiTraceGraphBuilder.buildGraph(trace);
    for (ApiNode<Event> apiNode : apiTraceGraph.getApiNodeList()) {
        Optional<Event> entryEvent = apiNode.getEntryApiBoundaryEvent();
        int apiTraceErrorCount = (int) apiNode.getEvents().stream().filter(this::findIfEventHasError).count();
        if (entryEvent.isPresent()) {
            addEnrichedAttribute(entryEvent.get(), EnrichedSpanConstants.API_TRACE_ERROR_SPAN_COUNT_ATTRIBUTE, AttributeValueCreator.create(apiTraceErrorCount));
        }
    }
    // Find the earliest Event from this trace and check if that's an ENTRY type.
    Event earliestEvent = getEarliestEvent(trace);
    if (EnrichedSpanUtils.isEntrySpan(earliestEvent)) {
        LOG.debug("Found earliest Event in this trace. It is {}", earliestEvent);
        Metrics metrics = earliestEvent.getMetrics();
        if (metrics != null && metrics.getMetricMap() != null && metrics.getMetricMap().containsKey(EnrichedSpanConstants.getValue(ErrorMetrics.ERROR_METRICS_ERROR_COUNT))) {
            trace.getAttributes().getAttributeMap().put(EnrichedSpanConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_TRANSACTION_HAS_ERROR), AttributeValueCreator.create(true));
        }
    }
    // Count the no. of errors and exceptions in this trace overall. These need not have caused
    // the trace to error out but it's a good metric to track anyways.
    // Trace is considered to have an error if there is an error in the entry span only.
    double exceptionCount = 0.0d;
    double errorCount = 0.0d;
    for (Event event : trace.getEventList()) {
        Map<String, MetricValue> metricMap = event.getMetrics().getMetricMap();
        if (metricMap != null && metricMap.containsKey(EnrichedSpanConstants.getValue(ErrorMetrics.ERROR_METRICS_ERROR_COUNT))) {
            errorCount += metricMap.get(EnrichedSpanConstants.getValue(ErrorMetrics.ERROR_METRICS_ERROR_COUNT)).getValue();
        }
        if (metricMap != null && metricMap.containsKey(EnrichedSpanConstants.getValue(ErrorMetrics.ERROR_METRICS_EXCEPTION_COUNT))) {
            exceptionCount += metricMap.get(EnrichedSpanConstants.getValue(ErrorMetrics.ERROR_METRICS_EXCEPTION_COUNT)).getValue();
        }
    }
    if (exceptionCount > 0.0d) {
        trace.getMetrics().getMetricMap().put(EnrichedSpanConstants.getValue(ErrorMetrics.ERROR_METRICS_TOTAL_SPANS_WITH_EXCEPTIONS), MetricValueCreator.create(exceptionCount));
    }
    if (errorCount > 0.0d) {
        trace.getMetrics().getMetricMap().put(EnrichedSpanConstants.getValue(ErrorMetrics.ERROR_METRICS_TOTAL_SPANS_WITH_ERRORS), MetricValueCreator.create(errorCount));
    }
}
Also used : Metrics(org.hypertrace.core.datamodel.Metrics) ErrorMetrics(org.hypertrace.traceenricher.enrichedspan.constants.v1.ErrorMetrics) MetricValue(org.hypertrace.core.datamodel.MetricValue) Event(org.hypertrace.core.datamodel.Event) ApiTraceGraph(org.hypertrace.traceenricher.trace.util.ApiTraceGraph)

Aggregations

Event (org.hypertrace.core.datamodel.Event)1 MetricValue (org.hypertrace.core.datamodel.MetricValue)1 Metrics (org.hypertrace.core.datamodel.Metrics)1 ErrorMetrics (org.hypertrace.traceenricher.enrichedspan.constants.v1.ErrorMetrics)1 ApiTraceGraph (org.hypertrace.traceenricher.trace.util.ApiTraceGraph)1