def test_compound_metrics():
    metrics1 = Metrics()
    metrics2 = Metrics()

    metrics1.register(Label('value', 'first'))
    metrics2.register(Label('value', 'second'))
    assert CompoundMetrics(metrics1, metrics2).sample() == {'value': 'second'}

    metrics1.register(Label('other', 'third'))
    assert CompoundMetrics(metrics1, metrics2).sample() == {
        'value': 'second',
        'other': 'third'
    }
Beispiel #2
0
def test_basic_scoping():
    lb = Label('ping', 'pong')
    rm = RootMetrics()
    rm.register(lb)
    rm.scope('bing').register(lb)
    assert rm.sample() == {'ping': 'pong', 'bing.ping': 'pong'}
    rm.clear()
Beispiel #3
0
def test_basic_registration_and_clear():
    lb = Label('ping', 'pong')
    rm = RootMetrics()
    rm.register(lb)
    assert rm.sample() == {'ping': 'pong'}
    rm.clear()
    assert rm.sample() == {}
Beispiel #4
0
def test_named_gauge_types():
  with pytest.raises(TypeError):
    ag = AtomicGauge(0)
  with pytest.raises(TypeError):
    ag = AtomicGauge(None)
  with pytest.raises(TypeError):
    lb = Label(None, 3)
  with pytest.raises(TypeError):
    mg = MutatorGauge({})
Beispiel #5
0
def register_build_properties(metrics):
    if not HAS_PEX:
        return
    rm = metrics.scope('build')
    try:
        build_properties = PEX().info.build_properties
    except PEX.NotFound:
        return
    for key, value in build_properties.items():
        rm.register(Label(str(key), str(value)))
Beispiel #6
0
def register_diagnostics(metrics):
    rm = metrics.scope('sys')
    now = time.time()
    rm.register(LambdaGauge('uptime', lambda: time.time() - now))
    rm.register(Label('argv', repr(sys.argv)))
    rm.register(Label('path', repr(sys.path)))
    rm.register(Label('version', sys.version))
    rm.register(Label('platform', sys.platform))
    rm.register(Label('executable', sys.executable))
    rm.register(Label('prefix', sys.prefix))
    rm.register(Label('exec_prefix', sys.exec_prefix))
    rm.register(Label('uname', ' '.join(os.uname())))
def test_metric_sample():
    metrics = Metrics()
    sampler = MetricSampler(metrics)
    assert sampler.sample() == {}
    sampler.iterate()
    assert sampler.sample() == {}
    metrics.register(Label('herp', 'derp'))
    assert sampler.sample() == {}
    sampler.iterate()
    assert sampler.sample() == {'herp': 'derp'}
Beispiel #8
0
def register_diagnostics():
    import os, sys, time
    rm = RootMetrics().scope('sys')
    now = time.time()
    rm.register(LambdaGauge('uptime', lambda: time.time() - now))
    rm.register(Label('argv', repr(sys.argv)))
    rm.register(Label('path', repr(sys.path)))
    rm.register(LambdaGauge('modules', lambda: ', '.join(sys.modules.keys())))
    rm.register(Label('version', sys.version))
    rm.register(Label('platform', sys.platform))
    rm.register(Label('executable', sys.executable))
    rm.register(Label('prefix', sys.prefix))
    rm.register(Label('exec_prefix', sys.exec_prefix))
    rm.register(Label('uname', ' '.join(os.uname())))
Beispiel #9
0
def test_nontrivial_gauges():
    for label_value in [
            'a', 0, 2.5, [1, 2, "3"], {
                'a': 'b'
            }, {
                'c': None
            }, False
    ]:
        lb = Label('ping', label_value)
        rm = RootMetrics()
        rm.register(lb)
        assert rm.sample() == {'ping': label_value}
        rm.clear()
        assert rm.sample() == {}
def test_metric_read_write():
    metrics = Metrics()

    with temporary_file() as fp:
        os.unlink(fp.name)

        writer = DiskMetricWriter(metrics, fp.name)
        reader = DiskMetricReader(fp.name)

        assert reader.sample() == {}
        reader.iterate()
        assert reader.sample() == {}

        writer.iterate()
        assert reader.sample() == {}
        reader.iterate()
        assert reader.sample() == {}

        metrics.register(Label('herp', 'derp'))
        writer.iterate()
        assert reader.sample() == {}
        reader.iterate()
        assert reader.sample() == {'herp': 'derp'}
Beispiel #11
0
def test_basic_immutable_gauge():
  lb = Label('a', 'b')
  assert lb.name() == 'a', 'name should be properly initialized'
  assert lb.read() == 'b', 'label should be properly set'

  # should handle a variety of types
  lb = Label('a', None)
  assert lb.read() == None

  lb = Label('a', [1,2,3])
  assert lb.read() == [1,2,3]

  lb = Label('b', {})
  assert lb.read() == {}
 def __init__(self):
     self.metrics.register(Label('value', 'derp value'))