Send a Network Event

  1. Report a Network Request Event using the HttpRequestTracker class. This call automatically adds an event to the in-memory buffer, so you need to explicitly import the class.
    import com.appdynamics.iot.HttpRequestTracker;
    ...        
    String url = "http://ip.jsontest.com/?callback=showMyIP";        
    // Add a Network Event
    try {
        URL thisUrl = new URL(url);
        // [Splunk AppDynamics Instrumentation] Get a Tracker            
        HttpURLConnection con = (HttpURLConnection) thisUrl.openConnection();            
        final HttpRequestTracker tracker = Instrumentation.beginHttpRequest(thisUrl);
        con.setRequestMethod("POST");
        con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");            
        int responseCode = con.getResponseCode();            
        con.setDoInput(true);
        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.flush();
        wr.close();
        System.out.println("Response Code :" + responseCode);            
        // [Splunk AppDynamics Instrumentation] Retrieve the headers from the response
        Map<String, List<String>> headerFields = null;
        System.out.println("Sending 'POST' request to URL :" + url);
        BufferedReader in;
        String inputLine;
        new InputStreamReader(con.getErrorStream()));
        if (responseCode >= 200 && responseCode < 300) {
            in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        } else {
            in = new BufferedReader(
        }
        StringBuffer response = new StringBuffer();
        if (headerFields != null && headerFields.size() > 0){
            while ((inputLine = in.readLine()) != null) {
               response.append(inputLine);
            }
            in.close();
            // [Splunk AppDynamics Instrumentation] Initiate adding NetworkRequestEvent
            if (responseCode >= 200 && responseCode < 300) {
                tracker.withResponseCode(responseCode).withError(response.toString()).reportDone();
                       .withResponseHeaderFields(headerFields)                    
                       .reportDone();
            } else {
                tracker.withResponseCode(responseCode).reportDone();
            }
        } else {
            tracker.withResponseCode(responseCode)
        } 
    // End: Add for Splunk AppDynamics Instrumentation - Initiate adding NetworkRequestEvent
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (Exception ex) {            
        ex.printStackTrace();
    }
  2. Send all the events to the EUM Server. This is a blocking call. The application can send it on a separate thread. The recommendation is to batch a number of events together before calling the sendAllEvents method.
    Instrumentation.sendAllEvents();