Tracking api usage #47
-
This is an issue I've run up against in contextmod while using snoowrap. Not sure if its in the scope for snoots but figured I'd lay it out here in case it was possible to incorporate. The scenario:
The problem: Using snoowrap to track how the api is consumed is not granular. I can access the "overall" usage for the entire bot by counting api requests but I can't "know" from where the api request originated from (rule/action/etc) -- only as specific as (maybe) subreddit level but not from anything specific to the configuration. Solution?? I would like to be able to "scope" an api client arbitrarily, analogous to something like a database transaction. So that I can do something like "when Rule A is run then any api calls made within the execution context of Rule A are tagged as such" so that after Rule A is run I can count how many calls were made. Maybe using an eventlistener? I don't have much more of idea than that. And I don't know how far along snoots is that this would be massive to implement but thought i'd share an "advanced" usage concept that would help non-trivial bot developers better "debug" their api usage. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Baking that into snoots itself is possible, but it's a lot of extra work for not a lot of reward, since making it generic enough to fit everyone's needs would add a lot of API surface. The good news though is that since you are working in Node there is actually already a way to do what you want, and it should even work with snoowrap: AsyncLocalStorage. ALS is a way to attach a piece of data to an async context, rather than a lexical scope. I've used it in multiple server-side projects for tracking a request / event id for better log tracing, but you can just as easily use it for what you're describing here. If all you need is a single value for what rule / action is currently running you can use it directly via AsyncLocalStorage.run. If you need more complicated behavior, like inheritance or more complex data types you can either make that logic yourself or use something like cls-hooked (I've used it with good results before, though be warned that while it has worked perfectly for me so far it is fairly old at this point). |
Beta Was this translation helpful? Give feedback.
Baking that into snoots itself is possible, but it's a lot of extra work for not a lot of reward, since making it generic enough to fit everyone's needs would add a lot of API surface. The good news though is that since you are working in Node there is actually already a way to do what you want, and it should even work with snoowrap: AsyncLocalStorage. ALS is a way to attach a piece of data to an async context, rather than a lexical scope. I've used it in multiple server-side projects for tracking a request / event id for better log tracing, but you can just as easily use it for what you're describing here. If all you need is a single value for what rule / action is currently running you …