react-native-site24x7-rn
Version:
Monitor react native mobile applications with site24x7 Mobile RUM
103 lines (97 loc) • 4.84 kB
JavaScript
import { NativeModules } from 'react-native';
import XHRInterceptor from 'react-native/Libraries/Network/XHRInterceptor';
import S24Utils from './site24x7Utils';
const { Site24x7Rn } = NativeModules;
const trackNetwork = false;
const ignoredUrlDefaultList = ['col.site24x7rum'];
class networkTracker {
constructor(enableNetworkTracking,ignoredUrls){
this.ignoredUrlsList = ignoredUrls.concat(ignoredUrlDefaultList);
this.networkRequestsDataMap = new Map();
if(enableNetworkTracking){
this.monitorNetworkRequests();
}
}
/**
* This method returns a callback method to utilize in the XHRInterceptor.setOpenCallback method.
* It determines the method request, url and XHRInterceptor specific for this device.
* Using that information, this method will create an instance of this device to store for later data gathering.
*
* @param {string} method - The request operation being performed.
* @param {string} url - The destination this request is reaching.
* @param {any} xhr - The interceptor that picked up the request.
*/
handleRequestOpen(method, url, xhr) {
// If this URL is on the IGNORE list, then do nothing.
if (S24Utils.shouldIgnoreFromList(url, this.ignoredUrlsList,true)) {
return;
}
// Obtain the device ID
const id = S24Utils.getDeviceGuid();
// Set the ID of the XHRInterceptor to the device ID
xhr._id_ = id;
// Store the ID and the action taken on the device in a map, ID => REQUEST_META
this.networkRequestsDataMap.set(id,{name: S24Utils.sanitiseURL(url), method: method})
}
/**
* When the XHRInterceptor receives a send request, this method is called. It stores the current time in the relevant
* device RequestMeta object (last known activity).
* @param {string} data - UNUSED.
* @param {any} xhr - The interceptor that picked up the send request.
*/
handleRequestSend(data, xhr) {
// Extract the XHRInterceptor's ID (also the Device's base ID). Use that to get the RequestMeta object from the map
const { _id_ } = xhr;
const requestMeta = this.networkRequestsDataMap.get(_id_);
// If the object exists, then store the current time
if (requestMeta) {
requestMeta.sendTime = Date.now();
}
}
/**
* This method returns a callback method to utilize in the XHRInterceptor.setResponseCallback method.
* Upon receiving a response, the XHRInterceptor calls this method. This method acts like an intermediate step for the
* NetworkTimingCallback. Before calling the 'sendNetworkTimingEvent', this method finds the duration since this device
* has last sent a request (called the handleRequestSend method above), and then it calls the 'sendNetworkTimingEvent'
* parsing the name and sendTime from the RequestMeta along with the calculated duration (Time taken from request to
* response).
* @param {number} status
* @param {number} timeout
* @param {string} resp
* @param {string} respUrl
* @param {string} respType
* @param {any} xhr
*/
handleResponse(status, timeout, resp, respUrl, respType, xhr) {
// Extract the XHRInterceptor's ID (also the Device's base ID). Use that to get the RequestMeta object from the map
const { _id_ } = xhr;
const requestMeta = this.networkRequestsDataMap.get(_id_);
// If the object exists, then ...
if (requestMeta) {
// Extract the name and send time from the Request
const { name, sendTime } = requestMeta;
const duration = Date.now() - sendTime;
//Site24x7Rn.addHttpCall(requestMeta.name,requestMeta.method,''+requestMeta.sendTime,''+duration,''+status,S24Utils.getCurrentScreen());
this.addHttpCalls(requestMeta.name,requestMeta.method,requestMeta.sendTime,duration,status);
}
}
/**
* Option to add http calls manually with url,method,sendTime,statuscode,loadtime of the screen
*
* @param {string} requestUrl
* @param {string} requestMethod
* @param {number} sendTime
* @param {number} loadtime
* @param {number} statusCode
*/
addHttpCalls(requestUrl,requestMethod,sendTime,loadtime,statusCode){
Site24x7Rn.addHttpCall(requestUrl,requestMethod,''+sendTime,''+loadtime,''+statusCode,S24Utils.getCurrentScreen());
}
monitorNetworkRequests(){
XHRInterceptor.setOpenCallback(this.handleRequestOpen.bind(this));
XHRInterceptor.setSendCallback(this.handleRequestSend.bind(this));
XHRInterceptor.setResponseCallback(this.handleResponse.bind(this));
XHRInterceptor.enableInterception();
}
}
module.exports = networkTracker;