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().

    How to monitor a web request:

    1. Generate a new unique tag associated with the user action.
    2. Generate a WebRequestTiming object based on the unique tag.
    3. Place the Dynatrace HTTP header on your web request.
    4. Start web request timing before the HTTP request is sent.
    5. Stop web request timing.
      1. The HTTP response is received, and the response body is obtained.
      2. A connection exception occurs.
    Web request timing example:
      URL url = new URL("https://www.example.com");
    
     // First, create a custom action
     DTXAction webAction = Dynatrace.enterAction("Search request");
     // [1] Generate a new unique tag associated with the user action
     String uniqueRequestTag = webAction.getRequestTag();
     // [2] Generate a WebRequestTiming object based on the unique tag
     WebRequestTiming timing = Dynatrace.getWebRequestTiming(uniqueRequestTag);
    
     // Define your OkHttp request. This varies greatly depending on your implementation
     Request request = new Request.Builder()
             .url(url)
             // Define your headers for the OkHttp request
             .addHeader(yourKey1, yourValue1)
             .addHeader(yourKey2, yourValue2)
             // [3] Place the Dynatrace HTTP header on your web request
             .addHeader(Dynatrace.getRequestTagHeader(), uniqueRequestTag)
             .build();
    
     // [4] Start web request timing before the HTTP request is sent
     timing.startWebRequestTiming();
     try (Response response = client.newCall(request).execute()) {
         if (response.isSuccessful()) {
             // handle response
             String body = response.body().string();
         }
    
         // [5.1] The HTTP response is received, and the response body is obtained
         timing.stopWebRequestTiming(url, response.code(), response.message());
     } catch (IOException e) {
         // [5.2] A connection exception occurs
         timing.stopWebRequestTiming(url, -1, e.toString());
    
         // user-defined exception handling
     }
     finally {
         // Lastly, end the custom action
         webAction.leaveAction();
     }
     
    • Method Detail

      • startWebRequestTiming

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

        To complete the timing you need to call stopWebRequestTiming and specify the response details there

      • 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()
        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 valid scheme otherwise 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

        @Deprecated
        public void stopWebRequestTiming()
        Deprecated.
        As of version 8.255, instead use one of the other stopWebRequestTiming methods that allow you to specify the response details
        Stop, finalize, and send a web request timing event. This can only be called once on each object.
        Only use this method together with Dynatrace.getWebRequestTiming(HttpURLConnection)