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

Add "createNotStarted" method that lazily creates context #3401

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
import io.micrometer.common.lang.NonNull;
import io.micrometer.common.lang.Nullable;
import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationPredicate;
import io.micrometer.observation.ObservationRegistry;
import io.micrometer.observation.GlobalObservationConvention;
import io.micrometer.observation.ObservationConvention;

import java.util.Objects;
import java.util.function.Supplier;

/**
* In order to describe your samples via e.g. enums instead of Strings you can use this
Expand Down Expand Up @@ -187,6 +189,32 @@ default <T extends Observation.Context> Observation observation(@Nullable Observ
return observation;
}

/**
* Creates an {@link Observation} for the given {@link ObservationConvention}. You
* need to manually start it. When the {@link ObservationRegistry} is a no-op, this
* method fast returns a no-op {@link Observation} and skips the creation of the
* {@link Observation.Context}. This quick check avoids unnecessary
* {@link Observation.Context} creations. More detailed check, such as
* {@link ObservationPredicate}, is performed at the later stage after
* {@link Observation.Context} creation.
* @param customConvention convention that (if not {@code null}) will override any
* pre-configured conventions
* @param defaultConvention default convention that will be picked if there was
* neither custom convention nor a pre-configured one via
* {@link ObservationRegistry.ObservationConfig#observationConvention(GlobalObservationConvention[])}
* @param contextSupplier observation context supplier
* @param registry observation registry
* @return observation
*/
default <T extends Observation.Context> Observation createNotStarted(
Copy link
Member

Choose a reason for hiding this comment

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

Can we also add a null check since the registry is @Nullable? See:

if (registry == null || registry.isNoop()
|| !registry.observationConfig().isObservationEnabled(name, context)) {
return NOOP;
}

Also, should not we do this in the Observation too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

registry is non-null on this method, so don't need null check here.

I'll add documentation that this no-op registry check is a quick check and further decision(ObservationPredicate) would happen after context is created.

Copy link
Member

Choose a reason for hiding this comment

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

I'll go to an eye doctor. :D

Copy link
Member

Choose a reason for hiding this comment

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

I think we should call these methods createNotStarted but I'm not sure we all agreed on that.

I think either we call all of them createNotStarted or observation.

@Nullable ObservationConvention<T> customConvention, @NonNull ObservationConvention<T> defaultConvention,
@NonNull Supplier<T> contextSupplier, @NonNull ObservationRegistry registry) {
if (registry.isNoop()) {
return Observation.NOOP;
}
return observation(customConvention, defaultConvention, contextSupplier.get(), registry);
}

/**
* Creates and starts an {@link Observation}.
* @param registry observation registry
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
*/
package io.micrometer.observation.docs;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Supplier;

import io.micrometer.common.KeyValue;
import io.micrometer.common.KeyValues;
import io.micrometer.observation.Observation;
import io.micrometer.observation.Observation.Context;
import io.micrometer.observation.ObservationFilter;
import io.micrometer.observation.ObservationRegistry;
import io.micrometer.observation.GlobalObservationConvention;
Expand Down Expand Up @@ -116,6 +120,22 @@ void keyValuesShouldBeAlwaysAdded() {
assertThat(context.getHighCardinalityKeyValues()).containsOnly(KeyValue.of("global", "high cardinality"));
}

@Test
void createNotStartedShouldNotCreateContextWithNoopRegistry() {
ObservationRegistry registry = ObservationRegistry.NOOP;

AtomicBoolean isCalled = new AtomicBoolean();
Supplier<Context> supplier = () -> {
isCalled.set(true);
return new Observation.Context();
};

Observation observation = TestConventionObservation.CONTEXTUAL_NAME.createNotStarted(null,
new FirstObservationConvention(), supplier, registry);
assertThat(observation.isNoop()).isTrue();
assertThat(isCalled).isFalse();
}

private ObservationRegistry observationRegistry() {
ObservationRegistry registry = ObservationRegistry.create();
registry.observationConfig().observationHandler(context -> true);
Expand Down