Search in sources :

Example 1 with TracingTracer

use of org.wso2.carbon.apimgt.tracing.TracingTracer 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 2 with TracingTracer

use of org.wso2.carbon.apimgt.tracing.TracingTracer 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 3 with TracingTracer

use of org.wso2.carbon.apimgt.tracing.TracingTracer 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)

Example 4 with TracingTracer

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

the class ThrottleHandler method handleRequest.

/**
 * Handle incoming requests and call throttling method to perform throttling.
 *
 * @param messageContext message context object which contains message details.
 * @return return true if message flow need to continue and pass requests to next handler in chain. Else return
 * false to notify error with handler
 */
public boolean handleRequest(MessageContext messageContext) {
    if (Utils.isGraphQLSubscriptionRequest(messageContext)) {
        if (log.isDebugEnabled()) {
            log.debug("Skipping GraphQL subscription handshake request.");
        }
        return true;
    }
    if (ServiceReferenceHolder.getInstance().getThrottleDataPublisher() == null) {
        log.error("Cannot publish events to traffic manager because ThrottleDataPublisher " + "has not been initialised");
        return true;
    }
    Timer timer3 = getTimer(MetricManager.name(APIConstants.METRICS_PREFIX, this.getClass().getSimpleName(), THROTTLE_MAIN));
    Timer.Context context3 = timer3.start();
    TracingSpan throttleLatencySpan = null;
    if (Util.tracingEnabled()) {
        TracingSpan responseLatencySpan = (TracingSpan) messageContext.getProperty(APIMgtGatewayConstants.RESOURCE_SPAN);
        TracingTracer tracer = Util.getGlobalTracer();
        throttleLatencySpan = Util.startSpan(APIMgtGatewayConstants.THROTTLE_LATENCY, responseLatencySpan, tracer);
    }
    long executionStartTime = System.currentTimeMillis();
    if (!ExtensionListenerUtil.preProcessRequest(messageContext, type)) {
        return false;
    }
    try {
        boolean throttleResponse = doThrottle(messageContext);
        if (!ExtensionListenerUtil.postProcessRequest(messageContext, type)) {
            return false;
        }
        return throttleResponse;
    } catch (Exception e) {
        if (Util.tracingEnabled() && throttleLatencySpan != null) {
            Util.setTag(throttleLatencySpan, APIMgtGatewayConstants.ERROR, APIMgtGatewayConstants.THROTTLE_HANDLER_ERROR);
        }
        throw e;
    } finally {
        messageContext.setProperty(APIMgtGatewayConstants.THROTTLING_LATENCY, System.currentTimeMillis() - executionStartTime);
        context3.stop();
        if (Util.tracingEnabled()) {
            Util.finishSpan(throttleLatencySpan);
        }
    }
}
Also used : Timer(org.wso2.carbon.metrics.manager.Timer) TracingTracer(org.wso2.carbon.apimgt.tracing.TracingTracer) TracingSpan(org.wso2.carbon.apimgt.tracing.TracingSpan) XMLStreamException(javax.xml.stream.XMLStreamException) ThrottleException(org.apache.synapse.commons.throttle.core.ThrottleException)

Example 5 with TracingTracer

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

the class APIThrottleHandler method handleRequest.

public boolean handleRequest(MessageContext messageContext) {
    Timer timer = getTimer();
    Timer.Context context = timer.start();
    long executionStartTime = System.nanoTime();
    TracingSpan throttlingLatencySpan = null;
    if (Util.tracingEnabled()) {
        TracingSpan responseLatencySpan = (TracingSpan) messageContext.getProperty(APIMgtGatewayConstants.RESOURCE_SPAN);
        TracingTracer tracer = Util.getGlobalTracer();
        throttlingLatencySpan = Util.startSpan(APIMgtGatewayConstants.THROTTLE_LATENCY, responseLatencySpan, tracer);
    }
    try {
        return doThrottle(messageContext);
    } catch (SynapseException e) {
        if (Util.tracingEnabled()) {
            Util.setTag(throttlingLatencySpan, APIMgtGatewayConstants.ERROR, APIMgtGatewayConstants.API_THROTTLE_HANDLER_ERROR);
        }
        throw e;
    } finally {
        messageContext.setProperty(APIMgtGatewayConstants.THROTTLING_LATENCY, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - executionStartTime));
        context.stop();
        if (Util.tracingEnabled()) {
            Util.finishSpan(throttlingLatencySpan);
        }
    }
}
Also used : Timer(org.wso2.carbon.metrics.manager.Timer) SynapseException(org.apache.synapse.SynapseException) TracingTracer(org.wso2.carbon.apimgt.tracing.TracingTracer) TracingSpan(org.wso2.carbon.apimgt.tracing.TracingSpan)

Aggregations

TracingSpan (org.wso2.carbon.apimgt.tracing.TracingSpan)11 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 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 OMElement (org.apache.axiom.om.OMElement)1 AxisFault (org.apache.axis2.AxisFault)1 EndpointReference (org.apache.axis2.addressing.EndpointReference)1 MessageContext (org.apache.axis2.context.MessageContext)1