Skip to content

Commit

Permalink
added SentryOkHttpEventListener empty constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanosiano committed May 25, 2023
1 parent 66d6ab8 commit 06d74ff
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
2 changes: 2 additions & 0 deletions sentry-android-okhttp/api/sentry-android-okhttp.api
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public final class io/sentry/android/okhttp/SentryOkHttpEventListener : okhttp3/
public synthetic fun <init> (Lio/sentry/IHub;Lokhttp3/EventListener$Factory;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (Lio/sentry/IHub;Lokhttp3/EventListener;)V
public synthetic fun <init> (Lio/sentry/IHub;Lokhttp3/EventListener;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (Lokhttp3/EventListener$Factory;)V
public fun <init> (Lokhttp3/EventListener;)V
public fun callEnd (Lokhttp3/Call;)V
public fun callFailed (Lokhttp3/Call;Ljava/io/IOException;)V
public fun callStart (Lokhttp3/Call;)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import okhttp3.Request
import okhttp3.Response
import java.util.concurrent.ConcurrentHashMap

private const val PROTOCOL_KEY = "protocol"
private const val ERROR_MESSAGE_KEY = "error_message"

internal class SentryOkHttpEvent(private val hub: IHub, private val request: Request) {
private val eventSpans: MutableMap<String, ISpan> = ConcurrentHashMap()
private val breadcrumb: Breadcrumb
Expand Down Expand Up @@ -54,17 +57,17 @@ internal class SentryOkHttpEvent(private val hub: IHub, private val request: Req
*/
fun setResponse(response: Response) {
this.response = response
breadcrumb.setData("protocol", response.protocol.name)
breadcrumb.setData(PROTOCOL_KEY, response.protocol.name)
breadcrumb.setData("status_code", response.code)
callRootSpan?.setData("protocol", response.protocol.name)
callRootSpan?.setData(PROTOCOL_KEY, response.protocol.name)
callRootSpan?.setData("http.status_code", response.code)
callRootSpan?.status = SpanStatus.fromHttpStatusCode(response.code)
}

fun setProtocol(protocolName: String?) {
if (protocolName != null) {
breadcrumb.setData("protocol", protocolName)
callRootSpan?.setData("protocol", protocolName)
breadcrumb.setData(PROTOCOL_KEY, protocolName)
callRootSpan?.setData(PROTOCOL_KEY, protocolName)
}
}

Expand All @@ -85,8 +88,8 @@ internal class SentryOkHttpEvent(private val hub: IHub, private val request: Req
/** Sets the [errorMessage] if not null. */
fun setError(errorMessage: String?) {
if (errorMessage != null) {
breadcrumb.setData("error_message", errorMessage)
callRootSpan?.setData("error_message", errorMessage)
breadcrumb.setData(ERROR_MESSAGE_KEY, errorMessage)
callRootSpan?.setData(ERROR_MESSAGE_KEY, errorMessage)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import java.io.IOException
import java.net.InetAddress
import java.net.InetSocketAddress
import java.net.Proxy
import java.util.concurrent.ConcurrentHashMap

/**
* Logs network performance event metrics to Sentry
Expand Down Expand Up @@ -56,9 +57,24 @@ class SentryOkHttpEventListener(
internal const val RESPONSE_HEADERS_EVENT = "responseHeaders"
internal const val RESPONSE_BODY_EVENT = "responseBody"

internal val eventMap: MutableMap<Call, SentryOkHttpEvent> = HashMap()
internal val eventMap: MutableMap<Call, SentryOkHttpEvent> = ConcurrentHashMap()
}

constructor() : this(
HubAdapter.getInstance(),
originalEventListenerCreator = null
)

constructor(originalEventListener: EventListener) : this(
HubAdapter.getInstance(),
originalEventListenerCreator = { originalEventListener }
)

constructor(originalEventListenerFactory: Factory) : this(
HubAdapter.getInstance(),
originalEventListenerCreator = { originalEventListenerFactory.create(it) }
)

constructor(hub: IHub = HubAdapter.getInstance(), originalEventListener: EventListener) : this(
hub,
originalEventListenerCreator = { originalEventListener }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ class SentryOkHttpInterceptorTest {
fixture.getSut()
val interceptor = SentryOkHttpInterceptor(fixture.hub)
val chain = mock<Interceptor.Chain>()
whenever(chain.call()).thenReturn(mock())
whenever(chain.proceed(any())).thenThrow(IOException())
whenever(chain.request()).thenReturn(getRequest())

Expand Down Expand Up @@ -499,6 +500,7 @@ class SentryOkHttpInterceptorTest {
captureFailedRequests = true
)
val chain = mock<Interceptor.Chain>()
whenever(chain.call()).thenReturn(mock())
whenever(chain.proceed(any())).thenThrow(IOException())
whenever(chain.request()).thenReturn(getRequest())

Expand Down

0 comments on commit 06d74ff

Please sign in to comment.