The OneAgent for Android provides comprehensive web request monitoring capabilities
HttpUrlConnection or OkHttp.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.
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:
The data captured will be structured and reported according to the fields specified in request.*.
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.
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}}}
The SDK supports W3C Trace Context for distributed tracing, allowing you to correlate mobile requests with backend services instrumented by Dynatrace.
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.
For networking libraries not covered by automatic instrumentation, you can manually report web requests using HttpRequestEventData.
import com.dynatrace.android.agent.Dynatraceimport com.dynatrace.android.agent.HttpRequestEventData// Create the request data with the URL and HTTP methodval 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 eventDynatrace.sendHttpRequestEvent(requestData)
When a request fails, include the error information:
val requestData = HttpRequestEventData("https://api.example.com/data", "POST").withDuration(1500).withThrowable(exception)Dynatrace.sendHttpRequestEvent(requestData)
To correlate manually reported requests with distributed traces, include the traceparent header:
// Report the event with the traceparentval requestData = HttpRequestEventData(url, "GET").withDuration(duration).withStatusCode(statusCode).withTraceparentHeader(traceparent)Dynatrace.sendHttpRequestEvent(requestData)
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 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.
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}})
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 detailsval serverTiming = response.header("Server-Timing")if (serverTiming != null) {event.put("event_properties.server_timing", serverTiming)}// Only peek response body, do not consume itval 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 propertiesval customHeader = request.header("X-Custom-Header")if (customHeader != null) {event.put("event_properties.custom_header", customHeader)}return event}}Dynatrace.addHttpEventModifier(modifier)
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 method | Parameter | Type | Description |
|---|---|---|---|
modifyEvent(request, response) | request | okhttp3.Request | The original OkHttp request object |
response | okhttp3.Response | The OkHttp response object (use peekBody() to read body) | |
modifyEvent(request, throwable) | request | okhttp3.Request | The original OkHttp request object |
throwable | Throwable | The exception that caused the request to fail |
When you no longer need the modifier, remove it using the modifier reference:
Dynatrace.removeEventModifier(modifier)
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 addedreturn event} else {// return null to drop an eventreturn null}}}Dynatrace.addHttpEventModifier(filterModifier)