Skip to content

Commit 3252cd4

Browse files
authored
do not warn if global registry used for context (Netflix#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.
1 parent d67b5af commit 3252cd4

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

servo-core/src/main/java/com/netflix/servo/SpectatorContext.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.netflix.spectator.api.Meter;
3232
import com.netflix.spectator.api.NoopRegistry;
3333
import com.netflix.spectator.api.Registry;
34+
import com.netflix.spectator.api.Spectator;
3435
import com.netflix.spectator.api.Timer;
3536
import com.netflix.spectator.api.patterns.PolledMeter;
3637
import org.slf4j.Logger;
@@ -78,7 +79,11 @@ private SpectatorContext() {
7879
*/
7980
public static void setRegistry(Registry registry) {
8081
SpectatorContext.registry = registry;
81-
if (registry instanceof NoopRegistry) {
82+
// Ignore if overwriting with the global registry. In some cases it is necessary to set
83+
// the context for Servo early before a proper registry can be created via injection. In
84+
// that case the global registry is the best option. If it is later overwritten with the
85+
// registry created by the injector, then that should not trigger a warning to the user.
86+
if (registry instanceof NoopRegistry || isGlobal(registry)) {
8287
initStacktrace = null;
8388
} else {
8489
Exception cause = initStacktrace;
@@ -94,6 +99,11 @@ public static void setRegistry(Registry registry) {
9499
}
95100
}
96101

102+
private static boolean isGlobal(Registry registry) {
103+
// Use identity check to see it is the global instance
104+
return registry == Spectator.globalRegistry();
105+
}
106+
97107
/**
98108
* Get the registry that was configured.
99109
*/

0 commit comments

Comments
 (0)