def test_remove_re(): target = "labels" keys = [re.compile("^(tim|hans|ute|furz|soda)$")] alert_group = AlertGroup.construct( **{ f"common_{target}": { "tim": "schwenke", "hans": "meier", }, "alerts": [ Alert.construct( **{ target: { "tim": "schwenke", "hans": "meier", "ronald": "fritz", "ute": "schneider", } }), Alert.construct( **{ target: { "tim": "schwenke", "hans": "meier", "furz": "ernst", } }), ], }) actions._remove_re(target, keys, alert_group) assert alert_group.__dict__[f"common_{target}"] == {} assert alert_group.alerts[0].__dict__[target] == {"ronald": "fritz"} assert alert_group.alerts[1].__dict__[target] == {}
def test_wrapped_remove_none_b(): alert_group = AlertGroup.construct( common_labels={ "tim": "schwenke", "hans": "meier", }, alerts=[ Alert.construct( labels={ "tim": "schwenke", "hans": "meier", "ronald": "fritz", "ute": "schneider", }), Alert.construct(labels={ "tim": "schwenke", "hans": "meier", "furz": "ernst", }), ], ) a = None b = Remove(re_labels=[re.compile("^(tim|hans|ute|furz|soda)$")]) actions.wrapped_remove(a, b, alert_group) assert alert_group.common_labels == {} assert alert_group.alerts[0].labels == {"ronald": "fritz"} assert alert_group.alerts[1].labels == {}
def test_add_specific_annotations(): alert_group = AlertGroup.construct( common_labels={ "alertname": "JustATestAlert", "namespace": "whatever/promstack", "severity": "info", }, common_annotations={ "investigate_link": "https://domain.com/grafanMz/container-intra", "summary": "Ratio(Max(Working Set, RSS) / Reservation) [5m Avg] \\u003c 40 %", "title": "Memory: Relativ memory usage low", }, alerts=[ Alert.construct( labels={ "alertname": "JustATestAlert", "name": "grafana", "namespace": "whatever/promstack", "severity": "info", }, annotations={ "description": "Lorem ipsum dolor sit", "investigate_link": "https://domain.com/grafanainer-intra", "summary": "Ratio(Max(Working Set, RSS) / Reserg] \\u003c 40 %", "title": "Memory: Relativ memory usage low", "specific": "specific", "very_specific": "very_specific", }, ), Alert.construct( labels={ "alertname": "JustATestAlert", "name": "grafana", "namespace": "whatever/promstack", "severity": "info", }, annotations={ "description": "Lorem ipsum dolor sit amete value 2.", "investigate_link": "https://domain.com/graontainer-intra", "summary": "Ratio(Max(Workition) [5m Avg] \\u003c 40 %", "title": "Memory: Relativ memory usage low", }, ), ], ) utils.add_specific(alert_group) assert alert_group.alerts[0].specific_annotations["specific"] == "specific" assert alert_group.alerts[0].specific_annotations[ "very_specific"] == "very_specific" assert alert_group.alerts[0].specific_annotations is not None assert alert_group.alerts[1].specific_annotations is not None assert alert_group.alerts[0].specific_labels is not None assert alert_group.alerts[1].specific_labels is not None
def test_create_alert_group_single_alert(helpers): base = AlertGroup.construct( **{ "blabla": "blabladfefef", "groupKeys": { "fefefe": "fefefwe", }, "group_labels": { "will": "do" }, }) alerts = [ Alert.construct( **{ "what": "ever", "annotations": { "a": "aa", "b": "bb", }, "labels": {}, }), Alert.construct( **{ "annotations": { "a": "aa", "b": "bb", }, "labels": { "c": "cc", "d": "dd", }, }), ] alert_group = splitting._create_alert_group(base, alerts) helpers.print_struct(alert_group, "data") assert alert_group.blabla == "blabladfefef" assert alert_group.groupKeys == {"fefefe": "fefefwe"} assert alert_group.common_annotations == { "a": "aa", "b": "bb", } assert alert_group.common_labels == {} assert alert_group.alerts[0] == { "what": "ever", "annotations": { "a": "aa", "b": "bb", }, "labels": {}, } assert alert_group.group_labels == {}
def test_create_data_dict_two_alerts(caplog, helpers): base = AlertGroup.construct( **{ "blabla": "blabladfefef", "groupKeys": { "fefefe": "fefefwe", }, "group_labels": { "hot": "hotv" }, }) alerts = [ Alert.construct( **{ "what": "ever", "annotations": { "red": "redv", "blue": "bluev", "anno": "foo", }, "labels": { "hot": "hotv", "cold": "coldv", }, }), Alert.construct( **{ "what": "ever", "annotations": { "orange": "orangev", "green": "greenv", "blue": "somethingelse", "red": "redv", "anno": "foo", }, "labels": { "c": "cc", "hot": "hotv", "d": "dd", }, }), ] alert_group = splitting._create_alert_group(base, alerts) helpers.print_struct(alert_group, "alert_group") assert alert_group.common_annotations == {"anno": "foo", "red": "redv"} assert alert_group.common_labels == {"hot": "hotv"} assert alert_group.group_labels == {"hot": "hotv"}
def test_group_alerts_single_alert(): list_of_alert_lists = splitting._group_alerts( "annotations", "foo", [Alert.construct(annotations={"foo": "bar"})]) assert isinstance(list_of_alert_lists[0][0], Alert) assert len(list_of_alert_lists) == 1 assert len(list_of_alert_lists[0]) == 1 assert list_of_alert_lists[0][0] == Alert.construct( annotations={"foo": "bar"}) list_of_alert_lists = splitting._group_alerts( "labels", "foo", [Alert.construct(labels={"foo": "bar"})]) assert isinstance(list_of_alert_lists[0][0], Alert) assert len(list_of_alert_lists) == 1 assert len(list_of_alert_lists[0]) == 1 assert list_of_alert_lists[0][0] == Alert.construct(labels={"foo": "bar"})
def test_wrapped_remove_a_b(): alert_group = AlertGroup.construct( common_annotations={}, common_labels={ "tim": "schwenke", "hans": "meier", }, alerts=[ Alert.construct( annotations={}, labels={ "tim": "schwenke", "hans": "meier", "ronald": "fritz", "ute": "schneider", }, ), Alert.construct( annotations={}, labels={ "tim": "schwenke", "hans": "meier", "furz": "ernst", }, ), ], ) a = Remove( labels=["ute", "tim", "furz"], annotations=["ute", "tim", "furz"], ) b = Remove( re_annotations=[re.compile("^(tim|hans)$")], re_labels=[re.compile("^(tim|hans)$")], ) actions.wrapped_remove(a, b, alert_group) assert alert_group.common_annotations == {} assert alert_group.common_labels == {} assert alert_group.alerts[0].annotations == {} assert alert_group.alerts[0].labels == {"ronald": "fritz"} assert alert_group.alerts[1].annotations == {} assert alert_group.alerts[1].labels == {}
def test_wrapped_override_a_b(): alert_group = AlertGroup.construct( common_labels={ "tim": "schwenke", }, alerts=[ Alert.construct(labels={ "tim": "schwenke", "ute": "meier", "frank": "sohn", }), Alert.construct(labels={ "tim": "schwenke", }), ], ) a = Override(labels={ "tim": "schwonkel", "ute": "freier", }) b = Override(labels={ "ute": "freier", "frank": "sohn", }) actions.wrapped_override(a, b, alert_group) assert alert_group.common_labels == { "tim": "schwonkel", "ute": "freier", "frank": "sohn", } assert alert_group.alerts[0].labels == { "tim": "schwonkel", "ute": "freier", "frank": "sohn", } assert alert_group.alerts[1].labels == { "tim": "schwonkel", "ute": "freier", "frank": "sohn", }
def test_wrapped_remove_none_none(): alert_group = AlertGroup.construct( common_labels={ "tim": "schwenke", "hans": "meier", }, alerts=[ Alert.construct( labels={ "tim": "schwenke", "hans": "meier", "ronald": "fritz", "ute": "schneider", }), Alert.construct(labels={ "tim": "schwenke", "hans": "meier", "furz": "ernst", }), ], ) a = None b = None actions.wrapped_remove(a, b, alert_group) assert alert_group.common_labels == { "tim": "schwenke", "hans": "meier", } assert alert_group.alerts[0].labels == { "tim": "schwenke", "hans": "meier", "ronald": "fritz", "ute": "schneider", } assert alert_group.alerts[1].labels == { "tim": "schwenke", "hans": "meier", "furz": "ernst", }
def test_override(): alert_group = AlertGroup.construct( common_labels={ "tim": "schwenke", }, alerts=[ Alert.construct(labels={ "tim": "schwenke", "ute": "meier", "frank": "sohn", }), Alert.construct(labels={ "tim": "schwenke", }), ], ) actions._override( "labels", { "tim": "schwonkel", "ute": "freier", "frank": "sohn", }, alert_group, ) assert alert_group.common_labels == { "tim": "schwonkel", "ute": "freier", "frank": "sohn", } assert alert_group.alerts[0].labels == { "tim": "schwonkel", "ute": "freier", "frank": "sohn", } assert alert_group.alerts[1].labels == { "tim": "schwonkel", "ute": "freier", "frank": "sohn", }
def test_wrapped_add_a1_a2(): alert_group = AlertGroup.construct( common_labels={ "tim": "schwenke", }, alerts=[ Alert.construct(labels={ "tim": "schwenke", "ute": "meier", "frank": "sohn", }), Alert.construct(labels={ "tim": "schwenke", }), ], ) a1 = Add(labels={ "tim": "schwonkel", "ute": "freier", }) a2 = Add(labels={ "ute": "freier", "frank": "sohn", }) actions.wrapped_add(a1, a2, alert_group) assert alert_group.common_labels == { "tim": "schwenke", "frank": "sohn", } assert alert_group.alerts[0].labels == { "tim": "schwenke", "ute": "meier", "frank": "sohn", } assert alert_group.alerts[1].labels == { "tim": "schwenke", "ute": "freier", "frank": "sohn", }
def test_group_alerts(helpers): alerts = [ Alert.construct( **{ "whatever": "fefe", "annotations": { "target_link": "a", "foo": "bar", }, }), Alert.construct( **{ "whatever": "fefe", "annotations": { "target_link": "a", "foo": "bar", }, }), Alert.construct( **{ "whatever": "fefe", "annotations": { "target_link": "b", "foo": "bar", }, }), Alert.construct(**{ "whatever": "fefe", "annotations": { "foo": "bar", }, }), Alert.construct(**{ "whatever": "fefe", "annotations": { "foo": "dsds", }, }), ] grouped_alerts = splitting._group_alerts("annotations", "target_link", alerts) helpers.print_struct(grouped_alerts, "grouped_alerts") assert len(grouped_alerts) == 3 assert [ { "whatever": "fefe", "annotations": { "foo": "bar" } }, { "whatever": "fefe", "annotations": { "foo": "dsds" } }, ] in grouped_alerts assert [ { "whatever": "fefe", "annotations": { "foo": "bar" } }, { "whatever": "fefe", "annotations": { "foo": "dsds" } }, ] in grouped_alerts assert [ { "whatever": "fefe", "annotations": { "target_link": "a", "foo": "bar" } }, { "whatever": "fefe", "annotations": { "target_link": "a", "foo": "bar" } }, ] in grouped_alerts assert isinstance(grouped_alerts[0][0], Alert)
def test_split(caplog, helpers): alert_group = AlertGroup.construct( **{ "receiver": "cisco webex", "status": "firing", "group_labels": { "alertname": "JustATestAlert", "namespace": "whatever/promstack", }, "common_labels": { "alertname": "JustATestAlert", "namespace": "whatever/promstack", "severity": "info", }, "common_annotations": { "investigate_link": "https://domain.com/grafana/d/fdwTIxFMz/container-intra", "summary": "Ratio(Max(Working Set, RSS) / Reservation) [5m Avg] \\u003c 40 %", "title": "Memory: Relativ memory usage low", }, "alerts": [ Alert.construct(**{ "labels": {}, "annotations": { "common": "common", }, }), Alert.construct( **{ "labels": {}, "annotations": { "common": "common", "whatever": "fefe", }, }), Alert.construct(**{ "labels": { "very": "unique" }, "annotations": {}, }), ], }) alert_groups = splitting.split("annotation", "common", alert_group) helpers.print_struct(alert_groups, "alert_groups") assert alert_groups[0].receiver == "cisco webex" assert alert_groups[0].group_labels == {} assert alert_groups[0].alerts == [ Alert.construct(**{ "labels": { "very": "unique" }, "annotations": {}, }) ] assert alert_groups[1].common_annotations == {"common": "common"} assert alert_groups[0].common_annotations == {}
def test_preprocess(helpers): routing = Routing( remove=Remove( annotations=["vw", "audi"], labels=["heinz"], re_annotations=["^(mercedes)$", "^__.*$"], re_labels=["^__.*$"], ), add=Add( annotations={"bmw": "value"}, labels={"simon": "humben"}, ), override=Override( annotations={"yung": "hurn"}, labels={"old": "barn"}, ), ) route = Route( name="whatever", add=Add( annotations={"bmw": "value"}, labels={"simon": "klumpen"}, ), ) alert_group = AlertGroup.construct( group_labels={"alertname": "WhatEver"}, common_labels={ "heinz": "meier", "__meta": "x", "severity": "warning" }, common_annotations={ "vw": "x", }, alerts=[ Alert.construct( labels={ "heinz": "meier", "__meta": "x", "severity": "warning", "yung": "barn", }, annotations={ "vw": "x", "audi": "x", "yung": "barn", }, ), Alert.construct( labels={ "heinz": "meier", "simon": "fart", "__meta": "x", "severity": "warning", }, annotations={ "vw": "x", }, ), ], ) alertgroup = preprocess(routing, route, alert_group)[0] helpers.print_struct(alertgroup) assert alertgroup.common_labels == { "severity": "warning", "old": "barn", } assert alertgroup.common_annotations == { "bmw": "value", "yung": "hurn", } assert alertgroup.alerts[0].labels == { "severity": "warning", "yung": "barn", "old": "barn", "simon": "klumpen", } assert alertgroup.alerts[0].annotations == { "yung": "hurn", "bmw": "value", } assert alertgroup.alerts[1].labels == { "simon": "fart", "severity": "warning", "simon": "fart", "old": "barn", } assert alertgroup.alerts[1].annotations == { "bmw": "value", "yung": "hurn", }