Class WebRequestTiming


  • public class WebRequestTiming
    extends Object
    This class represents a network timing event for reporting the time elapsed between two user-defined time points (startWebRequestTiming and stopWebRequestTiming). Each timing event requires an associated tag, generated by Dynatrace.getRequestTag() or DTXAction.getRequestTag().

    Web request timing example:

     // [1] Create an action for the web request
     DTXAction webAction = Dynatrace.enterAction("yourActionName");
    
     HttpURLConnection urlConnection = null;
     WebRequestTiming timing = null;
     try{
            URL url = new URL("http://www.example.com");
            urlConnection = (HttpURLConnection) url.openConnection();
            // [2] Tag the web request automatically and receive a WebRequestTiming instance
            timing = Dynatrace.getWebRequestTiming(urlConnection);
    
            // [3] Call startWebRequestTiming() to begin the timing (and then handle the input stream from the connection)
            timing.startWebRequestTiming();
    
            InputStream input = new BufferedInputStream(urlConnection.getInputStream());
            readStream(input);
    
            // [4] Once we're done reading, we can stop the timing
            timing.stopWebRequestTiming();
    
            processData();
     }
     catch (Exception exception) {
            // [5a] Finalize the timing when an error occurs
            if(timing != null) {
                    timing.stopWebRequestTiming();
            }
            // [5b] Attach the exception to the action
            webAction.reportError("Exception", e);
    
            // user-defined exception handling
            handleError(exception);
     }
     finally {
            if(urlConnection != null) {
                    urlConnection.disconnect();
            }
    
            // [6] Lastly finalize the timing of the action in the finally block.
            webAction.leaveAction();
     }
    
     
    • Method Detail

      • startWebRequestTiming

        public void startWebRequestTiming()
        Begin timing a web request event.

        You must call stopWebRequestTiming() to complete the timing.

      • stopWebRequestTiming

        public void stopWebRequestTiming​(String requestUrl,
                                         int respCode,
                                         String respPhrase)
                                  throws MalformedURLException
        Stop, finalize, and send a web request timing event. This can only be called once on each object. This method has to be called after startWebRequestTiming() The given requestUrl must have a scheme that is either http, https, ws, wss or file. Other schemes are rejected and the web request timing event is dropped.
        Parameters:
        respCode - Status code from response
        respPhrase - Response phrase from response
        requestUrl - The URL of the request as a string
        Throws:
        MalformedURLException - is never thrown but has been retained for API stability
      • stopWebRequestTiming

        public void stopWebRequestTiming​(URL requestUrl,
                                         int respCode,
                                         String respPhrase)
        Stop, finalize, and send a web request timing event. This can only be called once on each object. This method has to be called after startWebRequestTiming()
        Parameters:
        respCode - Status code from response
        respPhrase - Response phrase from response
        requestUrl - The URL of the request
      • stopWebRequestTiming

        public void stopWebRequestTiming​(URI requestUri,
                                         int respCode,
                                         String respPhrase)
        Stop, finalize, and send a web request timing event. This can only be called once on each object. This method has to be called after startWebRequestTiming(). The given URI must have a scheme that is either http, https, ws, wss or file. Other schemes are rejected and the web request timing event is dropped.
        Parameters:
        respCode - Status code from response
        respPhrase - Response phrase from response
        requestUri - The URL of the request
        Since:
        8.239
      • isFinalized

        public boolean isFinalized()
        Used to determine if the current object has been finalized. Finalized objects cannot be restarted or reused. Calling stopWebRequestTiming() finalizes the object and prepares the data for the next sending interval.
        Returns:
        true if the object has been finalized
        false if it has not been finalized
      • stopWebRequestTiming

        public void stopWebRequestTiming()
        Stop, finalize, and send a web request timing event. This can only be called once on each object.