Search in sources :

Example 1 with TracingSpan

use of org.wso2.carbon.apimgt.tracing.TracingSpan in project carbon-apimgt by wso2.

the class GatewayUtils method setAPIRelatedTags.

public static void setAPIRelatedTags(TracingSpan tracingSpan, org.apache.synapse.MessageContext messageContext) {
    API api = GatewayUtils.getAPI(messageContext);
    Object electedResource = messageContext.getProperty(APIMgtGatewayConstants.API_ELECTED_RESOURCE);
    if (electedResource != null) {
        Util.setTag(tracingSpan, APIMgtGatewayConstants.SPAN_RESOURCE, (String) electedResource);
    }
    if (api != null) {
        Util.setTag(tracingSpan, APIMgtGatewayConstants.SPAN_API_NAME, api.getApiName());
        Util.setTag(tracingSpan, APIMgtGatewayConstants.SPAN_API_VERSION, api.getApiVersion());
    }
    Object consumerKey = messageContext.getProperty(APIMgtGatewayConstants.CONSUMER_KEY);
    if (consumerKey != null) {
        Util.setTag(tracingSpan, APIMgtGatewayConstants.SPAN_APPLICATION_CONSUMER_KEY, (String) consumerKey);
    }
}
Also used : API(org.wso2.carbon.apimgt.keymgt.model.entity.API) JSONObject(net.minidev.json.JSONObject)

Example 2 with TracingSpan

use of org.wso2.carbon.apimgt.tracing.TracingSpan in project carbon-apimgt by wso2.

the class APIMgtGoogleAnalyticsTrackingHandler method handleRequest.

@MethodStats
@Override
public boolean handleRequest(MessageContext msgCtx) {
    TracingSpan span = null;
    TracingTracer tracer = null;
    Map<String, String> tracerSpecificCarrier = new HashMap<>();
    if (Util.tracingEnabled()) {
        TracingSpan responseLatencySpan = (TracingSpan) msgCtx.getProperty(APIMgtGatewayConstants.RESOURCE_SPAN);
        tracer = Util.getGlobalTracer();
        span = Util.startSpan(APIMgtGatewayConstants.GOOGLE_ANALYTICS_HANDLER, responseLatencySpan, tracer);
    }
    try {
        if (configKey == null) {
            throw new SynapseException("Google Analytics configuration unspecified for the API");
        }
        Entry entry = msgCtx.getConfiguration().getEntryDefinition(configKey);
        if (entry == null) {
            log.warn("Cannot find Google Analytics configuration using key: " + configKey);
            return true;
        }
        Object entryValue = null;
        boolean reCreate = false;
        if (entry.isDynamic()) {
            if ((!entry.isCached()) || (entry.isExpired()) || config == null) {
                entryValue = msgCtx.getEntry(this.configKey);
                if (this.version != entry.getVersion()) {
                    reCreate = true;
                }
            }
        } else if (config == null) {
            entryValue = msgCtx.getEntry(this.configKey);
        }
        if (reCreate || config == null) {
            if (entryValue == null || !(entryValue instanceof OMElement)) {
                log.warn("Unable to load Google Analytics configuration using key: " + configKey);
                return true;
            }
            version = entry.getVersion();
            config = getGoogleAnalyticsConfig((OMElement) entryValue);
        }
        if (config == null) {
            log.warn("Unable to create Google Analytics configuration using key: " + configKey);
            return true;
        }
        if (!config.isEnabled()) {
            return true;
        }
        try {
            if (Util.tracingEnabled()) {
                Util.inject(span, tracer, tracerSpecificCarrier);
                if (org.apache.axis2.context.MessageContext.getCurrentMessageContext() != null) {
                    Map headers = (Map) org.apache.axis2.context.MessageContext.getCurrentMessageContext().getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
                    headers.putAll(tracerSpecificCarrier);
                    org.apache.axis2.context.MessageContext.getCurrentMessageContext().setProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS, headers);
                }
            }
            trackPageView(msgCtx);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return true;
    } catch (Exception e) {
        if (Util.tracingEnabled() && span != null) {
            Util.setTag(span, APIMgtGatewayConstants.ERROR, APIMgtGatewayConstants.GOOGLE_ANALYTICS_ERROR);
        }
        throw e;
    } finally {
        if (Util.tracingEnabled()) {
            Util.finishSpan(span);
        }
    }
}
Also used : Entry(org.apache.synapse.config.Entry) SynapseException(org.apache.synapse.SynapseException) HashMap(java.util.HashMap) TracingTracer(org.wso2.carbon.apimgt.tracing.TracingTracer) OMElement(org.apache.axiom.om.OMElement) TracingSpan(org.wso2.carbon.apimgt.tracing.TracingSpan) HashMap(java.util.HashMap) Map(java.util.Map) SynapseException(org.apache.synapse.SynapseException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MethodStats(org.wso2.carbon.apimgt.gateway.MethodStats)

Example 3 with TracingSpan

use of org.wso2.carbon.apimgt.tracing.TracingSpan in project carbon-apimgt by wso2.

the class APIMgtLatencySynapseHandler method handleResponseInFlow.

@Override
public boolean handleResponseInFlow(MessageContext messageContext) {
    if (Util.tracingEnabled() && messageContext.getProperty(APIMgtGatewayConstants.BACKEND_LATENCY_SPAN) != null) {
        TracingSpan backendLatencySpan = (TracingSpan) messageContext.getProperty(APIMgtGatewayConstants.BACKEND_LATENCY_SPAN);
        GatewayUtils.setEndpointRelatedInformation(backendLatencySpan, messageContext);
        Util.finishSpan(backendLatencySpan);
    }
    return true;
}
Also used : TracingSpan(org.wso2.carbon.apimgt.tracing.TracingSpan)

Example 4 with TracingSpan

use of org.wso2.carbon.apimgt.tracing.TracingSpan in project carbon-apimgt by wso2.

the class APIMgtLatencySynapseHandler method handleRequestInFlow.

@Override
public boolean handleRequestInFlow(MessageContext messageContext) {
    TracingTracer tracer = ServiceReferenceHolder.getInstance().getTracer();
    if (Util.tracingEnabled()) {
        org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) messageContext).getAxis2MessageContext();
        Map headersMap = (Map) axis2MessageContext.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
        TracingSpan spanContext = Util.extract(tracer, headersMap);
        TracingSpan responseLatencySpan = Util.startSpan(APIMgtGatewayConstants.RESPONSE_LATENCY, spanContext, tracer);
        Util.setTag(responseLatencySpan, APIMgtGatewayConstants.SPAN_KIND, APIMgtGatewayConstants.SERVER);
        GatewayUtils.setRequestRelatedTags(responseLatencySpan, messageContext);
        messageContext.setProperty(APIMgtGatewayConstants.RESPONSE_LATENCY, responseLatencySpan);
    }
    return true;
}
Also used : TracingTracer(org.wso2.carbon.apimgt.tracing.TracingTracer) TracingSpan(org.wso2.carbon.apimgt.tracing.TracingSpan) Map(java.util.Map) HashMap(java.util.HashMap) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 5 with TracingSpan

use of org.wso2.carbon.apimgt.tracing.TracingSpan in project carbon-apimgt by wso2.

the class APIMgtLatencySynapseHandler method handleRequestOutFlow.

@Override
public boolean handleRequestOutFlow(MessageContext messageContext) {
    TracingTracer tracer = ServiceReferenceHolder.getInstance().getTracer();
    Map<String, String> tracerSpecificCarrier = new HashMap<>();
    if (Util.tracingEnabled()) {
        TracingSpan parentSpan = (TracingSpan) messageContext.getProperty(APIMgtGatewayConstants.RESOURCE_SPAN);
        TracingSpan backendLatencySpan = Util.startSpan(APIMgtGatewayConstants.BACKEND_LATENCY_SPAN, parentSpan, tracer);
        messageContext.setProperty(APIMgtGatewayConstants.BACKEND_LATENCY_SPAN, backendLatencySpan);
        Util.inject(backendLatencySpan, tracer, tracerSpecificCarrier);
        if (org.apache.axis2.context.MessageContext.getCurrentMessageContext() != null) {
            Map headers = (Map) org.apache.axis2.context.MessageContext.getCurrentMessageContext().getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
            headers.putAll(tracerSpecificCarrier);
            org.apache.axis2.context.MessageContext.getCurrentMessageContext().setProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS, headers);
        }
    }
    return true;
}
Also used : HashMap(java.util.HashMap) TracingTracer(org.wso2.carbon.apimgt.tracing.TracingTracer) TracingSpan(org.wso2.carbon.apimgt.tracing.TracingSpan) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

TracingSpan (org.wso2.carbon.apimgt.tracing.TracingSpan)13 TracingTracer (org.wso2.carbon.apimgt.tracing.TracingTracer)11 Timer (org.wso2.carbon.metrics.manager.Timer)7 Map (java.util.Map)5 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)5 MethodStats (org.wso2.carbon.apimgt.gateway.MethodStats)5 HashMap (java.util.HashMap)4 SynapseException (org.apache.synapse.SynapseException)2 API (org.wso2.carbon.apimgt.keymgt.model.entity.API)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 JSONObject (net.minidev.json.JSONObject)1 OMElement (org.apache.axiom.om.OMElement)1 AxisFault (org.apache.axis2.AxisFault)1