/** * Creates a new MetricRegistryInstance. * @param now A function returning DateTime.now(DateTimeZone.UTC). Allowing specifying it, for the benefit of unit tests. * @param api The api with which to register configuration-specific endpoints. * @return A metric registry instance, initialized based on this configuration. * @throws RuntimeException if anything goes wrong during registration, for example the name is already in use. */ public synchronized <T extends MetricRegistryInstance> T create(MetricRegistryConstructor<T> constructor, Supplier<DateTime> now, EndpointRegistration api) { if (needsResolve()) throw new IllegalStateException("Configuration.create invoked on unresolved configuration"); T spawn = constructor.construct(now, has_config_, api); Logger.getLogger(MetricRegistryInstance.class.getName()).log(Level.INFO, "Using configuration:\n{0}", this); try { getMonitors().forEach((MonitorStatement mon) -> { try { mon.apply(spawn); } catch (Exception ex) { Logger.getLogger(MetricRegistryInstance.class.getName()).log(Level.SEVERE, "unable to apply configuration " + mon, ex); throw new RuntimeException("failed to apply configuration " + mon, ex); } }); getRules().stream().map(RuleStatement::get).forEach(spawn::decorate); } catch (RuntimeException ex) { spawn.close(); throw ex; } return spawn; }