{"componentChunkName":"component---src-templates-platform-tsx","path":"/platforms/elixir/enriching-events/scopes/","result":{"data":{"file":{"id":"2d09d885-700e-5106-84fb-3801058c87b5","relativePath":"common/enriching-events/scopes.mdx","sourceInstanceName":"platforms","childMarkdownRemark":null,"childMdx":{"body":"function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }\n\nfunction _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }\n\nfunction _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }\n\n/* @jsx mdx */\nvar _frontmatter = {\n  \"title\": \"Scopes and Hubs\",\n  \"sidebar_order\": 200,\n  \"redirect_from\": [\"/learn/scopes/\"],\n  \"description\": \"SDKs will typically automatically manage the scopes for you in the framework integrations. Learn what a scope is and how you can use it to your advantage.\",\n  \"notSupported\": [\"native.breakpad\", \"native.crashpad\", \"native.minidumps\", \"native.ue4\"]\n};\n\nvar makeShortcode = function makeShortcode(name) {\n  return function MDXDefaultShortcode(props) {\n    console.warn(\"Component \" + name + \" was not imported, exported, or provided by MDXProvider as global scope\");\n    return mdx(\"div\", props);\n  };\n};\n\nvar Alert = makeShortcode(\"Alert\");\nvar PlatformContent = makeShortcode(\"PlatformContent\");\nvar layoutProps = {\n  _frontmatter: _frontmatter\n};\nvar MDXLayout = \"wrapper\";\nreturn function MDXContent(_ref) {\n  var components = _ref.components,\n      props = _objectWithoutProperties(_ref, [\"components\"]);\n\n  return mdx(MDXLayout, _extends({}, layoutProps, props, {\n    components: components,\n    mdxType: \"MDXLayout\"\n  }), mdx(\"p\", null, \"When an event is captured and sent to Sentry, SDKs will merge that event data with extra\\ninformation from the current scope. SDKs will typically automatically manage the scopes\\nfor you in the framework integrations and you don't need to think about them. However,\\nyou should know what a scope is and how you can use it for your advantage.\"), mdx(\"h2\", {\n    \"id\": \"whats-a-scope-whats-a-hub\",\n    \"style\": {\n      \"position\": \"relative\"\n    }\n  }, mdx(\"a\", _extends({\n    parentName: \"h2\"\n  }, {\n    \"href\": \"#whats-a-scope-whats-a-hub\",\n    \"aria-label\": \"whats a scope whats a hub permalink\",\n    \"className\": \"anchor before\"\n  }), mdx(\"svg\", _extends({\n    parentName: \"a\"\n  }, {\n    \"viewBox\": \"0 0 24 24\",\n    \"xmlns\": \"http://www.w3.org/2000/svg\"\n  }), mdx(\"path\", _extends({\n    parentName: \"svg\"\n  }, {\n    \"d\": \"M10.879 6.05L15 1.93A5.001 5.001 0 0 1 22.071 9l-4.121 4.121a1 1 0 0 1-1.414-1.414l4.12-4.121a3 3 0 1 0-4.242-4.243l-4.121 4.121a1 1 0 1 1-1.414-1.414zm2.242 11.9L9 22.07A5 5 0 1 1 1.929 15l4.121-4.121a1 1 0 0 1 1.414 1.414l-4.12 4.121a3 3 0 1 0 4.242 4.243l4.121-4.121a1 1 0 1 1 1.414 1.414zm-8.364-.122l13.071-13.07a1 1 0 0 1 1.415 1.414L6.172 19.242a1 1 0 1 1-1.415-1.414z\",\n    \"fill\": \"currentColor\"\n  })))), \"What's a Scope, what's a Hub\"), mdx(\"p\", null, \"You can think of the hub as the central point that our SDKs use to route an\\nevent to Sentry. When you call \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"init()\"), \" a hub is created and a client and a\\nblank scope are created on it. That hub is then associated with the current\\nthread and will internally hold a stack of scopes.\"), mdx(\"p\", null, \"The scope will hold useful information that should be sent along with the\\nevent. For instance \", mdx(\"a\", _extends({\n    parentName: \"p\"\n  }, {\n    \"href\": \"../context/\"\n  }), \"contexts\"), \" or\\n\", mdx(\"a\", _extends({\n    parentName: \"p\"\n  }, {\n    \"href\": \"../breadcrumbs/\"\n  }), \"breadcrumbs\"), \" are stored on\\nthe scope. When a scope is pushed, it inherits all data from the parent scope\\nand when it pops all modifications are reverted.\"), mdx(\"p\", null, \"The default SDK integrations will push and pop scopes intelligently. For\\ninstance web framework integrations will create and destroy scopes around your\\nroutes or controllers.\"), mdx(\"h2\", {\n    \"id\": \"how-do-the-scope-and-hub-work\",\n    \"style\": {\n      \"position\": \"relative\"\n    }\n  }, mdx(\"a\", _extends({\n    parentName: \"h2\"\n  }, {\n    \"href\": \"#how-do-the-scope-and-hub-work\",\n    \"aria-label\": \"how do the scope and hub work permalink\",\n    \"className\": \"anchor before\"\n  }), mdx(\"svg\", _extends({\n    parentName: \"a\"\n  }, {\n    \"viewBox\": \"0 0 24 24\",\n    \"xmlns\": \"http://www.w3.org/2000/svg\"\n  }), mdx(\"path\", _extends({\n    parentName: \"svg\"\n  }, {\n    \"d\": \"M10.879 6.05L15 1.93A5.001 5.001 0 0 1 22.071 9l-4.121 4.121a1 1 0 0 1-1.414-1.414l4.12-4.121a3 3 0 1 0-4.242-4.243l-4.121 4.121a1 1 0 1 1-1.414-1.414zm2.242 11.9L9 22.07A5 5 0 1 1 1.929 15l4.121-4.121a1 1 0 0 1 1.414 1.414l-4.12 4.121a3 3 0 1 0 4.242 4.243l4.121-4.121a1 1 0 1 1 1.414 1.414zm-8.364-.122l13.071-13.07a1 1 0 0 1 1.415 1.414L6.172 19.242a1 1 0 1 1-1.415-1.414z\",\n    \"fill\": \"currentColor\"\n  })))), \"How do the Scope and Hub Work\"), mdx(\"p\", null, \"As you start using an SDK a scope and hub are automatically created for you out\\nof the box. The hub you are unlikely to be interacting with directly unless you\\nare writing an integration or you want to create or destroy scopes. Scopes on the\\nother hand are more user facing. You can at any point in time call\\n\", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"configure-scope\"), \" to modify data stored on the scope. This is for instance\\nused to \", mdx(\"a\", _extends({\n    parentName: \"p\"\n  }, {\n    \"href\": \"../context/\"\n  }), \"modify the context\"), \".\"), mdx(Alert, {\n    mdxType: \"Alert\"\n  }, \"If you are very curious about how thread locality works: On platforms such as .NET or on Python 3.7 and later we will use \\\"ambient data\\\" to have either the hub flow with your code or the hub is already a singleton that internally manages the scope.\", mdx(\"p\", null, \"Effectively this means that when you spawn a task in .NET and the execution flow is\\nnot suppressed all the context you have bound to the scope in Sentry will flow along.\\nIf however you suppress the flow, you get new scope data.\")), mdx(\"p\", null, \"When you call a global function such as \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"capture_event\"), \" internally Sentry\\ndiscovers the current hub and asks it to capture an event. Internally the hub will\\nthen merge the event with the topmost scope's data.\"), mdx(\"h2\", {\n    \"id\": \"configuring-the-scope\",\n    \"style\": {\n      \"position\": \"relative\"\n    }\n  }, mdx(\"a\", _extends({\n    parentName: \"h2\"\n  }, {\n    \"href\": \"#configuring-the-scope\",\n    \"aria-label\": \"configuring the scope permalink\",\n    \"className\": \"anchor before\"\n  }), mdx(\"svg\", _extends({\n    parentName: \"a\"\n  }, {\n    \"viewBox\": \"0 0 24 24\",\n    \"xmlns\": \"http://www.w3.org/2000/svg\"\n  }), mdx(\"path\", _extends({\n    parentName: \"svg\"\n  }, {\n    \"d\": \"M10.879 6.05L15 1.93A5.001 5.001 0 0 1 22.071 9l-4.121 4.121a1 1 0 0 1-1.414-1.414l4.12-4.121a3 3 0 1 0-4.242-4.243l-4.121 4.121a1 1 0 1 1-1.414-1.414zm2.242 11.9L9 22.07A5 5 0 1 1 1.929 15l4.121-4.121a1 1 0 0 1 1.414 1.414l-4.12 4.121a3 3 0 1 0 4.242 4.243l4.121-4.121a1 1 0 1 1 1.414 1.414zm-8.364-.122l13.071-13.07a1 1 0 0 1 1.415 1.414L6.172 19.242a1 1 0 1 1-1.415-1.414z\",\n    \"fill\": \"currentColor\"\n  })))), \"Configuring the Scope\"), mdx(\"p\", null, \"The most useful operation when working with scopes is the \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"configure-scope\"), \" function.\\nIt can be used to reconfigure the current scope. This for instance can be used to\\nadd custom tags or to inform sentry about the currently authenticated user.\"), mdx(PlatformContent, {\n    includePath: \"configure-scope\",\n    mdxType: \"PlatformContent\"\n  }), mdx(\"p\", null, \"This can also be applied when unsetting a user at logout:\"), mdx(PlatformContent, {\n    includePath: \"unset-user\",\n    mdxType: \"PlatformContent\"\n  }), mdx(\"p\", null, \"To learn what useful information can be associated with scopes see\\n\", mdx(\"a\", _extends({\n    parentName: \"p\"\n  }, {\n    \"href\": \"../context/\"\n  }), \"the context documentation\"), \".\"), mdx(\"h2\", {\n    \"id\": \"local-scopes\",\n    \"style\": {\n      \"position\": \"relative\"\n    }\n  }, mdx(\"a\", _extends({\n    parentName: \"h2\"\n  }, {\n    \"href\": \"#local-scopes\",\n    \"aria-label\": \"local scopes permalink\",\n    \"className\": \"anchor before\"\n  }), mdx(\"svg\", _extends({\n    parentName: \"a\"\n  }, {\n    \"viewBox\": \"0 0 24 24\",\n    \"xmlns\": \"http://www.w3.org/2000/svg\"\n  }), mdx(\"path\", _extends({\n    parentName: \"svg\"\n  }, {\n    \"d\": \"M10.879 6.05L15 1.93A5.001 5.001 0 0 1 22.071 9l-4.121 4.121a1 1 0 0 1-1.414-1.414l4.12-4.121a3 3 0 1 0-4.242-4.243l-4.121 4.121a1 1 0 1 1-1.414-1.414zm2.242 11.9L9 22.07A5 5 0 1 1 1.929 15l4.121-4.121a1 1 0 0 1 1.414 1.414l-4.12 4.121a3 3 0 1 0 4.242 4.243l4.121-4.121a1 1 0 1 1 1.414 1.414zm-8.364-.122l13.071-13.07a1 1 0 0 1 1.415 1.414L6.172 19.242a1 1 0 1 1-1.415-1.414z\",\n    \"fill\": \"currentColor\"\n  })))), \"Local Scopes\"), mdx(\"p\", null, \"We also have support for pushing and configuring a scope in one go. This is\\ntypically called \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"with-scope\"), \" or \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"push-scope\"), \" which is also very helpful\\nif you only want to send data with one specific event. In the following example we are using\\nthat function to attach a \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"level\"), \" and a \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"tag\"), \" to only one specific error:\"), mdx(PlatformContent, {\n    includePath: \"with-scope\",\n    mdxType: \"PlatformContent\"\n  }), mdx(\"p\", null, \"While this example looks similar to \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"configure-scope\"), \" it's very different, in the sense that\\n\", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"configure-scope\"), \" actually changes the current active scope, all successive calls to \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"configure-scope\"), \"\\nwill keep the changes.\"), mdx(\"p\", null, \"While on the other hand using \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"with-scope\"), \" creates a clone of the current scope\\nand will stay isolated until the function call is completed. So you can either\\nset context information in there that you don't want to be somewhere else or not\\nattach any context information at all by calling \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"clear\"), \" on the scope, while the\\n\\\"global\\\" scope remains unchanged.\"));\n}\n;\nMDXContent.isMDXComponent = true;","tableOfContents":{"items":[{"url":"#whats-a-scope-whats-a-hub","title":"What's a Scope, what's a Hub"},{"url":"#how-do-the-scope-and-hub-work","title":"How do the Scope and Hub Work"},{"url":"#configuring-the-scope","title":"Configuring the Scope"},{"url":"#local-scopes","title":"Local Scopes"}]},"internal":{"type":"Mdx"}}}},"pageContext":{"excerpt":"When an event is captured and sent to Sentry, SDKs will merge that event data with extra\ninformation from the current scope. SDKs will typically automatically manage the scopes\nfor you in the framework integrations and you don't need to think about them. However,\nyou should know what a scope is and how you can use it for your advantage. What's a Scope, what's a Hub You can think of the hub as the central point that our SDKs use to route an\nevent to Sentry. When you call  init()  a hub is created and a client and a\nblank scope are created on it. That hub is then associated with the current\nthread and will internally hold a stack of scopes. The scope will hold useful information that should be sent along with the\nevent. For instance  contexts  or\n breadcrumbs  are stored on\nthe scope. When a scope is pushed, it inherits all data from the parent scope\nand when it pops all modifications are reverted. The default SDK integrations will push and pop scopes intelligently. For\ninstance web framework integrations will create and destroy scopes around your\nroutes or controllers. How do the Scope and Hub Work As you start using an SDK a scope and hub are automatically created for you out\nof the box. The hub you are unlikely to be interacting with directly unless you\nare writing an integration or you want to create or destroy scopes. Scopes on the\nother hand are more user facing. You can at any point in time call\n configure-scope  to modify data stored on the scope. This is for instance\nused to  modify the context . Effectively this means that when you spawn a task in .NET and the execution flow is\nnot suppressed all the context you have bound to the scope in Sentry will flow along.\nIf however you suppress the flow, you get new scope data. When you call a global function such as  capture_event  internally Sentry\ndiscovers the current hub and asks it to capture an event. Internally the hub will\nthen merge the event with the topmost scope's data. Configuring the Scope The most useful operation when working with scopes is the  configure-scope  function.\nIt can be used to reconfigure the current scope. This for instance can be used to\nadd custom tags or to inform sentry about the currently authenticated user. This can also be applied when unsetting a user at logout: To learn what useful information can be associated with scopes see\n the context documentation . Local Scopes We also have support for pushing and configuring a scope in one go. This is\ntypically called  with-scope  or  push-scope  which is also very helpful\nif you only want to send data with one specific event. In the following example we are using\nthat function to attach a  level  and a  tag  to only one specific error: While this example looks similar to  configure-scope  it's very different, in the sense that\n configure-scope  actually changes the current active scope, all successive calls to  configure-scope \nwill keep the changes. While on the other hand using  with-scope  creates a clone of the current scope\nand will stay isolated until the function call is completed. So you can either\nset context information in there that you don't want to be somewhere else or not\nattach any context information at all by calling  clear  on the scope, while the\n\"global\" scope remains unchanged.","title":"Scopes and Hubs","description":"SDKs will typically automatically manage the scopes for you in the framework integrations. Learn what a scope is and how you can use it to your advantage.","sidebar_order":200,"redirect_from":["/learn/scopes/"],"notSupported":["native.breakpad","native.crashpad","native.minidumps","native.ue4"],"platform":{"name":"elixir","title":"Elixir"},"notoc":true,"id":"2d09d885-700e-5106-84fb-3801058c87b5"}},"staticQueryHashes":["1218203755","1222113826","1222113826","1766336459","2158593473","2404336828","2472290386","2764967025","3818502851","4015007367","4192517163","4264099332","518019976"]}