Title: Support Deno.serve instrumentation · Issue #12450 · getsentry/sentry-javascript · GitHub
Open Graph Title: Support Deno.serve instrumentation · Issue #12450 · getsentry/sentry-javascript
X Title: Support Deno.serve instrumentation · Issue #12450 · getsentry/sentry-javascript
Description: Problem Statement similar to https://github.com/getsentry/sentry-javascript/blob/develop/packages/bun/src/integrations/bunserver.ts Solution Brainstorm Rough working implementation: import { captureException, continueTrace, SEMANTIC_ATTR...
Open Graph Description: Problem Statement similar to https://github.com/getsentry/sentry-javascript/blob/develop/packages/bun/src/integrations/bunserver.ts Solution Brainstorm Rough working implementation: import { captur...
X Description: Problem Statement similar to https://github.com/getsentry/sentry-javascript/blob/develop/packages/bun/src/integrations/bunserver.ts Solution Brainstorm Rough working implementation: import { captur...
Opengraph URL: https://github.com/getsentry/sentry-javascript/issues/12450
X: @github
Domain: github.com
IPv4 address: 140.82.112.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Support Deno.serve instrumentation ","articleBody":"### Problem Statement\r\n\r\nsimilar to https://github.com/getsentry/sentry-javascript/blob/develop/packages/bun/src/integrations/bunserver.ts\r\n\r\n### Solution Brainstorm\r\n\r\nRough working implementation:\r\n\r\n```ts\r\nimport {\r\n captureException,\r\n continueTrace,\r\n SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,\r\n SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,\r\n setHttpStatus,\r\n startSpan,\r\n withIsolationScope,\r\n} from 'https://deno.land/x/sentry@8.8.0/index.mjs'\r\nimport type { Integration, IntegrationFn, SpanAttributes } from 'npm:@sentry/types@8.8.0'\r\n\r\ntype PartialURL = {\r\n host?: string\r\n path?: string\r\n protocol?: string\r\n relative?: string\r\n search?: string\r\n hash?: string\r\n}\r\n\r\n/**\r\n * Parses string form of URL into an object\r\n * // borrowed from https://tools.ietf.org/html/rfc3986#appendix-B\r\n * // intentionally using regex and not \u003ca/> href parsing trick because React Native and other\r\n * // environments where DOM might not be available\r\n * @returns parsed URL object\r\n */\r\nexport function parseUrl(url: string): PartialURL {\r\n if (!url) {\r\n return {}\r\n }\r\n\r\n const match = url.match(/^(([^:/?#]+):)?(\\/\\/([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?$/)\r\n\r\n if (!match) {\r\n return {}\r\n }\r\n\r\n // coerce to undefined values to empty string so we don't get 'undefined'\r\n const query = match[6] || ''\r\n const fragment = match[8] || ''\r\n return {\r\n host: match[4],\r\n path: match[5],\r\n protocol: match[2],\r\n search: query,\r\n hash: fragment,\r\n relative: match[5] + query + fragment, // everything minus origin\r\n }\r\n}\r\n\r\n/**\r\n * Takes a URL object and returns a sanitized string which is safe to use as span name\r\n * see: https://develop.sentry.dev/sdk/data-handling/#structuring-data\r\n */\r\nfunction getSanitizedUrlString(url: PartialURL): string {\r\n const { protocol, host, path } = url\r\n\r\n const filteredHost = (host &&\r\n host\r\n // Always filter out authority\r\n .replace(/^.*@/, '[filtered]:[filtered]@')\r\n // Don't show standard :80 (http) and :443 (https) ports to reduce the noise\r\n // TODO: Use new URL global if it exists\r\n .replace(/(:80)$/, '')\r\n .replace(/(:443)$/, '')) ||\r\n ''\r\n\r\n return `${protocol ? `${protocol}://` : ''}${filteredHost}${path}`\r\n}\r\n\r\nfunction defineIntegration\u003cFn extends IntegrationFn>(fn: Fn): (...args: Parameters\u003cFn>) => Integration {\r\n return fn\r\n}\r\n\r\ntype RawHandler = (request: Request, info: Deno.ServeHandlerInfo) => Response | Promise\u003cResponse>\r\n\r\nconst INTEGRATION_NAME = 'DenoServer'\r\n\r\nconst _denoServerIntegration = (() => {\r\n return {\r\n name: INTEGRATION_NAME,\r\n setupOnce() {\r\n instrumentDenoServe()\r\n },\r\n }\r\n}) satisfies IntegrationFn\r\n\r\n/**\r\n * Instruments `Deno.serve` to automatically create transactions and capture errors.\r\n *\r\n * ```js\r\n * Sentry.init({\r\n * integrations: [\r\n * Sentry.denoServerIntegration(),\r\n * ],\r\n * })\r\n * ```\r\n */\r\nexport const denoServerIntegration = defineIntegration(_denoServerIntegration)\r\n\r\n/**\r\n * Instruments Deno.serve by patching it's options.\r\n */\r\nexport function instrumentDenoServe(): void {\r\n Deno.serve = new Proxy(Deno.serve, {\r\n apply(serveTarget, serveThisArg, serveArgs: any) {\r\n const [arg1, arg2] = serveArgs\r\n let handler: RawHandler | undefined\r\n\r\n let type = 0\r\n\r\n if (typeof arg1 === 'function') {\r\n handler = arg1\r\n type = 1\r\n } else if (typeof arg2 === 'function') {\r\n handler = arg2\r\n type = 2\r\n } else if (arg1 && typeof arg1 === 'object' && 'handler' in arg1 && typeof arg1.handler === 'function') {\r\n handler = arg1.handler\r\n type = 3\r\n } else if (arg2 && typeof arg2 === 'object' && 'handler' in arg2 && typeof arg2.handler === 'function') {\r\n handler = arg2.handler\r\n type = 4\r\n }\r\n\r\n if (handler) {\r\n handler = instrumentDenoServeOptions(handler)\r\n\r\n if (type === 1) {\r\n serveArgs[0] = handler\r\n } else if (type === 2) {\r\n serveArgs[1] = handler\r\n } else if (type === 3) {\r\n serveArgs[0].handler = handler\r\n } else if (type === 4) {\r\n serveArgs[1].handler = handler\r\n }\r\n }\r\n\r\n return serveTarget.apply(serveThisArg, serveArgs)\r\n },\r\n })\r\n}\r\n\r\n/**\r\n * Instruments Deno.serve `fetch` option to automatically create spans and capture errors.\r\n */\r\nfunction instrumentDenoServeOptions(handler: RawHandler): RawHandler {\r\n return new Proxy(handler, {\r\n apply(fetchTarget, fetchThisArg, fetchArgs: Parameters\u003ctypeof handler>) {\r\n return withIsolationScope((isolationScope) => {\r\n const request = fetchArgs[0]\r\n const upperCaseMethod = request.method.toUpperCase()\r\n if (upperCaseMethod === 'OPTIONS' || upperCaseMethod === 'HEAD') {\r\n return fetchTarget.apply(fetchThisArg, fetchArgs)\r\n }\r\n\r\n const parsedUrl = parseUrl(request.url)\r\n const attributes: SpanAttributes = {\r\n [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.deno.serve',\r\n 'http.request.method': request.method || 'GET',\r\n [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url',\r\n }\r\n if (parsedUrl.search) {\r\n attributes['http.query'] = parsedUrl.search\r\n }\r\n\r\n const url = getSanitizedUrlString(parsedUrl)\r\n\r\n isolationScope.setSDKProcessingMetadata({\r\n request: {\r\n url,\r\n method: request.method,\r\n headers: Object.fromEntries(request.headers),\r\n },\r\n })\r\n\r\n return continueTrace({\r\n sentryTrace: request.headers.get('sentry-trace') || '',\r\n baggage: request.headers.get('baggage'),\r\n }, () => {\r\n return startSpan(\r\n {\r\n attributes,\r\n op: 'http.server',\r\n name: `${request.method} ${parsedUrl.path || '/'}`,\r\n },\r\n async (span) => {\r\n try {\r\n const response = await (fetchTarget.apply(fetchThisArg, fetchArgs) as ReturnType\u003ctypeof handler>)\r\n if (response && response.status) {\r\n setHttpStatus(span, response.status)\r\n isolationScope.setContext('response', {\r\n headers: Object.fromEntries(response.headers),\r\n status_code: response.status,\r\n })\r\n }\r\n return response\r\n } catch (e) {\r\n captureException(e, {\r\n mechanism: {\r\n type: 'deno',\r\n handled: false,\r\n data: {\r\n function: 'serve',\r\n },\r\n },\r\n })\r\n throw e\r\n }\r\n },\r\n )\r\n })\r\n })\r\n },\r\n })\r\n}\r\n```\r\n\r\nneeds `requestDataIntegration`","author":{"url":"https://github.com/brc-dd","@type":"Person","name":"brc-dd"},"datePublished":"2024-06-11T07:44:56Z","url":"https://github.com/getsentry/sentry-javascript/issues/12450"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:0dd84a3c-cb91-a0dd-07e6-10fb34b46579 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8E1E:F551D:76B08F:9F318B:6B04A65B |
| html-safe-nonce | 1b66b3cc46a11a3389e8da6bfb5878f0750be0ab3c555c9115ae1ef864c57e13 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RTFFOkY1NTFEOjc2QjA4Rjo5RjMxOEI6NkIwNEE2NUIiLCJ2aXNpdG9yX2lkIjoiMjA4OTM4NTQ5NDAzODQ4ODY2NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 17c3cf0f22445f6514b8c92281677e29de0fcd48c8f728b1db0996af34ddb807 |
| hovercard-subject-tag | issue:2345652029 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/getsentry/sentry-javascript/12450/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d7791590f56fe8a62f6826109cae88b59195ea4c3970ee737f8c040d096d821c/getsentry/sentry-javascript/issues/12450 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d7791590f56fe8a62f6826109cae88b59195ea4c3970ee737f8c040d096d821c/getsentry/sentry-javascript/issues/12450 |
| og:image:alt | Problem Statement similar to https://github.com/getsentry/sentry-javascript/blob/develop/packages/bun/src/integrations/bunserver.ts Solution Brainstorm Rough working implementation: import { captur... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | brc-dd |
| hostname | github.com |
| expected-hostname | github.com |
| None | 188497a0f45a7aa8f7c866faa1950d1a7f8d82b59416adf549b222ad1d1dd19b |
| turbo-cache-control | no-preview |
| go-import | github.com/getsentry/sentry-javascript git https://github.com/getsentry/sentry-javascript.git |
| octolytics-dimension-user_id | 1396951 |
| octolytics-dimension-user_login | getsentry |
| octolytics-dimension-repository_id | 3328572 |
| octolytics-dimension-repository_nwo | getsentry/sentry-javascript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 3328572 |
| octolytics-dimension-repository_network_root_nwo | getsentry/sentry-javascript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 557cfbac6cdb532730c95c90f2cefdc0c4128029 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width