Skip to content

Commit

Permalink
do not warn if global registry used for context (#463)
Browse files Browse the repository at this point in the history
If the Spectator global registry is used for the SpectatorContext,
then do not log a warning. In some legacy apps the context must
be set early to the global registry to avoid missing some metrics.
It can then be overwritten by the primary registry after it has
been created later in the startup process.
  • Loading branch information
brharrington authored Jun 4, 2019
1 parent d67b5af commit 3252cd4
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion servo-core/src/main/java/com/netflix/servo/SpectatorContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.netflix.spectator.api.Meter;
import com.netflix.spectator.api.NoopRegistry;
import com.netflix.spectator.api.Registry;
import com.netflix.spectator.api.Spectator;
import com.netflix.spectator.api.Timer;
import com.netflix.spectator.api.patterns.PolledMeter;
import org.slf4j.Logger;
Expand Down Expand Up @@ -78,7 +79,11 @@ private SpectatorContext() {
*/
public static void setRegistry(Registry registry) {
SpectatorContext.registry = registry;
if (registry instanceof NoopRegistry) {
// Ignore if overwriting with the global registry. In some cases it is necessary to set
// the context for Servo early before a proper registry can be created via injection. In
// that case the global registry is the best option. If it is later overwritten with the
// registry created by the injector, then that should not trigger a warning to the user.
if (registry instanceof NoopRegistry || isGlobal(registry)) {
initStacktrace = null;
} else {
Exception cause = initStacktrace;
Expand All @@ -94,6 +99,11 @@ public static void setRegistry(Registry registry) {
}
}

private static boolean isGlobal(Registry registry) {
// Use identity check to see it is the global instance
return registry == Spectator.globalRegistry();
}

/**
* Get the registry that was configured.
*/
Expand Down

0 comments on commit 3252cd4

Please sign in to comment.