@Override public void apply(MetricRegistryInstance registry) throws Exception { registry.add(builder.build(registry::addEndpoint)); }
protected MetricRegistryInstance(@NonNull Supplier<DateTime> now, boolean has_config, @NonNull EndpointRegistration api) { api_ = api; has_config_ = has_config; decorators_.add(new MonitorMonitor(this)); now_ = requireNonNull(now); list_metrics_ = new ListMetrics(); addEndpoint("/monsoon/metrics", list_metrics_); }
@Override public final void addEndpoint(String pattern, HttpServlet handler) { getApi().addEndpoint(pattern, handler); }
final long failed_collections = registry_.getFailedCollections(); final boolean has_config = registry_.hasConfig(); final Optional<Duration> scrape_duration = registry_.getScrapeDuration(); final Optional<Duration> rule_eval_duration = registry_.getRuleEvalDuration(); final Optional<Duration> processor_duration = registry_.getProcessorDuration();
final DateTime now = now(); final CollectionContext cctx = beginCollection(now); final MutableTimeSeriesCollectionPair tsdata = cctx.tsdata(); streamGroups(now).forEach(tsdata.getCurrentCollection()::add); apply_rules_and_decorators_(ctx); final long t_rule_eval = System.nanoTime(); rule_eval_duration_ = Optional.of(Duration.millis(TimeUnit.NANOSECONDS.toMillis(t_rule_eval - t0)));
private synchronized Stream<MutableTimeSeriesValue> streamGroups(DateTime now) { final long t0 = System.nanoTime(); final CompletableFuture<GroupGenerator.TimeoutObject> timeout = new CompletableFuture<>(); Collection<MetricGroup> collections = derefFutures(generators_.stream() .map(generator -> { try { return generator.getGroups(THREADPOOL, timeout); } catch (Exception ex) { CompletableFuture<? extends Collection<? extends MetricGroup>> failure = new CompletableFuture<>(); failure.completeExceptionally(ex); return singleton(failure); } }) .flatMap(Collection::stream) .collect(Collectors.toList()), t0, timeout); /* Measure end time of collections. */ final long t_collections = System.nanoTime(); scrape_duration_ = Optional.of(Duration.millis(TimeUnit.NANOSECONDS.toMillis(t_collections - t0))); Stream<MutableTimeSeriesValue> groups = collections.stream() .collect(Collectors.toMap(MetricGroup::getName, Function.identity(), (x, y) -> y)) // Resolve group-name conflict, such that latest metric wins. .values() .stream() .map((mg) -> new MutableTimeSeriesValue(mg.getName(), Arrays.stream(mg.getMetrics()), Metric::getName, Metric::getValue)); return groups; }
/** * 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; }