Exemplo n.º 1
0
  @Override
  public ScheduledReporter getReporter(MetricConfig config) {

    try {
      String host = config.getString(ARG_HOST, null);
      int port = config.getInteger(ARG_PORT, -1);
      if (host == null || host.length() == 0 || port < 1) {
        throw new IllegalArgumentException(
            "Invalid host/port configuration. Host: " + host + " Port: " + port);
      }
      String addressingMode = config.getString(ARG_MODE_ADDRESSING, "MULTICAST");
      int ttl = config.getInteger(ARG_TTL, 1);
      GMetric gMetric =
          new GMetric(host, port, GMetric.UDPAddressingMode.valueOf(addressingMode), ttl);

      String prefix = config.getString(ARG_PREFIX, null);
      String conversionRate = config.getString(ARG_CONVERSION_RATE, null);
      String conversionDuration = config.getString(ARG_CONVERSION_DURATION, null);
      int dMax = config.getInteger(ARG_DMAX, 0);
      int tMax = config.getInteger(ARG_TMAX, 60);

      com.codahale.metrics.ganglia.GangliaReporter.Builder builder =
          com.codahale.metrics.ganglia.GangliaReporter.forRegistry(registry);

      if (prefix != null) {
        builder.prefixedWith(prefix);
      }
      if (conversionRate != null) {
        builder.convertRatesTo(TimeUnit.valueOf(conversionRate));
      }
      if (conversionDuration != null) {
        builder.convertDurationsTo(TimeUnit.valueOf(conversionDuration));
      }
      builder.withDMax(dMax);
      builder.withTMax(tMax);

      log.info(
          "Configured GangliaReporter with {host:{}, port:{}, dmax:{}, tmax:{}, ttl:{}, addressingMode:{}}",
          host,
          port,
          dMax,
          tMax,
          ttl,
          addressingMode);
      return builder.build(gMetric);
    } catch (IOException e) {
      throw new RuntimeException("Error while instantiating GangliaReporter.", e);
    }
  }
  @Override
  protected GangliaReporter createInstance() throws Exception {
    final GangliaReporter.Builder reporter = GangliaReporter.forRegistry(getMetricRegistry());

    if (hasProperty(PREFIX)) {
      reporter.prefixedWith(getProperty(PREFIX));
    }

    if (hasProperty(DURATION_UNIT)) {
      reporter.convertDurationsTo(getProperty(DURATION_UNIT, TimeUnit.class));
    }

    if (hasProperty(RATE_UNIT)) {
      reporter.convertRatesTo(getProperty(RATE_UNIT, TimeUnit.class));
    }

    if (hasProperty(FILTER_PATTERN)) {
      reporter.filter(metricFilterPattern(getProperty(FILTER_PATTERN)));
    } else if (hasProperty(FILTER_REF)) {
      reporter.filter(getPropertyRef(FILTER_REF, MetricFilter.class));
    }

    if (hasProperty(DMAX)) {
      reporter.withDMax(getProperty(DMAX, Integer.TYPE));
    }

    if (hasProperty(TMAX)) {
      reporter.withTMax(getProperty(TMAX, Integer.TYPE));
    }

    final GMetric gMetric =
        new GMetric(
            getProperty(GROUP),
            getProperty(PORT, Integer.TYPE),
            getProperty(UDP_MODE, UDPAddressingMode.class),
            getProperty(TTL, Integer.TYPE),
            !hasProperty(PROTOCOL) || getProperty(PROTOCOL).equals("v3.1"),
            hasProperty(UUID) ? java.util.UUID.fromString(getProperty(UUID)) : null,
            getProperty(SPOOF));

    return reporter.build(gMetric);
  }