diff --git a/common/utils/index.ts b/common/utils/index.ts index ae6a1e3db6..1b75a7088c 100644 --- a/common/utils/index.ts +++ b/common/utils/index.ts @@ -11,3 +11,4 @@ export * from './number' export * from './language' export * from './text' export * from './cache' +export * from './random' diff --git a/common/utils/random.ts b/common/utils/random.ts new file mode 100644 index 0000000000..fd1d389368 --- /dev/null +++ b/common/utils/random.ts @@ -0,0 +1,2 @@ +export const genSentryActionId = () => + Math.random().toString(36).substr(2, 9) diff --git a/common/utils/withApollo.ts b/common/utils/withApollo.ts index 2c50be2641..09a5972aa5 100644 --- a/common/utils/withApollo.ts +++ b/common/utils/withApollo.ts @@ -1,3 +1,4 @@ +import * as Sentry from '@sentry/browser' import { ApolloClient } from 'apollo-client' import { ApolloLink, split } from 'apollo-link' import { setContext } from 'apollo-link-context' @@ -10,6 +11,8 @@ import https from 'https' import withApollo from 'next-with-apollo' import getConfig from 'next/config' +import { genSentryActionId } from '~/common/utils' + import { inMemoryCache // setupPersistCache @@ -87,13 +90,28 @@ const authLink = setContext((_, { headers }) => { } }) +const sentryLink = setContext((_, { headers }) => { + // Add action id for Sentry + const actionId = genSentryActionId() + Sentry.configureScope((scope: any) => { + scope.setTag('action-id', actionId) + }) + + return { + headers: { + ...headers, + 'x-sentry-action-id': actionId + } + } +}) + export default withApollo(({ ctx, headers, initialState }) => { inMemoryCache.restore(initialState || {}) // setupPersistCache() return new ApolloClient({ - link: ApolloLink.from([errorLink, authLink, dataLink({ headers })]), + link: ApolloLink.from([errorLink, authLink, sentryLink, dataLink({ headers })]), cache: inMemoryCache }) })