@Bean @ConditionalOnProperty(value = "aws.metrics.enabled", matchIfMissing = true) SpectatorMetricCollector spectatorMetricsCollector(Registry registry) { SpectatorMetricCollector collector = new SpectatorMetricCollector(registry); AwsSdkMetrics.setMetricCollector(collector); return collector; }
@Test public void isMetricEnabled() { // originally disabled assertFalse(AwsSdkMetrics.isMetricsEnabled()); // set to NONE, so still disabled AwsSdkMetrics.setMetricCollector(MetricCollector.NONE); assertFalse(AwsSdkMetrics.isMetricsEnabled()); // set to a custom collector, so now considered enabled AwsSdkMetrics.setMetricCollector( new MetricCollector() { @Override public boolean start() { return true; } @Override public boolean stop() { return false; } @Override public boolean isEnabled() { return true; } @Override public RequestMetricCollector getRequestMetricCollector() { return RequestMetricCollector.NONE; } @Override public ServiceMetricCollector getServiceMetricCollector() { return ServiceMetricCollector.NONE; } }); assertTrue(AwsSdkMetrics.isMetricsEnabled()); }
/** * Starts the default AWS SDK request metric collector, but only if no request metric collector is * currently in use at the AWS SDK level. * * @return true if the default AWS SDK request metric collector has been successfully started by * this call; false otherwise. */ public static synchronized boolean enableDefaultMetrics() { if (mc == null || !mc.isEnabled()) { if (dirtyEnabling) { throw new IllegalStateException("Reentrancy is not allowed"); } dirtyEnabling = true; try { Class<?> c = Class.forName(DEFAULT_METRIC_COLLECTOR_FACTORY); MetricCollector.Factory f = (MetricCollector.Factory) c.newInstance(); MetricCollector instance = f.getInstance(); if (instance != null) { setMetricCollector(instance); return true; } } catch (Exception e) { LogFactory.getLog(AwsSdkMetrics.class).warn("Failed to enable the default metrics", e); } finally { dirtyEnabling = false; } } return false; }
/** Convenient method to disable the request metric collector at the AWS SDK level. */ public static void disableMetrics() { setMetricCollector(MetricCollector.NONE); }