[Feat] [SDK-399] Okhttp interceptor by buongarzoni · Pull Request #367 · rollbar/rollbar-java · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
e19ecf3
feat(okhttp): add telemetry interceptor
buongarzoni Mar 23, 2026
e141e7e
build(okhttp): update dependencies
buongarzoni Mar 24, 2026
ab26f42
chore(okhttp): add readme
buongarzoni Mar 24, 2026
ed9af20
chore(okhttp): fix lint
buongarzoni Mar 24, 2026
4f2e019
fix(okhttp): isolate NetworkTelemetryRecorder failures in interceptor
buongarzoni Apr 20, 2026
e7dd96f
build(okhttp): remove hardcoded version to inherit from root
buongarzoni Apr 20, 2026
ed91c57
build(okhttp): remove redundant plugins and repositories blocks
buongarzoni Apr 20, 2026
28807a1
fix(okhttp): strip query params from recorded URLs by default to prev…
buongarzoni Apr 27, 2026
02c7ea0
fix(okhttp): strip query params from recorded URLs by default to prev…
buongarzoni Apr 27, 2026
fd321cb
fix(okhttp): lint error, decrease line length
buongarzoni Apr 27, 2026
b4db532
fix(okhttp): attribute sanitizer exceptions to urlSanitizer in logs
buongarzoni May 1, 2026
ce27e7b
fix(okhttp): strip credentials and fragment from URLs in default sani…
buongarzoni May 1, 2026
2c505c1
fix(okhttp): replace JUL logger with SLF4J to match SDK conventions
buongarzoni May 1, 2026
3c4f505
build(okhttp): remove redundant mockito-core declaration
buongarzoni May 1, 2026
1d7dab4
docs(okhttp): add rollbar-java to installation snippet
buongarzoni May 1, 2026
f14a9eb
fix(okhttp): lint line length
buongarzoni May 1, 2026
0d75c63
style(okhttp): make test class public and use 2-space indentation
buongarzoni May 1, 2026
5835a82
style: add missing colon prefix to rollbar-okhttp in settings.gradle.kts
buongarzoni May 1, 2026
8af01d0
docs(okhttp): update sanitizer docs to list all stripped URL components
buongarzoni May 1, 2026
be12a54
fix(okhttp): replace java.util.function.Function with custom UrlSanit…
buongarzoni May 2, 2026
106f26d
fix(okhttp): remove incorrect group override so module publishes as c…
buongarzoni May 4, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions rollbar-okhttp/README.md
13 changes: 13 additions & 0 deletions rollbar-okhttp/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
dependencies {
testImplementation(platform("org.junit:junit-bom:5.14.3"))
testImplementation("org.junit.jupiter:junit-jupiter")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
testImplementation("com.squareup.okhttp3:mockwebserver:5.3.2")
implementation("com.squareup.okhttp3:okhttp:5.3.2")
Comment thread
buongarzoni marked this conversation as resolved.
api(project(":rollbar-api"))
api("org.slf4j:slf4j-api:1.7.25")
}

tasks.test {
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.rollbar.okhttp;

import com.rollbar.api.payload.data.Level;

/**
* Records network telemetry events and errors for HTTP requests.
*/
public interface NetworkTelemetryRecorder {
/**
* Records a completed network request as a telemetry event.
*
* @param level the severity level to attach to the telemetry event
* @param method the HTTP method (e.g. GET, POST)
* @param url the request URL with userinfo (basic-auth credentials), query parameters,
* and fragment stripped by default; supply a custom sanitizer to
* {@link RollbarOkHttpInterceptor} to change this behavior
* @param statusCode the HTTP response status code as a string (e.g. "200", "404")
*/
void recordNetworkEvent(Level level, String method, String url, String statusCode);

/**
* Records a network error event when an HTTP request fails with an exception.
*
* @param exception the exception thrown during the request
*/
void recordErrorEvent(Exception exception);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.rollbar.okhttp;

import com.rollbar.api.payload.data.Level;

import java.io.IOException;
import java.util.Objects;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RollbarOkHttpInterceptor implements Interceptor {

private static final Logger LOGGER = LoggerFactory.getLogger(RollbarOkHttpInterceptor.class);

private static final UrlSanitizer DEFAULT_URL_SANITIZER =
url -> url
.newBuilder()
.username("")
.password("")
.query(null)
.fragment(null)
.build()
.toString();

private final NetworkTelemetryRecorder recorder;
private final UrlSanitizer urlSanitizer;

public RollbarOkHttpInterceptor(NetworkTelemetryRecorder recorder) {
this(recorder, DEFAULT_URL_SANITIZER);
}

public RollbarOkHttpInterceptor(
NetworkTelemetryRecorder recorder,
UrlSanitizer urlSanitizer) {
this.recorder = recorder;
this.urlSanitizer = Objects.requireNonNull(urlSanitizer, "urlSanitizer must not be null");
}

@NotNull
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();

try {
Response response = chain.proceed(request);

if (response.code() >= 400 && recorder != null) {
String sanitizedUrl;
try {
sanitizedUrl = urlSanitizer.sanitize(request.url());
} catch (Exception sanitizerException) {
LOGGER.warn("urlSanitizer threw an exception; "
+ "suppressing to preserve the interceptor contract.", sanitizerException);
return response;
}
try {
recorder.recordNetworkEvent(
Level.CRITICAL,
request.method(),
sanitizedUrl,
String.valueOf(response.code()));
Comment thread
claude[bot] marked this conversation as resolved.
} catch (Exception recorderException) {
LOGGER.warn("NetworkTelemetryRecorder.recordNetworkEvent threw an exception; "
+ "suppressing to preserve the interceptor contract.", recorderException);
}
Comment thread
buongarzoni marked this conversation as resolved.
}

return response;

} catch (IOException e) {
if (recorder != null) {
try {
recorder.recordErrorEvent(e);
} catch (Exception recorderException) {
LOGGER.warn("NetworkTelemetryRecorder.recordErrorEvent threw an exception; "
+ "suppressing to preserve the original IOException.", recorderException);
}
}

throw e;
Comment thread
claude[bot] marked this conversation as resolved.
}
}
}
Loading
Loading