@Override public void init(SubsetConfiguration conf) { // Get StatsD host configurations. final String serverHost = conf.getString(SERVER_HOST_KEY); final int serverPort = Integer.parseInt(conf.getString(SERVER_PORT_KEY)); skipHostname = conf.getBoolean(SKIP_HOSTNAME_KEY, false); if (!skipHostname) { hostName = conf.getString(HOST_NAME_KEY, null); if (null == hostName) { hostName = NetUtils.getHostname(); } } serviceName = conf.getString(SERVICE_NAME_KEY, null); statsd = new StatsD(serverHost, serverPort); }
/** * 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", "", "")))); }
/** * 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")))); }
/** * 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", "", "")))); }
/** * Initialize the plugin * * @param conf the configuration object for the plugin */ @Override public void init(SubsetConfiguration conf) { nameMap = new TreeSet<>(); nameMap.add("DfsUsed"); nameMap.add("Capacity"); nameMap.add("Remaining"); nameMap.add("StorageInfo"); nameMap.add("NumFailedVolumes"); nameMap.add("LastVolumeFailureDate"); nameMap.add("EstimatedCapacityLostTotal"); nameMap.add("CacheUsed"); nameMap.add("CacheCapacity"); nameMap.add("NumBlocksCached"); nameMap.add("NumBlocksFailedToCache"); nameMap.add("NumBlocksFailedToUnCache"); nameMap.add("Context"); nameMap.add("Hostname"); }
@Override public void init(SubsetConfiguration conf) { // Get Graphite host configurations. String serverHost = conf.getString(SERVER_HOST_KEY); Integer serverPort = Integer.parseInt(conf.getString(SERVER_PORT_KEY)); // Get Graphite metrics graph prefix. metricsPrefix = conf.getString(METRICS_PREFIX); if (metricsPrefix == null) metricsPrefix = ""; try { // Open an connection to Graphite server. socket = new Socket(serverHost, serverPort); writer = new OutputStreamWriter(socket.getOutputStream()); } catch (Exception e) { throw new MetricsException("Error creating connection, " + serverHost + ":" + serverPort, e); } }
/** * 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"))); 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", "", "")))); }
/** * 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"))); shouldReject(bl, mockMetricsRecord("foo", Arrays.asList( tag("bar", "", "")))); shouldReject(bl, mockMetricsRecord("bar", Arrays.asList( tag("bar", "", ""), tag("foo", "", "f")))); }
@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(); }
@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); } }
@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); } } }
@Override public void init(SubsetConfiguration metrics2Properties) { properties = metrics2Properties; basePath = new Path(properties.getString(BASEPATH_KEY, BASEPATH_DEFAULT)); source = properties.getString(SOURCE_KEY, SOURCE_DEFAULT); ignoreError = properties.getBoolean(IGNORE_ERROR_KEY, false); allowAppend = properties.getBoolean(ALLOW_APPEND_KEY, false); conf = loadConf(); UserGroupInformation.setConfiguration(conf); // Don't do secure setup if it's not needed. if (UserGroupInformation.isSecurityEnabled()) { // Validate config so that we don't get an NPE checkForProperty(properties, KEYTAB_PROPERTY_KEY); checkForProperty(properties, USERNAME_PROPERTY_KEY); try { // Login as whoever we're supposed to be and let the hostname be pulled // from localhost. If security isn't enabled, this does nothing. SecurityUtil.login(conf, properties.getString(KEYTAB_PROPERTY_KEY), properties.getString(USERNAME_PROPERTY_KEY)); } catch (IOException ex) { throw new MetricsException("Error logging in securely: [" + ex.toString() + "]", ex); } } }
/** * Throw a {@link MetricsException} if the given property is not set. * * @param conf the configuration to test * @param key the key to validate */ private static void checkForProperty(SubsetConfiguration conf, String key) { if (!conf.containsKey(key)) { throw new MetricsException("Configuration is missing " + key + " property"); } }