def test_detector_with_assign_combinator(): """ We should correctly generate a detector comprised of two assignment functions """ cpu_util_string = 'cpu.utilization' sum_string = 'utilization_sum' count_string = 'utilization_count' mean_string = 'utilization_mean' sum_data = Data(cpu_util_string).sum() count_data = Data(cpu_util_string).count() utilization_sum = Assign(sum_string, sum_data) utilization_count = Assign(count_string, count_data) mean_data = Div(Ref(sum_string), Ref(count_string)) utilization_mean = Assign(mean_string, mean_data) detect = Detect(When(GT(Ref(mean_string), 50))).publish(label='detector') program = Program(utilization_sum, utilization_count, utilization_mean, detect) detector = Detector().with_program(program) assert detector.options["programText"] == "{0}\n{1}\n{2}\n{3}".format( str(utilization_sum), str(utilization_count), str(utilization_mean), str(detect)) assert program.statements.pop() == detect assert program.statements.pop() == utilization_mean assert program.statements.pop() == utilization_count assert program.statements.pop() == utilization_sum
def test_detector_from_chart_not_program(): """We should throw an error if we receive a chart that doesn't have a proper program. """ program = Data('awesome.metrics').publish(label='A') chart = TimeSeriesChart().with_program(program) with pytest.raises(ValueError): Detector().from_chart(chart, lambda x: x)
def test_detector_from_chart(): program = Program(Data('cpu.utilization').publish(label='Z')) chart = TimeSeriesChart().with_program(program) def helper(p): return Program(Detect(LT(p.find_label('Z'), 10)).publish(label='foo')) detector = Detector().from_chart(chart, helper) assert detector.options['programText'] == str(helper(program))
def fun(name, threshold): program = Program( Detect(GT(Data("cpu.utilization"), threshold)).publish(label=name)) rule = (Rule().for_label(name).with_severity( Severity.Info).with_notifications( EmailNotification("*****@*****.**"))) return (Detector(session=session).with_name(name).with_program( program).with_rules(rule))
def test_detector_from_chart_mod_prog(): """We shouldn't be able to muck about with programs from charts.""" program = Program(Data('disk.utilization').publish(label='X')) prog_size = len(program.statements) chart = TimeSeriesChart().with_program(program) def bad_helper(p): p.add_statements(Data("I shouldn't exist").publish(label='Y')) return Program(Detect(LT(p.find_label('Z'), 10)).publish(label='foo')) Detector().from_chart(chart, bad_helper) # bad_helper should not be allowed to add new program statements to # the originating chart's program. assert prog_size == len(program.statements)
def test_detector_invalid(method): with pytest.raises(ValueError): detector = Detector() fn = getattr(detector, method) fn(None)
def test_detector_with_teams(): team_ids = ['team1', 'team2', 'team3'] d = Detector().with_teams(*team_ids) assert d.options['teams'] == team_ids
def test_detector_with_tags(): tags = ['foo', 'bar', 'baz'] d = Detector().with_tags(*tags) assert d.options['tags'] == tags
def test_detector_with_visualization_options(): opts = VisualizationOptions()\ .with_time_config(TimeConfig().with_type(Time.Absolute)) d = Detector().with_visualization_options(opts) assert d.options['visualizationOptions'] == opts.options
def test_detector_with_max_dely(): d = Detector().with_max_delay(900) assert d.options['maxDelay'] == 900
def test_detector_with_program(): program = Program( Data('foo').publish(label='A'), Data('bar').publish(label='B')) d = Detector().with_program(program) assert d.options['programText'] == str(program)
def test_detector_with_rules(): rule = Rule().with_notifications(EmailNotification('*****@*****.**')) d = Detector().with_rules(rule) assert d.options['rules'] == [rule.options]
def test_detector_init(): assert Detector().options == {} assert Detector().endpoint == '/detector'
""" alert_label = 'CPU is too low for 75% of the last 2 minutes!' filters = And(Filter('app', 'my-app'), Filter('env', 'test')) data = Data('cpu.utilization', filter=filters).publish(label='A') cpu_too_low = Detect(When(LT(data, 50), '2m', 0.75)).publish(alert_label) program = Program(cpu_too_low) info_rule = Rule()\ .for_label(alert_label)\ .with_severity(Severity.Info)\ .with_notifications(EmailNotification('*****@*****.**')) detector = Detector()\ .with_name('TEST: example detector')\ .with_program(program)\ .with_rules(info_rule) """ Example 2: from an existing chart This is useful when you want to alert on behavior seen in a chart that was created using the signal_analog library. """ test_program = Program(data) chart = TimeSeriesChart()\ .with_name('TEST: example chart for detector')\ .with_program(test_program) # Defined above
def detector(threshold): return Detector(session=session)\ .with_name('test_update')\ .with_program(program(threshold))\ .with_rules(rule)