Skip to content

Commit

Permalink
Improve Sentry
Browse files Browse the repository at this point in the history
  • Loading branch information
devformatters authored and Zeck Li committed Apr 26, 2019
1 parent ad97b78 commit b07516f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions common/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export * from './number'
export * from './language'
export * from './text'
export * from './cache'
export * from './random'
2 changes: 2 additions & 0 deletions common/utils/random.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const genSentryActionId = () =>
Math.random().toString(36).substr(2, 9)
20 changes: 19 additions & 1 deletion common/utils/withApollo.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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
Expand Down Expand Up @@ -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
})
})

0 comments on commit b07516f

Please sign in to comment.