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, use Response.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 Type
    Method
    Description
    modifyEvent(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

      JSONObject modifyEvent(okhttp3.Request request, okhttp3.Response response)
      Enriches an event with additional fields for an OkHttp request and response.

      This method allows for adding metadata fields based on the Request and Response.

      Parameters:
      request - the OkHttp object representing the HTTP request
      response - the OkHttp object representing the server's response to the request
      Returns:
      a json object containing additional fields, or null if the event should be dropped
    • modifyEvent

      JSONObject modifyEvent(okhttp3.Request request, Throwable throwable)
      Enriches an event with additional fields for an OkHttp request and throwable.

      This method allows for adding metadata fields based on the Request and Throwable.

      Parameters:
      request - the OkHttp object representing the HTTP request
      throwable - representing the error that occurred during the request lifecycle
      Returns:
      a JSONObject containing additional fields, or null if the event should be dropped