Skip to content

Commit 2ae97e8

Browse files
committed
feat(okhttp) improve okhttp plugin eventlistener
1 parent f7ef874 commit 2ae97e8

5 files changed

Lines changed: 420 additions & 7 deletions

File tree

bugsnag-plugin-android-okhttp/api/bugsnag-plugin-android-okhttp.api

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
public final class com/bugsnag/android/okhttp/BugsnagOkHttpPlugin : okhttp3/EventListener, com/bugsnag/android/Plugin {
1+
public final class com/bugsnag/android/okhttp/BugsnagOkHttpPlugin : com/bugsnag/android/okhttp/util/DelegateEventListener, com/bugsnag/android/Plugin {
22
public fun <init> ()V
3-
public fun <init> (Lkotlin/jvm/functions/Function0;)V
4-
public synthetic fun <init> (Lkotlin/jvm/functions/Function0;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
3+
public fun <init> (Lokhttp3/EventListener;)V
4+
public fun <init> (Lokhttp3/EventListener;Lkotlin/jvm/functions/Function0;)V
5+
public synthetic fun <init> (Lokhttp3/EventListener;Lkotlin/jvm/functions/Function0;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
56
public fun callEnd (Lokhttp3/Call;)V
67
public fun callFailed (Lokhttp3/Call;Ljava/io/IOException;)V
78
public fun callStart (Lokhttp3/Call;)V
@@ -22,3 +23,34 @@ public final class com/bugsnag/android/okhttp/OkHttpDelivery : com/bugsnag/andro
2223
public fun deliver (Lcom/bugsnag/android/Session;Lcom/bugsnag/android/DeliveryParams;)Lcom/bugsnag/android/DeliveryStatus;
2324
}
2425

26+
public class com/bugsnag/android/okhttp/util/DelegateEventListener : okhttp3/EventListener {
27+
public fun <init> (Lokhttp3/EventListener;)V
28+
public fun cacheConditionalHit (Lokhttp3/Call;Lokhttp3/Response;)V
29+
public fun cacheHit (Lokhttp3/Call;Lokhttp3/Response;)V
30+
public fun cacheMiss (Lokhttp3/Call;)V
31+
public fun callEnd (Lokhttp3/Call;)V
32+
public fun callFailed (Lokhttp3/Call;Ljava/io/IOException;)V
33+
public fun callStart (Lokhttp3/Call;)V
34+
public fun canceled (Lokhttp3/Call;)V
35+
public fun connectEnd (Lokhttp3/Call;Ljava/net/InetSocketAddress;Ljava/net/Proxy;Lokhttp3/Protocol;)V
36+
public fun connectFailed (Lokhttp3/Call;Ljava/net/InetSocketAddress;Ljava/net/Proxy;Lokhttp3/Protocol;Ljava/io/IOException;)V
37+
public fun connectStart (Lokhttp3/Call;Ljava/net/InetSocketAddress;Ljava/net/Proxy;)V
38+
public fun connectionAcquired (Lokhttp3/Call;Lokhttp3/Connection;)V
39+
public fun connectionReleased (Lokhttp3/Call;Lokhttp3/Connection;)V
40+
public fun dnsEnd (Lokhttp3/Call;Ljava/lang/String;Ljava/util/List;)V
41+
public fun dnsStart (Lokhttp3/Call;Ljava/lang/String;)V
42+
protected final fun getDelegateEventListener ()Lokhttp3/EventListener;
43+
public fun proxySelectEnd (Lokhttp3/Call;Lokhttp3/HttpUrl;Ljava/util/List;)V
44+
public fun proxySelectStart (Lokhttp3/Call;Lokhttp3/HttpUrl;)V
45+
public fun requestBodyStart (Lokhttp3/Call;)V
46+
public fun requestFailed (Lokhttp3/Call;Ljava/io/IOException;)V
47+
public fun requestHeadersEnd (Lokhttp3/Call;Lokhttp3/Request;)V
48+
public fun requestHeadersStart (Lokhttp3/Call;)V
49+
public fun responseBodyStart (Lokhttp3/Call;)V
50+
public fun responseFailed (Lokhttp3/Call;Ljava/io/IOException;)V
51+
public fun responseHeadersStart (Lokhttp3/Call;)V
52+
public fun satisfactionFailure (Lokhttp3/Call;Lokhttp3/Response;)V
53+
public fun secureConnectEnd (Lokhttp3/Call;Lokhttp3/Handshake;)V
54+
public fun secureConnectStart (Lokhttp3/Call;)V
55+
}
56+

bugsnag-plugin-android-okhttp/detekt-baseline.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
<ID>MagicNumber:BugsnagOkHttpPlugin.kt$399</ID>
77
<ID>MagicNumber:BugsnagOkHttpPlugin.kt$400</ID>
88
<ID>MagicNumber:BugsnagOkHttpPlugin.kt$599</ID>
9+
<ID>TooManyFunctions:DelegateEventListener.kt$DelegateEventListener : EventListener</ID>
910
</CurrentIssues>
1011
</SmellBaseline>

bugsnag-plugin-android-okhttp/src/main/java/com/bugsnag/android/okhttp/BugsnagOkHttpPlugin.kt

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import androidx.annotation.VisibleForTesting
44
import com.bugsnag.android.BreadcrumbType
55
import com.bugsnag.android.Client
66
import com.bugsnag.android.Plugin
7+
import com.bugsnag.android.okhttp.util.DelegateEventListener
78
import com.bugsnag.android.shouldDiscardNetworkBreadcrumb
89
import okhttp3.Call
910
import okhttp3.EventListener
@@ -25,8 +26,9 @@ import java.util.concurrent.ConcurrentHashMap
2526
* https://square.github.io/okhttp/4.x/okhttp/okhttp3/-response-body/#the-response-body-must-be-closed
2627
*/
2728
class BugsnagOkHttpPlugin @JvmOverloads constructor(
29+
delegateEventListener: EventListener? = null,
2830
internal val timeProvider: () -> Long = { System.currentTimeMillis() }
29-
) : Plugin, EventListener() {
31+
) : Plugin, DelegateEventListener(delegateEventListener) {
3032

3133
internal val requestMap = ConcurrentHashMap<Call, NetworkRequestMetadata>()
3234
private var client: Client? = null
@@ -40,24 +42,39 @@ class BugsnagOkHttpPlugin @JvmOverloads constructor(
4042
}
4143

4244
override fun callStart(call: Call) {
45+
super.callStart(call)
4346
requestMap[call] = NetworkRequestMetadata(timeProvider())
4447
}
4548

4649
override fun requestBodyEnd(call: Call, byteCount: Long) {
50+
super.requestBodyEnd(call, byteCount)
4751
requestMap[call]?.requestBodyCount = byteCount
4852
}
4953

5054
override fun responseBodyEnd(call: Call, byteCount: Long) {
55+
super.responseBodyEnd(call, byteCount)
5156
requestMap[call]?.responseBodyCount = byteCount
5257
}
5358

5459
override fun responseHeadersEnd(call: Call, response: Response) {
60+
super.responseHeadersEnd(call, response)
5561
requestMap[call]?.status = response.code
5662
}
5763

58-
override fun callEnd(call: Call) = captureNetworkBreadcrumb(call)
59-
override fun callFailed(call: Call, ioe: IOException) = captureNetworkBreadcrumb(call)
60-
override fun canceled(call: Call) = captureNetworkBreadcrumb(call)
64+
override fun callEnd(call: Call) {
65+
super.callEnd(call)
66+
captureNetworkBreadcrumb(call)
67+
}
68+
69+
override fun callFailed(call: Call, ioe: IOException) {
70+
super.callFailed(call, ioe)
71+
captureNetworkBreadcrumb(call)
72+
}
73+
74+
override fun canceled(call: Call) {
75+
super.canceled(call)
76+
captureNetworkBreadcrumb(call)
77+
}
6178

6279
private fun captureNetworkBreadcrumb(call: Call) {
6380
client?.apply {
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package com.bugsnag.android.okhttp.util
2+
3+
import okhttp3.Call
4+
import okhttp3.Connection
5+
import okhttp3.EventListener
6+
import okhttp3.Handshake
7+
import okhttp3.HttpUrl
8+
import okhttp3.Protocol
9+
import okhttp3.Request
10+
import okhttp3.Response
11+
import java.io.IOException
12+
import java.net.InetAddress
13+
import java.net.InetSocketAddress
14+
import java.net.Proxy
15+
16+
open class DelegateEventListener(
17+
protected val delegateEventListener: EventListener?
18+
) : EventListener() {
19+
override fun callStart(call: Call) {
20+
delegateEventListener?.callStart(call)
21+
}
22+
23+
override fun callEnd(call: Call) {
24+
delegateEventListener?.callEnd(call)
25+
}
26+
27+
override fun callFailed(call: Call, ioe: IOException) {
28+
delegateEventListener?.callFailed(call, ioe)
29+
}
30+
31+
override fun canceled(call: Call) {
32+
delegateEventListener?.canceled(call)
33+
}
34+
35+
override fun cacheConditionalHit(call: Call, cachedResponse: Response) {
36+
delegateEventListener?.cacheConditionalHit(call, cachedResponse)
37+
}
38+
39+
override fun cacheHit(call: Call, response: Response) {
40+
delegateEventListener?.cacheHit(call, response)
41+
}
42+
43+
override fun cacheMiss(call: Call) {
44+
delegateEventListener?.cacheMiss(call)
45+
}
46+
47+
override fun connectEnd(
48+
call: Call,
49+
inetSocketAddress: InetSocketAddress,
50+
proxy: Proxy,
51+
protocol: Protocol?
52+
) {
53+
delegateEventListener?.connectEnd(call, inetSocketAddress, proxy, protocol)
54+
}
55+
56+
override fun connectFailed(
57+
call: Call,
58+
inetSocketAddress: InetSocketAddress,
59+
proxy: Proxy,
60+
protocol: Protocol?,
61+
ioe: IOException
62+
) {
63+
delegateEventListener?.connectFailed(call, inetSocketAddress, proxy, protocol, ioe)
64+
}
65+
66+
override fun connectStart(
67+
call: Call,
68+
inetSocketAddress: InetSocketAddress,
69+
proxy: Proxy
70+
) {
71+
delegateEventListener?.connectStart(call, inetSocketAddress, proxy)
72+
}
73+
74+
override fun connectionAcquired(call: Call, connection: Connection) {
75+
delegateEventListener?.connectionAcquired(call, connection)
76+
}
77+
78+
override fun connectionReleased(call: Call, connection: Connection) {
79+
delegateEventListener?.connectionReleased(call, connection)
80+
}
81+
82+
override fun dnsEnd(
83+
call: Call,
84+
domainName: String,
85+
inetAddressList: List<@JvmSuppressWildcards InetAddress>
86+
) {
87+
delegateEventListener?.dnsEnd(call, domainName, inetAddressList)
88+
}
89+
90+
override fun dnsStart(call: Call, domainName: String) {
91+
delegateEventListener?.dnsStart(call, domainName)
92+
}
93+
94+
override fun proxySelectEnd(
95+
call: Call,
96+
url: HttpUrl,
97+
proxies: List<@JvmSuppressWildcards Proxy>
98+
) {
99+
delegateEventListener?.proxySelectEnd(call, url, proxies)
100+
}
101+
102+
override fun proxySelectStart(call: Call, url: HttpUrl) {
103+
delegateEventListener?.proxySelectStart(call, url)
104+
}
105+
106+
override fun requestBodyStart(call: Call) {
107+
delegateEventListener?.requestBodyStart(call)
108+
}
109+
110+
override fun requestFailed(call: Call, ioe: IOException) {
111+
delegateEventListener?.requestFailed(call, ioe)
112+
}
113+
114+
override fun requestHeadersEnd(call: Call, request: Request) {
115+
delegateEventListener?.requestHeadersEnd(call, request)
116+
}
117+
118+
override fun requestHeadersStart(call: Call) {
119+
delegateEventListener?.requestHeadersStart(call)
120+
}
121+
122+
override fun responseBodyStart(call: Call) {
123+
delegateEventListener?.responseBodyStart(call)
124+
}
125+
126+
override fun responseFailed(call: Call, ioe: IOException) {
127+
delegateEventListener?.responseFailed(call, ioe)
128+
}
129+
130+
override fun responseHeadersStart(call: Call) {
131+
delegateEventListener?.responseHeadersStart(call)
132+
}
133+
134+
override fun satisfactionFailure(call: Call, response: Response) {
135+
delegateEventListener?.satisfactionFailure(call, response)
136+
}
137+
138+
override fun secureConnectEnd(call: Call, handshake: Handshake?) {
139+
delegateEventListener?.secureConnectEnd(call, handshake)
140+
}
141+
142+
override fun secureConnectStart(call: Call) {
143+
delegateEventListener?.secureConnectStart(call)
144+
}
145+
}

0 commit comments

Comments
 (0)