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);
}
}
}
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;
}
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;
}
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);
}
}
}
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);
}
}
}
Aggregations