-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Augment http logs with extra fields for gql requests
- Loading branch information
Showing
5 changed files
with
147 additions
and
76 deletions.
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 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