-
Notifications
You must be signed in to change notification settings - Fork 7
/
EmbraceOkHttp3NetworkInterceptor.kt
277 lines (252 loc) · 11.6 KB
/
EmbraceOkHttp3NetworkInterceptor.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package io.embrace.android.embracesdk.okhttp3
import io.embrace.android.embracesdk.Embrace
import io.embrace.android.embracesdk.annotation.InternalApi
import io.embrace.android.embracesdk.internal.clock.Clock
import io.embrace.android.embracesdk.internal.network.http.EmbraceHttpPathOverride
import io.embrace.android.embracesdk.internal.network.http.NetworkCaptureData
import io.embrace.android.embracesdk.network.EmbraceNetworkRequest
import io.embrace.android.embracesdk.network.http.HttpMethod
import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response
import okhttp3.internal.http.RealResponseBody
import okhttp3.internal.http.promisesBody
import okio.Buffer
import okio.GzipSource
import okio.buffer
import java.io.IOException
import kotlin.math.abs
/**
* Custom OkHttp Interceptor implementation that will log the results of the network call
* to Embrace.io.
*
* This interceptor will only intercept network request and responses from client app.
* OkHttp network interceptors are added almost at the end of stack, they are closer to "Wire"
* so they are able to see catch "real requests".
*
* Network Interceptors
* - Able to operate on intermediate responses like redirects and retries.
* - Not invoked for cached responses that short-circuit the network.
* - Observe the data just as it will be transmitted over the network.
* - Access to the Connection that carries the request.
*/
@InternalApi
public class EmbraceOkHttp3NetworkInterceptor internal constructor(
private val embrace: Embrace,
// A clock that mirrors the one used by OkHttp to get timestamps
private val systemClock: Clock
) : Interceptor {
public constructor() : this(Embrace.getInstance(), Clock { System.currentTimeMillis() })
@Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain): Response {
// If the SDK has not started, don't do anything
val originalRequest: Request = chain.request()
if (!embrace.isStarted || embrace.internalInterface.isInternalNetworkCaptureDisabled()) {
return chain.proceed(originalRequest)
}
val networkSpanForwardingEnabled = embrace.internalInterface.isNetworkSpanForwardingEnabled()
var traceparent: String? = null
if (networkSpanForwardingEnabled && originalRequest.header(TRACEPARENT_HEADER_NAME) == null) {
traceparent = embrace.generateW3cTraceparent()
}
val request =
if (traceparent == null) originalRequest else originalRequest.newBuilder().header(TRACEPARENT_HEADER_NAME, traceparent).build()
// Take a snapshot of the difference in the system and SDK clocks and send the request along the chain
val offset = sdkClockOffset()
val networkResponse: Response = chain.proceed(request)
// Get response and determine the size of the body
var contentLength: Long? = getContentLengthFromHeader(networkResponse)
if (contentLength == null) {
// If we get the body for a server-sent events stream, then we will wait forever
contentLength = getContentLengthFromBody(networkResponse, networkResponse.header(CONTENT_TYPE_HEADER_NAME))
}
if (contentLength == null) {
// Set the content length to 0 if we can't determine it
contentLength = 0L
}
var response: Response = networkResponse
var networkCaptureData: NetworkCaptureData? = null
val shouldCaptureNetworkData = embrace.internalInterface.shouldCaptureNetworkBody(request.url.toString(), request.method)
// If we need to capture the network response body,
if (shouldCaptureNetworkData) {
if (ENCODING_GZIP.equals(networkResponse.header(CONTENT_ENCODING_HEADER_NAME), ignoreCase = true) &&
networkResponse.promisesBody()
) {
val body = networkResponse.body
if (body != null) {
val strippedHeaders = networkResponse.headers.newBuilder()
.removeAll(CONTENT_ENCODING_HEADER_NAME)
.removeAll(CONTENT_LENGTH_HEADER_NAME)
.build()
val realResponseBody = RealResponseBody(
networkResponse.header(CONTENT_TYPE_HEADER_NAME),
-1L,
GzipSource(body.source()).buffer()
)
val responseBuilder = networkResponse.newBuilder().request(request)
responseBuilder.headers(strippedHeaders)
responseBuilder.body(realResponseBody)
response = responseBuilder.build()
}
}
networkCaptureData = getNetworkCaptureData(request, response)
}
embrace.recordNetworkRequest(
EmbraceNetworkRequest.fromCompletedRequest(
EmbraceHttpPathOverride.getURLString(EmbraceOkHttp3PathOverrideRequest(request)),
HttpMethod.fromString(request.method),
response.sentRequestAtMillis + offset,
response.receivedResponseAtMillis + offset,
if (request.body != null) request.body!!.contentLength() else 0,
contentLength,
response.code,
request.header(embrace.traceIdHeader),
if (networkSpanForwardingEnabled) request.header(TRACEPARENT_HEADER_NAME) else null,
networkCaptureData
)
)
return response
}
private fun getContentLengthFromHeader(networkResponse: Response): Long? {
var contentLength: Long? = null
val contentLengthHeaderValue = networkResponse.header(CONTENT_LENGTH_HEADER_NAME)
if (contentLengthHeaderValue != null) {
try {
contentLength = contentLengthHeaderValue.toLong()
} catch (ex: Exception) {
// Ignore
}
}
return contentLength
}
private fun getContentLengthFromBody(networkResponse: Response, contentType: String?): Long? {
var contentLength: Long? = null
// Tolerant of a charset specified in header, e.g. Content-Type: text/event-stream;charset=UTF-8
val serverSentEvent = contentType != null && contentType.startsWith(CONTENT_TYPE_EVENT_STREAM)
if (!serverSentEvent) {
try {
val body = networkResponse.body
if (body != null) {
val source = body.source()
source.request(Long.MAX_VALUE)
contentLength = source.buffer.size
}
} catch (ex: Exception) {
// Ignore
}
}
return contentLength
}
private fun getNetworkCaptureData(request: Request, response: Response): NetworkCaptureData {
var requestHeaders: Map<String, String>? = null
var requestQueryParams: String? = null
var responseHeaders: Map<String, String>? = null
var requestBodyBytes: ByteArray? = null
var responseBodyBytes: ByteArray? = null
var dataCaptureErrorMessage: String? = null
var partsAcquired = 0
try {
responseHeaders = getProcessedHeaders(response.headers.toMultimap())
partsAcquired++
requestHeaders = getProcessedHeaders(request.headers.toMultimap())
partsAcquired++
requestQueryParams = request.url.query
partsAcquired++
requestBodyBytes = getRequestBody(request)
partsAcquired++
if (response.promisesBody()) {
val responseBody = response.body
if (responseBody != null) {
val okResponseBodySource = responseBody.source()
okResponseBodySource.request(Int.MAX_VALUE.toLong())
responseBodyBytes = okResponseBodySource.buffer.snapshot().toByteArray()
}
}
} catch (e: Exception) {
val errors = StringBuilder()
var i = partsAcquired
while (i < 5) {
errors.append("'").append(networkCallDataParts[i]).append("'")
if (i != 4) {
errors.append(", ")
}
i++
}
dataCaptureErrorMessage = "There were errors in capturing the following part(s) of the network call: %s$errors"
embrace.logInternalError(
RuntimeException("Failure during the building of NetworkCaptureData. $dataCaptureErrorMessage", e)
)
}
return NetworkCaptureData(
requestHeaders,
requestQueryParams,
requestBodyBytes,
responseHeaders,
responseBodyBytes,
dataCaptureErrorMessage
)
}
private fun getProcessedHeaders(properties: Map<String, List<String>>): Map<String, String> {
val headers = HashMap<String, String>()
for ((key, value1) in properties) {
val builder = StringBuilder()
for (value in value1) {
builder.append(value)
}
headers[key] = builder.toString()
}
return headers
}
private fun getRequestBody(request: Request): ByteArray? {
try {
val requestCopy = request.newBuilder().build()
val requestBody = requestCopy.body
if (requestBody != null) {
val buffer = Buffer()
requestBody.writeTo(buffer)
return buffer.readByteArray()
}
} catch (e: IOException) {
embrace.logInternalError("Failed to capture okhttp request body.", e.javaClass.toString())
}
return null
}
/**
* Estimate the difference between the current time returned by the SDK clock and the system clock, the latter of which is used by
* OkHttp to determine timestamps
*/
private fun sdkClockOffset(): Long {
// To ensure that the offset is the result of clock drift, we take two samples and ensure that their difference is less than 1ms
// before we use the value. A 1 ms difference between the samples is possible given it could be the result of the time
// "ticking over" to the next millisecond, but given the calls take the order of microseconds, it should not go beyond that.
//
// Any difference that is greater than 1 ms is likely the result of a change to the system clock during this process, or some
// scheduling quirk that makes the result not trustworthy. In that case, we simply don't return an offset.
val sdkTime1 = embrace.internalInterface.getSdkCurrentTime()
val systemTime1 = systemClock.now()
val sdkTime2 = embrace.internalInterface.getSdkCurrentTime()
val systemTime2 = systemClock.now()
val diff1 = sdkTime1 - systemTime1
val diff2 = sdkTime2 - systemTime2
return if (abs(diff1 - diff2) <= 1L) {
(diff1 + diff2) / 2
} else {
0L
}
}
internal companion object {
internal const val ENCODING_GZIP = "gzip"
internal const val CONTENT_LENGTH_HEADER_NAME = "Content-Length"
internal const val CONTENT_ENCODING_HEADER_NAME = "Content-Encoding"
internal const val CONTENT_TYPE_HEADER_NAME = "Content-Type"
internal const val CONTENT_TYPE_EVENT_STREAM = "text/event-stream"
internal const val TRACEPARENT_HEADER_NAME = "traceparent"
private val networkCallDataParts = arrayOf(
"Response Headers",
"Request Headers",
"Query Parameters",
"Request Body",
"Response Body"
)
}
}