Skip to content

Commit 4b84b2a

Browse files
authored
Merge pull request #96 from GabrielSalla/internal-monitor-notification-config
Internal monitor notifications set through config
2 parents 7c188ba + a1a425b commit 4b84b2a

File tree

18 files changed

+426
-45
lines changed

18 files changed

+426
-45
lines changed

configs/configs-scalable.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ plugins:
77
load_sample_monitors: true
88
sample_monitors_path: sample_monitors
99
internal_monitors_path: internal_monitors
10+
internal_monitors_notification:
11+
enabled: true
12+
notification_class: plugin.slack.notifications.SlackNotification
13+
params:
14+
min_priority_to_send: low
15+
min_priority_to_mention: moderate
1016
monitors_load_schedule: "*/5 * * * *"
1117

1218
logging:

configs/configs.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ plugins:
77
load_sample_monitors: true
88
sample_monitors_path: sample_monitors
99
internal_monitors_path: internal_monitors
10+
internal_monitors_notification:
11+
enabled: true
12+
notification_class: plugin.slack.notifications.SlackNotification
13+
params:
14+
min_priority_to_send: low
15+
min_priority_to_mention: moderate
1016
monitors_load_schedule: "*/5 * * * *"
1117

1218
logging:

docs/configuration_file.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@ This document provides an overview of the configuration parameters available in
88
- `load_sample_monitors`: Boolean. Flag to enable the sample monitors.
99
- `sample_monitors_path`: String. Path relative to the project root, where the sample monitors are stored.
1010
- `internal_monitors_path`: String. Path relative to the project root, where the internal monitors are stored.
11+
- `internal_monitors_notification`: Map. Settings for the notification to be sent by the internal monitors.
12+
- `enabled`: Boolean. Flag to enable the internal monitors notification.
13+
- `notification_class`: String. Class to be used for the notification. Example: `plugin.my_plugin.notifications.SomeNotificationClass`.
14+
- `params`: Map. The desired parameters for the notification. Each notification class will have its own set of parameters. Check the documentation for each notification class to learn more about the available parameters.
1115
- `monitors_load_schedule`: String using Cron format. Schedule to reload monitors from the database.
1216

1317
## Logging
1418
- `logging.mode`: String. Logging mode. Can be "friendly" or "json".
1519
- `logging.format`: String. Settings for formatting the "friendly" logs.
16-
- `logging.fields`: Object. Fields to include in the "json" logs and their name from the `logging` module.
20+
- `logging.fields`: Map. Fields to include in the "json" logs and their name from the `logging` module.
1721

1822
Suggested configuration for `friendly` logs:
1923
```yaml
@@ -59,8 +63,8 @@ application_queue:
5963
## Controller Settings
6064
- `controller_process_schedule`: String using Cron format. Schedule to check if monitors need to be processed.
6165
- `controller_concurrency`: Integer. Number of monitors that can be processed at the same time by the Controller.
62-
- `controller_procedures`: Object. Procedures to be executed by the Controller and their settings.
63-
- `controller_procedures.monitors_stuck`: Object. Settings for the procedure to fix monitors stuck in "queued" or "running" status.
66+
- `controller_procedures`: Map. Procedures to be executed by the Controller and their settings.
67+
- `controller_procedures.monitors_stuck`: Map. Settings for the procedure to fix monitors stuck in "queued" or "running" status.
6468
- `controller_procedures.monitors_stuck.schedule`: String using Cron format. Schedule to execute the `monitors_stuck` procedure.
6569
- `controller_procedures.monitors_stuck.params.time_tolerance`: Integer. Time tolerance in seconds for a monitor to be considered as stuck. This parameter is directly impacted by the `executor_monitor_heartbeat_time` setting and the recommended value is 2 times the heartbeat time.
6670

docs/plugins/plugins.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,11 @@ Notifications must have the structure defined by the `src.notifications.base_not
5454
Notification base structure must be as follows:
5555
```python
5656
from data_models.monitor_options import reaction_function_type
57+
from monitor_utils import AlertPriority
5758

5859

5960
class Notification:
60-
min_priority_to_send: int = 5
61+
min_priority_to_send: AlertPriority = AlertPriority.low
6162

6263
def reactions_list(self) -> list[tuple[str, list[reaction_function_type]]]:
6364
...
@@ -66,10 +67,11 @@ class Notification:
6667
An example of a notification implementation is shown bellow, where there are 3 different events with reactions set for them.
6768
```python
6869
from data_models.monitor_options import reaction_function_type
70+
from monitor_utils import AlertPriority
6971

7072

7173
class MyNotification:
72-
min_priority_to_send: int = 5
74+
min_priority_to_send: AlertPriority = AlertPriority.low
7375

7476
def reactions_list(self) -> list[tuple[str, list[reaction_function_type]]]:
7577
"""Get a list of events that the notification will react to"""

docs/plugins/slack.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@ The following environment variables are used by the Slack plugin:
1212
- `SLACK_TOKEN`: The token used to send messages to Slack. This token is generated when you create your Slack app and install it in your workspace. Example: `xoxb-1234567890-1234567890123-12345678901234567890abcdef`.
1313
- `SLACK_WEBSOCKET_ENABLED`: A flag to enable or disable the websocket connection for receiving events from Slack. Set this to `true` to enable the websocket or `false` to disable it. Defaults to `false`.
1414
- `SLACK_APP_TOKEN`: The token used to start the websocket connection for receiving events from interactions with the Sentinela Slack app. This token is generated when you create your Slack app and enable the Socket Mode. Example: `xapp-1234567890-1234567890123-12345678901234567890abcdef`.
15-
- `SAMPLE_SLACK_CHANNEL`: The Slack channel where notifications for **sample monitors** will be sent. Example: `C0011223344`.
16-
- `SAMPLE_SLACK_MENTION`: The Slack user or group to mention in notifications for **sample monitors**. Example: `U0011223344`.
17-
- `SLACK_MAIN_CHANNEL`: The Slack channel where notifications for **internal monitors** will be sent. Example: `C0011223344`.
18-
- `SLACK_MAIN_MENTION`: The Slack user or group to mention in notifications for **internal monitors**. Example: `U0011223344`.
15+
- `SLACK_MAIN_CHANNEL`: The Slack channel where notifications for **internal monitors** and **sample monitors** will be sent. Example: `C0011223344`.
16+
- `SLACK_MAIN_MENTION`: The Slack user or group to mention in notifications for **internal monitors** and **sample monitors**. Example: `U0011223344`.
1917

2018
## Slack commands
2119
Sentinela provides two main ways to interact through Slack:
@@ -91,5 +89,15 @@ notification_options = [
9189
]
9290
```
9391

92+
### Using Slack notification for internal monitors
93+
To use the Slack notification for internal monitors, the settings for the `internal_monitors_notification` key in the `configs.yaml` file must be configured as follows:
94+
- `notification_class`: Should be set to `plugin.slack.notifications.SlackNotification`
95+
- `params`: Should include the desired parameters for the notification.
96+
- `channel` and `mention` will be obtained from the environment variables `SLACK_MAIN_CHANNEL` and `SLACK_MAIN_MENTION`, respectively, as specified in [Environment variables](#environment-variables).
97+
- `title` and `issues_fields` are specific to each monitor and are already defined in the internal monitors. If configured in the `params` field, they will be ignored.
98+
- `min_priority_to_send`, `min_priority_to_mention`, `mention_on_update`, and `issue_show_limit` can be set in the `params` field to customize the notification behavior. If not set, the default values will be used.
99+
100+
The provided settings will be applied to every internal and sample monitors.
101+
94102
## Services
95103
The Slack plugin includes a service that connects to the Slack websocket API to receive mentions and button press events. Any event received will queue an action to be processed by Sentinela.

internal_monitors/monitor_high_active_issues_count/monitor_high_active_issues_count.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,18 @@
33
affected from a high resource usage.
44
"""
55

6-
import os
76
from typing import TypedDict, cast
87

98
from databases import query_application
109
from monitor_utils import (
1110
AlertOptions,
12-
AlertPriority,
1311
IssueOptions,
1412
MonitorOptions,
1513
PriorityLevels,
1614
ValueRule,
1715
read_file,
1816
)
19-
from plugins.slack import SlackNotification
17+
from notifications.internal_monitor_notification import internal_monitor_notification
2018

2119
TRIGGER_THRESHOLD = 500
2220

@@ -66,13 +64,7 @@ def is_solved(issue_data: IssueDataType) -> bool:
6664
return active_issues_count < TRIGGER_THRESHOLD / 2
6765

6866

69-
notification_options = [
70-
SlackNotification(
71-
channel=os.environ["SLACK_MAIN_CHANNEL"],
72-
title="Monitor with high active issues count",
73-
issues_fields=["monitor_id", "monitor_name", "active_issues_count"],
74-
mention=os.environ["SLACK_MAIN_MENTION"],
75-
min_priority_to_send=AlertPriority.low,
76-
min_priority_to_mention=AlertPriority.moderate,
77-
)
78-
]
67+
notification_options = internal_monitor_notification(
68+
name="Monitor with high active issues count",
69+
issues_fields=["monitor_id", "monitor_name", "active_issues_count"],
70+
)

resources/kubernetes_template/config_map.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ data:
1313
load_sample_monitors: true
1414
sample_monitors_path: sample_monitors
1515
internal_monitors_path: internal_monitors
16+
internal_monitors_notification:
17+
enabled: true
18+
notification_class: plugin.slack.notifications.SlackNotification
19+
params:
20+
min_priority_to_send: low
21+
min_priority_to_mention: moderate
1622
monitors_load_schedule: "*/5 * * * *"
1723
1824
logging:

sample_monitors/test_monitor/test_monitor.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import os
21
import random
32
from typing import TypedDict
43

54
from monitor_utils import AlertOptions, CountRule, IssueOptions, MonitorOptions, PriorityLevels
6-
from plugins.slack import SlackNotification
5+
from notifications.internal_monitor_notification import internal_monitor_notification
76

87
monitor_options = MonitorOptions(
98
search_cron="* * * * *",
@@ -64,11 +63,6 @@ def is_solved(issue_data: IssueDataType) -> bool:
6463
return issue_data["value"] == 1
6564

6665

67-
notification_options = [
68-
SlackNotification(
69-
channel=os.environ["SAMPLE_SLACK_CHANNEL"],
70-
title="Test monitor",
71-
issues_fields=["id", "value"],
72-
mention=os.environ["SAMPLE_SLACK_MENTION"],
73-
)
74-
]
66+
notification_options = internal_monitor_notification(
67+
name="Test monitor", issues_fields=["id", "value"]
68+
)

src/configs/configs_loader.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@
22
from typing import Any, Literal
33

44
import yaml
5-
from pydantic.dataclasses import Field, dataclass
5+
from pydantic.dataclasses import dataclass
6+
from pydantic.fields import Field
7+
8+
9+
@dataclass
10+
class InternalMonitorsNotificationConfig:
11+
enabled: bool
12+
notification_class: str
13+
params: dict[str, Any] = Field(default_factory=dict)
614

715

816
@dataclass
@@ -40,6 +48,8 @@ class Configs:
4048
load_sample_monitors: bool
4149
sample_monitors_path: str
4250
internal_monitors_path: str
51+
internal_monitors_notification: InternalMonitorsNotificationConfig
52+
4353
monitors_load_schedule: str
4454

4555
application_database_settings: ApplicationDatabaseConfig

src/main.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@ async def init(controller_enabled: bool, executor_enabled: bool) -> None:
2828
log.setup()
2929
app.setup()
3030
registry.init()
31-
# Depends on internal database migrated
32-
await monitors_loader.init(controller_enabled)
33-
await http_server.init(controller_enabled)
3431

32+
# Plugins must be initialized before the monitors loader as the register process will require
33+
# the plugins to be loaded
3534
plugins.load_plugins()
3635
await plugins.services.init_plugin_services(controller_enabled, executor_enabled)
3736

37+
# Depends on internal database migrated
38+
await monitors_loader.init(controller_enabled)
39+
await http_server.init(controller_enabled)
40+
3841
# The following modules depend on the plugins being loaded
3942
await databases.init()
4043
await message_queue.init()

0 commit comments

Comments
 (0)