def setUp(self):
        """Set up a Downsampler, aggregating with the sum and average function."""
        capacity_precisions = (self.CAPACITY, self.PRECISION, self.CAPACITY,
                               self.PRECISION**2)
        retention_string = "%d*%ds:%d*%ds" % (capacity_precisions)
        retention = bg_accessor.Retention.from_string(retention_string)
        self.stage_0 = retention.stages[0]
        self.stage_1 = retention.stages[1]
        uid = uuid.uuid4()
        metric_metadata = bg_accessor.MetricMetadata(
            aggregator=bg_accessor.Aggregator.total, retention=retention)
        self.metric_sum = bg_accessor.Metric(self.METRIC_NAME_SUM, uid,
                                             metric_metadata)

        uid = uuid.uuid4()
        metric_metadata = bg_accessor.MetricMetadata(
            aggregator=bg_accessor.Aggregator.average, retention=retention)
        self.metric_avg = bg_accessor.Metric(self.METRIC_NAME_AVG, uid,
                                             metric_metadata)
        self.ds = bg_ds.Downsampler(self.CAPACITY)
示例#2
0
 def make_metric(self, name, metadata):
     """See bg_accessor.Accessor."""
     # Cleanup name (avoid double dots)
     name = ".".join(self._components_from_name(name))
     uid = uuid.uuid5(self._UUID_NAMESPACE, name)
     now = datetime.datetime.now()
     return bg_accessor.Metric(
         name,
         uid,
         metadata,
         created_on=now,
         updated_on=now,
         read_on=None,
     )
示例#3
0
    def _cache_get(self, metric_name):
        """Return a Metric from a the cache, None if no such metric."""
        encoded_metric_name = self._encode(metric_name)
        with self.__env.begin(self.__metric_to_metadata_db,
                              write=False) as txn:
            payload = txn.get(encoded_metric_name)

        if payload == self._EMPTY:
            return None, True

        if payload is not None:
            payload = self._decode(payload)

        if not payload:
            # cache miss
            return None, False

        # found something in the cache
        split = self.__split_payload(payload)

        if split is None:
            # invalid string => evict from cache
            with self.__env.begin(self.__metric_to_metadata_db,
                                  write=True) as txn:
                txn.delete(key=encoded_metric_name)
            return None, False

        # valid value => get id and metadata string
        # TODO: optimization: id is a UUID (known length)
        id_str, metadata_str, timestamp = split
        try:
            id = uuid.UUID(id_str)
        except Exception as e:
            logging.debug(str(e))
            with self.__env.begin(self.__metric_to_metadata_db,
                                  write=True) as txn:
                txn.delete(key=encoded_metric_name)
            return None, False

        # if the timestamp expired evict it in order to force
        # its recreation for the next time
        if self.__expired_timestamp(timestamp):
            with self.__env.begin(self.__metric_to_metadata_db,
                                  write=True) as txn:
                txn.delete(key=encoded_metric_name)

        metadata = self.metadata_from_str(metadata_str)
        return bg_accessor.Metric(metric_name, id, metadata), True
示例#4
0
def make_metric(name, metadata=None, accessor=None, **kwargs):
    """Create a bg_accessor.Metric with specified metadata."""
    encoded_name = bg_accessor.encode_metric_name(name)
    retention = kwargs.get("retention")
    if isinstance(retention, basestring):
        kwargs["retention"] = bg_accessor.Retention.from_string(retention)
    if metadata:
        assert isinstance(metadata, bg_accessor.MetricMetadata)
        assert not kwargs
    else:
        metadata = bg_accessor.MetricMetadata(**kwargs)
    if not accessor:
        uid = uuid.uuid5(_UUID_NAMESPACE, encoded_name)
        return bg_accessor.Metric(name, uid, metadata)
    else:
        return accessor.make_metric(name, metadata)
示例#5
0
 def make_metric(self, name, metadata):
     """See bg_accessor.Accessor."""
     # Cleanup name (avoid double dots)
     name = ".".join(self._components_from_name(name))
     uid = uuid.uuid5(self._UUID_NAMESPACE, name)
     return bg_accessor.Metric(name, uid, metadata)
示例#6
0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest

from mock import Mock

from biggraphite import accessor as bg_accessor
from biggraphite.drivers import hybrid

DEFAULT_METRIC_NAME = "foo.bar"
DEFAULT_METADATA = bg_accessor.MetricMetadata()
DEFAULT_METRIC = bg_accessor.Metric(DEFAULT_METRIC_NAME, "id",
                                    DEFAULT_METADATA)

DEFAULT_GLOB = "foo.bar.**"


class TestHybridAccessor(unittest.TestCase):
    def setUp(self):
        self._metadata_accessor = Mock()
        self._data_accessor = Mock()
        self._accessor = hybrid.HybridAccessor("test_hybrid",
                                               self._metadata_accessor,
                                               self._data_accessor)

    def test_connect_should_be_called_on_both_accessors(self):
        self._accessor.connect()