|
| 1 | +package io.sentry.android.okhttp |
| 2 | + |
| 3 | +import io.sentry.Breadcrumb |
| 4 | +import io.sentry.Hint |
| 5 | +import io.sentry.IHub |
| 6 | +import io.sentry.ISpan |
| 7 | +import io.sentry.SpanStatus |
| 8 | +import io.sentry.TypeCheckHint |
| 9 | +import io.sentry.android.okhttp.SentryOkHttpEventListener.Companion.CONNECTION_EVENT |
| 10 | +import io.sentry.android.okhttp.SentryOkHttpEventListener.Companion.CONNECT_EVENT |
| 11 | +import io.sentry.android.okhttp.SentryOkHttpEventListener.Companion.REQUEST_BODY_EVENT |
| 12 | +import io.sentry.android.okhttp.SentryOkHttpEventListener.Companion.REQUEST_HEADERS_EVENT |
| 13 | +import io.sentry.android.okhttp.SentryOkHttpEventListener.Companion.RESPONSE_BODY_EVENT |
| 14 | +import io.sentry.android.okhttp.SentryOkHttpEventListener.Companion.RESPONSE_HEADERS_EVENT |
| 15 | +import io.sentry.android.okhttp.SentryOkHttpEventListener.Companion.SECURE_CONNECT_EVENT |
| 16 | +import io.sentry.util.UrlUtils |
| 17 | +import okhttp3.Request |
| 18 | +import okhttp3.Response |
| 19 | +import java.util.concurrent.ConcurrentHashMap |
| 20 | + |
| 21 | +private const val PROTOCOL_KEY = "protocol" |
| 22 | +private const val ERROR_MESSAGE_KEY = "error_message" |
| 23 | + |
| 24 | +internal class SentryOkHttpEvent(private val hub: IHub, private val request: Request) { |
| 25 | + private val eventSpans: MutableMap<String, ISpan> = ConcurrentHashMap() |
| 26 | + private val breadcrumb: Breadcrumb |
| 27 | + internal val callRootSpan: ISpan? |
| 28 | + private var response: Response? = null |
| 29 | + |
| 30 | + init { |
| 31 | + val urlDetails = UrlUtils.parse(request.url.toString()) |
| 32 | + val url = urlDetails.urlOrFallback |
| 33 | + val host: String = request.url.host |
| 34 | + val encodedPath: String = request.url.encodedPath |
| 35 | + val method: String = request.method |
| 36 | + |
| 37 | + // We start the call span that will contain all the others |
| 38 | + callRootSpan = hub.span?.startChild("http.client", "$method $url") |
| 39 | + |
| 40 | + urlDetails.applyToSpan(callRootSpan) |
| 41 | + |
| 42 | + // We setup a breadcrumb with all meaningful data |
| 43 | + breadcrumb = Breadcrumb.http(url, method) |
| 44 | + breadcrumb.setData("host", host) |
| 45 | + breadcrumb.setData("path", encodedPath) |
| 46 | + |
| 47 | + // We add the same data to the root call span |
| 48 | + callRootSpan?.setData("url", url) |
| 49 | + callRootSpan?.setData("host", host) |
| 50 | + callRootSpan?.setData("path", encodedPath) |
| 51 | + callRootSpan?.setData("http.method", method) |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Sets the [Response] that will be sent in the breadcrumb [Hint]. |
| 56 | + * Also, it sets the protocol and status code in the breadcrumb and the call root span. |
| 57 | + */ |
| 58 | + fun setResponse(response: Response) { |
| 59 | + this.response = response |
| 60 | + breadcrumb.setData(PROTOCOL_KEY, response.protocol.name) |
| 61 | + breadcrumb.setData("status_code", response.code) |
| 62 | + callRootSpan?.setData(PROTOCOL_KEY, response.protocol.name) |
| 63 | + callRootSpan?.setData("http.status_code", response.code) |
| 64 | + callRootSpan?.status = SpanStatus.fromHttpStatusCode(response.code) |
| 65 | + } |
| 66 | + |
| 67 | + fun setProtocol(protocolName: String?) { |
| 68 | + if (protocolName != null) { |
| 69 | + breadcrumb.setData(PROTOCOL_KEY, protocolName) |
| 70 | + callRootSpan?.setData(PROTOCOL_KEY, protocolName) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + fun setRequestBodySize(byteCount: Long) { |
| 75 | + if (byteCount > -1) { |
| 76 | + breadcrumb.setData("request_content_length", byteCount) |
| 77 | + callRootSpan?.setData("http.request_content_length", byteCount) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + fun setResponseBodySize(byteCount: Long) { |
| 82 | + if (byteCount > -1) { |
| 83 | + breadcrumb.setData("response_content_length", byteCount) |
| 84 | + callRootSpan?.setData("http.response_content_length", byteCount) |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /** Sets the [errorMessage] if not null. */ |
| 89 | + fun setError(errorMessage: String?) { |
| 90 | + if (errorMessage != null) { |
| 91 | + breadcrumb.setData(ERROR_MESSAGE_KEY, errorMessage) |
| 92 | + callRootSpan?.setData(ERROR_MESSAGE_KEY, errorMessage) |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /** Starts a span, if the callRootSpan is not null. */ |
| 97 | + fun startSpan(event: String) { |
| 98 | + // Find the parent of the span being created. E.g. secureConnect is child of connect |
| 99 | + val parentSpan = when (event) { |
| 100 | + // PROXY_SELECT, DNS, CONNECT and CONNECTION are not children of one another |
| 101 | + SECURE_CONNECT_EVENT -> eventSpans[CONNECT_EVENT] |
| 102 | + REQUEST_HEADERS_EVENT -> eventSpans[CONNECTION_EVENT] |
| 103 | + REQUEST_BODY_EVENT -> eventSpans[CONNECTION_EVENT] |
| 104 | + RESPONSE_HEADERS_EVENT -> eventSpans[CONNECTION_EVENT] |
| 105 | + RESPONSE_BODY_EVENT -> eventSpans[CONNECTION_EVENT] |
| 106 | + else -> callRootSpan |
| 107 | + } ?: callRootSpan |
| 108 | + val span = parentSpan?.startChild("http.client.$event") ?: return |
| 109 | + eventSpans[event] = span |
| 110 | + } |
| 111 | + |
| 112 | + /** Finishes a previously started span, and runs [beforeFinish] on it and on the call root span. */ |
| 113 | + fun finishSpan(event: String, beforeFinish: ((span: ISpan) -> Unit)? = null) { |
| 114 | + val span = eventSpans[event] ?: return |
| 115 | + beforeFinish?.invoke(span) |
| 116 | + callRootSpan?.let { beforeFinish?.invoke(it) } |
| 117 | + span.finish() |
| 118 | + } |
| 119 | + |
| 120 | + /** Finishes the call root span, and runs [beforeFinish] on it. Then a breadcrumb is sent. */ |
| 121 | + fun finishEvent(beforeFinish: ((span: ISpan) -> Unit)? = null) { |
| 122 | + callRootSpan ?: return |
| 123 | + |
| 124 | + // We forcefully finish all spans, even if they should already have been finished through finishSpan() |
| 125 | + eventSpans.values.filter { !it.isFinished }.forEach { it.finish(SpanStatus.DEADLINE_EXCEEDED) } |
| 126 | + beforeFinish?.invoke(callRootSpan) |
| 127 | + callRootSpan.finish() |
| 128 | + |
| 129 | + // We put data in the hint and send a breadcrumb |
| 130 | + val hint = Hint() |
| 131 | + hint.set(TypeCheckHint.OKHTTP_REQUEST, request) |
| 132 | + response?.let { hint.set(TypeCheckHint.OKHTTP_RESPONSE, it) } |
| 133 | + |
| 134 | + hub.addBreadcrumb(breadcrumb, hint) |
| 135 | + return |
| 136 | + } |
| 137 | +} |
0 commit comments