Skip to content

Commit d44518b

Browse files
authored
Merge pull request #166 from grafana/dev
Merge dev to main
2 parents 5d23807 + 5711761 commit d44518b

File tree

12 files changed

+96
-51
lines changed

12 files changed

+96
-51
lines changed

CHANGELOG.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Change Log
22

3+
## 1.0.4 (2022-06-28)
4+
- Allow Telegram DMs without channel connection.
5+
36
## 1.0.3 (2022-06-27)
47
- Fix users public api endpoint. Now it returns users with all roles.
58
- Fix redundant notifications about gaps in schedules.
@@ -16,4 +19,4 @@
1619

1720
## 0.0.71 (2022-06-06)
1821

19-
- Initial Commit Release
22+
- Initial Commit Release

docs/sources/chat-options/configure-telegram.md

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ You can use Telegram to deliver alert group notifications to a dedicated channel
2020

2121
Each alert group notification is assigned a dedicated discussion. Users can perform notification actions (acknowledge, resolve, silence), create reports, and discuss alerts in the comments section of the discussions.
2222

23+
In case an integration route is not configured to use a Telegram channel, users will receive messages with alert group contents, logs and actions in their DMs.
24+
2325
## Connect to Telegram
2426

2527
Connect your organization's Telegram account to your Grafana OnCall instance by following the instructions provided in OnCall. You can use the following steps as a reference.

engine/apps/alerts/models/alert_group.py

-8
Original file line numberDiff line numberDiff line change
@@ -1557,14 +1557,6 @@ def notify_in_slack_enabled(self):
15571557
else:
15581558
return True
15591559

1560-
@property
1561-
def notify_in_telegram_enabled(self):
1562-
channel_filter = self.channel_filter_with_respect_to_escalation_snapshot
1563-
if channel_filter is not None:
1564-
return channel_filter.notify_in_telegram
1565-
else:
1566-
return True
1567-
15681560
@property
15691561
def is_presented_in_slack(self):
15701562
return self.slack_message and self.channel.organization.slack_team_identity

engine/apps/alerts/tasks/notify_user.py

+1-18
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,6 @@ def notify_user_task(
162162
user_to_be_notified_in_slack = (
163163
notification_policy.notify_by == UserNotificationPolicy.NotificationChannel.SLACK
164164
)
165-
user_to_be_notified_in_telegram = (
166-
notification_policy.notify_by == UserNotificationPolicy.NotificationChannel.TELEGRAM
167-
)
168-
169165
if user_to_be_notified_in_slack and alert_group.notify_in_slack_enabled is False:
170166
log_record = UserNotificationPolicyLogRecord(
171167
author=user,
@@ -178,18 +174,6 @@ def notify_user_task(
178174
notification_channel=notification_policy.notify_by,
179175
notification_error_code=UserNotificationPolicyLogRecord.ERROR_NOTIFICATION_POSTING_TO_SLACK_IS_DISABLED,
180176
)
181-
elif user_to_be_notified_in_telegram and alert_group.notify_in_telegram_enabled is False:
182-
log_record = UserNotificationPolicyLogRecord(
183-
author=user,
184-
type=UserNotificationPolicyLogRecord.TYPE_PERSONAL_NOTIFICATION_FAILED,
185-
notification_policy=notification_policy,
186-
alert_group=alert_group,
187-
reason=reason,
188-
slack_prevent_posting=prevent_posting_to_thread,
189-
notification_step=notification_policy.step,
190-
notification_channel=notification_policy.notify_by,
191-
notification_error_code=UserNotificationPolicyLogRecord.ERROR_NOTIFICATION_POSTING_TO_TELEGRAM_IS_DISABLED,
192-
)
193177
else:
194178
log_record = UserNotificationPolicyLogRecord(
195179
author=user,
@@ -292,8 +276,7 @@ def perform_notification(log_record_pk):
292276
)
293277

294278
elif notification_channel == UserNotificationPolicy.NotificationChannel.TELEGRAM:
295-
if alert_group.notify_in_telegram_enabled is True:
296-
TelegramToUserConnector.notify_user(user, alert_group, notification_policy)
279+
TelegramToUserConnector.notify_user(user, alert_group, notification_policy)
297280

298281
# TODO: restore email notifications
299282
# elif notification_channel == UserNotificationPolicy.NotificationChannel.EMAIL:

engine/apps/base/models/user_notification_policy_log_record.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class UserNotificationPolicyLogRecord(models.Model):
5959
ERROR_NOTIFICATION_MAIL_DELIVERY_FAILED,
6060
ERROR_NOTIFICATION_TELEGRAM_BOT_IS_DELETED,
6161
ERROR_NOTIFICATION_POSTING_TO_SLACK_IS_DISABLED,
62-
ERROR_NOTIFICATION_POSTING_TO_TELEGRAM_IS_DISABLED,
62+
ERROR_NOTIFICATION_POSTING_TO_TELEGRAM_IS_DISABLED, # deprecated
6363
ERROR_NOTIFICATION_IN_SLACK,
6464
ERROR_NOTIFICATION_IN_SLACK_TOKEN_ERROR,
6565
ERROR_NOTIFICATION_IN_SLACK_USER_NOT_IN_SLACK,
@@ -213,6 +213,7 @@ def render_log_line_action(self, for_slack=False, substitute_author_with_tag=Fal
213213
self.notification_error_code
214214
== UserNotificationPolicyLogRecord.ERROR_NOTIFICATION_POSTING_TO_TELEGRAM_IS_DISABLED
215215
):
216+
# deprecated
216217
result += f"failed to notify {user_verbal} in Telegram, because the incident is not posted to Telegram (reason: Telegram is disabled for the route)"
217218
elif (
218219
self.notification_error_code

engine/apps/telegram/alert_group_representative.py

-21
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,6 @@ def on_alert_group_update_log_report(cls, **kwargs):
6666
if not isinstance(alert_group, AlertGroup):
6767
alert_group = AlertGroup.all_objects.get(pk=alert_group)
6868

69-
# telegram notification is disabled for channel filter
70-
if alert_group.notify_in_telegram_enabled is False:
71-
logger.debug(f"Skipping alert group with id {alert_group.pk} since notify_in_telegram is disabled")
72-
return
73-
7469
messages_to_edit = alert_group.telegram_messages.filter(
7570
message_type__in=(
7671
TelegramMessage.LOG_MESSAGE,
@@ -90,30 +85,14 @@ def on_alert_group_action_triggered(cls, **kwargs):
9085
if not isinstance(log_record, AlertGroupLogRecord):
9186
log_record = AlertGroupLogRecord.objects.get(pk=log_record)
9287

93-
# telegram notification is disabled for channel filter
94-
if log_record.alert_group.notify_in_telegram_enabled is False:
95-
logger.debug(
96-
f"Skipping alert group with id {log_record.alert_group.pk} since notify_in_telegram is disabled"
97-
)
98-
return
99-
10088
instance = cls(log_record)
10189
if instance.is_applicable():
10290
handler = instance.get_handler()
10391
handler()
10492

10593
@staticmethod
10694
def on_create_alert(**kwargs):
107-
Alert = apps.get_model("alerts", "Alert")
108-
10995
alert_pk = kwargs["alert"]
110-
alert = Alert.objects.get(pk=alert_pk)
111-
112-
# telegram notification is disabled for channel filter
113-
if alert.group.notify_in_telegram_enabled is False:
114-
logger.debug(f"Skipping alert with id {alert.pk} since notify_in_telegram is disabled")
115-
return
116-
11796
on_create_alert_telegram_representative_async.apply_async((alert_pk,))
11897

11998
def get_handler(self):

engine/apps/telegram/models/connectors/channel.py

+3
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ def get_channel_for_alert_group(cls, alert_group: AlertGroup) -> Optional["Teleg
8282
if alert_group.channel_filter is None:
8383
return default_channel
8484

85+
if not alert_group.channel_filter.notify_in_telegram:
86+
return None
87+
8588
return alert_group.channel_filter.telegram_channel or default_channel
8689

8790
def make_channel_default(self, author):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import pytest
2+
3+
from apps.telegram.models import TelegramMessage, TelegramToOrganizationConnector
4+
5+
6+
@pytest.mark.django_db
7+
def test_get_channel_for_alert_group(
8+
make_organization, make_alert_receive_channel, make_channel_filter, make_alert_group, make_telegram_channel
9+
):
10+
organization = make_organization()
11+
12+
make_telegram_channel(organization, is_default_channel=True)
13+
telegram_channel = make_telegram_channel(organization)
14+
15+
alert_receive_channel = make_alert_receive_channel(organization)
16+
channel_filter = make_channel_filter(
17+
alert_receive_channel, notify_in_telegram=True, telegram_channel=telegram_channel
18+
)
19+
20+
alert_group = make_alert_group(alert_receive_channel, channel_filter=channel_filter)
21+
22+
channel = TelegramToOrganizationConnector.get_channel_for_alert_group(alert_group)
23+
assert channel is telegram_channel
24+
25+
26+
@pytest.mark.django_db
27+
def test_get_channel_telegram_disabled_for_route(
28+
make_organization, make_alert_receive_channel, make_channel_filter, make_alert_group, make_telegram_channel
29+
):
30+
organization = make_organization()
31+
32+
telegram_channel = make_telegram_channel(organization)
33+
34+
alert_receive_channel = make_alert_receive_channel(organization)
35+
channel_filter = make_channel_filter(
36+
alert_receive_channel, notify_in_telegram=False, telegram_channel=telegram_channel
37+
)
38+
39+
alert_group = make_alert_group(alert_receive_channel, channel_filter=channel_filter)
40+
41+
channel = TelegramToOrganizationConnector.get_channel_for_alert_group(alert_group)
42+
assert channel is None
43+
44+
45+
@pytest.mark.django_db
46+
def test_get_channel_for_alert_group_dm_messages_exist(
47+
make_organization,
48+
make_alert_receive_channel,
49+
make_channel_filter,
50+
make_alert_group,
51+
make_telegram_channel,
52+
make_telegram_message,
53+
):
54+
organization = make_organization()
55+
56+
telegram_channel = make_telegram_channel(organization)
57+
alert_receive_channel = make_alert_receive_channel(organization)
58+
channel_filter = make_channel_filter(
59+
alert_receive_channel, notify_in_telegram=True, telegram_channel=telegram_channel
60+
)
61+
62+
alert_group = make_alert_group(alert_receive_channel, channel_filter=channel_filter)
63+
make_telegram_message(alert_group=alert_group, message_type=TelegramMessage.PERSONAL_MESSAGE)
64+
65+
channel = TelegramToOrganizationConnector.get_channel_for_alert_group(alert_group)
66+
assert channel is None

helm/oncall/templates/_helpers.tpl

+3-1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ Create the name of the service account to use
8585
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
8686
imagePullPolicy: {{ .Values.image.pullPolicy }}
8787
command: ['sh', '-c', "until (python manage.py migrate --check); do echo Waiting for database migrations; sleep 2; done"]
88+
securityContext:
89+
{{ toYaml .Values.init.securityContext| nindent 4}}
8890
env:
8991
{{- include "snippet.oncall.env" . | nindent 12 }}
9092
{{- include "snippet.mysql.env" . | nindent 12 }}
@@ -93,4 +95,4 @@ Create the name of the service account to use
9395
{{- if .Values.env }}
9496
{{- toYaml .Values.env | nindent 12 }}
9597
{{- end }}
96-
{{- end }}
98+
{{- end }}

helm/oncall/templates/cert-issuer.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{{- if (index .Values "cert-manager") }}
1+
{{- if (index .Values "cert-manager").enabled }}
22
apiVersion: cert-manager.io/v1
33
kind: Issuer
44
metadata:

helm/oncall/templates/ingress-regular.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,13 @@ spec:
3838
name: {{ include "oncall.engine.fullname" . }}
3939
port:
4040
number: 8080
41+
{{ if .Values.grafana.enabled }}
4142
- path: /grafana
4243
pathType: Prefix
4344
backend:
4445
service:
4546
name: {{ include "oncall.grafana.fullname" . }}
4647
port:
4748
number: 80
49+
{{- end }}
4850
{{- end }}

helm/oncall/values.yaml

+12
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,15 @@ securityContext: {}
164164
# readOnlyRootFilesystem: true
165165
# runAsNonRoot: true
166166
# runAsUser: 1000
167+
168+
init:
169+
securityContext: {}
170+
# allowPrivilegeEscalation: false
171+
# capabilities:
172+
# drop:
173+
# - ALL
174+
# privileged: false
175+
# readOnlyRootFilesystem: true
176+
# runAsGroup: 1337
177+
# runAsNonRoot: true
178+
# runAsUser: 1337

0 commit comments

Comments
 (0)