Data filtering in Android

Overview

Data filtering, or Sanitizing, is a feature which lets you modify or discard specific chunks of debug data. Full description can be found here

Usage

A filter is a class implementing a Filter interface and overriding its filter() method. This method receives an event as an argument and returns either its modified version or null to block the events of this type from reaching backend.

Examples

  • Remove authentication token from request headers:
public class TokenFilter implements HTTPFilter {
    @Nullable
    @Override
    public HttpRequest filter(HttpRequest request) {
        if (request.getHeaders().containsKey("YOUR-AUTH-HEADER")) {
             request.getHeaders().put("YOUR-AUTH-HEADER", newValue);
        }
        return request;
    }

    @Nullable
    @Override
    public HttpResponse filter(HttpResponse response) {
        return response;
    }
}
class TokenFilter : HTTPFilter {
 
    override fun filter(request: HttpRequest): HttpRequest? {
        if (request.headers.containsKey("YOUR-AUTH-HEADER")) {
            request.headers["YOUR-AUTH-HEADER"] = newValue
        }
        return request
    }
  
    override fun filter(response: HttpResponse): HttpResponse? {
        return response
    }
}
  • Set log level to WARN for all messages with word token:
public class LogFilter implements Filter {
  
    @Nullable
    @Override
    public LogEvent filter(LogEvent event) {
        if (event.message.contains("token")) {
             event.level = LogLevel.WARN;
        }
        return event;
    }
}
class LogFilter : Filter {
  
    override fun LogEvent filter(event: LogEvent): LogEvent? {
        if (event.message.contains("token")) {
             event.level = LogLevel.WARN
        }
        return event
    }
}
  • Ignore the HTTP event (note that it won't block the request itself, but it will not be processed by HTTP Monitor):
public class SkipEventFilter implements HTTPFilter {
  
    @Nullable
    @Override
    public HttpRequest filter(HttpRequest request) {
        if (request.getUrl().contains("analytics")) {
             return null;
        }
        return request;
    }

    @Nullable
    @Override
    public HttpResponse filter(HttpResponse response) {
      	return response;
    }
}
class SkipEventFilter : HTTPFilter {
    
		override fun filter(request: HttpRequest?): HttpRequest? {
				if (request.url.contains("analytics")) {
            return null
        }
        return request
    }

    override fun filter(response: HttpResponse?): HttpResponse? {
				return response
		}
}