Esempio n. 1
0
def test_stream_creates_alert_for_red_check():
    sample_datapoint = yaml.load('''
        date: 1511346030123
        label:
            k1: v1
            k2: v2
        snapshot:
            foo: bar
            response:
                __value: 200
                __check:
                    state: red
    ''')
    m = Model()
    m.add_datapoint(**sample_datapoint)
    stream, = m.streams.get_all()
    assert stream.get_current_checks() == (
        1511346030123, {('response',): {'state': 'red'}})
    alert, = stream.get_current_check_alerts()
    assert alert == {
        'type': 'check_alert',
        'id': alert['id'],
        'stream_label': {'k1': 'v1', 'k2': 'v2'},
        'path': ('response',),
        'initial_state': 'red',
        'current_state': 'red',
        'start_date': 1511346030123,
        'end_date': None,
    }
def test_custom_check_with_alert():
    sample_datapoint = yaml.load('''
        date: 1511527495508
        label:
            agent: web_check
            host: wch1.example.com
        snapshot:
            by_endpoint:
                homepage_cs:
                    url: https://example.com/cs/
                    duration: 0.978
    ''')
    m = Model()
    ch = m.add_custom_check(
        label_filter={'agent': 'web_check'},
        path_filter=('by_endpoint', '*', 'duration'),
        red_value_filter={'$gte': 0.5})
    m.add_datapoint(**sample_datapoint)
    alert, = ch.get_current_alerts()
    assert alert == {
        'id': alert['id'],
        'stream_id': alert['stream_id'],
        'stream_label': {'agent': 'web_check', 'host': 'wch1.example.com'},
        'path': ('by_endpoint', 'homepage_cs', 'duration'),
        'initial_value': 0.978,
        'current_value': 0.978,
        'start_date': 1511527495508,
        'end_date': None,
    }
    assert ch.get_all_alerts() == [alert]
Esempio n. 3
0
def test_stream_creates_alert_for_expired_watchdog():
    sample_datapoint = yaml.load('''
        date: 1511346030123
        label:
            k1: v1
            k2: v2
        snapshot:
            foo: bar
            watchdog:
                __watchdog:
                    deadline: 1511346039000
    ''')
    m = Model()
    m.add_datapoint(**sample_datapoint)
    stream, = m.streams.get_all()
    assert stream.get_current_watchdogs() == (
        1511346030123, {('watchdog',): {'deadline': 1511346039000}})
    assert stream.get_current_watchdog_alerts() == []
    m.check_watchdogs(1511346038000)
    assert stream.get_current_watchdog_alerts() == []
    m.check_watchdogs(1511346039500)
    alert, = stream.get_current_watchdog_alerts()
    assert alert == {
        'deadline': 1511346039000,
        'end_date': None,
        'id': alert['id'],
        'path': ('watchdog',),
        'start_date': 1511346039500,
        'stream_label': {'k1': 'v1', 'k2': 'v2'},
        'type': 'watchdog_alert',
    }
    assert stream.get_all_watchdog_alerts() == [alert]
Esempio n. 4
0
def test_add_datapoint_and_check_stream_attributes():
    sample_datapoint = yaml.load('''
        date: 1511346030123
        label:
            k1: v1
            k2: v2
        snapshot:
            foo: bar
            response:
                __value: 200
                __check:
                    state: green
            watchdog:
                __watchdog:
                    deadline: 1511346090000
    ''')
    m = Model()
    m.add_datapoint(**sample_datapoint)
    stream, = m.streams.get_all()
    assert stream.id
    assert stream.label == {'k1': 'v1', 'k2': 'v2'}
    assert stream.dates == {1511346030123}
    assert stream.history_items == {
        ('foo',): {
            'value_history': {1511346030123: 'bar'},
        },
        ('response',): {
            'check_history': {1511346030123: {'state': 'green'}},
            'value_history': {1511346030123: 200}},
        ('watchdog',): {
            'watchdog_history': {1511346030123: {'deadline': 1511346090000}},
        },
    }
    assert stream.get_current_datapoint() == (
        1511346030123, {
            ('foo',): {'value': 'bar'},
            ('response',): {'check': {'state': 'green'}, 'value': 200},
            ('watchdog',): {'watchdog': {'deadline': 1511346090000}},
        })
    assert stream.get_current_checks() == (
        1511346030123, {('response',): {'state': 'green'}})
    assert stream.get_current_watchdogs() == (
        1511346030123, {('watchdog',): {'deadline': 1511346090000}})
def test_custom_check_with_no_alert():
    sample_datapoint = yaml.load('''
        date: 1511527495508
        label:
            agent: web_check
            host: wch1.example.com
        snapshot:
            by_endpoint:
                homepage_cs:
                    url: https://example.com/cs/
                    duration: 0.180
    ''')
    m = Model()
    ch = m.custom_checks.add_custom_check(
        label_filter={'agent': 'web_check'},
        path_filter=('by_endpoint', '*', 'duration'),
        red_value_filter={'$gte': 0.5})
    m.add_datapoint(**sample_datapoint)
    assert ch.get_current_alerts() == []
    assert ch.get_all_alerts() == []
Esempio n. 6
0
def test_stream_resolves_watchdog_alert():
    sample_datapoint = yaml.load('''
        date: 1511346030123
        label:
            k1: v1
            k2: v2
        snapshot:
            foo: bar
            watchdog:
                __watchdog:
                    deadline: 1511346039000
    ''')
    m = Model()
    m.add_datapoint(**sample_datapoint)
    stream, = m.streams.get_all()
    m.check_watchdogs(1511346039500)
    assert len(stream.get_current_watchdog_alerts()) == 1
    sample_datapoint['date'] = 1511346040000
    sample_datapoint['snapshot']['watchdog']['__watchdog']['deadline'] = 1511346090000
    m.add_datapoint(**sample_datapoint)
    m.check_watchdogs(1511346050000)
    assert stream.get_current_watchdog_alerts() == []
    alert, = stream.get_all_watchdog_alerts()
Esempio n. 7
0
def test_stream_resolves_check_alert():
    sample_datapoint = yaml.load('''
        date: 1511346030123
        label:
            k1: v1
            k2: v2
        snapshot:
            foo: bar
            response:
                __value: 200
                __check:
                    state: red
    ''')
    m = Model()
    m.add_datapoint(**sample_datapoint)
    stream, = m.streams.get_all()
    assert len(stream.get_current_check_alerts()) == 1
    sample_datapoint['date'] += 5000
    sample_datapoint['snapshot']['response']['__check']['state'] = 'green'
    m.add_datapoint(**sample_datapoint)
    assert stream.get_current_check_alerts() == []
    alert, = stream.get_all_check_alerts()
    assert alert['end_date'] == 1511346030123 + 5000
Esempio n. 8
0
def test_add_datapoint_and_serialize():
    skip()
    sample_datapoint = yaml.load('''
        date: 1511346030123
        label:
            k1: v1
            k2: v2
        snapshot:
            foo: bar
            response:
                __value: 200
                __check:
                    state: green
            watchdog:
                __watchdog:
                    deadline: 1511346090000
    ''')
    m = Model()
    m.add_datapoint(**sample_datapoint)
    m_s = m.serialize()
    print(yaml.dump(m_s))
    m2 = Model.deserialize(m_s)
    m2_s = m2.serialize()
    assert m_s == m2_s
Esempio n. 9
0
async def model(db):
    from overwatch_hub.model import Model
    async with Model(db=db, create_optional_indexes=False) as model:
        yield model
def test_custom_check_with_multiple_alerts():
    sample_datapoints = yaml.load('''
      - date: 1511527495508
        label:
            agent: web_check
            host: wch1.example.com
        snapshot:
            by_endpoint:
                homepage_cs:
                    url: https://example.com/cs/
                    duration: 0.978
                homepage_en:
                    url: https://example.com/en/
                    duration: 0.175
      - date: 1511527495678
        label:
            agent: web_check
            host: wch2.example.com
        snapshot:
            by_endpoint:
                homepage_cs:
                    url: https://example.com/cs/
                    duration: 0.810
                homepage_en:
                    url: https://example.com/en/
                    duration: 0.820
    ''')
    m = Model()
    ch = m.custom_checks.add_custom_check(
        label_filter={'agent': 'web_check'},
        path_filter=('by_endpoint', '*', 'duration'),
        red_value_filter={'$gte': 0.5})
    for dp in sample_datapoints:
        m.add_datapoint(**dp)
    alerts = ch.get_current_alerts()
    assert len(ch.get_all_alerts()) == len(alerts)
    #assert ch.get_all_alerts() == alerts
    assert len(alerts) == 3
    alerts.sort(key=lambda alert: (alert['start_date'], alert['path']))
    assert alerts == [
        {
            'id': alerts[0]['id'],
            'stream_id': alerts[0]['stream_id'],
            'stream_label': {'agent': 'web_check', 'host': 'wch1.example.com'},
            'path': ('by_endpoint', 'homepage_cs', 'duration'),
            'initial_value': 0.978,
            'current_value': 0.978,
            'start_date': 1511527495508,
            'end_date': None
        }, {
            'id': alerts[1]['id'],
            'stream_id': alerts[1]['stream_id'],
            'stream_label': {'agent': 'web_check', 'host': 'wch2.example.com'},
            'path': ('by_endpoint', 'homepage_cs', 'duration'),
            'initial_value': 0.81,
            'current_value': 0.81,
            'start_date': 1511527495678,
            'end_date': None
        }, {
            'id': alerts[2]['id'],
            'stream_id': alerts[2]['stream_id'],
            'stream_label': {'agent': 'web_check', 'host': 'wch2.example.com'},
            'path': ('by_endpoint', 'homepage_en', 'duration'),
            'initial_value': 0.82,
            'current_value': 0.82,
            'start_date': 1511527495678,
            'end_date': None
        }]