Java Code Examples for org.apache.commons.configuration.SubsetConfiguration

The following examples show how to use org.apache.commons.configuration.SubsetConfiguration. These examples are extracted from open source projects. 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 Project: singer   Source File: ConfigUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static LoggingAuditEventSenderConfig parseSenderConfig(AbstractConfiguration conf)
    throws ConfigurationException {
  try {
    LoggingAuditEventSenderConfig senderConfig = new LoggingAuditEventSenderConfig();
    SenderType senderType = SenderType.valueOf(conf.getString(
        LoggingAuditClientConfigDef.SENDER_TYPE).toUpperCase());
    if (!senderType.equals(SenderType.KAFKA)) {
      throw new ConfigurationException("Only Kafka Sender is supported now.");
    }
    senderConfig.setSenderType(senderType);
    senderConfig.setKafkaSenderConfig(parseKafkaSenderConfig(new SubsetConfiguration(conf,
        LoggingAuditClientConfigDef.KAFKA_SENDER_PREFIX)));
    return senderConfig;
  } catch (Exception e) {
    throw new ConfigurationException(
        "LoggingAuditEventSenderConfig can't be properly parsed due to " + e.getMessage());
  }
}
 
Example 2
Source Project: singer   Source File: LogConfigUtils.java    License: Apache License 2.0 6 votes vote down vote up
private static KubeConfig parseKubeConfig(PropertiesConfiguration configHeader) throws ConfigurationException {
  KubeConfig config = new KubeConfig();
  AbstractConfiguration subsetConfig = new SubsetConfiguration(configHeader,
      SingerConfigDef.SINGER_KUBE_CONFIG_PREFIX);
  if (subsetConfig.containsKey(SingerConfigDef.KUBE_POLL_FREQUENCY_SECONDS)) {
    config.setPollFrequencyInSeconds(
        subsetConfig.getInt(SingerConfigDef.KUBE_POLL_FREQUENCY_SECONDS));
  }

  if (subsetConfig.containsKey(SingerConfigDef.KUBE_POD_LOG_DIR)) {
    String logDirectory = subsetConfig.getString(SingerConfigDef.KUBE_POD_LOG_DIR);
    // normalize path before setting the property
    logDirectory = new File(logDirectory).toPath().normalize().toString() + "/";
    if (!logDirectory.isEmpty() && !logDirectory.endsWith("/")) {
      logDirectory += "/";
    }
    config.setPodLogDirectory(logDirectory);
  }

  if (subsetConfig.containsKey(SingerConfigDef.KUBE_DEFAULT_DELETION_TIMEOUT)) {
    config.setDefaultDeletionTimeoutInSeconds(
        subsetConfig.getInt(SingerConfigDef.KUBE_DEFAULT_DELETION_TIMEOUT));
  }
  return config;
}
 
Example 3
Source Project: singer   Source File: LogConfigUtils.java    License: Apache License 2.0 6 votes vote down vote up
private static PulsarProducerConfig parsePulsarProducerConfig(SubsetConfiguration subsetConfiguration) throws ConfigurationException {
  PulsarProducerConfig pulsarProducerConfig = new PulsarProducerConfig();

  if (subsetConfiguration.containsKey(SingerConfigDef.COMPRESSION_TYPE)) {
    pulsarProducerConfig
        .setCompressionType(subsetConfiguration.getString(SingerConfigDef.COMPRESSION_TYPE));
  }
  if (subsetConfiguration.containsKey(SingerConfigDef.PARTITIONER_CLASS)) {
    pulsarProducerConfig.setPartitionerClass(subsetConfiguration.getString(SingerConfigDef.PARTITIONER_CLASS));
  }
  if (subsetConfiguration.containsKey(SingerConfigDef.PULSAR_SERVICE_URL)) {
    pulsarProducerConfig
        .setServiceUrl(subsetConfiguration.getString(SingerConfigDef.PULSAR_SERVICE_URL));
  } else {
    throw new ConfigurationException(
        "Missing Pulsar service URL in producer config:" + SingerConfigDef.PULSAR_SERVICE_URL);
  }
  pulsarProducerConfig.setPulsarClusterSignature(pulsarProducerConfig.getServiceUrl());
  return pulsarProducerConfig;
}
 
Example 4
Source Project: singer   Source File: LogConfigUtils.java    License: Apache License 2.0 6 votes vote down vote up
private static LogStreamReaderConfig parseLogStreamReaderConfig(AbstractConfiguration readerConfiguration) throws ConfigurationException {
  readerConfiguration.setThrowExceptionOnMissing(true);
  String readerTypeString = readerConfiguration.getString("type");
  ReaderType type = ReaderType.valueOf(readerTypeString.toUpperCase());

  LogStreamReaderConfig readerConfig = new LogStreamReaderConfig(type);
  if (type.equals(ReaderType.THRIFT)) {
    ThriftReaderConfig thriftReaderConfig = parseThriftReaderConfig(
        new SubsetConfiguration(readerConfiguration, readerTypeString + "."));
    readerConfig.setThriftReaderConfig(thriftReaderConfig);
  } else if (type.equals(ReaderType.TEXT)) {
    TextReaderConfig textReaderConfig = parseTextReaderConfig(
        new SubsetConfiguration(readerConfiguration, readerTypeString + "."));
    readerConfig.setTextReaderConfig(textReaderConfig);
  }

  return readerConfig;
}
 
Example 5
Source Project: hadoop   Source File: TestPatternFilter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Filters should handle white-listing correctly
 */
@Test public void includeOnlyShouldOnlyIncludeMatched() {
  SubsetConfiguration wl = new ConfigBuilder()
      .add("p.include", "foo")
      .add("p.include.tags", "foo:f").subset("p");
  shouldAccept(wl, "foo");
  shouldAccept(wl, Arrays.asList(tag("bar", "", ""),
                                 tag("foo", "", "f")), new boolean[] {false, true});
  shouldAccept(wl, mockMetricsRecord("foo", Arrays.asList(
    tag("bar", "", ""), tag("foo", "", "f"))));
  shouldReject(wl, "bar");
  shouldReject(wl, Arrays.asList(tag("bar", "", "")));
  shouldReject(wl, Arrays.asList(tag("foo", "", "boo")));
  shouldReject(wl, mockMetricsRecord("bar", Arrays.asList(
    tag("foo", "", "f"))));
  shouldReject(wl, mockMetricsRecord("foo", Arrays.asList(
    tag("bar", "", ""))));
}
 
Example 6
Source Project: hadoop   Source File: TestPatternFilter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Filters should handle black-listing correctly
 */
@Test public void excludeOnlyShouldOnlyExcludeMatched() {
  SubsetConfiguration bl = new ConfigBuilder()
      .add("p.exclude", "foo")
      .add("p.exclude.tags", "foo:f").subset("p");
  shouldAccept(bl, "bar");
  shouldAccept(bl, Arrays.asList(tag("bar", "", "")));
  shouldAccept(bl, mockMetricsRecord("bar", Arrays.asList(
    tag("bar", "", ""))));
  shouldReject(bl, "foo");
  shouldReject(bl, Arrays.asList(tag("bar", "", ""),
                                 tag("foo", "", "f")), new boolean[] {true, false});
  shouldReject(bl, mockMetricsRecord("foo", Arrays.asList(
    tag("bar", "", ""))));
  shouldReject(bl, mockMetricsRecord("bar", Arrays.asList(
    tag("bar", "", ""), tag("foo", "", "f"))));
}
 
Example 7
Source Project: hadoop   Source File: TestPatternFilter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Filters should accepts unmatched item when both include and
 * exclude patterns are present.
 */
@Test public void shouldAcceptUnmatchedWhenBothAreConfigured() {
  SubsetConfiguration c = new ConfigBuilder()
      .add("p.include", "foo")
      .add("p.include.tags", "foo:f")
      .add("p.exclude", "bar")
      .add("p.exclude.tags", "bar:b").subset("p");
  shouldAccept(c, "foo");
  shouldAccept(c, Arrays.asList(tag("foo", "", "f")));
  shouldAccept(c, mockMetricsRecord("foo", Arrays.asList(
    tag("foo", "", "f"))));
  shouldReject(c, "bar");
  shouldReject(c, Arrays.asList(tag("bar", "", "b")));
  shouldReject(c, mockMetricsRecord("bar", Arrays.asList(
    tag("foo", "", "f"))));
  shouldReject(c, mockMetricsRecord("foo", Arrays.asList(
    tag("bar", "", "b"))));
  shouldAccept(c, "foobar");
  shouldAccept(c, Arrays.asList(tag("foobar", "", "")));
  shouldAccept(c, mockMetricsRecord("foobar", Arrays.asList(
    tag("foobar", "", ""))));
}
 
Example 8
Source Project: big-c   Source File: TestPatternFilter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Filters should handle white-listing correctly
 */
@Test public void includeOnlyShouldOnlyIncludeMatched() {
  SubsetConfiguration wl = new ConfigBuilder()
      .add("p.include", "foo")
      .add("p.include.tags", "foo:f").subset("p");
  shouldAccept(wl, "foo");
  shouldAccept(wl, Arrays.asList(tag("bar", "", ""),
                                 tag("foo", "", "f")), new boolean[] {false, true});
  shouldAccept(wl, mockMetricsRecord("foo", Arrays.asList(
    tag("bar", "", ""), tag("foo", "", "f"))));
  shouldReject(wl, "bar");
  shouldReject(wl, Arrays.asList(tag("bar", "", "")));
  shouldReject(wl, Arrays.asList(tag("foo", "", "boo")));
  shouldReject(wl, mockMetricsRecord("bar", Arrays.asList(
    tag("foo", "", "f"))));
  shouldReject(wl, mockMetricsRecord("foo", Arrays.asList(
    tag("bar", "", ""))));
}
 
Example 9
Source Project: big-c   Source File: TestPatternFilter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Filters should handle black-listing correctly
 */
@Test public void excludeOnlyShouldOnlyExcludeMatched() {
  SubsetConfiguration bl = new ConfigBuilder()
      .add("p.exclude", "foo")
      .add("p.exclude.tags", "foo:f").subset("p");
  shouldAccept(bl, "bar");
  shouldAccept(bl, Arrays.asList(tag("bar", "", "")));
  shouldAccept(bl, mockMetricsRecord("bar", Arrays.asList(
    tag("bar", "", ""))));
  shouldReject(bl, "foo");
  shouldReject(bl, Arrays.asList(tag("bar", "", ""),
                                 tag("foo", "", "f")), new boolean[] {true, false});
  shouldReject(bl, mockMetricsRecord("foo", Arrays.asList(
    tag("bar", "", ""))));
  shouldReject(bl, mockMetricsRecord("bar", Arrays.asList(
    tag("bar", "", ""), tag("foo", "", "f"))));
}
 
Example 10
Source Project: big-c   Source File: TestPatternFilter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Filters should accepts unmatched item when both include and
 * exclude patterns are present.
 */
@Test public void shouldAcceptUnmatchedWhenBothAreConfigured() {
  SubsetConfiguration c = new ConfigBuilder()
      .add("p.include", "foo")
      .add("p.include.tags", "foo:f")
      .add("p.exclude", "bar")
      .add("p.exclude.tags", "bar:b").subset("p");
  shouldAccept(c, "foo");
  shouldAccept(c, Arrays.asList(tag("foo", "", "f")));
  shouldAccept(c, mockMetricsRecord("foo", Arrays.asList(
    tag("foo", "", "f"))));
  shouldReject(c, "bar");
  shouldReject(c, Arrays.asList(tag("bar", "", "b")));
  shouldReject(c, mockMetricsRecord("bar", Arrays.asList(
    tag("foo", "", "f"))));
  shouldReject(c, mockMetricsRecord("foo", Arrays.asList(
    tag("bar", "", "b"))));
  shouldAccept(c, "foobar");
  shouldAccept(c, Arrays.asList(tag("foobar", "", "")));
  shouldAccept(c, mockMetricsRecord("foobar", Arrays.asList(
    tag("foobar", "", ""))));
}
 
Example 11
Source Project: singer   Source File: ConfigUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static Map<String, AuditConfig> parseAllAuditConfigs(AbstractConfiguration conf) {
  Map<String, AuditConfig> auditConfigs = new HashMap<>();
  if (conf != null && conf.containsKey(LoggingAuditClientConfigDef.AUDITED_TOPIC_NAMES)) {
    for (String name : conf.getStringArray(LoggingAuditClientConfigDef.AUDITED_TOPIC_NAMES)) {
      try {
        auditConfigs.put(name, parseAuditConfig(new SubsetConfiguration(conf, name + ".")));
      } catch (ConfigurationException e) {
        LOG.error("Can't parse TopicAuditConfig for {}", name);
      }
    }
  }
  return auditConfigs;
}
 
Example 12
Source Project: singer   Source File: ConfigUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static KafkaSenderConfig parseKafkaSenderConfig(AbstractConfiguration conf)
    throws ConfigurationException {
  KafkaSenderConfig kafkaSenderConfig = new KafkaSenderConfig();
  if (conf.containsKey(LoggingAuditClientConfigDef.KAFKA_TOPIC)) {
    kafkaSenderConfig.setTopic(conf.getString(LoggingAuditClientConfigDef.KAFKA_TOPIC));
  }
  if (conf.containsKey(LoggingAuditClientConfigDef.KAFKA_STOP_GRACE_PERIOD_IN_SECONDS)) {
    kafkaSenderConfig.setStopGracePeriodInSeconds(
        conf.getInt(LoggingAuditClientConfigDef.KAFKA_STOP_GRACE_PERIOD_IN_SECONDS));
  }
  kafkaSenderConfig.setKafkaProducerConfig(parseProducerConfig(
      new SubsetConfiguration(conf, LoggingAuditClientConfigDef.KAFKA_PRODUCER_CONFIG_PREFIX)));
  return kafkaSenderConfig;
}
 
Example 13
Source Project: singer   Source File: LogConfigUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Singer can restart itself if # of failures exceeds threshold, and in daily
 * cadence. The following is singer restart related configuration:
 *
 * singer.restart.onFailures=true singer.restart.numberOfFailuresAllowed=100
 * singer.restart.daily=true singer.restart.dailyRestartUtcTimeRangeBegin=02:30
 * singer.restart.dailyRestartUtcTimeRangeEnd=03:30
 */
private static SingerRestartConfig parseSingerRestartConfig(PropertiesConfiguration configHeader) throws ConfigurationException {
  SingerRestartConfig restartConfig = new SingerRestartConfig();
  AbstractConfiguration subsetConfig = new SubsetConfiguration(configHeader,
      SingerConfigDef.SINGER_RESTART_PREFIX);

  if (subsetConfig.containsKey(SingerConfigDef.ON_FAILURES)) {
    restartConfig.restartOnFailures = subsetConfig.getBoolean(SingerConfigDef.ON_FAILURES);
  }
  if (subsetConfig.containsKey(SingerConfigDef.NUMBER_OF_FAILURES_ALLOWED)) {
    restartConfig.numOfFailuesAllowed = subsetConfig
        .getInt(SingerConfigDef.NUMBER_OF_FAILURES_ALLOWED);
  }
  if (subsetConfig.containsKey(SingerConfigDef.DAILY_RESTART_FLAG)) {
    restartConfig.restartDaily = subsetConfig.getBoolean(SingerConfigDef.DAILY_RESTART_FLAG);
  }

  if (restartConfig.restartDaily) {
    if (!subsetConfig.containsKey(SingerConfigDef.DAILY_RESTART_TIME_BEGIN)
        || !subsetConfig.containsKey(SingerConfigDef.DAILY_RESTART_TIME_END)) {
      throw new ConfigurationException("Daily restart time range is not set correctly");
    }

    restartConfig.dailyRestartUtcTimeRangeBegin = subsetConfig
        .getString(SingerConfigDef.DAILY_RESTART_TIME_BEGIN);
    restartConfig.dailyRestartUtcTimeRangeEnd = subsetConfig
        .getString(SingerConfigDef.DAILY_RESTART_TIME_END);
    Date startTime = SingerUtils.convertToDate(restartConfig.dailyRestartUtcTimeRangeBegin);
    Date endTime = SingerUtils.convertToDate(restartConfig.dailyRestartUtcTimeRangeEnd);
    if (endTime.compareTo(startTime) <= 0) {
      throw new ConfigurationException("Daily restart end time is not later than start time");
    }
  }
  return restartConfig;
}
 
Example 14
Source Project: singer   Source File: LogConfigUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static SingerConfig parseFileBasedSingerConfig(String singerConfigFile) throws ConfigurationException {
  AbstractConfiguration singerConfiguration = new SubsetConfiguration(
      new PropertiesConfiguration(singerConfigFile), SingerConfigDef.SINGER_CONFIGURATION_PREFIX);
  singerConfiguration.setThrowExceptionOnMissing(true);
  SingerConfig result = parseCommonSingerConfigHeader(singerConfiguration);
  Set<String> logs = Sets.newHashSet(singerConfiguration.getStringArray("logs"));
  for (String log : logs) {
    AbstractConfiguration logConfiguration = new SubsetConfiguration(singerConfiguration,
        log + ".");
    result.addToLogConfigs(LogConfigUtils.parseLogConfig(log, logConfiguration));
  }
  return result;
}
 
Example 15
Source Project: singer   Source File: LogConfigUtils.java    License: Apache License 2.0 5 votes vote down vote up
private static LogStreamWriterConfig parseLogStreamWriterConfig(AbstractConfiguration writerConfiguration) throws ConfigurationException {
  writerConfiguration.setThrowExceptionOnMissing(true);
  String writerTypeString = writerConfiguration.getString("type");
  WriterType type = WriterType.valueOf(writerTypeString.toUpperCase());
  LogStreamWriterConfig writerConfig = new LogStreamWriterConfig(type);
  switch (type) {
  case KAFKA08:
  case KAFKA:
    writerConfig.setKafkaWriterConfig(parseKafkaWriterConfig(
        new SubsetConfiguration(writerConfiguration, writerTypeString + ".")));
    return writerConfig;
  case REALPIN:
    writerConfig.setRealpinWriterConfig(parseRealpinWriterConfig(
        new SubsetConfiguration(writerConfiguration, writerTypeString + ".")));
    return writerConfig;
  case DUMMY:
    writerConfig.setDummyWriteConfig(parseDummyWriterConfig(
        new SubsetConfiguration(writerConfiguration, writerTypeString + ".")));
    return writerConfig;
  case PULSAR:
    writerConfig.setPulsarWriterConfig(parsePulsarWriterConfig(
        new SubsetConfiguration(writerConfiguration, writerTypeString + ".")));
    return writerConfig;
  default:
    throw new ConfigurationException("Unsupported log writer type.");
  }
}
 
Example 16
Source Project: singer   Source File: LogConfigUtils.java    License: Apache License 2.0 5 votes vote down vote up
private static PulsarWriterConfig parsePulsarWriterConfig(SubsetConfiguration writerConfiguration) throws ConfigurationException {
  PulsarWriterConfig config = new PulsarWriterConfig();
  config.setTopic(writerConfiguration.getString("topic"));
  config.setProducerConfig(
      parsePulsarProducerConfig(new SubsetConfiguration(writerConfiguration, "producerConfig.")));
  return config;
}
 
Example 17
public static void createAuditConfigFromSingerLogConfigFile(String topic, String singerLogConfigFile){
  if (loggingAuditClient == null){
    return;
  }
  try {
    PropertiesConfiguration conf = new PropertiesConfiguration(singerLogConfigFile);
    AuditConfig auditConfig = ConfigUtils.parseAuditConfig(new SubsetConfiguration(conf,  "loggingaudit.thriftlogger."));
    loggingAuditClient.addAuditConfig(topic, auditConfig);
  } catch (ConfigurationException e){
    LOG.error("Couldn't successfully create AuditConfig for a topic");
  }
}
 
Example 18
Source Project: kylin-on-parquet-v2   Source File: StandaloneExample.java    License: Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    final MetricRegistry metrics = new MetricRegistry();

    final HadoopMetrics2Reporter metrics2Reporter = HadoopMetrics2Reporter.forRegistry(metrics).build(
            DefaultMetricsSystem.initialize("StandaloneTest"), // The application-level name
            "Test", // Component name
            "Test", // Component description
            "Test"); // Name for each metric record
    final ConsoleReporter consoleReporter = ConsoleReporter.forRegistry(metrics).build();

    MetricsSystem metrics2 = DefaultMetricsSystem.instance();
    // Writes to stdout without a filename configuration
    // Will be invoked every 10seconds by default
    FileSink sink = new FileSink();
    metrics2.register("filesink", "filesink", sink);
    sink.init(new SubsetConfiguration(null, null) {
        public String getString(String key) {
            if (key.equals("filename")) {
                return null;
            }
            return super.getString(key);
        }
    });

    // How often should the dropwizard reporter be invoked
    metrics2Reporter.start(500, TimeUnit.MILLISECONDS);
    // How often will the dropwziard metrics be logged to the console
    consoleReporter.start(2, TimeUnit.SECONDS);

    generateMetrics(metrics, 5000, 25, TimeUnit.MILLISECONDS, metrics2Reporter, 10);
}
 
Example 19
Source Project: hadoop   Source File: GraphiteSink.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void init(SubsetConfiguration conf) {
    // Get Graphite host configurations.
    final String serverHost = conf.getString(SERVER_HOST_KEY);
    final int serverPort = Integer.parseInt(conf.getString(SERVER_PORT_KEY));

    // Get Graphite metrics graph prefix.
    metricsPrefix = conf.getString(METRICS_PREFIX);
    if (metricsPrefix == null)
        metricsPrefix = "";

    graphite = new Graphite(serverHost, serverPort);
    graphite.connect();
}
 
Example 20
Source Project: hadoop   Source File: FileSink.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void init(SubsetConfiguration conf) {
  String filename = conf.getString(FILENAME_KEY);
  try {
    writer = filename == null ? System.out
        : new PrintStream(new FileOutputStream(new File(filename)),
                          true, "UTF-8");
  } catch (Exception e) {
    throw new MetricsException("Error creating "+ filename, e);
  }
}
 
Example 21
Source Project: hadoop   Source File: GangliaSink30.java    License: Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void init(SubsetConfiguration conf) {
  super.init(conf);

  conf.setListDelimiter(',');
  Iterator<String> it = (Iterator<String>) conf.getKeys();
  while (it.hasNext()) {
    String propertyName = it.next();
    if (propertyName.startsWith(TAGS_FOR_PREFIX_PROPERTY_PREFIX)) {
      String contextName = propertyName.substring(TAGS_FOR_PREFIX_PROPERTY_PREFIX.length());
      String[] tags = conf.getStringArray(propertyName);
      boolean useAllTags = false;
      Set<String> set = null;
      if (tags.length > 0) {
        set = new HashSet<String>();
        for (String tag : tags) {
          tag = tag.trim();
          useAllTags |= tag.equals("*");
          if (tag.length() > 0) {
            set.add(tag);
          }
        }
        if (useAllTags) {
          set = null;
        }
      }
      useTagsMap.put(contextName, set);
    }
  }
}
 
Example 22
Source Project: hadoop   Source File: TestGangliaSink.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testShouldCreateDatagramSocketByDefault() throws Exception {
    SubsetConfiguration conf = new ConfigBuilder()
            .subset("test.sink.ganglia");

    GangliaSink30 gangliaSink = new GangliaSink30();
    gangliaSink.init(conf);
    DatagramSocket socket = gangliaSink.getDatagramSocket();
    assertFalse("Did not create DatagramSocket", socket == null || socket instanceof MulticastSocket);
}
 
Example 23
Source Project: hadoop   Source File: TestGangliaSink.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testShouldCreateDatagramSocketIfMulticastIsDisabled() throws Exception {
    SubsetConfiguration conf = new ConfigBuilder()
            .add("test.sink.ganglia.multicast", false)
            .subset("test.sink.ganglia");
    GangliaSink30 gangliaSink = new GangliaSink30();
    gangliaSink.init(conf);
    DatagramSocket socket = gangliaSink.getDatagramSocket();
    assertFalse("Did not create DatagramSocket", socket == null || socket instanceof MulticastSocket);
}
 
Example 24
Source Project: hadoop   Source File: TestGangliaSink.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testShouldCreateMulticastSocket() throws Exception {
    SubsetConfiguration conf = new ConfigBuilder()
            .add("test.sink.ganglia.multicast", true)
            .subset("test.sink.ganglia");
    GangliaSink30 gangliaSink = new GangliaSink30();
    gangliaSink.init(conf);
    DatagramSocket socket = gangliaSink.getDatagramSocket();
    assertTrue("Did not create MulticastSocket", socket != null && socket instanceof MulticastSocket);
    int ttl = ((MulticastSocket) socket).getTimeToLive();
    assertEquals("Did not set default TTL", 1, ttl);
}
 
Example 25
Source Project: hadoop   Source File: TestGangliaSink.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testShouldSetMulticastSocketTtl() throws Exception {
    SubsetConfiguration conf = new ConfigBuilder()
            .add("test.sink.ganglia.multicast", true)
            .add("test.sink.ganglia.multicast.ttl", 3)
            .subset("test.sink.ganglia");
    GangliaSink30 gangliaSink = new GangliaSink30();
    gangliaSink.init(conf);
    DatagramSocket socket = gangliaSink.getDatagramSocket();
    assertTrue("Did not create MulticastSocket", socket != null && socket instanceof MulticastSocket);
    int ttl = ((MulticastSocket) socket).getTimeToLive();
    assertEquals("Did not set TTL", 3, ttl);
}
 
Example 26
Source Project: hadoop   Source File: TestMetricsCollectorImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Test public void recordBuilderShouldNoOpIfFiltered() {
  SubsetConfiguration fc = new ConfigBuilder()
      .add("p.exclude", "foo").subset("p");
  MetricsCollectorImpl mb = new MetricsCollectorImpl();
  mb.setRecordFilter(newGlobFilter(fc));
  MetricsRecordBuilderImpl rb = mb.addRecord("foo");
  rb.tag(info("foo", ""), "value").addGauge(info("g0", ""), 1);
  assertEquals("no tags", 0, rb.tags().size());
  assertEquals("no metrics", 0, rb.metrics().size());
  assertNull("null record", rb.getRecord());
  assertEquals("no records", 0, mb.getRecords().size());
}
 
Example 27
Source Project: hadoop   Source File: TestMetricsCollectorImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Test public void testPerMetricFiltering() {
  SubsetConfiguration fc = new ConfigBuilder()
      .add("p.exclude", "foo").subset("p");
  MetricsCollectorImpl mb = new MetricsCollectorImpl();
  mb.setMetricFilter(newGlobFilter(fc));
  MetricsRecordBuilderImpl rb = mb.addRecord("foo");
  rb.tag(info("foo", ""), "").addCounter(info("c0", ""), 0)
    .addGauge(info("foo", ""), 1);
  assertEquals("1 tag", 1, rb.tags().size());
  assertEquals("1 metric", 1, rb.metrics().size());
  assertEquals("expect foo tag", "foo", rb.tags().get(0).name());
  assertEquals("expect c0", "c0", rb.metrics().get(0).name());
}
 
Example 28
Source Project: hadoop   Source File: TestPatternFilter.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Filters should default to accept
 */
@Test public void emptyConfigShouldAccept() {
  SubsetConfiguration empty = new ConfigBuilder().subset("");
  shouldAccept(empty, "anything");
  shouldAccept(empty, Arrays.asList(tag("key", "desc", "value")));
  shouldAccept(empty, mockMetricsRecord("anything", Arrays.asList(
    tag("key", "desc", "value"))));
}
 
Example 29
Source Project: hadoop   Source File: TestPatternFilter.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Include patterns should take precedence over exclude patterns
 */
@Test public void includeShouldOverrideExclude() {
  SubsetConfiguration c = new ConfigBuilder()
      .add("p.include", "foo")
      .add("p.include.tags", "foo:f")
      .add("p.exclude", "foo")
      .add("p.exclude.tags", "foo:f").subset("p");
  shouldAccept(c, "foo");
  shouldAccept(c, Arrays.asList(tag("foo", "", "f")));
  shouldAccept(c, mockMetricsRecord("foo", Arrays.asList(
    tag("foo", "", "f"))));
}
 
Example 30
Source Project: hadoop   Source File: TestPatternFilter.java    License: Apache License 2.0 5 votes vote down vote up
private static void shouldAcceptImpl(final boolean expectAcceptList,  
    SubsetConfiguration conf, List<MetricsTag> tags, boolean[] expectedAcceptedSpec) {
  final MetricsFilter globFilter = newGlobFilter(conf);
  final MetricsFilter regexFilter = newRegexFilter(conf);
  
  // Test acceptance of the tag list:  
  assertEquals("accepts "+ tags, expectAcceptList, globFilter.accepts(tags));
  assertEquals("accepts "+ tags, expectAcceptList, regexFilter.accepts(tags));
  
  // Test results on each of the individual tags:
  int acceptedCount = 0;
  for (int i=0; i<tags.size(); i++) {
    MetricsTag tag = tags.get(i);
    boolean actGlob = globFilter.accepts(tag);
    boolean actRegex = regexFilter.accepts(tag);
    assertEquals("accepts "+tag, expectedAcceptedSpec[i], actGlob);
    // Both the filters should give the same result:
    assertEquals(actGlob, actRegex);
    if (actGlob) {
      acceptedCount++;
    }
  }
  if (expectAcceptList) {
    // At least one individual tag should be accepted:
    assertTrue("No tag of the following accepted: " + tags, acceptedCount > 0);
  } else {
    // At least one individual tag should be rejected: 
    assertTrue("No tag of the following rejected: " + tags, acceptedCount < tags.size());
  }
}