Giter Site home page Giter Site logo

Unable to instrument API call about arikawa HOT 10 CLOSED

syrm avatar syrm commented on July 30, 2024
Unable to instrument API call

from arikawa.

Comments (10)

diamondburned avatar diamondburned commented on July 30, 2024

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.

syrm avatar syrm commented on July 30, 2024

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.

diamondburned avatar diamondburned commented on July 30, 2024

Ah, for cmdroute specifically. I think in this case, you should do 2 things:

  1. Wrap cmdroute.Router behind a wrapper that also implements webhook.InteractionHandler, then add a trace there (perhaps name it command-routing), then
  2. 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.

syrm avatar syrm commented on July 30, 2024

Seems lots of work to do to instrument.
Is it possible to envisage a more cleaner/simpler way to do this ?

from arikawa.

diamondburned avatar diamondburned commented on July 30, 2024

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.

diamondburned avatar diamondburned commented on July 30, 2024

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.

syrm avatar syrm commented on July 30, 2024

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.

diamondburned avatar diamondburned commented on July 30, 2024

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.

syrm avatar syrm commented on July 30, 2024

Ok, thank you for your help anyway :-)

from arikawa.

syrm avatar syrm commented on July 30, 2024

Thanks for all your help

from arikawa.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.