Title: Serverless Function Tracing Needs an Isolated Scope · Issue #3572 · getsentry/sentry-javascript · GitHub
Open Graph Title: Serverless Function Tracing Needs an Isolated Scope · Issue #3572 · getsentry/sentry-javascript
X Title: Serverless Function Tracing Needs an Isolated Scope · Issue #3572 · getsentry/sentry-javascript
Description: Review the documentation: https://docs.sentry.io/ Search for existing issues: https://github.com/getsentry/sentry-javascript/issues Use the latest release: https://github.com/getsentry/sentry-javascript/releases Provide a link to the aff...
Open Graph Description: Review the documentation: https://docs.sentry.io/ Search for existing issues: https://github.com/getsentry/sentry-javascript/issues Use the latest release: https://github.com/getsentry/sentry-javas...
X Description: Review the documentation: https://docs.sentry.io/ Search for existing issues: https://github.com/getsentry/sentry-javascript/issues Use the latest release: https://github.com/getsentry/sentry-javas...
Opengraph URL: https://github.com/getsentry/sentry-javascript/issues/3572
X: @github
Domain: github.com
IPv4 address: 140.82.114.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Serverless Function Tracing Needs an Isolated Scope","articleBody":"\u003c!-- Requirements: please go through this checklist before opening a new issue -->\r\n\r\n- [x] Review the documentation: https://docs.sentry.io/\r\n- [x] Search for existing issues: https://github.com/getsentry/sentry-javascript/issues\r\n- [x] Use the latest release: https://github.com/getsentry/sentry-javascript/releases\r\n- [x] Provide a link to the affected event from your Sentry account: https://sentry.io/organizations/par-tech-inc/performance/tdp-log-processing:248e161ab69049b29769dbbe61ed5da6/?transaction=process_generator_sessions\r\n\r\n## Package + Version\r\n\r\n- [x] `@sentry/node` 6.3.6\r\n\r\n### Version:\r\n\r\n```\r\n6.3.6\r\n```\r\n\r\n## Description\r\n\r\nHere is my `withSentry` wrapper for Azure serverless Function invocations:\r\n\r\n```\r\nconst Sentry = require(\"@sentry/node\");\r\nconst Tracing = require(\"@sentry/tracing\");\r\nconst mongodb = require(\"mongodb\");\r\n\r\nconst config = require(\"../../config/sentry\");\r\n\r\nif (config.dsn) {\r\n Sentry.init({\r\n ...config,\r\n integrations: [\r\n // enable DB calls tracing\r\n new Tracing.Integrations.Mongo({ useMongoose: true })\r\n // new Tracing.Integrations.Mysql() // No integration available for mysql2.\r\n ]\r\n });\r\n}\r\n\r\n/**\r\n * Add Sentry transaction information to outgoing messages.\r\n * @param {*} transaction\r\n * @param {*} results\r\n * @returns\r\n */\r\nconst propagateTransaction = (transaction, results) => {\r\n if (results && transaction) {\r\n for (const message of Object.values(results)) {\r\n Object.assign(message, {\r\n sentryParentSpanId: transaction.spanId,\r\n sentryTraceId: transaction.traceId,\r\n sentrySampled: transaction.sampled\r\n });\r\n }\r\n }\r\n return results;\r\n};\r\n\r\n/**\r\n * A higher order function which logs error data to Sentry.\r\n *\r\n * @param {*} callback The actual function which might throw errors.\r\n */\r\nmodule.exports = callback => async (context, trigger, ...args) => {\r\n let transaction;\r\n if (config.dsn) {\r\n // Define a transaction for performance tracking.\r\n const transactionContext = {\r\n op: \"serverless-task\",\r\n name: context?.executionContext?.functionName || \"test-function\"\r\n };\r\n if (\"sentryParentSpanId\" in trigger) {\r\n Object.assign(transactionContext, {\r\n parentSpanId: trigger.sentryParentSpanId,\r\n traceId: trigger.sentryTraceId,\r\n sampled: trigger.sentrySampled\r\n });\r\n }\r\n transaction = Sentry.startTransaction(transactionContext);\r\n\r\n Sentry.configureScope(scope => {\r\n scope.setSpan(transaction);\r\n scope.setContext(\r\n \"executionContext\",\r\n context?.executionContext || {\r\n errorMessage:\r\n \"No executionContext defined. This could be a notification from a test environment.\"\r\n }\r\n );\r\n scope.setContext(\"functionTrigger\", trigger);\r\n });\r\n }\r\n\r\n try {\r\n return propagateTransaction(\r\n transaction,\r\n await callback(context, trigger, ...args)\r\n );\r\n } catch (error) {\r\n if (config.dsn) {\r\n Sentry.captureException(error, scope => {\r\n scope.setContext(\r\n \"executionContext\",\r\n context?.executionContext || {\r\n errorMessage:\r\n \"No executionContext defined. This could be a notification from a test environment.\"\r\n }\r\n );\r\n scope.setContext(\"functionTrigger\", trigger);\r\n scope.setContext(\r\n \"theFullContext\",\r\n Object.assign(\r\n {},\r\n ...Object.keys(context).map(k => ({\r\n [k]: JSON.stringify(context[k])\r\n }))\r\n )\r\n );\r\n });\r\n scope.setSpan(transaction);\r\n await Sentry.flush();\r\n }\r\n\r\n throw error;\r\n } finally {\r\n await transaction?.finish();\r\n }\r\n};\r\n```\r\n\r\nDefining an Azure Function like this: \r\n\r\n```\r\nmodule.exports = withSentry(processLogFile);\r\n```\r\n\r\nThis logs everything correctly for exceptions since the scope is set immediately before sending the log, but performance tracing associates DB children with the wrong span and other context. This appears to be because the DB integrations use the span set on the scope. Since we have a high volume application (on the order of 3400 invocations per minute), many function invocations run concurrently and, since `Sentry.configureScope()` configures the span in a global scope, logged child DB transactions are associated not with the invocation they actually ran during but instead with context of the function invocation that last called `Sentry.configureScope()`. In these images, there are dozens of MongoDB calls associated with the *_sessions function even though the inserts are actually all called by the parent *_timeseries function:\r\n\r\n\r\n\r\n","author":{"url":"https://github.com/derekrprice","@type":"Person","name":"derekrprice"},"datePublished":"2021-05-19T17:59:09Z","url":"https://github.com/getsentry/sentry-javascript/issues/3572"}
| 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:61965484-a817-9413-849d-7d510d0a3815 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DB56:3977D7:10D30F:170158:6B04B14D |
| html-safe-nonce | 1ee53a2e4b068e196b83f898c391bc1e3040ea57afb827e5f627d79a44cff149 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEQjU2OjM5NzdENzoxMEQzMEY6MTcwMTU4OjZCMDRCMTREIiwidmlzaXRvcl9pZCI6IjgxODYwOTAzMjUxNjk5NzU2MjkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 08c85c270deceeca10b747b87de8c8336b09e389baace92e1f7f2bd2a579e546 |
| hovercard-subject-tag | issue:895728221 |
| 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/3572/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1f134c96a05b5b60d3a2aaa93712af650c3866ed45e3035ec950f8584cb2ee8e/getsentry/sentry-javascript/issues/3572 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1f134c96a05b5b60d3a2aaa93712af650c3866ed45e3035ec950f8584cb2ee8e/getsentry/sentry-javascript/issues/3572 |
| og:image:alt | Review the documentation: https://docs.sentry.io/ Search for existing issues: https://github.com/getsentry/sentry-javascript/issues Use the latest release: https://github.com/getsentry/sentry-javas... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | derekrprice |
| hostname | github.com |
| expected-hostname | github.com |
| None | f1d110ea058a0915c1d8aa3fc2069fa15801e128f591e5762525abe77490ecb1 |
| 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 | ffa22a65e4c46d376940fe34b05e6a7e9c7fdb9b |
| 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