In WSO2 API Manager, we have two transports. HTTP servlet transport and Passthru / NIO transport. All the web application requests are handled through HTTP servlet transport which is on 9763 port and 9443 port with ssl and here we are using tomcat inside WSO2 products. All the service requests are served via Passthru / NIO transport which is on 8082 and 8243 with ssl. When we integrate API Manager with new relic in the way discussed in blog posts [5],[6], new relic only detects the calls made to tomcat transports. So we couldn’t get the API calls related data OOTB.
But by further analyzing new relic APIs I managed to find a workaround for this problem. New relic supports publishing custom events via their insights api[1]. So what we can do is publish these data via custom API handler[2]. Following is a sample implementation of a handler that I used to test the scenario. I will attach the full project herewith[7]. I have created an osgi bundle with this implementation so after building the jar with maven you can drop into $APIM_HOME/repository/components/dropins directory. And make sure you do the necessary changes to this class to match your scenario.
Since you need engage this handler to all apis you need add following to velocity template as mentioned in the documentation[3]. But for existing apis you will need to add this via source view as mentioned in documentation[3]. Or you can do the same by republishing all existing apis. You will need to add this to both
And further to execute this I added following to the newrelic.yml file.
You will be able to see API invocation data there. Good Luck :).
But by further analyzing new relic APIs I managed to find a workaround for this problem. New relic supports publishing custom events via their insights api[1]. So what we can do is publish these data via custom API handler[2]. Following is a sample implementation of a handler that I used to test the scenario. I will attach the full project herewith[7]. I have created an osgi bundle with this implementation so after building the jar with maven you can drop into $APIM_HOME/repository/components/dropins directory. And make sure you do the necessary changes to this class to match your scenario.
package org.wso2.carbon.custom.handler.newrelic;
import com.newrelic.api.agent.Agent;
import com.newrelic.api.agent.Trace;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.synapse.MessageContext;
import org.apache.synapse.rest.AbstractHandler;
import java.util.HashMap;
import java.util.Map;
import com.newrelic.api.agent.NewRelic;
public class NewRelicHandler extends AbstractHandler {
private static final Log log = LogFactory.getLog(NewRelicHandler.class);
@Trace
public boolean handleRequest(MessageContext messageContext) {
String endpoint = messageContext.getTo().getAddress();
Agent agent = NewRelic.getAgent();
Map<String, Object> eventAttributes = new HashMap<String, Object>();
eventAttributes.put("appId",32034852);
eventAttributes.put("appName","APIM");
eventAttributes.put("host","localhost");
eventAttributes.put("name",endpoint);
eventAttributes.put("transactionType","Web");
eventAttributes.put("type","Transaction");
eventAttributes.put("realAgentId",32034854);
eventAttributes.put("transactionSubType","API Call");
agent.getInsights().recordCustomEvent("API_calls", eventAttributes);
return true;
}
public boolean handleResponse(MessageContext messageContext) {
//TODO: handle response path
return true;
}
}
Since you need engage this handler to all apis you need add following to velocity template as mentioned in the documentation[3]. But for existing apis you will need to add this via source view as mentioned in documentation[3]. Or you can do the same by republishing all existing apis. You will need to add this to both
$APIM_HOME/repository/resources/api_templates/velocity_template.xml
and $APIM_HOME/repository/resources/api_templates/velocity_template.xml
files since this will be needed in default api as well.
<handler class="org.wso2.carbon.custom.handler.newrelic.NewRelicHandler" />
And further to execute this I added following to the newrelic.yml file.
custom_insights_events.max_samples_stored: 5000
custom_insights_events.enabled: true
Now create publish and subscribe an api (if you haven’t done already) and invoke the api several times. Then go to [4] and create a new dashboard with query
SELECT * FROM API_calls
You will be able to see API invocation data there. Good Luck :).
References
[1] https://docs.newrelic.com/docs/insights/new-relic-insights/custom-events/insert-custom-events-insights-api
[2] https://docs.wso2.com/display/AM1100/Writing+Custom+Handlers
[3] https://docs.wso2.com/display/AM1100/Writing+Custom+Handlers#WritingCustomHandlers-Engagingthecustomhandler
[4] https://insights.newrelic.com/
[5] http://blog.lasindu.com/2016/07/wso2-application-and-server-monitoring.html
[6] https://scktech.blogspot.com/2016/08/integrate-wso2-products-with-new-relic.html
[2] https://docs.wso2.com/display/AM1100/Writing+Custom+Handlers
[3] https://docs.wso2.com/display/AM1100/Writing+Custom+Handlers#WritingCustomHandlers-Engagingthecustomhandler
[4] https://insights.newrelic.com/
[5] http://blog.lasindu.com/2016/07/wso2-application-and-server-monitoring.html
[6] https://scktech.blogspot.com/2016/08/integrate-wso2-products-with-new-relic.html
Comments
Post a Comment