org.apache.hadoop.metrics.spi
Class AbstractMetricsContext

java.lang.Object
  extended by org.apache.hadoop.metrics.spi.AbstractMetricsContext
All Implemented Interfaces:
org.apache.hadoop.metrics.MetricsContext
Direct Known Subclasses:
CompositeContext, FileContext, GangliaContext, NoEmitMetricsContext, NullContext, NullContextWithUpdateThread

@InterfaceAudience.Public
@InterfaceStability.Evolving
public abstract class AbstractMetricsContext
extends Object
implements org.apache.hadoop.metrics.MetricsContext

The main class of the Service Provider Interface. This class should be extended in order to integrate the Metrics API with a specific metrics client library.

This class implements the internal table of metric data, and the timer on which data is to be sent to the metrics system. Subclasses must override the abstract emitRecord method in order to transmit the data.


Field Summary
 
Fields inherited from interface org.apache.hadoop.metrics.MetricsContext
DEFAULT_PERIOD
 
Constructor Summary
protected AbstractMetricsContext()
          Creates a new instance of AbstractMetricsContext
 
Method Summary
 void close()
          Stops monitoring and frees buffered data, returning this object to its initial state.
 org.apache.hadoop.metrics.MetricsRecord createRecord(String recordName)
          Creates a new AbstractMetricsRecord instance with the given recordName.
protected abstract  void emitRecord(String contextName, String recordName, OutputRecord outRec)
          Sends a record to the metrics system.
protected  void flush()
          Called each period after all records have been emitted, this method does nothing.
 Map<String,Collection<OutputRecord>> getAllRecords()
          Retrieves all the records managed by this MetricsContext.
protected  String getAttribute(String attributeName)
          Convenience method for subclasses to access factory attributes.
protected  Map<String,String> getAttributeTable(String tableName)
          Returns an attribute-value map derived from the factory attributes by finding all factory attributes that begin with contextName.tableName.
 org.apache.hadoop.metrics.ContextFactory getContextFactory()
          Returns the factory by which this context was created.
 String getContextName()
          Returns the context name.
 int getPeriod()
          Returns the timer period.
 void init(String contextName, org.apache.hadoop.metrics.ContextFactory factory)
          Initializes the context.
 boolean isMonitoring()
          Returns true if monitoring is currently in progress.
protected  org.apache.hadoop.metrics.MetricsRecord newRecord(String recordName)
          Subclasses should override this if they subclass MetricsRecordImpl.
protected  void parseAndSetPeriod(String attributeName)
          If a period is set in the attribute passed in, override the default with it.
 void registerUpdater(org.apache.hadoop.metrics.Updater updater)
          Registers a callback to be called at time intervals determined by the configuration.
protected  void remove(MetricsRecordImpl record)
          Called by MetricsRecordImpl.remove().
protected  void setPeriod(int period)
          Sets the timer period
 void startMonitoring()
          Starts or restarts monitoring, the emitting of metrics records.
 void stopMonitoring()
          Stops monitoring.
 void unregisterUpdater(org.apache.hadoop.metrics.Updater updater)
          Removes a callback, if it exists.
protected  void update(MetricsRecordImpl record)
          Called by MetricsRecordImpl.update().
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractMetricsContext

protected AbstractMetricsContext()
Creates a new instance of AbstractMetricsContext

Method Detail

init

public void init(String contextName,
                 org.apache.hadoop.metrics.ContextFactory factory)
Initializes the context.

Specified by:
init in interface org.apache.hadoop.metrics.MetricsContext
Parameters:
contextName - The given name for this context
factory - The creator of this context

getAttribute

protected String getAttribute(String attributeName)
Convenience method for subclasses to access factory attributes.


getAttributeTable

protected Map<String,String> getAttributeTable(String tableName)
Returns an attribute-value map derived from the factory attributes by finding all factory attributes that begin with contextName.tableName. The returned map consists of those attributes with the contextName and tableName stripped off.


getContextName

public String getContextName()
Returns the context name.

Specified by:
getContextName in interface org.apache.hadoop.metrics.MetricsContext
Returns:
the context name

getContextFactory

public org.apache.hadoop.metrics.ContextFactory getContextFactory()
Returns the factory by which this context was created.


startMonitoring

public void startMonitoring()
                     throws IOException
Starts or restarts monitoring, the emitting of metrics records.

Specified by:
startMonitoring in interface org.apache.hadoop.metrics.MetricsContext
Throws:
IOException

stopMonitoring

public void stopMonitoring()
Stops monitoring. This does not free buffered data.

Specified by:
stopMonitoring in interface org.apache.hadoop.metrics.MetricsContext
See Also:
close()

isMonitoring

public boolean isMonitoring()
Returns true if monitoring is currently in progress.

Specified by:
isMonitoring in interface org.apache.hadoop.metrics.MetricsContext

close

public void close()
Stops monitoring and frees buffered data, returning this object to its initial state.

Specified by:
close in interface org.apache.hadoop.metrics.MetricsContext

createRecord

public final org.apache.hadoop.metrics.MetricsRecord createRecord(String recordName)
Creates a new AbstractMetricsRecord instance with the given recordName. Throws an exception if the metrics implementation is configured with a fixed set of record names and recordName is not in that set.

Specified by:
createRecord in interface org.apache.hadoop.metrics.MetricsContext
Parameters:
recordName - the name of the record
Throws:
org.apache.hadoop.metrics.MetricsException - if recordName conflicts with configuration data

newRecord

protected org.apache.hadoop.metrics.MetricsRecord newRecord(String recordName)
Subclasses should override this if they subclass MetricsRecordImpl.

Parameters:
recordName - the name of the record
Returns:
newly created instance of MetricsRecordImpl or subclass

registerUpdater

public void registerUpdater(org.apache.hadoop.metrics.Updater updater)
Registers a callback to be called at time intervals determined by the configuration.

Specified by:
registerUpdater in interface org.apache.hadoop.metrics.MetricsContext
Parameters:
updater - object to be run periodically; it should update some metrics records

unregisterUpdater

public void unregisterUpdater(org.apache.hadoop.metrics.Updater updater)
Removes a callback, if it exists.

Specified by:
unregisterUpdater in interface org.apache.hadoop.metrics.MetricsContext
Parameters:
updater - object to be removed from the callback list

getAllRecords

public Map<String,Collection<OutputRecord>> getAllRecords()
Retrieves all the records managed by this MetricsContext. Useful for monitoring systems that are polling-based.

Specified by:
getAllRecords in interface org.apache.hadoop.metrics.MetricsContext
Returns:
A non-null collection of all monitoring records.

emitRecord

protected abstract void emitRecord(String contextName,
                                   String recordName,
                                   OutputRecord outRec)
                            throws IOException
Sends a record to the metrics system.

Throws:
IOException

flush

protected void flush()
              throws IOException
Called each period after all records have been emitted, this method does nothing. Subclasses may override it in order to perform some kind of flush.

Throws:
IOException

update

protected void update(MetricsRecordImpl record)
Called by MetricsRecordImpl.update(). Creates or updates a row in the internal table of metric data.


remove

protected void remove(MetricsRecordImpl record)
Called by MetricsRecordImpl.remove(). Removes all matching rows in the internal table of metric data. A row matches if it has the same tag names and values as record, but it may also have additional tags.


getPeriod

public int getPeriod()
Returns the timer period.

Specified by:
getPeriod in interface org.apache.hadoop.metrics.MetricsContext

setPeriod

protected void setPeriod(int period)
Sets the timer period


parseAndSetPeriod

protected void parseAndSetPeriod(String attributeName)
If a period is set in the attribute passed in, override the default with it.



Copyright © 2014 Apache Software Foundation. All Rights Reserved.