vert.x icon indicating copy to clipboard operation
vert.x copied to clipboard

listen Vertx.Context lifecycle

Open alexeysharandin opened this issue 2 years ago • 3 comments

Good day everyone!

I kindly ask you to look this request. I think it's not require a huge development in the framework but this functionality can help to people who using Vert.x minimize a time to dev testing and debugging code. This request more related to enterprise development and solutions where are tons of business logic and spaghetti code.

Motivation:

Vert.x is reactive framework and working with events. (sorry if my terminology is wrong or different from official). This code executed in atomic events and sometimes need to spend a lot of time to understand what is execution path and "why this code working minutes instead of milliseconds". This small proposal can help to application profiling and improve invistigations speed in development.

Introduction:

At this moment Vert.x have 2 functionalities(maybe I miss something) to track what is happen with the logic what in context:

  1. io.vertx.core.spi.metrics.VertxMetrics
  2. io.vertx.core.spi.tracing.VertxTracer This functionality can help with "single events" but very interesting to see what is happen from begining to the end.

What's want to see:

Events what happen in the context. Something like: Context.OnCreated Context.OnFeatureBegin Context.FeatureComplete Context.FeatureFail Context.OnFeatureEnd Context.OnClosed (looks like ContextInternal.addCloseHook)

maybe it's not full list and you see some additional events.

In general this functionality can help to map all events in one context and see something like classical stacktrace and give ability to some manipulations with business logic like tracking path with parameters/timings/etc.

As I mentoned above - it's something like profiling in application level. At this moment it's possible to do only with additional code in each method what executed in Vert.x context or over org.objectweb.asm.* and hacking a classes.

What are you think about it?

Thanks.

alexeysharandin avatar Apr 14 '22 10:04 alexeysharandin

this might be lot of overhead because a context is created each time an HttpServerRequest is handled.

vietj avatar Apr 15 '22 06:04 vietj

what do you mean by "feature" ?

vietj avatar Apr 15 '22 06:04 vietj

What I see after some research:

this might be lot of overhead because a context is created each time an HttpServerRequest is handled.

Right context created each time but no overhead here because we need to fire events only without some additional Objects or anything else. For example in Quarkus I see new io.vertx.core.impl.DuplicatedContext for each request. In this case I propose to add only 2 methods to io.vertx.core.spi.tracing.VertxTracer like contextCreated(ctx)/contextReleased(ctx) and fire them when it's needed.

what do you mean by "feature" ?

sorry what showing example from wrapper but here less of code and this functionality partially implemented (using io.smallrye.mutiny in Quarkus for tests).

    @GET
    @Path("vertx")
    @Produces(MediaType.TEXT_PLAIN)
    public Uni<String> mutinyVertxTest() {
        Uni<String> uni = SerializableUni
                .createFrom() // fire event io.smallrye.mutiny.infrastructure.onUniCreation
                .item(this::a) // fire event io.smallrye.mutiny.infrastructure.decorate(Supplier<T> supplier)
                .onItem() 
                .transform(this::b) // io.smallrye.mutiny.infrastructure.decorate(Function<I, O> function)
                .onItem()
                .transform(this::c); // io.smallrye.mutiny.infrastructure.decorate(Function<I, O> function)
        return uni;    
    }

    private String a() {
        return "a";
    }
    
    private String b(String s) {
        return s + "b";
    }

    private String c(String s) {
        return s + "c";
    }

What is not implemented in mutiny -> fire events when Supplier.get or Function.apply called (will discuss with owners).

It's possible to use without additional events in core Vert.x but need to fire Context created/released events to map it.

What not enought here - not able to extract what is runned in Supplier/Function. Is it lambda or some method of some class. It's possible to extract if extend these interfaces and add Serializable to them.

alexeysharandin avatar Apr 15 '22 10:04 alexeysharandin