in spectator-agent/src/main/java/com/netflix/spectator/agent/Agent.java [113:176]
public static void premain(String arg, Instrumentation instrumentation) throws Exception {
// Setup logging
final List<Object> resources = parseResourceList(arg);
Config config = loadConfig(resources);
LOGGER.debug("loaded configuration: {}", config.root().render());
createDependencyProperties(config);
// Setup Registry
AtlasRegistry registry = new AtlasRegistry(Clock.SYSTEM, new AgentAtlasConfig(config));
// Add to global registry for http stats and GC logger
Spectator.globalRegistry().add(registry);
// Enable GC logger
GcLogger gcLogger = new GcLogger();
if (config.getBoolean("collection.gc")) {
gcLogger.start(null);
}
// Enable JVM data collection
if (config.getBoolean("collection.jvm")) {
Jmx.registerStandardMXBeans(registry);
}
// Enable JMX query collection
if (config.getBoolean("collection.jmx")) {
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor(r -> {
Thread t = new Thread(r, "spectator-agent-jmx");
t.setDaemon(true);
return t;
});
// Poll the JMX data at least once per step interval unless it is less than a second
// to avoid to much overhead
Duration step = Duration.parse(config.getString("atlas.step"));
long delay = Math.max(1000L, step.toMillis() / 2L);
// Keep track of last time the configs have been loaded
final AtomicLong lastUpdated = new AtomicLong(System.currentTimeMillis());
final JmxPoller poller = new JmxPoller(registry);
poller.updateConfigs(config.getConfigList("jmx.mappings"));
exec.scheduleWithFixedDelay(() -> {
try {
List<File> updatedConfigs = findUpdatedConfigs(resources, lastUpdated.get());
if (!updatedConfigs.isEmpty()) {
LOGGER.info("detected updated config files: {}", updatedConfigs);
lastUpdated.set(System.currentTimeMillis());
Config cfg = loadConfig(resources);
poller.updateConfigs(cfg.getConfigList("jmx.mappings"));
}
} catch (Exception e) {
LOGGER.warn("failed to update jmx config mappings, using previous config", e);
}
poller.poll();
}, delay, delay, TimeUnit.MILLISECONDS);
}
// Start collection for the registry
registry.start();
// Shutdown registry
Runtime.getRuntime().addShutdownHook(new Thread(registry::stop, "spectator-agent-shutdown"));
}