Search in sources :

Example 6 with TracingSpan

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

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

Example 8 with TracingSpan

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

the class APIAuthenticationHandler method handleRequest.

@MethodStats
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "EXS_EXCEPTION_SOFTENING_RETURN_FALSE", justification = "Error is sent through payload")
public boolean handleRequest(MessageContext messageContext) {
    TracingSpan keySpan = null;
    if (Util.tracingEnabled()) {
        TracingSpan responseLatencySpan = (TracingSpan) messageContext.getProperty(APIMgtGatewayConstants.RESOURCE_SPAN);
        TracingTracer tracer = Util.getGlobalTracer();
        keySpan = Util.startSpan(APIMgtGatewayConstants.KEY_VALIDATION, responseLatencySpan, tracer);
        messageContext.setProperty(APIMgtGatewayConstants.KEY_VALIDATION, keySpan);
        org.apache.axis2.context.MessageContext axis2MC = ((Axis2MessageContext) messageContext).getAxis2MessageContext();
        axis2MC.setProperty(APIMgtGatewayConstants.KEY_VALIDATION, keySpan);
    }
    Timer.Context context = startMetricTimer();
    long startTime = System.nanoTime();
    long endTime;
    long difference;
    if (Utils.isGraphQLSubscriptionRequest(messageContext)) {
        if (log.isDebugEnabled()) {
            log.debug("Skipping GraphQL subscription handshake request.");
        }
        return true;
    }
    try {
        if (isAnalyticsEnabled()) {
            long currentTime = System.currentTimeMillis();
            messageContext.setProperty("api.ut.requestTime", Long.toString(currentTime));
        }
        messageContext.setProperty(APIMgtGatewayConstants.API_TYPE, apiType);
        if (ExtensionListenerUtil.preProcessRequest(messageContext, type)) {
            if (!isAuthenticatorsInitialized) {
                initializeAuthenticators();
            }
            if (!isOauthParamsInitialized) {
                initOAuthParams();
            }
            String authenticationScheme = getAPIKeyValidator().getResourceAuthenticationScheme(messageContext);
            if (APIConstants.AUTH_NO_AUTHENTICATION.equals(authenticationScheme)) {
                if (log.isDebugEnabled()) {
                    log.debug("Found Authentication Scheme: ".concat(authenticationScheme));
                }
                handleNoAuthentication(messageContext);
                return true;
            }
            try {
                if (isAuthenticate(messageContext)) {
                    setAPIParametersToMessageContext(messageContext);
                    return ExtensionListenerUtil.postProcessRequest(messageContext, type);
                }
            } catch (APIManagementException e) {
                if (log.isDebugEnabled()) {
                    log.debug("Authentication of message context failed", e);
                }
            }
        }
    } catch (APISecurityException e) {
        if (Util.tracingEnabled() && keySpan != null) {
            Util.setTag(keySpan, APIMgtGatewayConstants.ERROR, APIMgtGatewayConstants.KEY_SPAN_ERROR);
        }
        if (log.isDebugEnabled()) {
            // We do the calculations only if the debug logs are enabled. Otherwise this would be an overhead
            // to all the gateway calls that is happening.
            endTime = System.nanoTime();
            difference = (endTime - startTime) / 1000000;
            String messageDetails = logMessageDetails(messageContext);
            log.debug("Call to Key Manager : " + messageDetails + ", elapsedTimeInMilliseconds=" + difference / 1000000);
        }
        String errorMessage = APISecurityConstants.getAuthenticationFailureMessage(e.getErrorCode());
        if (APISecurityConstants.API_AUTH_GENERAL_ERROR_MESSAGE.equals(errorMessage)) {
            log.error("API authentication failure due to " + APISecurityConstants.API_AUTH_GENERAL_ERROR_MESSAGE, e);
        } else {
            // We do not need to log known authentication failures as errors since these are not product errors.
            log.warn("API authentication failure due to " + errorMessage);
            if (log.isDebugEnabled()) {
                log.debug("API authentication failed with error " + e.getErrorCode(), e);
            }
        }
        handleAuthFailure(messageContext, e);
    } finally {
        if (Util.tracingEnabled()) {
            Util.finishSpan(keySpan);
        }
        messageContext.setProperty(APIMgtGatewayConstants.SECURITY_LATENCY, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime));
        stopMetricTimer(context);
    }
    return false;
}
Also used : Timer(org.wso2.carbon.metrics.manager.Timer) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) TracingTracer(org.wso2.carbon.apimgt.tracing.TracingTracer) TracingSpan(org.wso2.carbon.apimgt.tracing.TracingSpan) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) MethodStats(org.wso2.carbon.apimgt.gateway.MethodStats)

Example 9 with TracingSpan

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

the class APIMgtLatencyStatsHandler method handleRequest.

public boolean handleRequest(MessageContext messageContext) {
    if (Util.tracingEnabled()) {
        TracingSpan responseLatencySpan = (TracingSpan) messageContext.getProperty(APIMgtGatewayConstants.RESPONSE_LATENCY);
        TracingTracer tracer = Util.getGlobalTracer();
        TracingSpan span = Util.startSpan(APIMgtGatewayConstants.RESOURCE_SPAN, responseLatencySpan, tracer);
        messageContext.setProperty(APIMgtGatewayConstants.RESOURCE_SPAN, span);
    }
    messageContext.setProperty(APIMgtGatewayConstants.API_UUID_PROPERTY, apiUUID);
    org.apache.axis2.context.MessageContext axis2MsgContext = ((Axis2MessageContext) messageContext).getAxis2MessageContext();
    if (messageContext.getProperty(APIMgtGatewayConstants.REQUEST_EXECUTION_START_TIME) == null) {
        messageContext.setProperty(APIMgtGatewayConstants.REQUEST_EXECUTION_START_TIME, Long.toString(System.currentTimeMillis()));
        String method = (String) (axis2MsgContext.getProperty(Constants.Configuration.HTTP_METHOD));
        messageContext.setProperty(APIMgtGatewayConstants.HTTP_METHOD, method);
    }
    /*
        * The axis2 message context is set here so that the method level logging can access the transport headers
        */
    org.apache.axis2.context.MessageContext.setCurrentMessageContext(axis2MsgContext);
    long currentTime = System.currentTimeMillis();
    messageContext.setProperty("api.ut.requestTime", Long.toString(currentTime));
    setSwaggerToMessageContext(messageContext);
    return true;
}
Also used : TracingTracer(org.wso2.carbon.apimgt.tracing.TracingTracer) TracingSpan(org.wso2.carbon.apimgt.tracing.TracingSpan) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 10 with TracingSpan

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

the class APIManagerExtensionHandler method handleRequest.

@MethodStats
public boolean handleRequest(MessageContext messageContext) {
    // (only for graphQL APIs)
    if (messageContext.getProperty(APIConstants.API_TYPE) != null && APIConstants.GRAPHQL_API.equals(messageContext.getProperty(APIConstants.API_TYPE).toString())) {
        ((Axis2MessageContext) messageContext).getAxis2MessageContext().setProperty(Constants.Configuration.HTTP_METHOD, messageContext.getProperty(APIConstants.HTTP_VERB));
    }
    Timer.Context context = startMetricTimer(DIRECTION_IN);
    long executionStartTime = System.nanoTime();
    TracingSpan requestMediationSpan = null;
    if (Util.tracingEnabled()) {
        TracingSpan responseLatencySpan = (TracingSpan) messageContext.getProperty(APIMgtGatewayConstants.RESOURCE_SPAN);
        TracingTracer tracer = Util.getGlobalTracer();
        requestMediationSpan = Util.startSpan(APIMgtGatewayConstants.REQUEST_MEDIATION, responseLatencySpan, tracer);
    }
    try {
        boolean isMediated = mediate(messageContext, DIRECTION_IN);
        if (isMediated) {
            String requestDestination = null;
            EndpointReference objectTo = ((Axis2MessageContext) messageContext).getAxis2MessageContext().getOptions().getTo();
            if (objectTo != null) {
                requestDestination = objectTo.getAddress();
            }
            if (requestDestination != null) {
                messageContext.setProperty(APIMgtGatewayConstants.SYNAPSE_ENDPOINT_ADDRESS, requestDestination);
            }
        }
        return isMediated;
    } catch (Exception e) {
        if (Util.tracingEnabled() && requestMediationSpan != null) {
            Util.setTag(requestMediationSpan, APIMgtGatewayConstants.ERROR, APIMgtGatewayConstants.REQUEST_MEDIATION_ERROR);
        }
        throw e;
    } finally {
        if (Util.tracingEnabled()) {
            Util.finishSpan(requestMediationSpan);
        }
        messageContext.setProperty(APIMgtGatewayConstants.REQUEST_MEDIATION_LATENCY, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - executionStartTime));
        stopMetricTimer(context);
    }
}
Also used : Timer(org.wso2.carbon.metrics.manager.Timer) TracingTracer(org.wso2.carbon.apimgt.tracing.TracingTracer) TracingSpan(org.wso2.carbon.apimgt.tracing.TracingSpan) EndpointReference(org.apache.axis2.addressing.EndpointReference) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) MethodStats(org.wso2.carbon.apimgt.gateway.MethodStats)

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