From 3252cd452a81a6dd4ff44d680d5eff055e38026f Mon Sep 17 00:00:00 2001 From: brharrington Date: Tue, 4 Jun 2019 09:22:20 -0700 Subject: [PATCH] do not warn if global registry used for context (#463) 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. --- .../java/com/netflix/servo/SpectatorContext.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/servo-core/src/main/java/com/netflix/servo/SpectatorContext.java b/servo-core/src/main/java/com/netflix/servo/SpectatorContext.java index fc75053d..bc6e5ef0 100644 --- a/servo-core/src/main/java/com/netflix/servo/SpectatorContext.java +++ b/servo-core/src/main/java/com/netflix/servo/SpectatorContext.java @@ -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; @@ -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; @@ -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. */