/** Registers all the sources configured in the metrics config. */ private void registerSources() { Properties instConfig = mMetricsConfig.getInstanceProperties(mInstance); Map<String, Properties> sourceConfigs = mMetricsConfig.subProperties(instConfig, SOURCE_REGEX); for (Map.Entry<String, Properties> entry : sourceConfigs.entrySet()) { String classPath = entry.getValue().getProperty("class"); if (classPath != null) { try { Source source = (Source) Class.forName(classPath).newInstance(); registerSource(source); } catch (Exception e) { LOG.error("Source class {} cannot be instantiated", classPath, e); } } } }
@Test public void shouldGetMetricsConfig() { when(settingsFacade.getProperty(METRICS_ENABLED)).thenReturn("true"); when(settingsFacade.getProperty(CONSOLE_REPORTER_ENABLED)).thenReturn("true"); when(settingsFacade.getProperty(CONSOLE_REPORTER_REPORTING_FREQUENCY_VALUE)).thenReturn("1"); when(settingsFacade.getProperty(CONSOLE_REPORTER_REPORTING_FREQUENCY_UNIT)) .thenReturn("SECONDS"); when(settingsFacade.getProperty(CONSOLE_REPORTER_CONVERT_RATES_UNIT)) .thenReturn("MILLISECONDS"); when(settingsFacade.getProperty(CONSOLE_REPORTER_CONVERT_DURATIONS_UNIT)).thenReturn("SECONDS"); when(settingsFacade.getProperty(GRAPHITE_REPORTER_ENABLED)).thenReturn("true"); when(settingsFacade.getProperty(GRAPHITE_REPORTER_GRAPHITE_SERVER_URI)) .thenReturn("http://foo.com/graphite"); when(settingsFacade.getProperty(GRAPHITE_REPORTER_GRAPHITE_SERVER_PORT)).thenReturn("2003"); when(settingsFacade.getProperty(GRAPHITE_REPORTER_REPORTING_FREQUENCY_VALUE)).thenReturn("1"); when(settingsFacade.getProperty(GRAPHITE_REPORTER_REPORTING_FREQUENCY_UNIT)) .thenReturn("SECONDS"); when(settingsFacade.getProperty(GRAPHITE_REPORTER_CONVERT_RATES_UNIT)) .thenReturn("MILLISECONDS"); when(settingsFacade.getProperty(GRAPHITE_REPORTER_CONVERT_DURATIONS_UNIT)) .thenReturn("SECONDS"); MetricsConfig config = metricsConfigFacade.getMetricsConfig(); assertNotNull(config); assertEquals(config.isMetricsEnabled(), true); ConsoleReporterConfig crConfig = config.getConsoleReporterConfig(); assertNotNull(crConfig); assertEquals(crConfig.isEnabled(), true); assertEquals(crConfig.getFrequency(), 1); assertEquals(crConfig.getFrequencyUnit(), TimeUnit.SECONDS); assertEquals(crConfig.getConvertRates(), TimeUnit.MILLISECONDS); assertEquals(crConfig.getConvertDurations(), TimeUnit.SECONDS); GraphiteReporterConfig grConfig = config.getGraphiteReporterConfig(); assertNotNull(grConfig); assertEquals(grConfig.isEnabled(), true); assertEquals(grConfig.getServerUri(), "http://foo.com/graphite"); assertEquals(grConfig.getServerPort(), 2003); assertEquals(grConfig.getFrequency(), 1); assertEquals(grConfig.getFrequencyUnit(), TimeUnit.SECONDS); assertEquals(grConfig.getConvertRates(), TimeUnit.MILLISECONDS); assertEquals(grConfig.getConvertDurations(), TimeUnit.SECONDS); }
/** Registers all the sinks configured in the metrics config. */ private void registerSinks() { Properties instConfig = mMetricsConfig.getInstanceProperties(mInstance); Map<String, Properties> sinkConfigs = mMetricsConfig.subProperties(instConfig, SINK_REGEX); for (Map.Entry<String, Properties> entry : sinkConfigs.entrySet()) { String classPath = entry.getValue().getProperty("class"); if (classPath != null) { try { Sink sink = (Sink) Class.forName(classPath) .getConstructor(Properties.class, MetricRegistry.class) .newInstance(entry.getValue(), mMetricRegistry); if (entry.getKey().equals("servlet")) { mMetricsServlet = (MetricsServlet) sink; } else { mSinks.add(sink); } } catch (Exception e) { LOG.error("Sink class {} cannot be instantiated", classPath, e); } } } }
/** * Starts sinks from a given metrics configuration. This is made public for unit test. * * @param config the metrics config */ public static synchronized void startSinksFromConfig(MetricsConfig config) { if (sSinks != null) { LOG.warn("Sinks have already been started."); return; } sSinks = new ArrayList<>(); Map<String, Properties> sinkConfigs = MetricsConfig.subProperties(config.getProperties(), SINK_REGEX); for (Map.Entry<String, Properties> entry : sinkConfigs.entrySet()) { String classPath = entry.getValue().getProperty("class"); if (classPath != null) { try { Sink sink = (Sink) Class.forName(classPath) .getConstructor(Properties.class, MetricRegistry.class) .newInstance(entry.getValue(), METRIC_REGISTRY); sSinks.add(sink); } catch (Exception e) { LOG.error("Sink class {} cannot be instantiated", classPath, e); } } } }
MetricsSourceAdapter( String prefix, String name, String description, MetricsSource source, Iterable<MetricsTag> injectedTags, int period, MetricsConfig conf) { this( prefix, name, description, source, injectedTags, conf.getFilter(RECORD_FILTER_KEY), conf.getFilter(METRIC_FILTER_KEY), period); }