org.apache.flink.metrics.reporter.MetricReporter Java Examples

The following examples show how to use org.apache.flink.metrics.reporter.MetricReporter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ReporterSetup.java    From flink with Apache License 2.0 6 votes vote down vote up
private static List<ReporterSetup> setupReporters(Map<String, MetricReporterFactory> reporterFactories, List<Tuple2<String, Configuration>> reporterConfigurations) {
	List<ReporterSetup> reporterSetups = new ArrayList<>(reporterConfigurations.size());
	for (Tuple2<String, Configuration> reporterConfiguration: reporterConfigurations) {
		String reporterName = reporterConfiguration.f0;
		Configuration reporterConfig = reporterConfiguration.f1;

		try {
			Optional<MetricReporter> metricReporterOptional = loadReporter(reporterName, reporterConfig, reporterFactories);
			metricReporterOptional.ifPresent(reporter -> {
				MetricConfig metricConfig = new MetricConfig();
				reporterConfig.addAllToProperties(metricConfig);
				reporterSetups.add(createReporterSetup(reporterName, metricConfig, reporter));
			});
		}
		catch (Throwable t) {
			LOG.error("Could not instantiate metrics reporter {}. Metrics might not be exposed/reported.", reporterName, t);
		}
	}
	return reporterSetups;
}
 
Example #2
Source File: AbstractMetricGroupTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testLogicalScopeCachingForMultipleReporters() throws Exception {
	MetricRegistryImpl testRegistry = new MetricRegistryImpl(
		MetricRegistryConfiguration.defaultMetricRegistryConfiguration(),
		Arrays.asList(
			ReporterSetup.forReporter("test1", new LogicalScopeReporter1()),
			ReporterSetup.forReporter("test2", new LogicalScopeReporter2())));
	try {
		MetricGroup tmGroup = new TaskManagerMetricGroup(testRegistry, "host", "id")
			.addGroup("B")
			.addGroup("C");
		tmGroup.counter("1");
		assertEquals("Reporters were not properly instantiated", 2, testRegistry.getReporters().size());
		for (MetricReporter reporter : testRegistry.getReporters()) {
			ScopeCheckingTestReporter typedReporter = (ScopeCheckingTestReporter) reporter;
			if (typedReporter.failureCause != null) {
				throw typedReporter.failureCause;
			}
		}
	} finally {
		testRegistry.shutdown().get();
	}
}
 
Example #3
Source File: AbstractMetricGroupTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testScopeCachingForMultipleReporters() throws Exception {
	Configuration config = new Configuration();
	config.setString(MetricOptions.SCOPE_NAMING_TM, "A.B.C.D");
	config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test1." + ConfigConstants.METRICS_REPORTER_CLASS_SUFFIX, TestReporter1.class.getName());
	config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test1." + ConfigConstants.METRICS_REPORTER_SCOPE_DELIMITER, "-");
	config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test2." + ConfigConstants.METRICS_REPORTER_CLASS_SUFFIX, TestReporter2.class.getName());
	config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test2." + ConfigConstants.METRICS_REPORTER_SCOPE_DELIMITER, "!");

	MetricRegistryImpl testRegistry = new MetricRegistryImpl(MetricRegistryConfiguration.fromConfiguration(config));
	try {
		MetricGroup tmGroup = new TaskManagerMetricGroup(testRegistry, "host", "id");
		tmGroup.counter("1");
		assertEquals("Reporters were not properly instantiated", 2, testRegistry.getReporters().size());
		for (MetricReporter reporter : testRegistry.getReporters()) {
			ScopeCheckingTestReporter typedReporter = (ScopeCheckingTestReporter) reporter;
			if (typedReporter.failureCause != null) {
				throw typedReporter.failureCause;
			}
		}
	} finally {
		testRegistry.shutdown().get();
	}
}
 
Example #4
Source File: AbstractMetricGroupTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testLogicalScopeCachingForMultipleReporters() throws Exception {
	Configuration config = new Configuration();
	config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test1." + ConfigConstants.METRICS_REPORTER_CLASS_SUFFIX, LogicalScopeReporter1.class.getName());
	config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test2." + ConfigConstants.METRICS_REPORTER_CLASS_SUFFIX, LogicalScopeReporter2.class.getName());

	MetricRegistryImpl testRegistry = new MetricRegistryImpl(MetricRegistryConfiguration.fromConfiguration(config));
	try {
		MetricGroup tmGroup = new TaskManagerMetricGroup(testRegistry, "host", "id")
			.addGroup("B")
			.addGroup("C");
		tmGroup.counter("1");
		assertEquals("Reporters were not properly instantiated", 2, testRegistry.getReporters().size());
		for (MetricReporter reporter : testRegistry.getReporters()) {
			ScopeCheckingTestReporter typedReporter = (ScopeCheckingTestReporter) reporter;
			if (typedReporter.failureCause != null) {
				throw typedReporter.failureCause;
			}
		}
	} finally {
		testRegistry.shutdown().get();
	}
}
 
Example #5
Source File: MetricRegistryImplTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testConfigurableDelimiterForReportersInGroup() throws Exception {
	Configuration config = new Configuration();
	config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test1." + ConfigConstants.METRICS_REPORTER_SCOPE_DELIMITER, "_");
	config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test1." + ConfigConstants.METRICS_REPORTER_CLASS_SUFFIX, TestReporter8.class.getName());
	config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test2." + ConfigConstants.METRICS_REPORTER_SCOPE_DELIMITER, "-");
	config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test2." + ConfigConstants.METRICS_REPORTER_CLASS_SUFFIX, TestReporter8.class.getName());
	config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test3." + ConfigConstants.METRICS_REPORTER_SCOPE_DELIMITER, "AA");
	config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test3." + ConfigConstants.METRICS_REPORTER_CLASS_SUFFIX, TestReporter8.class.getName());
	config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test4." + ConfigConstants.METRICS_REPORTER_CLASS_SUFFIX, TestReporter8.class.getName());
	config.setString(MetricOptions.SCOPE_NAMING_TM, "A.B");

	MetricRegistryImpl registry = new MetricRegistryImpl(MetricRegistryConfiguration.fromConfiguration(config));
	List<MetricReporter> reporters = registry.getReporters();
	((TestReporter8) reporters.get(0)).expectedDelimiter = '_'; //test1  reporter
	((TestReporter8) reporters.get(1)).expectedDelimiter = '-'; //test2 reporter
	((TestReporter8) reporters.get(2)).expectedDelimiter = GLOBAL_DEFAULT_DELIMITER; //test3 reporter, because 'AA' - not correct delimiter
	((TestReporter8) reporters.get(3)).expectedDelimiter = GLOBAL_DEFAULT_DELIMITER; //for test4 reporter use global delimiter

	TaskManagerMetricGroup group = new TaskManagerMetricGroup(registry, "host", "id");
	group.counter("C");
	group.close();
	registry.shutdown().get();
	assertEquals(4, TestReporter8.numCorrectDelimitersForRegister);
	assertEquals(4, TestReporter8.numCorrectDelimitersForUnregister);
}
 
Example #6
Source File: AbstractMetricGroupTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testLogicalScopeCachingForMultipleReporters() throws Exception {
	MetricRegistryImpl testRegistry = new MetricRegistryImpl(
		MetricRegistryConfiguration.defaultMetricRegistryConfiguration(),
		Arrays.asList(
			ReporterSetup.forReporter("test1", new LogicalScopeReporter1()),
			ReporterSetup.forReporter("test2", new LogicalScopeReporter2())));
	try {
		MetricGroup tmGroup = new TaskManagerMetricGroup(testRegistry, "host", "id")
			.addGroup("B")
			.addGroup("C");
		tmGroup.counter("1");
		assertEquals("Reporters were not properly instantiated", 2, testRegistry.getReporters().size());
		for (MetricReporter reporter : testRegistry.getReporters()) {
			ScopeCheckingTestReporter typedReporter = (ScopeCheckingTestReporter) reporter;
			if (typedReporter.failureCause != null) {
				throw typedReporter.failureCause;
			}
		}
	} finally {
		testRegistry.shutdown().get();
	}
}
 
Example #7
Source File: ReporterSetup.java    From flink with Apache License 2.0 6 votes vote down vote up
private static Optional<MetricReporter> loadViaReflection(
		final String reporterClassName,
		final String reporterName,
		final Configuration reporterConfig,
		final Map<String, MetricReporterFactory> reporterFactories) throws ClassNotFoundException, IllegalAccessException, InstantiationException {

	final Class<?> reporterClass = Class.forName(reporterClassName);

	final InstantiateViaFactory alternativeFactoryAnnotation = reporterClass.getAnnotation(InstantiateViaFactory.class);
	if (alternativeFactoryAnnotation != null) {
		final String alternativeFactoryClassName = alternativeFactoryAnnotation.factoryClassName();
		LOG.info("The reporter configuration of {} is out-dated (but still supported)." +
				" Please configure a factory class instead: '{}{}.{}: {}' to ensure that the configuration" +
				" continues to work with future versions.",
			reporterName,
			ConfigConstants.METRICS_REPORTER_PREFIX,
			reporterName,
			ConfigConstants.METRICS_REPORTER_FACTORY_CLASS_SUFFIX,
			alternativeFactoryClassName);
		return loadViaFactory(alternativeFactoryClassName, reporterName, reporterConfig, reporterFactories);
	}

	return Optional.of((MetricReporter) reporterClass.newInstance());
}
 
Example #8
Source File: ReporterSetup.java    From flink with Apache License 2.0 6 votes vote down vote up
private static Optional<MetricReporter> loadViaReflection(
		final String reporterClassName,
		final String reporterName,
		final Configuration reporterConfig,
		final Map<String, MetricReporterFactory> reporterFactories) throws ClassNotFoundException, IllegalAccessException, InstantiationException {

	final Class<?> reporterClass = Class.forName(reporterClassName);

	final InstantiateViaFactory alternativeFactoryAnnotation = reporterClass.getAnnotation(InstantiateViaFactory.class);
	if (alternativeFactoryAnnotation != null) {
		final String alternativeFactoryClassName = alternativeFactoryAnnotation.factoryClassName();
		LOG.info("The reporter configuration of {} is out-dated (but still supported)." +
				" Please configure a factory class instead: '{}{}.{}: {}' to ensure that the configuration" +
				" continues to work with future versions.",
			reporterName,
			ConfigConstants.METRICS_REPORTER_PREFIX,
			reporterName,
			ConfigConstants.METRICS_REPORTER_FACTORY_CLASS_SUFFIX,
			alternativeFactoryClassName);
		return loadViaFactory(alternativeFactoryClassName, reporterName, reporterConfig, reporterFactories);
	}

	return Optional.of((MetricReporter) reporterClass.newInstance());
}
 
Example #9
Source File: ReporterSetup.java    From flink with Apache License 2.0 6 votes vote down vote up
private static Optional<MetricReporter> loadViaFactory(
		final String factoryClassName,
		final String reporterName,
		final Configuration reporterConfig,
		final Map<String, MetricReporterFactory> reporterFactories) {

	MetricReporterFactory factory = reporterFactories.get(factoryClassName);

	if (factory == null) {
		LOG.warn("The reporter factory ({}) could not be found for reporter {}. Available factories: ", factoryClassName, reporterName, reporterFactories.keySet());
		return Optional.empty();
	} else {
		final MetricConfig metricConfig = new MetricConfig();
		reporterConfig.addAllToProperties(metricConfig);

		return Optional.of(factory.createMetricReporter(metricConfig));
	}
}
 
Example #10
Source File: ReporterSetup.java    From flink with Apache License 2.0 6 votes vote down vote up
private static Optional<MetricReporter> loadReporter(
		final String reporterName,
		final Configuration reporterConfig,
		final Map<String, MetricReporterFactory> reporterFactories)
		throws ClassNotFoundException, IllegalAccessException, InstantiationException {

	final String reporterClassName = reporterConfig.getString(ConfigConstants.METRICS_REPORTER_CLASS_SUFFIX, null);
	final String factoryClassName = reporterConfig.getString(ConfigConstants.METRICS_REPORTER_FACTORY_CLASS_SUFFIX, null);

	if (factoryClassName != null) {
		return loadViaFactory(factoryClassName, reporterName, reporterConfig, reporterFactories);
	}

	if (reporterClassName != null) {
		return loadViaReflection(reporterClassName, reporterName, reporterConfig, reporterFactories);
	}

	LOG.warn("No reporter class nor factory set for reporter {}. Metrics might not be exposed/reported.", reporterName);
	return Optional.empty();
}
 
Example #11
Source File: ReporterSetupTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testReporterSetupSupplier() throws Exception {
	final Configuration config = new Configuration();

	config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "reporter1." + ConfigConstants.METRICS_REPORTER_CLASS_SUFFIX, TestReporter1.class.getName());

	final List<ReporterSetup> reporterSetups = ReporterSetup.fromConfiguration(config);

	Assert.assertEquals(1, reporterSetups.size());

	final ReporterSetup reporterSetup = reporterSetups.get(0);
	final MetricReporter metricReporter = reporterSetup.getReporter();
	Assert.assertThat(metricReporter, instanceOf(TestReporter1.class));
}
 
Example #12
Source File: AbstractMetricGroupTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testScopeCachingForMultipleReporters() throws Exception {
	Configuration config = new Configuration();
	config.setString(MetricOptions.SCOPE_NAMING_TM, "A.B.C.D");

	MetricConfig metricConfig1 = new MetricConfig();
	metricConfig1.setProperty(ConfigConstants.METRICS_REPORTER_SCOPE_DELIMITER, "-");

	MetricConfig metricConfig2 = new MetricConfig();
	metricConfig2.setProperty(ConfigConstants.METRICS_REPORTER_SCOPE_DELIMITER, "!");

	config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test1." + ConfigConstants.METRICS_REPORTER_CLASS_SUFFIX, TestReporter1.class.getName());
	config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test1." + ConfigConstants.METRICS_REPORTER_SCOPE_DELIMITER, "-");
	config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test2." + ConfigConstants.METRICS_REPORTER_CLASS_SUFFIX, TestReporter2.class.getName());
	config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test2." + ConfigConstants.METRICS_REPORTER_SCOPE_DELIMITER, "!");

	MetricRegistryImpl testRegistry = new MetricRegistryImpl(
		MetricRegistryConfiguration.fromConfiguration(config),
		Arrays.asList(
			ReporterSetup.forReporter("test1", metricConfig1, new TestReporter1()),
			ReporterSetup.forReporter("test2", metricConfig2, new TestReporter2())));
	try {
		MetricGroup tmGroup = new TaskManagerMetricGroup(testRegistry, "host", "id");
		tmGroup.counter("1");
		assertEquals("Reporters were not properly instantiated", 2, testRegistry.getReporters().size());
		for (MetricReporter reporter : testRegistry.getReporters()) {
			ScopeCheckingTestReporter typedReporter = (ScopeCheckingTestReporter) reporter;
			if (typedReporter.failureCause != null) {
				throw typedReporter.failureCause;
			}
		}
	} finally {
		testRegistry.shutdown().get();
	}
}
 
Example #13
Source File: ReporterSetupTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testReporterSetupSupplier() throws Exception {
	final Configuration config = new Configuration();

	config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "reporter1." + ConfigConstants.METRICS_REPORTER_CLASS_SUFFIX, TestReporter1.class.getName());

	final List<ReporterSetup> reporterSetups = ReporterSetup.fromConfiguration(config, null);

	Assert.assertEquals(1, reporterSetups.size());

	final ReporterSetup reporterSetup = reporterSetups.get(0);
	final MetricReporter metricReporter = reporterSetup.getReporter();
	Assert.assertThat(metricReporter, instanceOf(TestReporter1.class));
}
 
Example #14
Source File: AbstractMetricGroupTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testScopeCachingForMultipleReporters() throws Exception {
	Configuration config = new Configuration();
	config.setString(MetricOptions.SCOPE_NAMING_TM, "A.B.C.D");

	MetricConfig metricConfig1 = new MetricConfig();
	metricConfig1.setProperty(ConfigConstants.METRICS_REPORTER_SCOPE_DELIMITER, "-");

	MetricConfig metricConfig2 = new MetricConfig();
	metricConfig2.setProperty(ConfigConstants.METRICS_REPORTER_SCOPE_DELIMITER, "!");

	config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test1." + ConfigConstants.METRICS_REPORTER_CLASS_SUFFIX, TestReporter1.class.getName());
	config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test1." + ConfigConstants.METRICS_REPORTER_SCOPE_DELIMITER, "-");
	config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test2." + ConfigConstants.METRICS_REPORTER_CLASS_SUFFIX, TestReporter2.class.getName());
	config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test2." + ConfigConstants.METRICS_REPORTER_SCOPE_DELIMITER, "!");

	MetricRegistryImpl testRegistry = new MetricRegistryImpl(
		MetricRegistryConfiguration.fromConfiguration(config),
		Arrays.asList(
			ReporterSetup.forReporter("test1", metricConfig1, new TestReporter1()),
			ReporterSetup.forReporter("test2", metricConfig2, new TestReporter2())));
	try {
		MetricGroup tmGroup = new TaskManagerMetricGroup(testRegistry, "host", "id");
		tmGroup.counter("1");
		assertEquals("Reporters were not properly instantiated", 2, testRegistry.getReporters().size());
		for (MetricReporter reporter : testRegistry.getReporters()) {
			ScopeCheckingTestReporter typedReporter = (ScopeCheckingTestReporter) reporter;
			if (typedReporter.failureCause != null) {
				throw typedReporter.failureCause;
			}
		}
	} finally {
		testRegistry.shutdown().get();
	}
}
 
Example #15
Source File: InfluxdbReporterTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testReporterRegistration() throws Exception {
	MetricRegistryImpl metricRegistry = createMetricRegistry(InfluxdbReporterOptions.RETENTION_POLICY.defaultValue());
	try {
		assertEquals(1, metricRegistry.getReporters().size());
		MetricReporter reporter = metricRegistry.getReporters().get(0);
		assertTrue(reporter instanceof InfluxdbReporter);
	} finally {
		metricRegistry.shutdown().get();
	}
}
 
Example #16
Source File: InfluxdbReporterTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testReporterRegistration() throws Exception {
	MetricRegistryImpl metricRegistry = createMetricRegistry();
	try {
		assertEquals(1, metricRegistry.getReporters().size());
		MetricReporter reporter = metricRegistry.getReporters().get(0);
		assertTrue(reporter instanceof InfluxdbReporter);
	} finally {
		metricRegistry.shutdown().get();
	}
}
 
Example #17
Source File: ReporterSetup.java    From flink with Apache License 2.0 5 votes vote down vote up
private static Optional<MetricReporter> loadViaFactory(
	final Configuration reporterConfig,
	final MetricReporterFactory factory) {

	final MetricConfig metricConfig = new MetricConfig();
	reporterConfig.addAllToProperties(metricConfig);

	return Optional.of(factory.createMetricReporter(metricConfig));
}
 
Example #18
Source File: ReporterSetup.java    From flink with Apache License 2.0 5 votes vote down vote up
private static Optional<MetricReporter> loadViaFactory(
		final String factoryClassName,
		final String reporterName,
		final Configuration reporterConfig,
		final Map<String, MetricReporterFactory> reporterFactories) {

	MetricReporterFactory factory = reporterFactories.get(factoryClassName);

	if (factory == null) {
		LOG.warn("The reporter factory ({}) could not be found for reporter {}. Available factories: {}.", factoryClassName, reporterName, reporterFactories.keySet());
		return Optional.empty();
	} else {
		return loadViaFactory(reporterConfig, factory);
	}
}
 
Example #19
Source File: ReporterSetup.java    From flink with Apache License 2.0 5 votes vote down vote up
private static Optional<MetricReporter> loadReporter(
		final String reporterName,
		final Configuration reporterConfig,
		final Map<String, MetricReporterFactory> reporterFactories)
		throws ClassNotFoundException, IllegalAccessException, InstantiationException {

	final String reporterClassName = reporterConfig.getString(ConfigConstants.METRICS_REPORTER_CLASS_SUFFIX, null);
	final String factoryClassName = reporterConfig.getString(ConfigConstants.METRICS_REPORTER_FACTORY_CLASS_SUFFIX, null);

	if (factoryClassName != null) {
		return loadViaFactory(factoryClassName, reporterName, reporterConfig, reporterFactories);
	}

	if (reporterClassName != null) {
		final Optional<MetricReporterFactory> interceptingFactory = reporterFactories.values().stream()
			.filter(factory -> {
				InterceptInstantiationViaReflection annotation = factory.getClass().getAnnotation(InterceptInstantiationViaReflection.class);
				return annotation != null && annotation.reporterClassName().equals(reporterClassName);
			})
			.findAny();

		if (interceptingFactory.isPresent()) {
			return loadViaFactory(reporterConfig, interceptingFactory.get());
		}

		return loadViaReflection(reporterClassName, reporterName, reporterConfig, reporterFactories);
	}

	LOG.warn("No reporter class nor factory set for reporter {}. Metrics might not be exposed/reported.", reporterName);
	return Optional.empty();
}
 
Example #20
Source File: InfluxdbReporterTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testReporterRegistration() throws Exception {
	MetricRegistryImpl metricRegistry = createMetricRegistry(InfluxdbReporterOptions.RETENTION_POLICY.defaultValue(),
		InfluxdbReporterOptions.CONSISTENCY.defaultValue());
	try {
		assertEquals(1, metricRegistry.getReporters().size());
		MetricReporter reporter = metricRegistry.getReporters().get(0);
		assertTrue(reporter instanceof InfluxdbReporter);
	} finally {
		metricRegistry.shutdown().get();
	}
}
 
Example #21
Source File: JMXReporterTest.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Verifies that multiple JMXReporters can be started on the same machine and register metrics at the MBeanServer.
 *
 * @throws Exception if the attribute/mbean could not be found or the test is broken
 */
@Test
public void testPortConflictHandling() throws Exception {
	ReporterSetup reporterSetup1 = ReporterSetup.forReporter("test1", new JMXReporter("9020-9035"));
	ReporterSetup reporterSetup2 = ReporterSetup.forReporter("test2", new JMXReporter("9020-9035"));

	MetricRegistryImpl reg = new MetricRegistryImpl(
		MetricRegistryConfiguration.defaultMetricRegistryConfiguration(),
		Arrays.asList(reporterSetup1, reporterSetup2));

	TaskManagerMetricGroup mg = new TaskManagerMetricGroup(reg, "host", "tm");

	List<MetricReporter> reporters = reg.getReporters();

	assertTrue(reporters.size() == 2);

	MetricReporter rep1 = reporters.get(0);
	MetricReporter rep2 = reporters.get(1);

	Gauge<Integer> g1 = new Gauge<Integer>() {
		@Override
		public Integer getValue() {
			return 1;
		}
	};
	Gauge<Integer> g2 = new Gauge<Integer>() {
		@Override
		public Integer getValue() {
			return 2;
		}
	};

	rep1.notifyOfAddedMetric(g1, "rep1", new FrontMetricGroup<>(createReporterScopedSettings(0), mg));
	rep2.notifyOfAddedMetric(g2, "rep2", new FrontMetricGroup<>(createReporterScopedSettings(0), mg));

	MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();

	ObjectName objectName1 = new ObjectName(JMX_DOMAIN_PREFIX + "taskmanager.rep1", JMXReporter.generateJmxTable(mg.getAllVariables()));
	ObjectName objectName2 = new ObjectName(JMX_DOMAIN_PREFIX + "taskmanager.rep2", JMXReporter.generateJmxTable(mg.getAllVariables()));

	assertEquals(1, mBeanServer.getAttribute(objectName1, "Value"));
	assertEquals(2, mBeanServer.getAttribute(objectName2, "Value"));

	rep1.notifyOfRemovedMetric(g1, "rep1", null);
	rep1.notifyOfRemovedMetric(g2, "rep2", null);

	mg.close();
	reg.shutdown().get();
}
 
Example #22
Source File: DatadogHttpReporterFactory.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public MetricReporter createMetricReporter(Properties properties) {
	return new DatadogHttpReporter();
}
 
Example #23
Source File: JMXReporterTest.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Verifies that we can connect to multiple JMXReporters running on the same machine.
 *
 * @throws Exception
 */
@Test
public void testJMXAvailability() throws Exception {
	ReporterSetup reporterSetup1 = ReporterSetup.forReporter("test1", new JMXReporter("9040-9055"));
	ReporterSetup reporterSetup2 = ReporterSetup.forReporter("test2", new JMXReporter("9040-9055"));

	MetricRegistryImpl reg = new MetricRegistryImpl(
		MetricRegistryConfiguration.defaultMetricRegistryConfiguration(),
		Arrays.asList(reporterSetup1, reporterSetup2));

	TaskManagerMetricGroup mg = new TaskManagerMetricGroup(reg, "host", "tm");

	List<MetricReporter> reporters = reg.getReporters();

	assertTrue(reporters.size() == 2);

	MetricReporter rep1 = reporters.get(0);
	MetricReporter rep2 = reporters.get(1);

	Gauge<Integer> g1 = new Gauge<Integer>() {
		@Override
		public Integer getValue() {
			return 1;
		}
	};
	Gauge<Integer> g2 = new Gauge<Integer>() {
		@Override
		public Integer getValue() {
			return 2;
		}
	};

	rep1.notifyOfAddedMetric(g1, "rep1", new FrontMetricGroup<>(createReporterScopedSettings(0), mg));

	rep2.notifyOfAddedMetric(g2, "rep2", new FrontMetricGroup<>(createReporterScopedSettings(1), mg));

	ObjectName objectName1 = new ObjectName(JMX_DOMAIN_PREFIX + "taskmanager.rep1", JMXReporter.generateJmxTable(mg.getAllVariables()));
	ObjectName objectName2 = new ObjectName(JMX_DOMAIN_PREFIX + "taskmanager.rep2", JMXReporter.generateJmxTable(mg.getAllVariables()));

	JMXServiceURL url1 = new JMXServiceURL("service:jmx:rmi://localhost:" + ((JMXReporter) rep1).getPort().get() + "/jndi/rmi://localhost:" + ((JMXReporter) rep1).getPort().get() + "/jmxrmi");
	JMXConnector jmxCon1 = JMXConnectorFactory.connect(url1);
	MBeanServerConnection mCon1 = jmxCon1.getMBeanServerConnection();

	assertEquals(1, mCon1.getAttribute(objectName1, "Value"));
	assertEquals(2, mCon1.getAttribute(objectName2, "Value"));

	jmxCon1.close();

	JMXServiceURL url2 = new JMXServiceURL("service:jmx:rmi://localhost:" + ((JMXReporter) rep2).getPort().get() + "/jndi/rmi://localhost:" + ((JMXReporter) rep2).getPort().get() + "/jmxrmi");
	JMXConnector jmxCon2 = JMXConnectorFactory.connect(url2);
	MBeanServerConnection mCon2 = jmxCon2.getMBeanServerConnection();

	assertEquals(1, mCon2.getAttribute(objectName1, "Value"));
	assertEquals(2, mCon2.getAttribute(objectName2, "Value"));

	rep1.notifyOfRemovedMetric(g1, "rep1", null);
	rep1.notifyOfRemovedMetric(g2, "rep2", null);

	jmxCon2.close();

	rep1.close();
	rep2.close();
	mg.close();
	reg.shutdown().get();
}
 
Example #24
Source File: ReporterSetupTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public MetricReporter createMetricReporter(Properties config) {
	return new InstantiationTypeTrackingTestReporter(true);
}
 
Example #25
Source File: Slf4jReporterFactory.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public MetricReporter createMetricReporter(Properties properties) {
	return new Slf4jReporter();
}
 
Example #26
Source File: ReporterSetupTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public MetricReporter createMetricReporter(Properties config) {
	lastConfig = config;
	return new TestReporter();
}
 
Example #27
Source File: MetricRegistryImpl.java    From flink with Apache License 2.0 4 votes vote down vote up
public MetricReporter getReporter() {
	return reporter;
}
 
Example #28
Source File: MetricRegistryImpl.java    From flink with Apache License 2.0 4 votes vote down vote up
private ReporterAndSettings(MetricReporter reporter, ReporterScopedSettings settings) {
	this.reporter = Preconditions.checkNotNull(reporter);
	this.settings = Preconditions.checkNotNull(settings);
}
 
Example #29
Source File: MetricRegistryImpl.java    From flink with Apache License 2.0 4 votes vote down vote up
@VisibleForTesting
public List<MetricReporter> getReporters() {
	return reporters.stream().map(ReporterAndSettings::getReporter).collect(Collectors.toList());
}
 
Example #30
Source File: MetricRegistryImpl.java    From flink with Apache License 2.0 4 votes vote down vote up
@VisibleForTesting
MetricRegistryImpl(MetricRegistryConfiguration config, Collection<ReporterSetup> reporterConfigurations, ScheduledExecutorService scheduledExecutor) {
	this.maximumFramesize = config.getQueryServiceMessageSizeLimit();
	this.scopeFormats = config.getScopeFormats();
	this.globalDelimiter = config.getDelimiter();
	this.terminationFuture = new CompletableFuture<>();
	this.isShutdown = false;

	// second, instantiate any custom configured reporters
	this.reporters = new ArrayList<>(4);

	this.executor = scheduledExecutor;

	this.queryService = null;
	this.metricQueryServiceRpcService = null;

	if (reporterConfigurations.isEmpty()) {
		// no reporters defined
		// by default, don't report anything
		LOG.info("No metrics reporter configured, no metrics will be exposed/reported.");
	} else {
		for (ReporterSetup reporterSetup : reporterConfigurations) {
			final String namedReporter = reporterSetup.getName();

			try {
				final MetricReporter reporterInstance = reporterSetup.getReporter();
				final String className = reporterInstance.getClass().getName();

				if (reporterInstance instanceof Scheduled) {
					final Duration period = getConfiguredIntervalOrDefault(reporterSetup);

					LOG.info("Periodically reporting metrics in intervals of {} for reporter {} of type {}.", TimeUtils.formatWithHighestUnit(period), namedReporter, className);

					executor.scheduleWithFixedDelay(
							new MetricRegistryImpl.ReporterTask((Scheduled) reporterInstance), period.toMillis(), period.toMillis(), TimeUnit.MILLISECONDS);
				} else {
					LOG.info("Reporting metrics for reporter {} of type {}.", namedReporter, className);
				}

				String delimiterForReporter = reporterSetup.getDelimiter().orElse(String.valueOf(globalDelimiter));
				if (delimiterForReporter.length() != 1) {
					LOG.warn("Failed to parse delimiter '{}' for reporter '{}', using global delimiter '{}'.", delimiterForReporter, namedReporter, globalDelimiter);
					delimiterForReporter = String.valueOf(globalDelimiter);
				}

				reporters.add(new ReporterAndSettings(
					reporterInstance,
					new ReporterScopedSettings(
						reporters.size(),
						delimiterForReporter.charAt(0),
						reporterSetup.getExcludedVariables())));
			}
			catch (Throwable t) {
				LOG.error("Could not instantiate metrics reporter {}. Metrics might not be exposed/reported.", namedReporter, t);
			}
		}
	}
}