Instantly share code, notes, and snippets.
Last active
March 22, 2023 15:33
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save Lms24/ff3e243549bf986f50c1d7a92cc3a99c to your computer and use it in GitHub Desktop.
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
import type { HandleServerError, RequestEvent } from '@sveltejs/kit'; | |
import * as SentryNode from '@sentry/node'; | |
import type { Handle } from '@sveltejs/kit'; | |
import '@sentry/tracing'; | |
import type { Transaction } from '@sentry/types'; | |
import { | |
baggageHeaderToDynamicSamplingContext, | |
dynamicSamplingContextToSentryBaggageHeader | |
} from '@sentry/utils'; | |
import parse from 'node-html-parser'; | |
import { sequence } from '@sveltejs/kit/hooks'; | |
import { RewriteFrames } from '@sentry/integrations'; | |
console.log('Initting Sentry on SERVER!'); | |
SentryNode.init({ | |
dsn: 'https://75ab9da4161c45f89b679ee9629a37eb@o4504389074223104.ingest.sentry.io/4504389080514560', | |
tracesSampleRate: 1.0, | |
debug: true, | |
integrations: [new SentryNode.Integrations.Http(), new RewriteFrames()] | |
}); | |
SentryNode.setTag('svelteKit', 'server'); | |
export const handleError = (({ error, event }) => { | |
SentryNode.captureException(error, { contexts: { sveltekit: { event } } }); | |
console.log('>>>', typeof error, error, event); | |
console.log('msg:', error.message); | |
return { message: error.message }; | |
}) satisfies HandleServerError; | |
const sentryHandle = (async (props) => { | |
const { event, resolve } = props; | |
console.log('--------------SERVER-HANDLE-START--------------------'); | |
console.log(event.request); | |
const transaction = createTransaction(event); | |
const response = await resolve(event, { | |
transformPageChunk: ({ html }) => { | |
console.log('>>> this is parse', parse); | |
if (!parse || typeof parse !== 'function') { | |
return html; | |
} | |
const document = parse(html); | |
const head = document.querySelector('head'); | |
if (!head) { | |
return html; | |
} | |
head?.set_content( | |
head?.innerHTML + | |
`<meta name="sentry-trace" content="${transaction?.toTraceparent()}"/>` + | |
`<meta name="baggage" content="${dynamicSamplingContextToSentryBaggageHeader( | |
transaction?.getDynamicSamplingContext() || {} | |
)}"/>` | |
); | |
return document.toString(); | |
} | |
}); | |
const eventType = getServerEventType(event); | |
if (eventType === 'page') { | |
const html = response.body; | |
console.log(html); | |
} | |
finishTransaction(transaction, response); | |
console.log('--------------SERVER-HANDLE-STOP--------------------'); | |
return response; | |
}) satisfies Handle; | |
export const handle = sequence(sentryHandle); | |
function createTransaction(event: RequestEvent): Transaction | undefined { | |
const transaction = SentryNode.startTransaction({ | |
name: `${event.request.method} ${event.route.id}`, | |
op: 'http.server', | |
description: `${event.request.method} ${event.route.id}` | |
}); | |
if (!transaction) { | |
return undefined; | |
} | |
transaction.traceId = | |
event.request.headers.get('sentry-trace')?.split('-')[0] || transaction.traceId; | |
transaction.sampled = | |
event.request.headers.get('sentry-trace')?.split('-')[2] === '1' || transaction.sampled; | |
transaction.setMetadata({ | |
...transaction.metadata, | |
dynamicSamplingContext: baggageHeaderToDynamicSamplingContext( | |
event.request.headers.get('baggage') | |
), | |
source: 'route' | |
}); | |
SentryNode.getCurrentHub().getScope()?.setSpan(transaction); | |
addTags(event, transaction); | |
return transaction; | |
} | |
function finishTransaction(transaction: Transaction | undefined, response: Response) { | |
transaction?.setHttpStatus(response.status); | |
transaction?.finish(); | |
} | |
function addTags(event: RequestEvent | undefined, transaction: Transaction | undefined) { | |
if (!transaction || !event) { | |
return; | |
} | |
const serverEventType = getServerEventType(event); | |
transaction.tags = { | |
...transaction.tags, | |
...(serverEventType === 'data' && { data_request: true }), | |
...(serverEventType === 'page' && { page_request: true }), | |
...(serverEventType === 'api' && { api_request: true }) | |
}; | |
} | |
type ServerEventType = 'data' | 'api' | 'page'; | |
function getServerEventType(event: RequestEvent): ServerEventType { | |
if (event.isDataRequest === true) { | |
return 'data'; | |
} | |
if (event.route.id && event.route.id.includes('/api/')) { | |
return 'api'; | |
} | |
return 'page'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment