private static List<InstrumentedPlugin> instrument(Iterable<NamedPlugin> namedPlugins, Environment environment) { return stream(namedPlugins.spliterator(), false) .map(namedPlugin -> new InstrumentedPlugin(namedPlugin, environment)) .collect(toList()); } }
private Throwable recordAndWrapError(StatusRecordingChain chain, Throwable error) { if (chain.upstreamException) { return error; } recordException(error); return new PluginException(error, plugin.name()); }
@Override public Eventual<LiveHttpResponse> intercept(LiveHttpRequest request, Chain originalChain) { StatusRecordingChain chain = new StatusRecordingChain(originalChain); try { return new Eventual<>(toPublisher( toObservable(plugin.intercept(request, chain)) .doOnNext(response -> recordStatusCode(chain, response)) .onErrorResumeNext(error -> error(recordAndWrapError(chain, error))))); } catch (Throwable e) { recordException(e); return Eventual.error(new PluginException(e, plugin.name())); } }
public InstrumentedPlugin(NamedPlugin plugin, Environment environment) { this.plugin = requireNonNull(plugin); requireNonNull(environment); this.errorStatusMetrics = new SimpleCache<>(statusCode -> environment.metricRegistry().meter("plugins." + plugin.name() + ".response.status." + statusCode.code())); this.exceptionMetrics = new SimpleCache<>(type -> environment.metricRegistry().meter("plugins." + plugin.name() + ".exception." + formattedExceptionName(type))); this.errors = environment.metricRegistry().meter("plugins." + plugin.name() + ".errors"); LOGGER.info("Plugin {} instrumented", plugin.name()); }