Class AbstractMetricsContext
- java.lang.Object
-
- com.mapr.org.apache.hadoop.metrics.spi.AbstractMetricsContext
-
- All Implemented Interfaces:
MetricsContext
- Direct Known Subclasses:
GangliaContext,NullContext
@Public @Evolving public abstract class AbstractMetricsContext extends java.lang.Object implements 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 abstractemitRecordmethod in order to transmit the data.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classAbstractMetricsContext.MetricMapstatic classAbstractMetricsContext.TagMap
-
Field Summary
-
Fields inherited from interface com.mapr.org.apache.hadoop.metrics.MetricsContext
DEFAULT_PERIOD
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractMetricsContext()Creates a new instance of AbstractMetricsContext
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidclose()Stops monitoring and frees buffered data, returning this object to its initial state.MetricsRecordcreateRecord(java.lang.String recordName)Creates a new AbstractMetricsRecord instance with the givenrecordName.protected abstract voidemitRecord(java.lang.String contextName, java.lang.String recordName, OutputRecord outRec)Sends a record to the metrics system.protected voidflush()Called each period after all records have been emitted, this method does nothing.java.util.Map<java.lang.String,java.util.Collection<OutputRecord>>getAllRecords()Retrieves all the records managed by this MetricsContext.protected java.lang.StringgetAttribute(java.lang.String attributeName)Convenience method for subclasses to access factory attributes.protected java.util.Map<java.lang.String,java.lang.String>getAttributeTable(java.lang.String tableName)Returns an attribute-value map derived from the factory attributes by finding all factory attributes that begin with contextName.tableName.ContextFactorygetContextFactory()Returns the factory by which this context was created.java.lang.StringgetContextName()Returns the context name.intgetPeriod()Returns the timer period.voidinit(java.lang.String contextName, ContextFactory factory)Initializes the context.booleanisMonitoring()Returns true if monitoring is currently in progress.protected MetricsRecordnewRecord(java.lang.String recordName)Subclasses should override this if they subclass MetricsRecordImpl.protected voidparseAndSetPeriod(java.lang.String attributeName)If a period is set in the attribute passed in, override the default with it.voidregisterUpdater(Updater updater)Registers a callback to be called at time intervals determined by the configuration.protected voidremove(MetricsRecordImpl record)Called by MetricsRecordImpl.remove().protected voidsetPeriod(int period)Sets the timer periodvoidstartMonitoring()Starts or restarts monitoring, the emitting of metrics records.voidstopMonitoring()Stops monitoring.voidunregisterUpdater(Updater updater)Removes a callback, if it exists.protected voidupdate(MetricsRecordImpl record)Called by MetricsRecordImpl.update().
-
-
-
Method Detail
-
init
public void init(java.lang.String contextName, ContextFactory factory)Initializes the context.- Specified by:
initin interfaceMetricsContext- Parameters:
contextName- The given name for this contextfactory- The creator of this context
-
getAttribute
protected java.lang.String getAttribute(java.lang.String attributeName)
Convenience method for subclasses to access factory attributes.
-
getAttributeTable
protected java.util.Map<java.lang.String,java.lang.String> getAttributeTable(java.lang.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 java.lang.String getContextName()
Returns the context name.- Specified by:
getContextNamein interfaceMetricsContext- Returns:
- the context name
-
getContextFactory
public ContextFactory getContextFactory()
Returns the factory by which this context was created.
-
startMonitoring
public void startMonitoring() throws java.io.IOExceptionStarts or restarts monitoring, the emitting of metrics records.- Specified by:
startMonitoringin interfaceMetricsContext- Throws:
java.io.IOException
-
stopMonitoring
public void stopMonitoring()
Stops monitoring. This does not free buffered data.- Specified by:
stopMonitoringin interfaceMetricsContext- See Also:
close()
-
isMonitoring
public boolean isMonitoring()
Returns true if monitoring is currently in progress.- Specified by:
isMonitoringin interfaceMetricsContext
-
close
public void close()
Stops monitoring and frees buffered data, returning this object to its initial state.- Specified by:
closein interfaceMetricsContext
-
createRecord
public final MetricsRecord createRecord(java.lang.String recordName)
Creates a new AbstractMetricsRecord instance with the givenrecordName. Throws an exception if the metrics implementation is configured with a fixed set of record names andrecordNameis not in that set.- Specified by:
createRecordin interfaceMetricsContext- Parameters:
recordName- the name of the record- Throws:
MetricsException- if recordName conflicts with configuration data
-
newRecord
protected MetricsRecord newRecord(java.lang.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(Updater updater)
Registers a callback to be called at time intervals determined by the configuration.- Specified by:
registerUpdaterin interfaceMetricsContext- Parameters:
updater- object to be run periodically; it should update some metrics records
-
unregisterUpdater
public void unregisterUpdater(Updater updater)
Removes a callback, if it exists.- Specified by:
unregisterUpdaterin interfaceMetricsContext- Parameters:
updater- object to be removed from the callback list
-
getAllRecords
public java.util.Map<java.lang.String,java.util.Collection<OutputRecord>> getAllRecords()
Retrieves all the records managed by this MetricsContext. Useful for monitoring systems that are polling-based.- Specified by:
getAllRecordsin interfaceMetricsContext- Returns:
- A non-null collection of all monitoring records.
-
emitRecord
protected abstract void emitRecord(java.lang.String contextName, java.lang.String recordName, OutputRecord outRec) throws java.io.IOExceptionSends a record to the metrics system.- Throws:
java.io.IOException
-
flush
protected void flush() throws java.io.IOExceptionCalled 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:
java.io.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:
getPeriodin interfaceMetricsContext
-
setPeriod
protected void setPeriod(int period)
Sets the timer period
-
parseAndSetPeriod
protected void parseAndSetPeriod(java.lang.String attributeName)
If a period is set in the attribute passed in, override the default with it.
-
-