Java Code Examples for org.apache.flink.metrics.reporter.MetricReporter#open()

The following examples show how to use org.apache.flink.metrics.reporter.MetricReporter#open() . 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: MetricRegistryImpl.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a new MetricRegistry and starts the configured reporter.
 */
public MetricRegistryImpl(MetricRegistryConfiguration config) {
	this.maximumFramesize = config.getQueryServiceMessageSizeLimit();
	this.scopeFormats = config.getScopeFormats();
	this.globalDelimiter = config.getDelimiter();
	this.delimiters = new ArrayList<>(10);
	this.terminationFuture = new CompletableFuture<>();
	this.isShutdown = false;

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

	List<Tuple2<String, Configuration>> reporterConfigurations = config.getReporterConfigurations();

	this.executor = Executors.newSingleThreadScheduledExecutor(new ExecutorThreadFactory("Flink-MetricRegistry"));

	this.queryService = null;
	this.metricQueryServicePath = 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 {
		// we have some reporters so
		for (Tuple2<String, Configuration> reporterConfiguration: reporterConfigurations) {
			String namedReporter = reporterConfiguration.f0;
			Configuration reporterConfig = reporterConfiguration.f1;

			final String className = reporterConfig.getString(ConfigConstants.METRICS_REPORTER_CLASS_SUFFIX, null);
			if (className == null) {
				LOG.error("No reporter class set for reporter " + namedReporter + ". Metrics might not be exposed/reported.");
				continue;
			}

			try {
				String configuredPeriod = reporterConfig.getString(ConfigConstants.METRICS_REPORTER_INTERVAL_SUFFIX, null);
				TimeUnit timeunit = TimeUnit.SECONDS;
				long period = 10;

				if (configuredPeriod != null) {
					try {
						String[] interval = configuredPeriod.split(" ");
						period = Long.parseLong(interval[0]);
						timeunit = TimeUnit.valueOf(interval[1]);
					}
					catch (Exception e) {
						LOG.error("Cannot parse report interval from config: " + configuredPeriod +
								" - please use values like '10 SECONDS' or '500 MILLISECONDS'. " +
								"Using default reporting interval.");
					}
				}

				Class<?> reporterClass = Class.forName(className);
				MetricReporter reporterInstance = (MetricReporter) reporterClass.newInstance();

				MetricConfig metricConfig = new MetricConfig();
				reporterConfig.addAllToProperties(metricConfig);
				LOG.info("Configuring {} with {}.", namedReporter, metricConfig);
				reporterInstance.open(metricConfig);

				if (reporterInstance instanceof Scheduled) {
					LOG.info("Periodically reporting metrics in intervals of {} {} for reporter {} of type {}.", period, timeunit.name(), namedReporter, className);

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

				String delimiterForReporter = reporterConfig.getString(ConfigConstants.METRICS_REPORTER_SCOPE_DELIMITER, 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);
				}
				this.delimiters.add(delimiterForReporter.charAt(0));
			}
			catch (Throwable t) {
				LOG.error("Could not instantiate metrics reporter {}. Metrics might not be exposed/reported.", namedReporter, t);
			}
		}
	}
}
 
Example 2
Source File: ReporterSetup.java    From flink with Apache License 2.0 4 votes vote down vote up
private static ReporterSetup createReporterSetup(String reporterName, MetricConfig metricConfig, MetricReporter reporter) {
	LOG.info("Configuring {} with {}.", reporterName, metricConfig);
	reporter.open(metricConfig);

	return new ReporterSetup(reporterName, metricConfig, reporter);
}
 
Example 3
Source File: ReporterSetup.java    From flink with Apache License 2.0 4 votes vote down vote up
private static ReporterSetup createReporterSetup(String reporterName, MetricConfig metricConfig, MetricReporter reporter) {
	reporter.open(metricConfig);

	return new ReporterSetup(reporterName, metricConfig, reporter);
}