Title: How do I dependency inject Sentry? · getsentry/sentry-java · Discussion #3047 · GitHub
Open Graph Title: How do I dependency inject Sentry? · getsentry/sentry-java · Discussion #3047
X Title: How do I dependency inject Sentry? · getsentry/sentry-java · Discussion #3047
Description: How do I dependency inject Sentry?
Open Graph Description: I'm working on upgrading from Sentry Java 1.x to Sentry Java 6.x and I've been using dependency inject to inject my SentryClient into classes. In the past, it was easy. Sentry.init returned a Sentr...
X Description: I'm working on upgrading from Sentry Java 1.x to Sentry Java 6.x and I've been using dependency inject to inject my SentryClient into classes. In the past, it was easy. Sentry.init returned...
Opengraph URL: https://github.com/getsentry/sentry-java/discussions/3047
X: @github
Domain: Github.com
{"@context":"https://schema.org","@type":"QAPage","mainEntity":{"@type":"Question","name":"How do I dependency inject Sentry?","text":"\u003cp dir=\"auto\"\u003eI'm working on upgrading from Sentry Java 1.x to Sentry Java 6.x and I've been using dependency inject to inject my SentryClient into classes. In the past, it was easy. \u003ccode class=\"notranslate\"\u003eSentry.init\u003c/code\u003e returned a \u003ccode class=\"notranslate\"\u003eSentryClient\u003c/code\u003e and I passed that around my Guice/Dagger context.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eNow, \u003ccode class=\"notranslate\"\u003eSentry.init\u003c/code\u003e returns an \u003ccode class=\"notranslate\"\u003eIHub\u003c/code\u003e which contains the same methods as \u003ccode class=\"notranslate\"\u003eISentryClient\u003c/code\u003e and more. But the docs recommend to use \u003ccode class=\"notranslate\"\u003eISentryClient\u003c/code\u003e when you only need to report events and pass \u003ccode class=\"notranslate\"\u003eIHub\u003c/code\u003e when you need to configure the client (add tags, etc.)\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eThere appears to be no public way to get an \u003ccode class=\"notranslate\"\u003eISentryClient\u003c/code\u003e from the response of \u003ccode class=\"notranslate\"\u003eSentry.init\u003c/code\u003e, nor does there appear to be a way to initialize a \u003ccode class=\"notranslate\"\u003eHub\u003c/code\u003e with a specific \u003ccode class=\"notranslate\"\u003eSentryClient\u003c/code\u003e without actually initializing a \u003ccode class=\"notranslate\"\u003eSentryClient\u003c/code\u003e in the \u003ccode class=\"notranslate\"\u003eHub\u003c/code\u003e constructor.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eWhat is the recommended way to dependency inject a SentryClient?\u003c/p\u003e","upvoteCount":1,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"\u003cp dir=\"auto\"\u003eHello \u003ca class=\"user-mention notranslate\" data-hovercard-type=\"user\" data-hovercard-url=\"/users/ajacques/hovercard\" data-octo-click=\"hovercard-link-click\" data-octo-dimensions=\"link_type:self\" href=\"https://github.com/ajacques\"\u003e@ajacques\u003c/a\u003e how about injecting \u003ccode class=\"notranslate\"\u003eIHub\u003c/code\u003e instances into your classes? You could use \u003ccode class=\"notranslate\"\u003eHubAdapter\u003c/code\u003e to forward to \u003ccode class=\"notranslate\"\u003eSentry\u003c/code\u003e static method calls in case this is just for making your application more easily testable and mocking Sentry.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eAre you planning on using multiple Sentry projects / DSNs to send data to from a single app? If no, there should be no need to deal with clients directly.\u003c/p\u003e\n\u003cblockquote\u003e\n\u003cp dir=\"auto\"\u003ethe docs recommend to use ISentryClient when you only need to report events and pass IHub when you need to configure the client (add tags, etc.)\u003c/p\u003e\n\u003c/blockquote\u003e\n\u003cp dir=\"auto\"\u003eCan you please link the docs page?\u003c/p\u003e","upvoteCount":1,"url":"https://github.com/getsentry/sentry-java/discussions/3047#discussioncomment-7550320"}}}
| route-pattern | /_view_fragments/Voltron::DiscussionsFragmentsController/show/:user_id/:repository/:discussion_number/discussion_layout(.:format) |
| route-controller | voltron_discussions_fragments |
| route-action | discussion_layout |
| fetch-nonce | v2:773d8f59-e19e-1e81-8e9e-4fd33b005e1e |
| current-catalog-service-hash | 9f0abe34da433c9b6db74bffa2466494a717b579a96b30a5d252e5090baea7be |
| request-id | 84A0:29D042:43316F9:59EC7F5:6A6518D7 |
| html-safe-nonce | 078d47a40ac4e67595e3429febb41ce4362c8adbaeb33c9aff671a398a92c10e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NEEwOjI5RDA0Mjo0MzMxNkY5OjU5RUM3RjU6NkE2NTE4RDciLCJ2aXNpdG9yX2lkIjoiODM3NTY3Njc1MjkwNDkxOTI1NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | b5c3d587b8cd5975b16e3407b7485cc9b963de937a838362fa41d2f13682cb16 |
| hovercard-subject-tag | discussion:5837455 |
| github-keyboard-shortcuts | repository,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/Voltron::DiscussionsFragmentsController/show/getsentry/sentry-java/3047/discussion_layout |
| twitter:image | https://opengraph.githubassets.com/12949a96de1267a9684e3a93299ac02f1f5a5a523a9926222bd2fd27978097f2/getsentry/sentry-java/discussions/3047 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/12949a96de1267a9684e3a93299ac02f1f5a5a523a9926222bd2fd27978097f2/getsentry/sentry-java/discussions/3047 |
| og:image:alt | I'm working on upgrading from Sentry Java 1.x to Sentry Java 6.x and I've been using dependency inject to inject my SentryClient into classes. In the past, it was easy. Sentry.init returned a Sentr... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 52c76df668885aaff23b50bdca1fa1ea44ac9c1553e888ebc70ff1e4daa4625b |
| turbo-cache-control | no-preview |
| go-import | github.com/getsentry/sentry-java git https://github.com/getsentry/sentry-java.git |
| octolytics-dimension-user_id | 1396951 |
| octolytics-dimension-user_login | getsentry |
| octolytics-dimension-repository_id | 3368190 |
| octolytics-dimension-repository_nwo | getsentry/sentry-java |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 3368190 |
| octolytics-dimension-repository_network_root_nwo | getsentry/sentry-java |
| 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 | 309153364422b3c499922d1a2a6404910a58ed8e |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width