Package com.dynatrace.agent.api
Interface OkHttpEventModifier
public interface OkHttpEventModifier
Interface definition for enriching automatically monitored HTTP web requests from the framework OkHttp.
You can enrich or alter data from HTTP events by using web request information provided by the framework OkHttp. OneAgent executes the provided modifier as part of the OkHttp web request handling and provides access to the OkHttp web request details.
Example: Report the first 1KB from the response body
Important: When consuming the response body, useResponse.peekBody(long)
to avoid consuming the body directly, as it can only be consumed once. This ensures the body remains available for
subsequent operations.
public class CustomOkHttpEventModifier implements OkHttpEventModifier {
@Override
public JSONObject modifyEvent(Request request, Response response) {
JSONObject metadata = new JSONObject();
// Add event property
metadata.put("event_properties.method", request.method());
// Safely peek at the response body (e.g., up to 1 KB)
try {
String responseBody = response.peekBody(1024).toString();
metadata.put("event_properties.response_body", responseBody);
} catch (IOException exception) {
metadata.put("event_properties.response_body", null);
metadata.put("event_properties.error_message", exception.getMessage());
metadata.put("event_properties.error_type", exception.getClass().getSimpleName());
}
return metadata;
}
@Override
public JSONObject modifyEvent(Request request, Throwable throwable) {
JSONObject metadata = new JSONObject();
// Add event property
metadata.put("event_properties.method", request.method());
// Add error details
metadata.put("event_properties.error_message", throwable.getMessage());
metadata.put("event_properties.error_type", throwable.getClass().getSimpleName());
return metadata;
}
}
- Since:
- 8.329
-
Method Summary
Modifier and TypeMethodDescriptionmodifyEvent(okhttp3.Request request, Throwable throwable) Enriches an event with additional fields for an OkHttp request and throwable.modifyEvent(okhttp3.Request request, okhttp3.Response response) Enriches an event with additional fields for an OkHttp request and response.
-
Method Details
-
modifyEvent
Enriches an event with additional fields for an OkHttp request and response.This method allows for adding metadata fields based on the
RequestandResponse.- Parameters:
request- the OkHttp object representing the HTTP requestresponse- the OkHttp object representing the server's response to the request- Returns:
- a json object containing additional fields, or
nullif the event should be dropped
-
modifyEvent
Enriches an event with additional fields for an OkHttp request and throwable.This method allows for adding metadata fields based on the
RequestandThrowable.- Parameters:
request- the OkHttp object representing the HTTP requestthrowable- representing the error that occurred during the request lifecycle- Returns:
- a
JSONObjectcontaining additional fields, ornullif the event should be dropped
-