Web request performance monitoring

  • Latest Dynatrace
  • Explanation

The OneAgent for Android provides comprehensive web request monitoring capabilities

  • Automatic instrumentation—automatically captures all web requests made via HttpUrlConnection or OkHttp.
  • W3C Trace Context—enables distributed tracing by propagating trace context headers.
  • Manual reporting—report web requests from custom networking implementations.
  • Event enrichment—add custom properties to web request events.

Automatic web request instrumentation

The OneAgent for Android automatically instruments web requests made using Android's built-in HttpUrlConnection API and stores them as events in Grail. The same is true for requests made with the widely used OkHttp library.

Each event consists of well-defined key-value fields as specified in the Semantic Dictionary for user events.

This data can be queried directly in Grail using DQL.

What is captured automatically

An exact overview of the reported data and its structure can be found in the (semantic-dictionary-user-events#request). The following list provides an overview of data that is automatically monitored by OneAgent:

  • Request URL.
  • HTTP method (GET, POST, PUT, DELETE, etc.).
  • Response status code.
  • Request duration.
  • Exception (if the request fails).

The data captured will be structured and reported according to the fields specified in request.*.

Supported APIs

Automatic instrumentation works with the following:

If your web request library is based on one of these supported frameworks, the internal classes of the library are automatically instrumented. For example, Retrofit version 2 is based on OkHttp, so all Retrofit web requests are automatically instrumented.

Limitations

  • Non-HTTP protocols are not supported.
  • Requests from unsupported frameworks must be manually instrumented.
  • Using several monitoring tools simultaneously with web request instrumentation functionality enabled isn't recommended. This can lead to compatibility problems, incorrect or invalid data reporting, and loss of monitoring information. If you choose to do so, thoroughly test to ensure the tools work together correctly.

Disable automatic instrumentation

To disable automatic web request instrumentation, set the webRequests.enabled flag to false in the Dynatrace configurations block of your app's build.gradle file:

dynatrace {
configurations {
sampleConfig {
webRequests.enabled false
}
}
}

W3C Trace Context (distributed tracing)

The SDK supports W3C Trace Context for distributed tracing, allowing you to correlate mobile requests with backend services instrumented by Dynatrace.

How it works

When automatic instrumentation is enabled, the SDK automatically adds the W3C trace context header to outgoing requests:

  • traceparent—identifies the request as part of a distributed trace.

This enables end-to-end visibility from the mobile app through your backend services.

Report web requests manually

For networking libraries not covered by automatic instrumentation, you can manually report web requests using HttpRequestEventData.

Basic usage

import com.dynatrace.android.agent.Dynatrace
import com.dynatrace.android.agent.HttpRequestEventData
// Create the request data with the URL and HTTP method
val requestData = HttpRequestEventData("https://api.example.com/data", "GET")
.withDuration(250) // Duration in milliseconds
.withStatusCode(200) // HTTP status code
.withBytesSent(128) // Bytes sent
.withBytesReceived(4096) // Bytes received
// Send the web request event
Dynatrace.sendHttpRequestEvent(requestData)

Failed request

When a request fails, include the error information:

val requestData = HttpRequestEventData("https://api.example.com/data", "POST")
.withDuration(1500)
.withThrowable(exception)
Dynatrace.sendHttpRequestEvent(requestData)

Include trace context

To correlate manually reported requests with distributed traces, include the traceparent header:

// Report the event with the traceparent
val requestData = HttpRequestEventData(url, "GET")
.withDuration(duration)
.withStatusCode(statusCode)
.withTraceparentHeader(traceparent)
Dynatrace.sendHttpRequestEvent(requestData)

Add custom properties

Add custom event properties to capture additional context:

val requestData = HttpRequestEventData(url, "POST")
.withDuration(300)
.withStatusCode(201)
.addEventProperty("event_properties.api_version", "v2")
.addEventProperty("event_properties.endpoint", "users")
Dynatrace.sendHttpRequestEvent(requestData)

Custom property keys must be prefixed with event_properties.—properties without this prefix will be dropped.

Event enrichment (modifiers)

Event modifiers allow you to enrich web request events with additional data before they are sent. This is useful for adding context from the request/response that isn't captured automatically. OneAgent for Android provides this feature currently only for requests made with the OkHttp framework. Additionally, you can enrich request events using the generic EventModifier interface.

When you use those two approaches together, you can enrich web events twice. There is priority of modifiers applied — first the OkHttpEventModifier and then the generic EventModifier.

Add an event modifier

To add a web request modifier, implement the EventModifier interface and pass it to the Dynatrace.addEventModifier method.

Dynatrace.addEventModifier(object : EventModifier {
override fun modifyEvent(event: JSONObject): JSONObject? {
event.put("event_properties.<YourCustomProperty>", "<YourCustomValue>")
return event
}
})

Add a web request modifier

To add a web request modifier, implement the OkHttpEventModifier interface and pass it to the Dynatrace.addHttpEventModifier method. The modifier can access the request and response objects to extract additional data and add it to the event. In case of exception, the modifier can access the request and throwable objects to extract additional data and add it to the event.

Because of the way OkHttp handles the response body, you must use peekBody() instead of body() to avoid consuming the response stream. This way, the body is still available to later be consumed by your normal response handling.

If you use body() to access the response body in the modifier, you will receive an exception when you later try to consume the body again.

val modifier: OkHttpEventModifier = object : OkHttpEventModifier {
override fun modifyEvent(request: Request, response: Response): JSONObject {
val event = JSONObject()
// Access response details
val serverTiming = response.header("Server-Timing")
if (serverTiming != null) {
event.put("event_properties.server_timing", serverTiming)
}
// Only peek response body, do not consume it
val body = response.peekBody(1000)
event.put("event_properties.body", body.toString())
return event
}
override fun modifyEvent(request: Request, throwable: Throwable): JSONObject {
val event = JSONObject()
// Add custom headers or request info as properties
val customHeader = request.header("X-Custom-Header")
if (customHeader != null) {
event.put("event_properties.custom_header", customHeader)
}
return event
}
}
Dynatrace.addHttpEventModifier(modifier)

Available context properties

OkHttpEventModifier provides callback methods for two scenarios:

  • modifyEvent(Request request, Response response)
  • modifyEvent(Request request, Throwable throwable)

The first method is called when the request is completed and the response is available. Note that the response might not be successful, for example, when the server returns a 4xx or 5xx status code.

The second method is called when the request failed with an exception.

Callback methodParameterTypeDescription
modifyEvent(request, response)requestokhttp3.RequestThe original OkHttp request object
responseokhttp3.ResponseThe OkHttp response object (use peekBody() to read body)
modifyEvent(request, throwable)requestokhttp3.RequestThe original OkHttp request object
throwableThrowableThe exception that caused the request to fail

Remove a modifier

When you no longer need the modifier, remove it using the modifier reference:

Dynatrace.removeEventModifier(modifier)

Filter requests

Return null from the modifier to prevent the event from being sent:

val filterModifier: OkHttpEventModifier = object : OkHttpEventModifier {
override fun modifyEvent(request: Request, response: Response?): JSONObject? {
if (request.url.toString().contains("analytics.example.com")) {
return null // return null to drop event
} else {
return JSONObject()
}
}
override fun modifyEvent(request: Request?, throwable: Throwable?): JSONObject? {
if (throwable is IOException) {
val event = JSONObject()
// throwable info is automatically added
return event
} else {
// return null to drop an event
return null
}
}
}
Dynatrace.addHttpEventModifier(filterModifier)
Related tags
Digital Experience