Setting sentry's context

So I recently upgraded to the latest snapshot, and I'm trying to migrate this:
sentry?.captureException(e) {
setContexts(
"character", mapOf(
"name" to character.name,
"level" to character.level,
"userId" to character.userId,
"world" to character.world
)
)
setContexts(
"guild", mapOf(
"name" to guild.name, "id" to guild.longId
)
)
}
sentry?.captureException(e) {
setContexts(
"character", mapOf(
"name" to character.name,
"level" to character.level,
"userId" to character.userId,
"world" to character.world
)
)
setContexts(
"guild", mapOf(
"name" to guild.name, "id" to guild.longId
)
)
}
First thing I tried was to replace captureException with captureThrowable. This sentry instance is coming from EventContext in a custom KordEx event.
Solution:
See the announcement I just made - I've added two parameters to the capture functions, which should be sufficient
Jump to solution
34 Replies
gdude
gdude•8mo ago
Using captureThrowable is correct, yes I don't think you need to add the guild context? I'm pretty sure we do that for you ah, no, that's just for things you don't catch anyway, I think probably adding contexts to the sentry context might be an approach? I'm not sure tbh the sentry context objects are meant to be transactional, in that they're tied to the execution of an event handler, command, component, etc so it's more intended for you to add breadcrumbs than directly add context but I should be able to expose that ok, I've extended the API a bit will build and release
Solution
gdude
gdude•8mo ago
See the announcement I just made - I've added two parameters to the capture functions, which should be sufficient
Galarzaa
Galarzaa•8mo ago
Perfect! Thank you!
I don't think you need to add the guild context? I'm pretty sure we do that for you
Either way it is not an event that has a specific guild attached to it, but rather, it finds the relevant guilds where it has to announce something. In this context, I want to log reasons for why an announcement could not be made to a guild. Any way, I guess it is better to set the guild's object in the lambda for that instead 🤔
sentry?.captureThrowable(e,
mapOf("character" to character.toSentryMap())) {
this.guild = guild
}
sentry?.captureThrowable(e,
mapOf("character" to character.toSentryMap())) {
this.guild = guild
}
gdude
gdude•8mo ago
huh? There's no guild propery in that builder
Galarzaa
Galarzaa•8mo ago
There is, SentryCapture.guild
public abstract class SentryCapture : KordExKoinComponent {
/** Channel object to automatically populate data from. **/
public open var channel: Channel? = null

/** Guild object to automatically populate data from. **/
public open var guild: Guild? = null
public abstract class SentryCapture : KordExKoinComponent {
/** Channel object to automatically populate data from. **/
public open var channel: Channel? = null

/** Guild object to automatically populate data from. **/
public open var guild: Guild? = null
gdude
gdude•8mo ago
oh yeah, you're right I forgot about that wait but then the API change I made doesn't make sense what is character in this case? the RPG character from before?
Galarzaa
Galarzaa•8mo ago
Yes
gdude
gdude•8mo ago
because it feels to me like it doesn't make sense to only add this context to the Sentry context when an exception happens like it'd make much more sense to have a generic breadcrumb that's like "Loaded character" with the data earlier on beginning to remember why I implemented it the way I did now I should leave more notes in my code yeah, so you'd just do like
Galarzaa
Galarzaa•8mo ago
To be honest, I'm not exactly sure how breadcrums display in sentry or work. In another component of my project that uses sentry directly, I'm also using context on exceptions, so I can see them attached to the issue like:
No description
gdude
gdude•8mo ago
sentry?.breadcrumb {
message = "Loaded character"
// Copy data from `character` into `this.data`
}
sentry?.breadcrumb {
message = "Loaded character"
// Copy data from `character` into `this.data`
}
I see well, breadcrumb data generally just displays as an object, so maybe being able to add to the primary context makes sense but I don't think it makes sense to only do that at submission time that results in a lot of duplicated code instead, I think it might be better to either use a breadcrumb, or swap to an API that allows you to store extra context at the SentryContext level, and then have that submitted whenever anything is captured that makes the most sense to me, at least this is largely because kordex itself will submit things to sentry like if you run a command and there's an uncaptured exception you probably still want the context on sentry this needs a fair bit more engineering than what I pushed this morning, but does it seem like a better approach to you? in that sense it'd be sentry.context("character", whatever) and then just don't catch the exception (or rethrow it, or handle it yourself and just call the capture function)
Galarzaa
Galarzaa•8mo ago
That reminds me I did have a command error, but the issue raised on issue had no info about the command that was used. i don't know if it was lost due to the exception happening inside a component's callback 🤔
gdude
gdude•8mo ago
that's what happened, yeah
Galarzaa
Galarzaa•8mo ago
But that was a separate issue, and that was still on 1.6.0, hence why I'm upgrading, so I can test that again and not report an unsupported version 😅
gdude
gdude•8mo ago
I couldn't figure out how to handle an optional command/eventhandler coupling with component builders without rewriting half the library so I didn't
Galarzaa
Galarzaa•8mo ago
I have a lot of those 🙃 , usually confirmation messages for important actions
gdude
gdude•8mo ago
the API is too generic to be tied to the parent builders but anyway that's a different problem what do you think of my proposal above
Galarzaa
Galarzaa•8mo ago
Yeah, I was checking sentry docs to see how that would look and it looks good to me:
No description
Galarzaa
Galarzaa•8mo ago
How do capturing event and messages work? Specially with free tier quotas? 🤔
gdude
gdude•8mo ago
Not sure on the quotas I think the correct attitude to developing with the Sentry extension is probably worth documenting when I eventually get to that
Galarzaa
Galarzaa•8mo ago
I've only had experience with Sentry on python before but using it on python is like cheating, way too much info available without doing much, it just shows you all the values of variables in the scope right there:
No description
Galarzaa
Galarzaa•8mo ago
Also not much documentation for kotlin, specially coroutines
gdude
gdude•8mo ago
Sentry doesn't actually support Kotlin It's just the Java library I think the JVM is capable of collecting some of that kind of info, but it would be slow probably
Galarzaa
Galarzaa•8mo ago
There is a Kotlin extension, I was actually having some trouble due to my context being mixed due in coroutines (in another module without kordex), like it would contain the context of a different part of the code. So I had to do some changes for that. Maybe it would be worth looking into it for kordex too: https://docs.sentry.io/platforms/java/enriching-events/scopes/#kotlin-coroutines
Scopes and Hubs
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.
gdude
gdude•8mo ago
This doesn't look useful for kordex Actual Sentry scopes don't get created until just before they're submitted The global API is avoided entirely Sentry's global API is a horrible anti-pattern
Galarzaa
Galarzaa•8mo ago
Then maybe I should look into that lol, in my other modules, because what I have on these other modules doesn't seem very good lol Anyway, thanks for the help!
gdude
gdude•8mo ago
No worries, I am going to change this API again though so don't get too comfortable I'll do a version bump tho
Galarzaa
Galarzaa•8mo ago
Yeah, I'm going to stick with breadcrums instead like you suggested, sorry for the confusion
gdude
gdude•8mo ago
Alright, no worries
Galarzaa
Galarzaa•8mo ago
Don't want to open yet another thread on sentry, but I found a small issue on the recent changes to contexts. Seems like contexts have to be objects (or maps): Got this on Sentry:
No description
gdude
gdude•8mo ago
Oh, huh, it needs to be an object?
Galarzaa
Galarzaa•8mo ago
yes
gdude
gdude•8mo ago
Thanks for the heads up, I'll look at it tomorrow
Galarzaa
Galarzaa•5mo ago
Just a bit of a shameless "bump", but I opened a PR related to this a couple of weeks ago: https://github.com/Kord-Extensions/kord-extensions/pull/295
GitHub
Change extension name sentry context to a mapping by Galarzaa90 · P...
We discussed this briefly on Discord: https://canary.discord.com/channels/1121419906995458098/1210756178515206164 But basically, Sentry is expecting an object, not a single value, so it shows this ...
Want results from more Discord servers?
Add your server