-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[andr] Augment http logs with extra fields for gql requests #141
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b7b4bc1
Augment http logs with extra fields for gql requests
murki 328df92
lint
murki 2a4d4f0
fix
murki 8355f15
add test coverage
murki e0d9d5f
add check for span name
murki 8505772
remove
murki cddb3a4
Merge branch 'main' into murki/BIT-4116-2
murki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
71 changes: 0 additions & 71 deletions
71
.../capture-apollo3/src/main/kotlin/io/bitdrift/capture/apollo3/CaptureApollo3Interceptor.kt
This file was deleted.
Oops, something went wrong.
53 changes: 53 additions & 0 deletions
53
...m/capture-apollo3/src/main/kotlin/io/bitdrift/capture/apollo3/CaptureApolloInterceptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// capture-sdk - bitdrift's client SDK | ||
// Copyright Bitdrift, Inc. All rights reserved. | ||
// | ||
// Use of this source code is governed by a source available license that can be found in the | ||
// LICENSE file or at: | ||
// https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt | ||
|
||
package io.bitdrift.capture.apollo3 | ||
|
||
import com.apollographql.apollo3.api.ApolloRequest | ||
import com.apollographql.apollo3.api.ApolloResponse | ||
import com.apollographql.apollo3.api.Mutation | ||
import com.apollographql.apollo3.api.Operation | ||
import com.apollographql.apollo3.api.Query | ||
import com.apollographql.apollo3.api.Subscription | ||
import com.apollographql.apollo3.interceptor.ApolloInterceptor | ||
import com.apollographql.apollo3.interceptor.ApolloInterceptorChain | ||
import io.bitdrift.capture.Capture | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
/** | ||
* An [ApolloInterceptor] that logs request and response events to the [Capture.Logger]. | ||
*/ | ||
class CaptureApolloInterceptor: ApolloInterceptor { | ||
|
||
override fun <D : Operation.Data> intercept(request: ApolloRequest<D>, chain: ApolloInterceptorChain): Flow<ApolloResponse<D>> { | ||
// Use special header format that is recognized by the CaptureOkHttpEventListener to be transformed into a span | ||
val requestBuilder = request.newBuilder() | ||
.addHttpHeader("x-capture-span-key", "gql") | ||
.addHttpHeader("x-capture-span-gql-name", "graphql") | ||
.addHttpHeader("x-capture-span-gql-field-operation-name", request.operation.name()) | ||
.addHttpHeader("x-capture-span-gql-field-operation-id", request.operation.id()) | ||
.addHttpHeader("x-capture-span-gql-field-operation-type", request.operation.type()) | ||
// TODO(murki): Augment request logs with | ||
// request.executionContext[CustomScalarAdapters]?.let { | ||
// addHttpHeader("x-capture-span-gql-field-operation-variables", request.operation.variables(it).valueMap.toString()) | ||
// } | ||
|
||
val modifiedRequest = requestBuilder.build() | ||
|
||
// TODO(murki): Augment response logs with response.errors | ||
return chain.proceed(modifiedRequest) | ||
} | ||
|
||
private fun <D : Operation.Data> Operation<D>.type(): String { | ||
return when (this) { | ||
is Query -> "query" | ||
is Mutation -> "mutation" | ||
is Subscription -> "subscription" | ||
else -> this.javaClass.simpleName | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ import androidx.compose.ui.platform.ViewCompositionStrategy | |
import androidx.fragment.app.Fragment | ||
import androidx.navigation.fragment.findNavController | ||
import com.apollographql.apollo3.ApolloClient | ||
import com.apollographql.apollo3.network.okHttpClient | ||
import com.example.rocketreserver.LaunchListQuery | ||
import com.github.michaelbull.result.onFailure | ||
import com.github.michaelbull.result.onSuccess | ||
|
@@ -36,7 +37,7 @@ import io.bitdrift.capture.Capture.Logger | |
import io.bitdrift.capture.CaptureJniLibrary | ||
import io.bitdrift.capture.LogLevel | ||
import io.bitdrift.capture.LoggerImpl | ||
import io.bitdrift.capture.apollo3.CaptureApollo3Interceptor | ||
import io.bitdrift.capture.apollo3.CaptureApolloInterceptor | ||
import io.bitdrift.capture.network.okhttp.CaptureOkHttpEventListenerFactory | ||
import io.bitdrift.gradletestapp.databinding.FragmentFirstBinding | ||
import kotlinx.coroutines.MainScope | ||
|
@@ -141,20 +142,14 @@ class FirstFragment : Fragment() { | |
AppExitReason.entries | ||
) | ||
|
||
okHttpClient = provideOkHttpClient() | ||
apolloClient = provideApolloClient() | ||
} | ||
|
||
private fun provideOkHttpClient(): OkHttpClient { | ||
return OkHttpClient.Builder() | ||
okHttpClient = OkHttpClient.Builder() | ||
.eventListenerFactory(CaptureOkHttpEventListenerFactory()) | ||
.build() | ||
} | ||
|
||
private fun provideApolloClient(): ApolloClient { | ||
return ApolloClient.Builder() | ||
apolloClient = ApolloClient.Builder() | ||
.serverUrl("https://apollo-fullstack-tutorial.herokuapp.com/graphql") | ||
.addInterceptor(CaptureApollo3Interceptor()) | ||
.okHttpClient(okHttpClient) | ||
.addInterceptor(CaptureApolloInterceptor()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the extra integration point the customers would need to add. Note that this only works with okhttpclients that use our |
||
.build() | ||
} | ||
|
||
|
@@ -223,8 +218,12 @@ class FirstFragment : Fragment() { | |
|
||
private fun performGraphQlRequest(view: View) { | ||
MainScope().launch { | ||
val response = apolloClient.query(LaunchListQuery()).execute() | ||
Logger.logDebug(mapOf("response_data" to response.data.toString())) { "GraphQL response data received" } | ||
try { | ||
val response = apolloClient.query(LaunchListQuery()).execute() | ||
Logger.logDebug(mapOf("response_data" to response.data.toString())) { "GraphQL response data received" } | ||
} catch (e: Exception) { | ||
Timber.e(e, "GraphQL request failed") | ||
} | ||
} | ||
|
||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
? Is this needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's questionable whether gql variables https://graphql.org/learn/queries/#variables could contain PII, plus they're generally pretty big in size. Since we don't currently show it in our default dashboards I think we can ship this version without including them just yet