-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Take Supplier<Context> instead of Context #3443
Changes from 2 commits
5b298a2
e883514
f06a6c1
4bbe048
d8e73eb
9d5ff0f
34b98a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,13 +97,14 @@ private ObservationOrTimerCompatibleInstrumentation(MeterRegistry meterRegistry, | |
this.defaultConvention = defaultConvention; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private void start(Supplier<T> contextSupplier) { | ||
if (observationRegistry.isNoop()) { | ||
timerSample = Timer.start(meterRegistry); | ||
} | ||
else { | ||
context = contextSupplier.get(); | ||
observation = Observation.start(convention, defaultConvention, context, observationRegistry); | ||
observation = Observation.start(convention, defaultConvention, contextSupplier, observationRegistry); | ||
context = (T) observation.getContext(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Switched the order here so the supplier isn't called twice.
shakuzen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,19 +59,20 @@ public interface Observation extends ObservationView { | |
* @return started observation | ||
*/ | ||
static Observation start(String name, ObservationRegistry registry) { | ||
return start(name, null, registry); | ||
return start(name, Context::new, registry); | ||
} | ||
|
||
/** | ||
* Creates and starts an {@link Observation}. When no registry is passed or | ||
* observation is not applicable will return a no-op observation. | ||
* @param name name of the observation | ||
* @param context mutable context | ||
* @param contextSupplier mutable context | ||
* @param registry observation registry | ||
* @return started observation | ||
*/ | ||
static Observation start(String name, @Nullable Context context, @Nullable ObservationRegistry registry) { | ||
return createNotStarted(name, context, registry).start(); | ||
static <T extends Context> Observation start(String name, Supplier<T> contextSupplier, | ||
@Nullable ObservationRegistry registry) { | ||
return createNotStarted(name, contextSupplier, registry).start(); | ||
} | ||
|
||
/** | ||
|
@@ -84,7 +85,7 @@ static Observation start(String name, @Nullable Context context, @Nullable Obser | |
* @return created but not started observation | ||
*/ | ||
static Observation createNotStarted(String name, @Nullable ObservationRegistry registry) { | ||
return createNotStarted(name, null, registry); | ||
return createNotStarted(name, Context::new, registry); | ||
} | ||
|
||
/** | ||
|
@@ -93,14 +94,17 @@ static Observation createNotStarted(String name, @Nullable ObservationRegistry r | |
* registry is passed or observation is not applicable will return a no-op | ||
* observation. | ||
* @param name name of the observation | ||
* @param context mutable context | ||
* @param contextSupplier supplier for mutable context | ||
* @param registry observation registry | ||
* @return created but not started observation | ||
*/ | ||
static Observation createNotStarted(String name, @Nullable Context context, | ||
static <T extends Context> Observation createNotStarted(String name, Supplier<T> contextSupplier, | ||
@Nullable ObservationRegistry registry) { | ||
if (registry == null || registry.isNoop() | ||
|| !registry.observationConfig().isObservationEnabled(name, context)) { | ||
if (registry == null || registry.isNoop()) { | ||
return NOOP; | ||
} | ||
Context context = contextSupplier.get(); | ||
if (!registry.observationConfig().isObservationEnabled(name, context)) { | ||
return NOOP; | ||
} | ||
return new SimpleObservation(name, registry, context == null ? new Context() : context); | ||
|
@@ -120,21 +124,24 @@ static Observation createNotStarted(String name, @Nullable Context context, | |
* picked. | ||
* @param defaultConvention default convention when no custom convention was passed, | ||
* nor a configured one was found | ||
* @param context the observation context | ||
* @param contextSupplier supplier for the observation context | ||
* @param registry observation registry | ||
* @return created but not started observation | ||
*/ | ||
static <T extends Context> Observation createNotStarted(@Nullable ObservationConvention<T> customConvention, | ||
@NonNull ObservationConvention<T> defaultConvention, @NonNull T context, | ||
@NonNull ObservationRegistry registry) { | ||
ObservationConvention<T> defaultConvention, Supplier<T> contextSupplier, ObservationRegistry registry) { | ||
if (registry.isNoop()) { | ||
return Observation.NOOP; | ||
} | ||
ObservationConvention<T> convention; | ||
if (customConvention != null) { | ||
convention = customConvention; | ||
} | ||
else { | ||
convention = registry.observationConfig().getObservationConvention(context, defaultConvention); | ||
convention = registry.observationConfig().getObservationConvention(contextSupplier.get(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will result in the supplier being called twice, which feels less than ideal. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we review the noop checks for the whole class, probably refactor it into a method (or two) and do the registry based check early? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One issue is interfaces can't have private methods. But regardless of that, it's a little hard to centralize this much more than it is, I think, due to the variety of factory methods and the different logic they have. Suggestions welcome, though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if we create a dedicated factory/builder class instead of default/static factory methods? The class would centralize noop checks for all variants of observation creations if possible. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at this more, we have two constructors for SimpleObservation, so I think we're going to have at least two places in factory methods we need to check for a no-op registry. It turns out we end up with 3 now because there is the option to make an Observation with a name, with a convention, or to have the convention looked up. The convention look-up requires the context, so we need to get the context already at that point. I've refactored this method to not call one of the other methods and instead instantiate the SimpleObservation itself, which avoids supplying the context twice. It leads to a little code duplication, but the look-up of the convention in the registry is a notable difference that happens in the middle of the logic. On a dedicated factory/builder class, that's an option. I worry it's a bigger change and we're basically out of time to iterate on and get feedback on such things. I don't know that it would change a lot. The no-op check that can be centralized I think is just calling A tangential thing is, we're not consistent on whether we accept a |
||
defaultConvention); | ||
} | ||
return Observation.createNotStarted(convention, context, registry); | ||
return Observation.createNotStarted(convention, contextSupplier, registry); | ||
} | ||
|
||
/** | ||
|
@@ -146,21 +153,21 @@ static <T extends Context> Observation createNotStarted(@Nullable ObservationCon | |
*/ | ||
static Observation start(ObservationConvention<? extends Context> observationConvention, | ||
@Nullable ObservationRegistry registry) { | ||
return start(observationConvention, null, registry); | ||
return start(observationConvention, () -> null, registry); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I couldn't get rid of this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll update the parameter type to |
||
} | ||
|
||
/** | ||
* Creates and starts an {@link Observation}. When no registry is passed or | ||
* observation is not applicable will return a no-op observation. | ||
* @param <T> type of context | ||
* @param observationConvention observation convention | ||
* @param context mutable context | ||
* @param contextSupplier mutable context | ||
* @param registry observation registry | ||
* @return started observation | ||
*/ | ||
static <T extends Context> Observation start(ObservationConvention<T> observationConvention, @Nullable T context, | ||
@Nullable ObservationRegistry registry) { | ||
return createNotStarted(observationConvention, context, registry).start(); | ||
static <T extends Context> Observation start(ObservationConvention<T> observationConvention, | ||
Supplier<T> contextSupplier, @Nullable ObservationRegistry registry) { | ||
return createNotStarted(observationConvention, contextSupplier, registry).start(); | ||
} | ||
|
||
/** | ||
|
@@ -172,17 +179,16 @@ static <T extends Context> Observation start(ObservationConvention<T> observatio | |
* was found. | ||
* @param <T> type of context | ||
* @param registry observation registry | ||
* @param context the observation context | ||
* @param contextSupplier the observation context | ||
* @param customConvention custom convention. If {@code null}, the default one will be | ||
* picked. | ||
* @param defaultConvention default convention when no custom convention was passed, | ||
* nor a configured one was found | ||
* @return started observation | ||
*/ | ||
static <T extends Context> Observation start(@Nullable ObservationConvention<T> customConvention, | ||
@NonNull ObservationConvention<T> defaultConvention, @NonNull T context, | ||
@NonNull ObservationRegistry registry) { | ||
return createNotStarted(customConvention, defaultConvention, context, registry).start(); | ||
ObservationConvention<T> defaultConvention, Supplier<T> contextSupplier, ObservationRegistry registry) { | ||
return createNotStarted(customConvention, defaultConvention, contextSupplier, registry).start(); | ||
} | ||
|
||
/** | ||
|
@@ -196,7 +202,7 @@ static <T extends Context> Observation start(@Nullable ObservationConvention<T> | |
*/ | ||
static Observation createNotStarted(ObservationConvention<? extends Context> observationConvention, | ||
@Nullable ObservationRegistry registry) { | ||
return createNotStarted(observationConvention, null, registry); | ||
return createNotStarted(observationConvention, () -> null, registry); | ||
} | ||
|
||
/** | ||
|
@@ -215,18 +221,19 @@ static Observation createNotStarted(ObservationConvention<? extends Context> obs | |
* </p> | ||
* @param <T> type of context | ||
* @param observationConvention observation convention | ||
* @param context mutable context | ||
* @param contextSupplier mutable context | ||
* @param registry observation registry | ||
* @return created but not started observation | ||
*/ | ||
static <T extends Context> Observation createNotStarted(ObservationConvention<T> observationConvention, | ||
@Nullable T context, @Nullable ObservationRegistry registry) { | ||
if (registry == null || registry.isNoop() | ||
|| !registry.observationConfig().isObservationEnabled(observationConvention.getName(), context) | ||
|| observationConvention == NoopObservationConvention.INSTANCE) { | ||
Supplier<T> contextSupplier, @Nullable ObservationRegistry registry) { | ||
if (registry == null || registry.isNoop() || observationConvention == NoopObservationConvention.INSTANCE) { | ||
return NOOP; | ||
} | ||
T context = contextSupplier.get(); | ||
if (!registry.observationConfig().isObservationEnabled(observationConvention.getName(), context)) { | ||
return NOOP; | ||
} | ||
|
||
return new SimpleObservation(observationConvention, registry, context == null ? new Context() : context); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Demonstrates one way to deal with this breaking change for users. Then they can pass the, in this case, OkHttpContext instead of using a lambda.