From d9e202e873a1990565d108793199001c78beda9b Mon Sep 17 00:00:00 2001 From: Matt Stover Date: Thu, 14 Nov 2024 14:31:30 -0800 Subject: [PATCH 1/2] fix: new config to enable stellate debug header and customFetch for logging --- config/dev-custom-host.js | 4 +++- config/dynamic.js | 2 ++ src/api/Auth0Link.js | 2 ++ src/api/HttpLink.js | 5 +++++ src/api/apollo.js | 1 + src/server-entry.js | 15 ++++++++++++++- 6 files changed, 27 insertions(+), 2 deletions(-) diff --git a/config/dev-custom-host.js b/config/dev-custom-host.js index 7935fdf310..48dacc7e37 100644 --- a/config/dev-custom-host.js +++ b/config/dev-custom-host.js @@ -18,8 +18,10 @@ export default merge(base, devVm, { gaId: 'UA-11686022-7', // dev-vm property enableSentry: false, sentryURI: 'https://7ce141b23c4a4e6091c206d08442f0e9@o7540.ingest.sentry.io/1201287', + // apolloQueryFetchLogging: process.env.APOLLO_QUERY_FETCH_LOGGING || true, // Testing values for stellate, uncomment and adjust as needed - // stellateGraphqlUri: process.env.STELLATE_GRAPHQL_URI || 'https://kiva.stellate.sh', + // stellateDebugHeaders: process.env.STELLATE_DEBUG_HEADERS || true, + // stellateGraphqlUri: process.env.STELLATE_GRAPHQL_URI || 'https://kiva-staging.stellate.sh', // eslint-disable-next-line max-len // stellateCachedOperations: process.env.STELLATE_CACHED_OPERATIONS || 'configSetting,experimentIds,experimentSetting', auth0: { diff --git a/config/dynamic.js b/config/dynamic.js index 93f3d7235c..028d163ff3 100644 --- a/config/dynamic.js +++ b/config/dynamic.js @@ -26,6 +26,7 @@ export const app = { defaultIndex: `${env}_fundraising_popularity`, }, apolloBatching, + apolloQueryFetchLogging: process.env.APOLLO_QUERY_FETCH_LOGGING || false, apolloNetworkErrorRetryActive, apolloNetworkErrorRetryAttempts, auth0: { @@ -87,6 +88,7 @@ export const app = { sentryURI: process.env.SENTRY_URI || 'https://7ce141b23c4a4e6091c206d08442f0e9@o7540.ingest.sentry.io/1201287', sentryTraceSampleRate: process.env.SENTRY_TRACE_RATE || 0.25, snowplowUri: process.env.SNOWPLOW_URI || 'events.fivetran.com/snowplow/v5qt54ocr2nm', + stellateDebugHeaders: process.env.STELLATE_DEBUG_HEADERS || false, stellateGraphqlUri: process.env.STELLATE_GRAPHQL_URI || null, stellateCachedOperations: process.env.STELLATE_CACHED_OPERATIONS || '', transport: 'https', diff --git a/src/api/Auth0Link.js b/src/api/Auth0Link.js index 28764de555..ac9c6c0196 100644 --- a/src/api/Auth0Link.js +++ b/src/api/Auth0Link.js @@ -9,11 +9,13 @@ function getAuthContext(context, user, token) { authorization: `Bearer ${token}` }; } + console.log(`Updated Auth Context: ${JSON.stringify(context?.headers, null, 2)}`); return context; } export default ({ kvAuth0 }) => { return setContext((operation, previousContext) => { + console.log('operation keys: ', Object.keys(operation)); // If auth0 is not enabled, don't add anything to the context if (!kvAuth0.enabled) return getAuthContext(previousContext); diff --git a/src/api/HttpLink.js b/src/api/HttpLink.js index 013a1002f9..afcf506836 100644 --- a/src/api/HttpLink.js +++ b/src/api/HttpLink.js @@ -6,6 +6,7 @@ export default ({ uri = '', fetch, apolloBatching, + stellateDebugHeaders, stellateGraphqlUri, stellateCachedOperations, }) => { @@ -26,6 +27,10 @@ export default ({ // Create a new options object for stellate const stellateOptions = { ...options, + headers: stellateDebugHeaders ? { + ...options?.headers, + 'gcdn-debug': 1, + } : {}, uri: stellateGraphqlUri ?? uri, }; diff --git a/src/api/apollo.js b/src/api/apollo.js index 22c1e6a95d..5121322f8e 100644 --- a/src/api/apollo.js +++ b/src/api/apollo.js @@ -53,6 +53,7 @@ export default function createApolloClient({ uri, fetch, apolloBatching: appConfig?.apolloBatching ?? true, + stellateDebugHeaders: appConfig?.stellateDebugHeaders, stellateGraphqlUri: appConfig?.stellateGraphqlUri, stellateCachedOperations: appConfig?.stellateCachedOperations, }), diff --git a/src/server-entry.js b/src/server-entry.js index 573b5cd279..22f9924533 100755 --- a/src/server-entry.js +++ b/src/server-entry.js @@ -22,6 +22,19 @@ import fetch from 'make-fetch-happen'; const isDev = process.env.NODE_ENV !== 'production'; +// custom fetch wrapper to log fetch requests +const customFetch = async (uri, options) => { + const response = await fetch(uri, options); + // Log the outgoing options + logFormatter(`Fetch Options: ${uri}, Options: ${JSON.stringify(options)}`); + + // Log the full response + // eslint-disable-next-line max-len + logFormatter(`Server fetch: ${uri}, Status: ${response.status}, Headers: ${JSON.stringify(response.headers.raw())}`); + + return response; +}; + function fillTemplate(template, data) { let html = template; Object.keys(data).forEach(key => { @@ -167,7 +180,7 @@ export default async context => { device, kvAuth0, locale, - fetch, + fetch: config?.apolloQueryFetchLogging ? customFetch : fetch, url, isServer: true, }); From acb00520a80e39f106d73333035330c82e5ef0ed Mon Sep 17 00:00:00 2001 From: Matt Stover Date: Thu, 14 Nov 2024 16:41:33 -0800 Subject: [PATCH 2/2] fix: remove unwanted console logs --- src/api/Auth0Link.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/api/Auth0Link.js b/src/api/Auth0Link.js index ac9c6c0196..28764de555 100644 --- a/src/api/Auth0Link.js +++ b/src/api/Auth0Link.js @@ -9,13 +9,11 @@ function getAuthContext(context, user, token) { authorization: `Bearer ${token}` }; } - console.log(`Updated Auth Context: ${JSON.stringify(context?.headers, null, 2)}`); return context; } export default ({ kvAuth0 }) => { return setContext((operation, previousContext) => { - console.log('operation keys: ', Object.keys(operation)); // If auth0 is not enabled, don't add anything to the context if (!kvAuth0.enabled) return getAuthContext(previousContext);