private void setAttributes() throws IOException { InputStream is = getClass().getResourceAsStream(PROPERTIES_FILE); if (is != null) { Properties properties = new Properties(); properties.load(is); //for (Object propertyNameObj : properties.keySet()) { Iterator it = properties.keySet().iterator(); while (it.hasNext()) { String propertyName = (String) it.next(); String propertyValue = properties.getProperty(propertyName); setAttribute(propertyName, propertyValue); } } }
/** * Returns an attribute-value map derived from the factory attributes * by finding all factory attributes that begin with * <i>contextName</i>.<i>tableName</i>. The returned map consists of * those attributes with the contextName and tableName stripped off. */ protected Map<String,String> getAttributeTable(String tableName) { String prefix = contextName + "." + tableName + "."; Map<String,String> result = new HashMap<String,String>(); for (String attributeName : factory.getAttributeNames()) { if (attributeName.startsWith(prefix)) { String name = attributeName.substring(prefix.length()); String value = (String) factory.getAttribute(attributeName); result.put(name, value); } } return result; }
/** * Returns the singleton ContextFactory instance, constructing it if * necessary. <p/> * * When the instance is constructed, this method checks if the file * <code>hadoop-metrics.properties</code> exists on the class path. If it * exists, it must be in the format defined by java.util.Properties, and all * the properties in the file are set as attributes on the newly created * ContextFactory instance. * * @return the singleton ContextFactory instance */ public static synchronized ContextFactory getFactory() throws IOException { if (theFactory == null) { theFactory = new ContextFactory(); theFactory.setAttributes(); } return theFactory; }
/** * Utility method to return the named context. * If the desired context cannot be created for any reason, the exception * is logged, and a null context is returned. */ public static MetricsContext getContext(String refName, String contextName) { MetricsContext metricsContext; try { metricsContext = ContextFactory.getFactory().getContext(refName, contextName); if (!metricsContext.isMonitoring()) { metricsContext.startMonitoring(); } } catch (Exception ex) { LOG.error("Unable to create metrics context " + contextName, ex); metricsContext = ContextFactory.getNullContext(contextName); } return metricsContext; }
public Metrics(Conf conf) throws IOException { ContextFactory contextFactory = ContextFactory.getFactory(); for (Conf attr : conf.getChild("hadoopMetricsAttributes").getChildren("attribute")) { contextFactory.setAttribute(attr.getAttribute("name"), attr.getAttribute(("value"))); } boolean enableJvmMetrics = conf.getChild("enableJvmMetrics").getValueAsBoolean(); if (enableJvmMetrics) { JvmMetrics.init("lily", "aLilySession"); } } }
@Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = new PrintWriter(response.getOutputStream()); String format = request.getParameter("format"); Collection<MetricsContext> allContexts = ContextFactory.getFactory().getAllContexts(); if ("json".equals(format)) { // Uses Jetty's built-in JSON support to convert the map into JSON. out.print(new JSON().toJSON(makeMap(allContexts))); } else { printMap(out, makeMap(allContexts)); } out.close(); }
/** * Initializes, for testing, two NoEmitMetricsContext's, and adds one value * to the first of them. */ public void setUp() throws IOException { nc1 = new NoEmitMetricsContext(); nc1.init("test1", ContextFactory.getFactory()); nc2 = new NoEmitMetricsContext(); nc2.init("test2", ContextFactory.getFactory()); contexts = new ArrayList<MetricsContext>(); contexts.add(nc1); contexts.add(nc2); MetricsRecord r = nc1.createRecord("testRecord"); r.setTag("testTag1", "testTagValue1"); r.setTag("testTag2", "testTagValue2"); r.setMetric("testMetric1", 1); r.setMetric("testMetric2", 33); r.update(); Map<String, Collection<OutputRecord>> m = nc1.getAllRecords(); assertEquals(1, m.size()); assertEquals(1, m.values().size()); Collection<OutputRecord> outputRecords = m.values().iterator().next(); assertEquals(1, outputRecords.size()); outputRecord = outputRecords.iterator().next(); }
/** * Convenience method for subclasses to access factory attributes. */ protected String getAttribute(String attributeName) { String factoryAttribute = contextName + "." + attributeName; return (String) factory.getAttribute(factoryAttribute); }
public RegionServerMetrics() { MetricsContext context = MetricsUtil.getContext("hbase"); metricsRecord = MetricsUtil.createRecord(context, "regionserver"); String name = Thread.currentThread().getName(); metricsRecord.setTag("RegionServer", name); context.registerUpdater(this); // Add jvmmetrics. JvmMetrics.init("RegionServer", name); // Add Hbase Info metrics HBaseInfo.init(); // export for JMX statistics = new RegionServerStatistics(this.registry, name); // get custom attributes try { Object m = ContextFactory.getFactory().getAttribute("hbase.extendedperiod"); if (m instanceof String) { this.extendedPeriod = Long.parseLong((String) m)*1000; } } catch (IOException ioe) { LOG.info("Couldn't load ContextFactory for Metrics config info"); } LOG.info("Initialized"); }
public synchronized MetricsContext getContext(String contextName) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException { return getContext(contextName, contextName); }
/** * Utility method to return the named context. * If the desired context cannot be created for any reason, the exception * is logged, and a null context is returned. */ public static MetricsContext getContext(String contextName) { MetricsContext metricsContext; try { metricsContext = ContextFactory.getFactory().getContext(contextName); if (!metricsContext.isMonitoring()) { metricsContext.startMonitoring(); } } catch (Exception ex) { LOG.error("Unable to create metrics context " + contextName, ex); metricsContext = ContextFactory.getNullContext(contextName); } return metricsContext; }
@Test public void testShouldSetMulticastSocketTtl() throws Exception { GangliaContext context = new GangliaContext(); ContextFactory factory = ContextFactory.getFactory(); factory.setAttribute("gangliaContext.multicast", "true"); factory.setAttribute("gangliaContext.multicast.ttl", "10"); context.init("gangliaContext", factory); MulticastSocket multicastSocket = (MulticastSocket) context.datagramSocket; assertEquals("Did not set TTL", multicastSocket.getTimeToLive(), 10); }
@Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if (!HttpServer2.isInstrumentationAccessAllowed(getServletContext(), request, response)) { return; } String format = request.getParameter("format"); Collection<MetricsContext> allContexts = ContextFactory.getFactory().getAllContexts(); if ("json".equals(format)) { response.setContentType("application/json; charset=utf-8"); PrintWriter out = response.getWriter(); try { // Uses Jetty's built-in JSON support to convert the map into JSON. out.print(new JSON().toJSON(makeMap(allContexts))); } finally { out.close(); } } else { PrintWriter out = response.getWriter(); try { printMap(out, makeMap(allContexts)); } finally { out.close(); } } }
@Test public void testCloseShouldCloseTheSocketWhichIsCreatedByInit() throws Exception { AbstractMetricsContext context=new GangliaContext(); context.init("gangliaContext", ContextFactory.getFactory()); GangliaContext gangliaContext =(GangliaContext) context; assertFalse("Socket already closed",gangliaContext.datagramSocket.isClosed()); context.close(); assertTrue("Socket not closed",gangliaContext.datagramSocket.isClosed()); } }
/** * Convenience method for subclasses to access factory attributes. */ protected String getAttribute(String attributeName) { String factoryAttribute = contextName + "." + attributeName; return (String) factory.getAttribute(factoryAttribute); }
public MasterMetrics(final String name) { MetricsContext context = MetricsUtil.getContext("hbase"); metricsRecord = MetricsUtil.createRecord(context, "master"); metricsRecord.setTag("Master", name); context.registerUpdater(this); JvmMetrics.init("Master", name); HBaseInfo.init(); // expose the MBean for metrics masterStatistics = new MasterStatistics(this.registry); // get custom attributes try { Object m = ContextFactory.getFactory().getAttribute("hbase.extendedperiod"); if (m instanceof String) { this.extendedPeriod = Long.parseLong((String) m)*1000; } } catch (IOException ioe) { LOG.info("Couldn't load ContextFactory for Metrics config info"); } LOG.info("Initialized"); }
public synchronized MetricsContext getContext(String contextName) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException { return getContext(contextName, contextName); }
/** * Utility method to return the named context. * If the desired context cannot be created for any reason, the exception * is logged, and a null context is returned. */ public static MetricsContext getContext(String refName, String contextName) { MetricsContext metricsContext; try { metricsContext = ContextFactory.getFactory().getContext(refName, contextName); if (!metricsContext.isMonitoring()) { metricsContext.startMonitoring(); } } catch (Exception ex) { LOG.error("Unable to create metrics context " + contextName, ex); metricsContext = ContextFactory.getNullContext(contextName); } return metricsContext; }
@Test public void testShouldSetMulticastSocketTtl() throws Exception { GangliaContext context = new GangliaContext(); ContextFactory factory = ContextFactory.getFactory(); factory.setAttribute("gangliaContext.multicast", "true"); factory.setAttribute("gangliaContext.multicast.ttl", "10"); context.init("gangliaContext", factory); MulticastSocket multicastSocket = (MulticastSocket) context.datagramSocket; assertEquals("Did not set TTL", multicastSocket.getTimeToLive(), 10); }
/** * Returns an attribute-value map derived from the factory attributes * by finding all factory attributes that begin with * <i>contextName</i>.<i>tableName</i>. The returned map consists of * those attributes with the contextName and tableName stripped off. */ protected Map<String,String> getAttributeTable(String tableName) { String prefix = contextName + "." + tableName + "."; Map<String,String> result = new HashMap<String,String>(); for (String attributeName : factory.getAttributeNames()) { if (attributeName.startsWith(prefix)) { String name = attributeName.substring(prefix.length()); String value = (String) factory.getAttribute(attributeName); result.put(name, value); } } return result; }