In previous post I described how to publish API traffic to new relic[1]. I have done some modifications to the handler to cater $subject. You will need to change the code to match your exact requirements. And I did some improvements to the code as well. Please find my modified code attachment[2].
I have added following code segments to the handler let me describe of each segment.
Following segment is to build message when POST type is used. Otherwise inside handler we will get an empty envelop.
try {
RelayUtils.buildMessage(((Axis2MessageContext)messageContext).getAxis2MessageContext());
} catch (Exception e) {
log.warn("Error occured while building message");
if(log.isDebugEnabled()){
log.debug("Exception thrown while building message", e);
}
}
Here I push data to new relic, each parameter as a different column. So please change the code as it match your requirement.
Following code segment will extract post data from the request. But this to work properly you will need to set content-type in requests and also backend responses.
try {
OMElement bodyFirstElement = messageContext.getEnvelope().getBody().getFirstElement();
if (bodyFirstElement != null) {
Iterator iterator = bodyFirstElement.getChildElements();
while (iterator.hasNext()) {
OMElement element = iterator.next();
eventAttributes.put(element.getLocalName(), element.getText());
}
}
} catch (Exception e){
log.warn("Error occured while extracting data form the request, hense ignoring");
if(log.isDebugEnabled()){
log.debug("Exception thrown while extracting data from request", e);
}
}
Following code segment will capture the query params you provide the api.
try {
Object urlPostfix = ((Axis2MessageContext) messageContext).getAxis2MessageContext().getProperty("REST_URL_POSTFIX");
else if (urlPostfix != null) {
String[] urlParts = urlPostfix.toString().split("\\?");
if (urlParts.length > 0) {
String queryParamString = urlParts[1];
String[] pairs = queryParamString.split("&");
for (String pair : pairs) {
String[] keyValues = pair.split("=");
if (keyValues.length > 0) {
eventAttributes.put(keyValues[0], keyValues[1]);
} else {
eventAttributes.put(keyValues[0], "");
}
}
}
}
} catch (Exception e){
log.warn("Error occured while extracting data form the request, hense ignoring");
if(log.isDebugEnabled()){
log.debug("Exception thrown while extracting data from request", e);
}
}
Good Luck!!! :)
Comments
Post a Comment