Class WebRequestTiming
startWebRequestTiming and stopWebRequestTiming). Each timing event requires an associated tag,
generated by Dynatrace.getRequestTag() or DTXAction.getRequestTag().
How to monitor a web request:
- Generate a new unique tag associated with the user action.
- Generate a
WebRequestTimingobject based on the unique tag. - Place the Dynatrace HTTP header on your web request.
- Start web request timing before the HTTP request is sent.
- Stop web request timing.
- The HTTP response is received, and the response body is obtained.
- A connection exception occurs.
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 Summary
Modifier and TypeMethodDescriptionbooleanUsed to determine if the current object has been finalized.voidBegin timing a web request event.voidDeprecated.voidstopWebRequestTiming(String requestUrl, int respCode, String respPhrase) Stop, finalize, and send a web request timing event.voidstopWebRequestTiming(URI requestUri, int respCode, String respPhrase) Stop, finalize, and send a web request timing event.voidstopWebRequestTiming(URI requestUri, int respCode, String respPhrase, long requestSize, long responseSize) Stop, finalize, and send a web request timing event.voidstopWebRequestTiming(URL requestUrl, int respCode, String respPhrase) Stop, finalize, and send a web request timing event.
-
Method Details
-
startWebRequestTiming
public void startWebRequestTiming()Begin timing a web request event.To complete the timing you need to call
stopWebRequestTimingand 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 responserespPhrase- Response phrase from responserequestUrl- The URL of the request as a string- Throws:
MalformedURLException- is never thrown but has been retained for API stability
-
stopWebRequestTiming
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 responserespPhrase- Response phrase from responserequestUrl- The URL of the request
-
stopWebRequestTiming
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 responserespPhrase- Response phrase from responserequestUri- The URL of the request- Since:
- 8.239
- See Also:
-
stopWebRequestTiming
public void stopWebRequestTiming(URI requestUri, int respCode, String respPhrase, long requestSize, long responseSize) 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 responserespPhrase- Response phrase from responserequestUri- The URL of the requestrequestSize- request sizeresponseSize- response size- Since:
- 8.283
-
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:
trueif the object has been finalized
falseif it has not been finalized
-
stopWebRequestTiming
Deprecated.As of version 8.255, instead use one of the otherstopWebRequestTimingmethods that allow you to specify the response detailsStop, 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)
-
stopWebRequestTimingmethods that allow you to specify the response details