From 8d41e42bd768c67f728dc641ee0b158aa103e914 Mon Sep 17 00:00:00 2001 From: brharrington Date: Wed, 10 Apr 2019 10:10:15 -0700 Subject: [PATCH] warn if SpectatorContext registry is overwritten (#458) Adds a warning level log message if the registry used with the SpectatorContext is overwritten. This should help with debugging cases where Servo metrics are not being reported properly because of initialization bugs in the application. --- .../com/netflix/servo/SpectatorContext.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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 9e84aa5e..fc75053d 100644 --- a/servo-core/src/main/java/com/netflix/servo/SpectatorContext.java +++ b/servo-core/src/main/java/com/netflix/servo/SpectatorContext.java @@ -69,11 +69,29 @@ private SpectatorContext() { private static volatile Registry registry = new NoopRegistry(); + // Used to keep track of where the registry was being set. This can be useful to help + // debug issues if missing metrics due to it being set from multiple places. + private static volatile Exception initStacktrace = null; + /** * Set the registry to use. By default it will use the NoopRegistry. */ public static void setRegistry(Registry registry) { SpectatorContext.registry = registry; + if (registry instanceof NoopRegistry) { + initStacktrace = null; + } else { + Exception cause = initStacktrace; + Exception e = new IllegalStateException( + "called SpectatorContext.setRegistry(" + registry.getClass().getName() + ")", + cause); + e.fillInStackTrace(); + initStacktrace = e; + if (cause != null) { + LOGGER.warn("Registry used with Servo's SpectatorContext has been overwritten. This could " + + "result in missing metrics.", e); + } + } } /**