Skip to content

Commit

Permalink
warn if SpectatorContext registry is overwritten (#458)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
brharrington authored and dmuino committed Apr 10, 2019
1 parent 07dcf57 commit 8d41e42
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions servo-core/src/main/java/com/netflix/servo/SpectatorContext.java
Expand Up @@ -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);
}
}
}

/**
Expand Down

0 comments on commit 8d41e42

Please sign in to comment.