-
-
Notifications
You must be signed in to change notification settings - Fork 468
Expand file tree
/
Copy pathSentryApolloInterceptor.kt
More file actions
219 lines (192 loc) · 8.79 KB
/
SentryApolloInterceptor.kt
File metadata and controls
219 lines (192 loc) · 8.79 KB
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
package io.sentry.apollo
import com.apollographql.apollo.api.Mutation
import com.apollographql.apollo.api.Query
import com.apollographql.apollo.api.Subscription
import com.apollographql.apollo.exception.ApolloException
import com.apollographql.apollo.exception.ApolloHttpException
import com.apollographql.apollo.interceptor.ApolloInterceptor
import com.apollographql.apollo.interceptor.ApolloInterceptor.CallBack
import com.apollographql.apollo.interceptor.ApolloInterceptor.FetchSourceType
import com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorRequest
import com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorResponse
import com.apollographql.apollo.interceptor.ApolloInterceptorChain
import com.apollographql.apollo.request.RequestHeaders
import io.sentry.BaggageHeader
import io.sentry.Breadcrumb
import io.sentry.Hint
import io.sentry.IScopes
import io.sentry.ISpan
import io.sentry.ScopesAdapter
import io.sentry.SentryIntegrationPackageStorage
import io.sentry.SentryLevel
import io.sentry.SpanDataConvention
import io.sentry.SpanStatus
import io.sentry.TypeCheckHint.APOLLO_REQUEST
import io.sentry.TypeCheckHint.APOLLO_RESPONSE
import io.sentry.util.IntegrationUtils.addIntegrationToSdkVersion
import io.sentry.util.SpanUtils
import io.sentry.util.TracingUtils
import java.util.concurrent.Executor
private const val TRACE_ORIGIN = "auto.graphql.apollo"
class SentryApolloInterceptor(
private val scopes: IScopes = ScopesAdapter.getInstance(),
private val beforeSpan: BeforeSpanCallback? = null
) : ApolloInterceptor {
private companion object {
init {
SentryIntegrationPackageStorage.getInstance().addPackage("maven:io.sentry:sentry-apollo", BuildConfig.VERSION_NAME)
}
}
constructor(scopes: IScopes) : this(scopes, null)
constructor(beforeSpan: BeforeSpanCallback) : this(ScopesAdapter.getInstance(), beforeSpan)
init {
addIntegrationToSdkVersion("Apollo")
SentryIntegrationPackageStorage.getInstance().addPackage("maven:io.sentry:sentry-apollo", BuildConfig.VERSION_NAME)
}
override fun interceptAsync(request: InterceptorRequest, chain: ApolloInterceptorChain, dispatcher: Executor, callBack: CallBack) {
val activeSpan = if (io.sentry.util.Platform.isAndroid()) scopes.transaction else scopes.span
if (activeSpan == null) {
val headers = addTracingHeaders(request, null)
val modifiedRequest = request.toBuilder().requestHeaders(headers).build()
chain.proceedAsync(modifiedRequest, dispatcher, callBack)
} else {
val span = startChild(request, activeSpan)
span.spanContext.origin = TRACE_ORIGIN
val headers = addTracingHeaders(request, span)
val requestWithHeader = request.toBuilder().requestHeaders(headers).build()
span.setData("operationId", requestWithHeader.operation.operationId())
span.setData("variables", requestWithHeader.operation.variables().valueMap().toString())
chain.proceedAsync(
requestWithHeader,
dispatcher,
object : CallBack {
override fun onResponse(response: InterceptorResponse) {
// onResponse is called only for statuses 2xx
val statusCode: Int? = response.httpResponse.map { it.code() }.orNull()
if (statusCode != null) {
span.status = SpanStatus.fromHttpStatusCode(statusCode, SpanStatus.UNKNOWN)
span.setData(SpanDataConvention.HTTP_STATUS_CODE_KEY, statusCode)
} else {
span.status = SpanStatus.UNKNOWN
}
response.httpResponse.map { it.request().method() }.orNull()?.let {
span.setData(
SpanDataConvention.HTTP_METHOD_KEY,
it.uppercase()
)
}
finish(span, requestWithHeader, response)
callBack.onResponse(response)
}
override fun onFetch(sourceType: FetchSourceType) {
callBack.onFetch(sourceType)
}
override fun onFailure(e: ApolloException) {
span.apply {
status = if (e is ApolloHttpException) {
setData(SpanDataConvention.HTTP_STATUS_CODE_KEY, e.code())
SpanStatus.fromHttpStatusCode(e.code(), SpanStatus.INTERNAL_ERROR)
} else {
SpanStatus.INTERNAL_ERROR
}
throwable = e
}
finish(span, requestWithHeader)
callBack.onFailure(e)
}
override fun onCompleted() {
callBack.onCompleted()
}
}
)
}
}
override fun dispose() {}
private fun addTracingHeaders(request: InterceptorRequest, span: ISpan?): RequestHeaders {
val requestHeaderBuilder = request.requestHeaders.toBuilder()
if (scopes.options.isTraceSampling && !isIgnored()) {
// we have no access to URI, no way to verify tracing origins
TracingUtils.trace(
scopes,
listOf(request.requestHeaders.headerValue(BaggageHeader.BAGGAGE_HEADER)),
span
)?.let { tracingHeaders ->
requestHeaderBuilder.addHeader(
tracingHeaders.sentryTraceHeader.name,
tracingHeaders.sentryTraceHeader.value
)
tracingHeaders.baggageHeader?.let {
requestHeaderBuilder.addHeader(it.name, it.value)
}
}
}
return requestHeaderBuilder.build()
}
private fun isIgnored(): Boolean {
return SpanUtils.isIgnored(scopes.getOptions().getIgnoredSpanOrigins(), TRACE_ORIGIN)
}
private fun startChild(request: InterceptorRequest, activeSpan: ISpan): ISpan {
val operation = request.operation.name().name()
val operationType = when (request.operation) {
is Query -> "query"
is Mutation -> "mutation"
is Subscription -> "subscription"
else -> request.operation.javaClass.simpleName
}
val op = "http.graphql.$operationType"
val description = "$operationType $operation"
return activeSpan.startChild(op, description)
}
private fun finish(span: ISpan, request: InterceptorRequest, response: InterceptorResponse? = null) {
var newSpan: ISpan? = span
if (beforeSpan != null) {
try {
newSpan = beforeSpan.execute(span, request, response)
} catch (e: Exception) {
scopes.options.logger.log(SentryLevel.ERROR, "An error occurred while executing beforeSpan on ApolloInterceptor", e)
}
}
if (newSpan == null) {
// span is dropped
span.spanContext.sampled = false
} else {
span.finish()
}
response?.let {
if (it.httpResponse.isPresent) {
val httpResponse = it.httpResponse.get()
val httpRequest = httpResponse.request()
val breadcrumb = Breadcrumb.http(httpRequest.url().toString(), httpRequest.method(), httpResponse.code())
httpRequest.body()?.contentLength().ifHasValidLength { contentLength ->
breadcrumb.setData("request_body_size", contentLength)
}
httpResponse.body()?.contentLength().ifHasValidLength { contentLength ->
breadcrumb.setData("response_body_size", contentLength)
}
val hint = Hint().apply {
set(APOLLO_REQUEST, httpRequest)
set(APOLLO_RESPONSE, httpResponse)
}
scopes.addBreadcrumb(breadcrumb, hint)
}
}
}
private fun Long?.ifHasValidLength(fn: (Long) -> Unit) {
if (this != null && this != -1L) {
fn.invoke(this)
}
}
/**
* The BeforeSpan callback
*/
fun interface BeforeSpanCallback {
/**
* Mutates span before being added.
*
* @param span the span to mutate or drop
* @param request the HTTP request executed by okHttp
* @param response the HTTP response received by okHttp
*/
fun execute(span: ISpan, request: InterceptorRequest, response: InterceptorResponse?): ISpan?
}
}