use of org.wso2.carbon.apimgt.tracing.TracingTracer 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;
}
use of org.wso2.carbon.apimgt.tracing.TracingTracer 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;
}
use of org.wso2.carbon.apimgt.tracing.TracingTracer 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);
}
}
use of org.wso2.carbon.apimgt.tracing.TracingTracer in project carbon-apimgt by wso2.
the class APIManagerExtensionHandler method handleResponse.
@MethodStats
public boolean handleResponse(MessageContext messageContext) {
Timer.Context context = startMetricTimer(DIRECTION_OUT);
long executionStartTime = System.nanoTime();
TracingSpan responseMediationSpan = null;
if (Util.tracingEnabled()) {
TracingSpan responseLatencySpan = (TracingSpan) messageContext.getProperty(APIMgtGatewayConstants.RESOURCE_SPAN);
TracingTracer tracer = Util.getGlobalTracer();
responseMediationSpan = Util.startSpan(APIMgtGatewayConstants.RESPONSE_MEDIATION, responseLatencySpan, tracer);
}
try {
return mediate(messageContext, DIRECTION_OUT);
} catch (Exception e) {
if (Util.tracingEnabled() && responseMediationSpan != null) {
Util.setTag(responseMediationSpan, APIMgtGatewayConstants.ERROR, APIMgtGatewayConstants.RESPONSE_MEDIATION_ERROR);
}
throw e;
} finally {
if (Util.tracingEnabled()) {
Util.finishSpan(responseMediationSpan);
}
messageContext.setProperty(APIMgtGatewayConstants.RESPONSE_MEDIATION_LATENCY, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - executionStartTime));
stopMetricTimer(context);
}
}
use of org.wso2.carbon.apimgt.tracing.TracingTracer in project carbon-apimgt by wso2.
the class CORSRequestHandler method handleRequest.
@MethodStats
public boolean handleRequest(MessageContext messageContext) {
Timer.Context context = startMetricTimer();
TracingSpan CORSRequestHandlerSpan = null;
if (Util.tracingEnabled()) {
TracingSpan responseLatencySpan = (TracingSpan) messageContext.getProperty(APIMgtGatewayConstants.RESOURCE_SPAN);
TracingTracer tracer = Util.getGlobalTracer();
CORSRequestHandlerSpan = Util.startSpan(APIMgtGatewayConstants.CORS_REQUEST_HANDLER, responseLatencySpan, tracer);
}
if (Utils.isGraphQLSubscriptionRequest(messageContext)) {
if (log.isDebugEnabled()) {
log.debug("Skipping GraphQL subscription handshake request.");
}
return true;
}
try {
if (!initializeHeaderValues) {
initializeHeaders();
}
String apiContext = (String) messageContext.getProperty(RESTConstants.REST_API_CONTEXT);
String apiVersion = (String) messageContext.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION);
String httpMethod = (String) ((Axis2MessageContext) messageContext).getAxis2MessageContext().getProperty(Constants.Configuration.HTTP_METHOD);
API selectedApi = Utils.getSelectedAPI(messageContext);
org.apache.axis2.context.MessageContext axis2MC = ((Axis2MessageContext) messageContext).getAxis2MessageContext();
Map headers = (Map) axis2MC.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
String corsRequestMethod = (String) headers.get(APIConstants.CORSHeaders.ACCESS_CONTROL_REQUEST_METHOD);
Resource selectedResource = null;
Utils.setSubRequestPath(selectedApi, messageContext);
if (selectedApi != null) {
Resource[] allAPIResources = selectedApi.getResources();
Set<Resource> acceptableResources = new LinkedHashSet<>();
for (Resource resource : allAPIResources) {
// If the requesting method is OPTIONS or if the Resource contains the requesting method
if ((RESTConstants.METHOD_OPTIONS.equals(httpMethod) && resource.getMethods() != null && Arrays.asList(resource.getMethods()).contains(corsRequestMethod)) || (resource.getMethods() != null && Arrays.asList(resource.getMethods()).contains(httpMethod))) {
acceptableResources.add(resource);
}
}
if (!acceptableResources.isEmpty()) {
for (RESTDispatcher dispatcher : RESTUtils.getDispatchers()) {
Resource resource = dispatcher.findResource(messageContext, acceptableResources);
if (resource != null) {
selectedResource = resource;
break;
}
}
if (selectedResource == null) {
handleResourceNotFound(messageContext, Arrays.asList(allAPIResources));
return false;
}
} else // If no acceptable resources are found
{
// We're going to send a 405 or a 404. Run the following logic to determine which.
handleResourceNotFound(messageContext, Arrays.asList(allAPIResources));
return false;
}
// No matching resource found
if (selectedResource == null) {
// Respond with a 404
onResourceNotFoundError(messageContext, HttpStatus.SC_NOT_FOUND, APIMgtGatewayConstants.RESOURCE_NOT_FOUND_ERROR_MSG);
return false;
}
}
String resourceString = selectedResource.getDispatcherHelper().getString();
String resourceCacheKey = APIUtil.getResourceInfoDTOCacheKey(apiContext, apiVersion, resourceString, httpMethod);
messageContext.setProperty(APIConstants.API_ELECTED_RESOURCE, resourceString);
messageContext.setProperty(APIConstants.API_RESOURCE_CACHE_KEY, resourceCacheKey);
messageContext.setProperty(APIConstants.REST_METHOD, httpMethod);
// If this is an OPTIONS request
if (APIConstants.SupportedHTTPVerbs.OPTIONS.name().equalsIgnoreCase(httpMethod)) {
// If the OPTIONS method is explicity specified in the resource
if (Arrays.asList(selectedResource.getMethods()).contains(APIConstants.SupportedHTTPVerbs.OPTIONS.name())) {
// We will not handle the CORS headers, let the back-end do it.
return true;
}
setCORSHeaders(messageContext, selectedResource);
Mediator corsSequence = messageContext.getSequence(APIConstants.CORS_SEQUENCE_NAME);
if (corsSequence != null) {
corsSequence.mediate(messageContext);
}
Utils.send(messageContext, HttpStatus.SC_OK);
return false;
} else if (APIConstants.IMPLEMENTATION_TYPE_INLINE.equalsIgnoreCase(apiImplementationType)) {
setCORSHeaders(messageContext, selectedResource);
messageContext.getSequence(APIConstants.CORS_SEQUENCE_NAME).mediate(messageContext);
}
setCORSHeaders(messageContext, selectedResource);
return true;
} catch (Exception e) {
if (Util.tracingEnabled() && CORSRequestHandlerSpan != null) {
Util.setTag(CORSRequestHandlerSpan, APIMgtGatewayConstants.ERROR, APIMgtGatewayConstants.CORS_REQUEST_HANDLER_ERROR);
}
throw e;
} finally {
stopMetricTimer(context);
if (Util.tracingEnabled()) {
Util.finishSpan(CORSRequestHandlerSpan);
}
}
}
Aggregations