Comments (10)
Hi, how would you want the code to be changed in order for this to be applicable?
Currently, Arikawa has a PreRequest
and PostRequest
API in the httputil.Client
type that you should be able to use to inject a tracing middleware. You should also be able to access the context passed into client.WithContext
.
from arikawa.
Let me show an example of problem :
r.AddFunc("ping", func(ctx context.Context, data cmdroute.CommandData) *api.InteractionResponseData {
ctx, span := tracer.Start(ctx, "operation-name")
// big code that takes time
span.End()
return &api.InteractionResponseData{Content: option.NewNullableString("Pong!")}
})
To instrument API call time, properly, I need to use ctx or span to build an event for this ctx/span.
I'm not able to retrieve this ctx/span in httputil.Client
from arikawa.
Ah, for cmdroute
specifically. I think in this case, you should do 2 things:
- Wrap
cmdroute.Router
behind a wrapper that also implementswebhook.InteractionHandler
, then add a trace there (perhaps name itcommand-routing
), then - Create a top-level middleware that adds another trace (perhaps
command-route-handler
).
Subtracting command-route-handler
from command-routing
should give you the overhead of cmdroute
including the API call required to do this.
The API call itself should still be made over PreRequest
, FYI. They all should be sharing the same client.
from arikawa.
Seems lots of work to do to instrument.
Is it possible to envisage a more cleaner/simpler way to do this ?
from arikawa.
I don't think the router is getting first-class support for tracing anytime soon, but I also don't think it's necessarily a lot of code.
from arikawa.
This should work:
// TraceCommandRouter injects tracing into a cmdroute.Router.
// The name is used as the span name.
// A new handler is returned that should be used instead of the original.
func TraceCommandRouter(name string, r *cmdroute.Router) webhook.InteractionHandler {
r.Use(func(next cmdroute.InteractionHandler) cmdroute.InteractionHandler {
return cmdroute.InteractionHandlerFunc(func(ev *discord.InteractionEvent) *api.InteractionResponse {
span := tracer.StartSpan(name + "-handler")
defer span.Finish()
return next.HandleInteraction(ev)
})
})
return cmdroute.InteractionHandlerFunc(func(ev *discord.InteractionEvent) *api.InteractionResponse {
span := tracer.StartSpan(name + "-router")
defer span.Finish()
return r.HandleInteraction(ev)
})
}
Usage:
r := cmdroute.NewRouter()
h.AddInteractionHandler(tracer.TraceCommandRouter("discord-interactions", r))
// use r as usual
r.Add(...)
r.Add(...)
r.Add(...)
from arikawa.
Thank you for taking the time to help me.
I'm sorry for the delay but it took me a long time to get your proposal to work (I'm not very good at it).
With your solution I can't retrieve the command name to make a clean trace.
And I want to instrument the time of my code and the time of the API call separately (but in the same trace), not a sum of the two. I don't think your solution can do that.
from arikawa.
You should definitely be able to get the command name from ev
, but the router doesn't expose that anywhere. Beyond routing, it doesn't really care what the command name is.
A non-intrusive API could be to expose the internal (*cmdroute.Router, string)
pair that is used for routing and tuck it in a context, but I'm not too sure about that myself. Also, middlewares don't have access to that context anyway.
Another way to do this would be to add r.Group
and r.With
so that we're able to do
r.Group(func(r *cmdroute.Router) {
r.Use(trace("group1"))
r.Add(...)
})
r.With(trace("group2")).Add(...)
But this API isn't a thing yet.
from arikawa.
Ok, thank you for your help anyway :-)
from arikawa.
Thanks for all your help
from arikawa.
Related Issues (20)
- v3: cmdroute: Add Router.Group() HOT 2
- SelectComponent should be StringSelectComponent? HOT 1
- Description for StringSelectComponent is incorrect
- Could not find Message Content Intent HOT 1
- v3: Fix regression from c07f574
- Discord doesn't display command results SOLVED HOT 1
- Default avatar is calculated incorrectly for migrated accounts
- v4: Generate API and Gateway structures
- v4: Automatically emit Gateway commands for State wrappers
- v4: Return api.InteractionResponse for CommandHandler
- v4: ComponentHandler should allow type-constructing handlers
- Support for api/v9/guilds/<guildid>/members/supplemental
- feat: Spacebar support
- v3: Inconsistent channel permission values for large guilds between sessions HOT 9
- v3: Add cmdroute.Router.Group
- v3: CommandInteraction Doesn't Support Attachment Options
- Groups do not use autocompleter
- Responding interaction with no CommandHandler return value HOT 4
- Dynamic routing for cmdroute.Router HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from arikawa.