-
Notifications
You must be signed in to change notification settings - Fork 74
[Feat] [SDK-399] Okhttp interceptor #367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
buongarzoni
merged 21 commits into
master
from
feat/SDK-399/network-telemetry-interceptor
May 5, 2026
Merged
Changes from 6 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
e19ecf3
feat(okhttp): add telemetry interceptor
buongarzoni e141e7e
build(okhttp): update dependencies
buongarzoni ab26f42
chore(okhttp): add readme
buongarzoni ed9af20
chore(okhttp): fix lint
buongarzoni 4f2e019
fix(okhttp): isolate NetworkTelemetryRecorder failures in interceptor
buongarzoni e7dd96f
build(okhttp): remove hardcoded version to inherit from root
buongarzoni ed91c57
build(okhttp): remove redundant plugins and repositories blocks
buongarzoni 28807a1
fix(okhttp): strip query params from recorded URLs by default to prev…
buongarzoni 02c7ea0
fix(okhttp): strip query params from recorded URLs by default to prev…
buongarzoni fd321cb
fix(okhttp): lint error, decrease line length
buongarzoni b4db532
fix(okhttp): attribute sanitizer exceptions to urlSanitizer in logs
buongarzoni ce27e7b
fix(okhttp): strip credentials and fragment from URLs in default sani…
buongarzoni 2c505c1
fix(okhttp): replace JUL logger with SLF4J to match SDK conventions
buongarzoni 3c4f505
build(okhttp): remove redundant mockito-core declaration
buongarzoni 1d7dab4
docs(okhttp): add rollbar-java to installation snippet
buongarzoni f14a9eb
fix(okhttp): lint line length
buongarzoni 0d75c63
style(okhttp): make test class public and use 2-space indentation
buongarzoni 5835a82
style: add missing colon prefix to rollbar-okhttp in settings.gradle.kts
buongarzoni 8af01d0
docs(okhttp): update sanitizer docs to list all stripped URL components
buongarzoni be12a54
fix(okhttp): replace java.util.function.Function with custom UrlSanit…
buongarzoni 106f26d
fix(okhttp): remove incorrect group override so module publishes as c…
buongarzoni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| # Rollbar OkHttp Integration | ||
|
|
||
| This module provides an [OkHttp Interceptor](https://square.github.io/okhttp/features/interceptors/) that automatically captures network telemetry for the Rollbar Java SDK. | ||
|
|
||
| It records: | ||
|
|
||
| - **Network telemetry events** for HTTP responses with status code `>= 400` (client and server errors). | ||
| - **Error events** for connection failures, timeouts, and other I/O exceptions. | ||
|
|
||
| ## Installation | ||
|
|
||
| ### Gradle (Kotlin DSL) | ||
|
|
||
| ```kotlin | ||
| dependencies { | ||
| implementation("com.rollbar:rollbar-okhttp:<version>") | ||
| implementation("com.squareup.okhttp3:okhttp:<okhttp-version>") | ||
| } | ||
| ``` | ||
|
|
||
| ### Gradle (Groovy) | ||
|
|
||
| ```groovy | ||
| dependencies { | ||
| implementation 'com.rollbar:rollbar-okhttp:<version>' | ||
| implementation 'com.squareup.okhttp3:okhttp:<okhttp-version>' | ||
| } | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| ### 1. Implement `NetworkTelemetryRecorder` | ||
|
|
||
| ```java | ||
| NetworkTelemetryRecorder recorder = new NetworkTelemetryRecorder() { | ||
| @Override | ||
| public void recordNetworkEvent(Level level, String method, String url, String statusCode) { | ||
| rollbar.recordNetworkEventFor(level, method, url, statusCode); | ||
| } | ||
|
|
||
| @Override | ||
| public void recordErrorEvent(Exception exception) { | ||
| rollbar.log(exception); | ||
| } | ||
| }; | ||
| ``` | ||
|
|
||
| ### 2. Add the interceptor to your OkHttpClient | ||
|
|
||
| ```java | ||
| OkHttpClient client = new OkHttpClient.Builder() | ||
| .addInterceptor(new RollbarOkHttpInterceptor(recorder)) | ||
| .build(); | ||
| ``` | ||
|
|
||
| ### 3. Make requests as usual | ||
|
|
||
| ```java | ||
| Request request = new Request.Builder() | ||
| .url("https://api.example.com/data") | ||
| .build(); | ||
|
|
||
| Response response = client.newCall(request).execute(); | ||
| ``` | ||
|
|
||
| The interceptor will automatically record telemetry events to Rollbar without interfering with the request/response flow. | ||
|
|
||
| ## Behavior | ||
|
|
||
| | Scenario | Action | | ||
| |-----------------------------------|---------------------------------------------------------| | ||
| | Recorder is `null` | No telemetry or log is recorded | | ||
| | Response status `< 400` | No telemetry recorded, response returned normally | | ||
| | Response status `>= 400` | Records a network telemetry event with `Level.CRITICAL` | | ||
| | Connection failure / timeout | Records an error event, then rethrows the `IOException` | | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| plugins { | ||
| id("java") | ||
| } | ||
|
buongarzoni marked this conversation as resolved.
Outdated
|
||
|
|
||
| group = "com.rollbar.okhttp" | ||
|
buongarzoni marked this conversation as resolved.
Outdated
brianr marked this conversation as resolved.
Outdated
|
||
|
|
||
| repositories { | ||
| mavenCentral() | ||
| } | ||
|
|
||
| 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") | ||
| testImplementation("org.mockito:mockito-core:5.23.0") | ||
|
buongarzoni marked this conversation as resolved.
Outdated
|
||
| implementation("com.squareup.okhttp3:okhttp:5.3.2") | ||
|
buongarzoni marked this conversation as resolved.
|
||
| api(project(":rollbar-api")) | ||
| } | ||
|
|
||
| tasks.test { | ||
| useJUnitPlatform() | ||
| } | ||
9 changes: 9 additions & 0 deletions
9
rollbar-okhttp/src/main/java/com/rollbar/okhttp/NetworkTelemetryRecorder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package com.rollbar.okhttp; | ||
|
|
||
| import com.rollbar.api.payload.data.Level; | ||
|
|
||
| public interface NetworkTelemetryRecorder { | ||
| void recordNetworkEvent(Level level, String method, String url, String statusCode); | ||
|
|
||
| void recordErrorEvent(Exception exception); | ||
| } |
61 changes: 61 additions & 0 deletions
61
rollbar-okhttp/src/main/java/com/rollbar/okhttp/RollbarOkHttpInterceptor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package com.rollbar.okhttp; | ||
|
|
||
| import com.rollbar.api.payload.data.Level; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.logging.Logger; | ||
|
|
||
| import okhttp3.Interceptor; | ||
| import okhttp3.Request; | ||
| import okhttp3.Response; | ||
|
|
||
| public class RollbarOkHttpInterceptor implements Interceptor { | ||
|
|
||
| private static final Logger LOGGER = Logger.getLogger(RollbarOkHttpInterceptor.class.getName()); | ||
|
|
||
| private final NetworkTelemetryRecorder recorder; | ||
|
|
||
| public RollbarOkHttpInterceptor(NetworkTelemetryRecorder recorder) { | ||
| this.recorder = recorder; | ||
| } | ||
|
|
||
| @Override | ||
| public Response intercept(Chain chain) throws IOException { | ||
| Request request = chain.request(); | ||
|
|
||
| try { | ||
| Response response = chain.proceed(request); | ||
|
|
||
| if (response.code() >= 400 && recorder != null) { | ||
| try { | ||
| recorder.recordNetworkEvent( | ||
| Level.CRITICAL, | ||
| request.method(), | ||
| request.url().toString(), | ||
|
buongarzoni marked this conversation as resolved.
Outdated
|
||
| String.valueOf(response.code())); | ||
|
claude[bot] marked this conversation as resolved.
|
||
| } catch (Exception recorderException) { | ||
| LOGGER.log(java.util.logging.Level.WARNING, | ||
| "NetworkTelemetryRecorder.recordNetworkEvent threw an exception; " | ||
| + "suppressing to preserve the interceptor contract.", | ||
| recorderException); | ||
| } | ||
|
buongarzoni marked this conversation as resolved.
|
||
| } | ||
|
|
||
| return response; | ||
|
|
||
| } catch (IOException e) { | ||
| if (recorder != null) { | ||
| try { | ||
| recorder.recordErrorEvent(e); | ||
| } catch (Exception recorderException) { | ||
| LOGGER.log(java.util.logging.Level.WARNING, | ||
| "NetworkTelemetryRecorder.recordErrorEvent threw an exception; " | ||
| + "suppressing to preserve the original IOException.", | ||
| recorderException); | ||
| } | ||
| } | ||
|
|
||
| throw e; | ||
|
claude[bot] marked this conversation as resolved.
|
||
| } | ||
| } | ||
| } | ||
177 changes: 177 additions & 0 deletions
177
rollbar-okhttp/src/test/java/com/rollbar/okhttp/RollbarOkHttpInterceptorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| package com.rollbar.okhttp; | ||
|
|
||
| import com.rollbar.api.payload.data.Level; | ||
| import okhttp3.OkHttpClient; | ||
| import okhttp3.Request; | ||
| import okhttp3.Response; | ||
| import okhttp3.mockwebserver.MockResponse; | ||
| import okhttp3.mockwebserver.MockWebServer; | ||
| import okhttp3.mockwebserver.SocketPolicy; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
| import static org.mockito.Mockito.*; | ||
|
|
||
| class RollbarOkHttpInterceptorTest { | ||
|
buongarzoni marked this conversation as resolved.
Outdated
|
||
|
|
||
| private MockWebServer server; | ||
| private NetworkTelemetryRecorder recorder; | ||
| private OkHttpClient client; | ||
|
|
||
| @BeforeEach | ||
| void setUp() throws IOException { | ||
| server = new MockWebServer(); | ||
| server.start(); | ||
|
|
||
| recorder = mock(NetworkTelemetryRecorder.class); | ||
|
|
||
| client = new OkHttpClient.Builder() | ||
| .addInterceptor(new RollbarOkHttpInterceptor(recorder)) | ||
| .build(); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void tearDown() throws IOException { | ||
| server.shutdown(); | ||
| } | ||
|
|
||
| @Test | ||
| void successfulResponse_doesNotRecordEvent() throws IOException { | ||
| server.enqueue(new MockResponse().setResponseCode(200)); | ||
|
|
||
| Request request = new Request.Builder().url(server.url("/ok")).build(); | ||
| Response response = client.newCall(request).execute(); | ||
| response.close(); | ||
|
|
||
| assertEquals(200, response.code()); | ||
| verifyNoInteractions(recorder); | ||
| } | ||
|
|
||
| @Test | ||
| void redirectResponse_doesNotRecordEvent() throws IOException { | ||
| server.enqueue(new MockResponse().setResponseCode(301).addHeader("Location", "/other")); | ||
|
|
||
| OkHttpClient noFollowClient = client.newBuilder().followRedirects(false).build(); | ||
| Request request = new Request.Builder().url(server.url("/redirect")).build(); | ||
| Response response = noFollowClient.newCall(request).execute(); | ||
| response.close(); | ||
|
|
||
| assertEquals(301, response.code()); | ||
| verifyNoInteractions(recorder); | ||
| } | ||
|
|
||
| @Test | ||
| void clientErrorResponse_recordsNetworkEvent() throws IOException { | ||
| server.enqueue(new MockResponse().setResponseCode(404)); | ||
|
|
||
| Request request = new Request.Builder().url(server.url("/not-found")).build(); | ||
| Response response = client.newCall(request).execute(); | ||
| response.close(); | ||
|
|
||
| assertEquals(404, response.code()); | ||
| verify(recorder).recordNetworkEvent( | ||
| eq(Level.CRITICAL), eq("GET"), contains("/not-found"), eq("404")); | ||
| verify(recorder, never()).recordErrorEvent(any()); | ||
| } | ||
|
|
||
| @Test | ||
| void serverErrorResponse_recordsNetworkEvent() throws IOException { | ||
| server.enqueue(new MockResponse().setResponseCode(500)); | ||
|
|
||
| Request request = new Request.Builder().url(server.url("/error")).build(); | ||
| Response response = client.newCall(request).execute(); | ||
| response.close(); | ||
|
|
||
| assertEquals(500, response.code()); | ||
| verify(recorder).recordNetworkEvent( | ||
| eq(Level.CRITICAL), eq("GET"), contains("/error"), eq("500")); | ||
| verify(recorder, never()).recordErrorEvent(any()); | ||
| } | ||
|
|
||
| @Test | ||
| void connectionFailure_recordsErrorEvent() { | ||
| server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.DISCONNECT_AT_START)); | ||
|
|
||
| Request request = new Request.Builder().url(server.url("/fail")).build(); | ||
|
|
||
| assertThrows(IOException.class, () -> client.newCall(request).execute()); | ||
|
|
||
| verify(recorder).recordErrorEvent(any(IOException.class)); | ||
| verify(recorder, never()).recordNetworkEvent(any(), any(), any(), any()); | ||
| } | ||
|
|
||
| @Test | ||
| void postRequest_recordsCorrectMethod() throws IOException { | ||
| server.enqueue(new MockResponse().setResponseCode(500)); | ||
|
|
||
| Request request = new Request.Builder() | ||
| .url(server.url("/post")) | ||
| .post(okhttp3.RequestBody.create("body", okhttp3.MediaType.parse("text/plain"))) | ||
| .build(); | ||
| Response response = client.newCall(request).execute(); | ||
| response.close(); | ||
|
|
||
| verify(recorder).recordNetworkEvent(eq(Level.CRITICAL), eq("POST"), any(), eq("500")); | ||
| } | ||
|
|
||
| @Test | ||
| void nullRecorder_errorResponse_doesNotThrowNPE() throws IOException { | ||
| server.enqueue(new MockResponse().setResponseCode(500)); | ||
|
|
||
| OkHttpClient nullRecorderClient = new OkHttpClient.Builder() | ||
| .addInterceptor(new RollbarOkHttpInterceptor(null)) | ||
| .build(); | ||
|
|
||
| Request request = new Request.Builder().url(server.url("/error")).build(); | ||
| Response response = nullRecorderClient.newCall(request).execute(); | ||
| response.close(); | ||
|
|
||
| assertEquals(500, response.code()); | ||
| } | ||
|
|
||
| @Test | ||
| void nullRecorder_connectionFailure_doesNotThrow() { | ||
| server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.DISCONNECT_AT_START)); | ||
|
|
||
| OkHttpClient nullRecorderClient = new OkHttpClient.Builder() | ||
| .addInterceptor(new RollbarOkHttpInterceptor(null)) | ||
| .build(); | ||
|
|
||
| Request request = new Request.Builder().url(server.url("/fail")).build(); | ||
|
|
||
| assertThrows(IOException.class, () -> nullRecorderClient.newCall(request).execute()); | ||
| } | ||
|
|
||
| @Test | ||
| void recorderThrowsOnErrorResponse_responseStillReturned() throws IOException { | ||
| server.enqueue(new MockResponse().setResponseCode(500)); | ||
|
|
||
| doThrow(new RuntimeException("recorder boom")) | ||
| .when(recorder) | ||
| .recordNetworkEvent(any(), any(), any(), any()); | ||
|
|
||
| Request request = new Request.Builder().url(server.url("/error")).build(); | ||
| Response response = client.newCall(request).execute(); | ||
| response.close(); | ||
|
|
||
| assertEquals(500, response.code()); | ||
| } | ||
|
|
||
| @Test | ||
| void recorderThrowsOnConnectionFailure_originalIOExceptionPropagates() { | ||
| server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.DISCONNECT_AT_START)); | ||
|
|
||
| doThrow(new RuntimeException("recorder boom")) | ||
| .when(recorder) | ||
| .recordErrorEvent(any()); | ||
|
|
||
| Request request = new Request.Builder().url(server.url("/fail")).build(); | ||
|
|
||
| assertThrows(IOException.class, () -> client.newCall(request).execute()); | ||
| verify(recorder).recordErrorEvent(any(IOException.class)); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.