Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Observe instantaneous events #3247

Merged

Conversation

jonatan-ivanov
Copy link
Member

@jonatan-ivanov jonatan-ivanov commented Jun 23, 2022

This PR makes possible to signal instantaneous events that will be sent to handlers so they can record these Events.

A few notes, questions:

  • I wanted to keep it simple for now so Events don't have KeyValues but handlers have access to the Context. Is there a use-case for being able to attach KeyValues to events?
  • Events can be extended so arbitrary fields can be added (adding data is also possible to the Context).
  • The Events are not available from the Context and event history is not preserved (in the Context). Both should be simple to implement though.
  • Because of the above, the signature of the method in the handler contains the Event and the Context too: onEvent(Observation.Event event, Observation.Context context).
  • I think it would be nice to be consistent to the other methods and pass only the Context but that has a few consequences:
    • The last Event or the Event history should be available from the Context which would make it bigger and it can be problematic if concurrent events are possible.
    • Harder to use since users should know where they can get the event and when will it be available for them.
  • In this PR we track the events using a Counter but adding events/annotations to the Span should be simple:
    @Override
    public void onEvent(Observation.Event event, Observation.Context context) {
        Span span = getTracingContext(context).getSpan();
        span.event(event.getContextualName());
    }
  • Run ObservationHandlerSample for an example, you should see the Counter (custom.event) and a log entry simulating logging/tracing scenarios: [...] i.m.o.ObservationTextPublisher - EVENT - event.name='custom.event' [...]

fyi: TimerObservationHandler was renamed to DefaultMeterObservationHandler because it can create Counters not only Timers.

@jonatan-ivanov jonatan-ivanov linked an issue Jun 23, 2022 that may be closed by this pull request
# Conflicts:
#	micrometer-core/src/test/java/io/micrometer/core/instrument/binder/okhttp3/OkHttpMetricsEventListenerTest.java
#	micrometer-observation/src/main/java/io/micrometer/observation/ObservationHandler.java
@jonatan-ivanov jonatan-ivanov added this to the 1.10.0-M4 milestone Jul 16, 2022
@jonatan-ivanov jonatan-ivanov marked this pull request as ready for review July 16, 2022 00:16
# Conflicts:
#	micrometer-observation/src/main/java/io/micrometer/observation/Observation.java
# Conflicts:
#	micrometer-core/src/test/java/io/micrometer/core/instrument/binder/okhttp3/OkHttpMetricsEventListenerTest.java
Copy link
Member

@shakuzen shakuzen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I just left one question about the naming of the counter in the default meter handler.

public boolean supportsContext(Observation.Context context) {
return true;
public void onEvent(Observation.Event event, Observation.Context context) {
Counter.builder(event.getName()).tags(createTags(context)).register(meterRegistry).increment();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the event is scoped to the Observation, I wonder if the Counter name should somehow include both. observation name + event name, perhaps? I'm not sure how this looks given typical use cases, though, which is probably the best gauge of what makes sense. Unless events are supposed to be standalone things that just happen to occur during an observation?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes a lot of sense to me. Since we are attaching an Event to an Observation, I think it is a fair assumption that they belong together.

I can imagine a use-case where Events are just happening standalone, not connected to an Observation but to cover that use-case, I would use a different component (if we want to cover that at all).

A real-life example I can think of is applying the same business logic for two different endpoints or sources: let's say we are accepting messages over HTTP and AMQP but the workflow/business logic is the same for both.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Observe instantaneous events
3 participants