Пример #1
0
 /**
  * Un-registers the given {@link org.apache.flink.metrics.Metric} with this registry.
  *
  * @param metric the metric that should be removed
  * @param metricName the name of the metric
  * @param group the group that contains the metric
  */
 public void unregister(Metric metric, String metricName, AbstractMetricGroup group) {
   try {
     if (reporter != null) {
       reporter.notifyOfRemovedMetric(metric, metricName, group);
     }
   } catch (Exception e) {
     LOG.error("Error while registering metric.", e);
   }
 }
Пример #2
0
 /**
  * Shuts down this registry and the associated {@link
  * org.apache.flink.metrics.reporter.MetricReporter}.
  */
 public void shutdown() {
   if (reporter != null) {
     try {
       reporter.close();
     } catch (Throwable t) {
       LOG.warn("Metrics reporter did not shut down cleanly", t);
     }
   }
   shutdownExecutor();
 }
Пример #3
0
  /** Creates a new MetricRegistry and starts the configured reporter. */
  public MetricRegistry(Configuration config) {
    // first parse the scopeName formats, these are needed for all reporters
    ScopeFormats scopeFormats;
    try {
      scopeFormats = createScopeConfig(config);
    } catch (Exception e) {
      LOG.warn("Failed to parse scopeName format, using default scopeName formats", e);
      scopeFormats = new ScopeFormats();
    }
    this.scopeFormats = scopeFormats;

    // second, instantiate any custom configured reporters

    final String className = config.getString(KEY_METRICS_REPORTER_CLASS, null);
    if (className == null) {
      // by default, create JMX metrics
      LOG.info("No metrics reporter configured, exposing metrics via JMX");
      this.reporter = startJmxReporter(config);
      this.executor = null;
    } else {
      MetricReporter reporter;
      ScheduledExecutorService executor = null;
      try {
        String configuredPeriod = config.getString(KEY_METRICS_REPORTER_INTERVAL, 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.");
          }
        }

        Configuration reporterConfig = createReporterConfig(config, timeunit, period);

        Class<?> reporterClass = Class.forName(className);
        reporter = (MetricReporter) reporterClass.newInstance();
        reporter.open(reporterConfig);

        if (reporter instanceof Scheduled) {
          executor = Executors.newSingleThreadScheduledExecutor();
          LOG.info("Periodically reporting metrics in intervals of {} {}", period, timeunit.name());

          executor.scheduleWithFixedDelay(
              new ReporterTask((Scheduled) reporter), period, period, timeunit);
        }
      } catch (Throwable t) {
        shutdownExecutor();
        LOG.error(
            "Could not instantiate custom metrics reporter. Defaulting to JMX metrics export.", t);
        reporter = startJmxReporter(config);
      }

      this.reporter = reporter;
      this.executor = executor;
    }
  }