Search in sources :

Example 6 with SpanWrapper

use of org.apache.synapse.aspects.flow.statistics.opentracing.models.SpanWrapper in project wso2-synapse by wso2.

the class LatestActiveParentResolver method resolveParentForEndpointOrInboundEndpoint.

/**
 * Resolves the latest active span wrapper - which is either a Call mediator, a Send mediator,
 * or a flow continuable mediator, as the parent span wrapper for endpoints or inbound endpoints.
 * @param spanStore The span store object.
 * @return          Resolved parent span wrapper.
 */
public static SpanWrapper resolveParentForEndpointOrInboundEndpoint(SpanStore spanStore) {
    List<SpanWrapper> parentableSpans = spanStore.getActiveSpanWrappers();
    for (int i = parentableSpans.size() - 1; i >= 0; i--) {
        SpanWrapper spanWrapper = parentableSpans.get(i);
        StatisticDataUnit statisticDataUnit = spanWrapper.getStatisticDataUnit();
        if (isCallMediator(statisticDataUnit) || isSendMediator(statisticDataUnit) || isFlowContinuableMediator(statisticDataUnit)) {
            return spanWrapper;
        }
    }
    return null;
}
Also used : StatisticDataUnit(org.apache.synapse.aspects.flow.statistics.data.raw.StatisticDataUnit) SpanWrapper(org.apache.synapse.aspects.flow.statistics.opentracing.models.SpanWrapper)

Example 7 with SpanWrapper

use of org.apache.synapse.aspects.flow.statistics.opentracing.models.SpanWrapper in project wso2-synapse by wso2.

the class ParentResolver method resolveParent.

/**
 * Resolves parent spans before a child span starts.
 *
 * The message flow parent index which is explicitly reported by the statistic data unit is checked first.
 * The parent is finalized if it gives an acceptable parent as implemented inside.
 *
 * During an unacceptable parent in the above case, the component's unique id will be examined in the
 * artifact holder's structuring elements stack, in order to track the parent holder.
 *
 * In case if both of the above fail, the most recent active span will be chosen as the parent.
 *
 * The very first span of the scope will always return null as the parent, which creates a new trace.
 *
 * @param child     Statistic data unit of the child.
 * @param spanStore Span store from where, existing spans are referred.
 * @param synCtx    Message context.
 * @return          Resolved parent span wrapper. Null if no parent.
 */
public static SpanWrapper resolveParent(StatisticDataUnit child, SpanStore spanStore, MessageContext synCtx) {
    // Try resolving based on statistic data unit message flow representation
    SpanWrapper parent = MessageFlowRepresentationBasedParentResolver.resolveParent(child, spanStore);
    if (parent != null && parent.getSpan() != null) {
        return parent;
    }
    // Try resolving based on the structuring element stack of the artifact holder
    parent = ArtifactHolderBasedParentResolver.resolveParent(child, spanStore, synCtx);
    if (parent != null && parent.getSpan() != null) {
        return parent;
    }
    // Resolve based on latest active span
    return LatestActiveParentResolver.resolveParent(spanStore);
}
Also used : SpanWrapper(org.apache.synapse.aspects.flow.statistics.opentracing.models.SpanWrapper)

Example 8 with SpanWrapper

use of org.apache.synapse.aspects.flow.statistics.opentracing.models.SpanWrapper in project wso2-synapse by wso2.

the class SpanStore method addSpanWrapper.

/**
 * Denotes the beginning of a span. Adds appropriate elements to necessary data structures.
 * @param spanId            Index of the span wrapper
 * @param activeSpan        Reference to the span object, that have been started
 * @param statisticDataUnit The statistic data unit object
 * @param parentSpanWrapper Parent span wrapper of the created span wrapper
 * @param synCtx            Message Context that is reported during the open event
 * @return                  Created span wrapper object
 */
public SpanWrapper addSpanWrapper(String spanId, Span activeSpan, StatisticDataUnit statisticDataUnit, SpanWrapper parentSpanWrapper, MessageContext synCtx) {
    SpanWrapper spanWrapper = new SpanWrapper(spanId, activeSpan, statisticDataUnit, parentSpanWrapper);
    spanWrappers.put(spanId, spanWrapper);
    spanWrapper.addKnownSynCtxHashCodeToAllParents(TracingUtils.getSystemIdentityHashCode(synCtx));
    if (parentSpanWrapper != null) {
        parentSpanWrapper.addChildComponentUniqueId(statisticDataUnit.getComponentId());
        if (TracingUtils.isAnonymousSequence(spanWrapper.getStatisticDataUnit())) {
            /*
                Add this anonymous sequence to the parent.
                Note that, anonymous sequences are not pushed to the continuation stack
                */
            parentSpanWrapper.addAnonymousSequence(spanId, spanWrapper);
        }
    }
    componentUniqueIdWiseSpanWrappers.put(statisticDataUnit.getComponentId(), spanWrapper);
    activeSpanWrappers.add(spanWrapper);
    return spanWrapper;
}
Also used : SpanWrapper(org.apache.synapse.aspects.flow.statistics.opentracing.models.SpanWrapper)

Example 9 with SpanWrapper

use of org.apache.synapse.aspects.flow.statistics.opentracing.models.SpanWrapper in project wso2-synapse by wso2.

the class JaegerSpanHandler method startSpan.

/**
 * Starts a span, and stores necessary information in the span store to retrieve them back when needed.
 * @param statisticDataUnit Statistic data unit object, which was collected during a statistic event.
 * @param synCtx            Message context.
 * @param spanStore         Span store object.
 */
private void startSpan(StatisticDataUnit statisticDataUnit, MessageContext synCtx, SpanStore spanStore) {
    SpanWrapper parentSpanWrapper = ParentResolver.resolveParent(statisticDataUnit, spanStore, synCtx);
    Span parentSpan = null;
    if (parentSpanWrapper != null) {
        parentSpan = parentSpanWrapper.getSpan();
    }
    Span span;
    SpanContext spanContext;
    Map<String, String> tracerSpecificCarrier = new HashMap<>();
    Map headersMap = (Map) ((Axis2MessageContext) synCtx).getAxis2MessageContext().getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
    Object statusCode = ((Axis2MessageContext) synCtx).getAxis2MessageContext().getProperty("HTTP_SC");
    Object statusDescription = ((Axis2MessageContext) synCtx).getAxis2MessageContext().getProperty("HTTP_DESC");
    // We only need to extract span context from headers when there are trp headers available
    if (isOuterLevelSpan(statisticDataUnit, spanStore) && headersMap != null) {
        // Extract span context from headers
        spanContext = tracer.extract(Format.Builtin.HTTP_HEADERS, new TextMapExtractAdapter(headersMap));
        span = tracer.buildSpan(statisticDataUnit.getComponentName()).asChildOf(spanContext).start();
    } else {
        span = tracer.buildSpan(statisticDataUnit.getComponentName()).asChildOf(parentSpan).start();
        spanContext = span.context();
    }
    // Fix null pointer issue occurs when spanContext become null
    if (spanContext != null) {
        // Set tracing headers
        tracer.inject(spanContext, Format.Builtin.HTTP_HEADERS, new TextMapInjectAdapter(tracerSpecificCarrier));
    }
    // <property name="TRANSPORT_HEADERS" action="remove" scope="axis2"/>
    if (headersMap != null) {
        headersMap.putAll(tracerSpecificCarrier);
        statisticDataUnit.setTransportHeaderMap(headersMap);
    }
    if (statusCode != null) {
        statisticDataUnit.setStatusCode(statusCode.toString());
    }
    if (statusDescription != null) {
        statisticDataUnit.setStatusDescription(statusDescription.toString());
    }
    if (statisticDataUnit.getComponentType() != null & statisticDataUnit.getComponentType() == ComponentType.ENDPOINT) {
        statisticDataUnit.setEndpoint(synCtx.getEndpoint(statisticDataUnit.getComponentName()));
    }
    String spanId = TracingUtils.extractId(statisticDataUnit);
    SpanWrapper spanWrapper = spanStore.addSpanWrapper(spanId, span, statisticDataUnit, parentSpanWrapper, synCtx);
    if (isOuterLevelSpan(statisticDataUnit, spanStore)) {
        spanStore.assignOuterLevelSpan(spanWrapper);
    }
}
Also used : SpanWrapper(org.apache.synapse.aspects.flow.statistics.opentracing.models.SpanWrapper) TextMapExtractAdapter(io.opentracing.propagation.TextMapExtractAdapter) SpanContext(io.opentracing.SpanContext) HashMap(java.util.HashMap) TextMapInjectAdapter(io.opentracing.propagation.TextMapInjectAdapter) Span(io.opentracing.Span) HashMap(java.util.HashMap) Map(java.util.Map) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 10 with SpanWrapper

use of org.apache.synapse.aspects.flow.statistics.opentracing.models.SpanWrapper in project wso2-synapse by wso2.

the class MessageFlowRepresentationBasedParentResolver method resolveParent.

/**
 * Resolves the parent span wrapper based on the parent index, that is explicitly reported by the child statistic
 * data unit.
 * @param child     Child statistic data unit.
 * @param spanStore The span store object.
 * @return          Resolved parent span wrapper.
 */
public static SpanWrapper resolveParent(StatisticDataUnit child, SpanStore spanStore) {
    String parentId = String.valueOf(child.getParentIndex());
    SpanWrapper parent = spanStore.getSpanWrapper(parentId);
    if (parent != null) {
        if (isEndpointOrInboundEndpoint(child)) {
            /*
                An endpoint can be only parented by either a Call mediator or a Send mediator.
                 */
            if (isCallMediator(parent.getStatisticDataUnit()) || isSendMediator(parent.getStatisticDataUnit())) {
                return parent;
            }
            /*
                Else:
                The parent will be chosen by the latest active parent resolver.
                Endpoints won't have information in artifact holder structuring element stack.
                 */
            return LatestActiveParentResolver.resolveParentForEndpointOrInboundEndpoint(spanStore);
        }
        if (TracingUtils.isAnonymousSequence(parent.getStatisticDataUnit()) || TracingUtils.isAnonymousSequence(child)) {
            if (isFlowContinuableMediator(parent.getStatisticDataUnit()) || isForeachMediator(parent.getStatisticDataUnit())) {
                return parent;
            }
            return getLatestEligibleParent(spanStore);
        }
    }
    return null;
}
Also used : SpanWrapper(org.apache.synapse.aspects.flow.statistics.opentracing.models.SpanWrapper)

Aggregations

SpanWrapper (org.apache.synapse.aspects.flow.statistics.opentracing.models.SpanWrapper)11 StatisticDataUnit (org.apache.synapse.aspects.flow.statistics.data.raw.StatisticDataUnit)2 Span (io.opentracing.Span)1 SpanContext (io.opentracing.SpanContext)1 TextMapExtractAdapter (io.opentracing.propagation.TextMapExtractAdapter)1 TextMapInjectAdapter (io.opentracing.propagation.TextMapInjectAdapter)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 BasicStatisticDataUnit (org.apache.synapse.aspects.flow.statistics.data.raw.BasicStatisticDataUnit)1 TracingScope (org.apache.synapse.aspects.flow.statistics.opentracing.management.scoping.TracingScope)1 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)1