Skip to content

Commit

Permalink
Merge pull request #166 from grafana/dev
Browse files Browse the repository at this point in the history
Merge dev to main
  • Loading branch information
matiasb authored Jun 28, 2022
2 parents 5d23807 + 5711761 commit d44518b
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 51 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Change Log

## 1.0.4 (2022-06-28)
- Allow Telegram DMs without channel connection.

## 1.0.3 (2022-06-27)
- Fix users public api endpoint. Now it returns users with all roles.
- Fix redundant notifications about gaps in schedules.
Expand All @@ -16,4 +19,4 @@

## 0.0.71 (2022-06-06)

- Initial Commit Release
- Initial Commit Release
2 changes: 2 additions & 0 deletions docs/sources/chat-options/configure-telegram.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ You can use Telegram to deliver alert group notifications to a dedicated channel

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.

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.

## Connect to Telegram

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.
Expand Down
8 changes: 0 additions & 8 deletions engine/apps/alerts/models/alert_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -1557,14 +1557,6 @@ def notify_in_slack_enabled(self):
else:
return True

@property
def notify_in_telegram_enabled(self):
channel_filter = self.channel_filter_with_respect_to_escalation_snapshot
if channel_filter is not None:
return channel_filter.notify_in_telegram
else:
return True

@property
def is_presented_in_slack(self):
return self.slack_message and self.channel.organization.slack_team_identity
Expand Down
19 changes: 1 addition & 18 deletions engine/apps/alerts/tasks/notify_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,6 @@ def notify_user_task(
user_to_be_notified_in_slack = (
notification_policy.notify_by == UserNotificationPolicy.NotificationChannel.SLACK
)
user_to_be_notified_in_telegram = (
notification_policy.notify_by == UserNotificationPolicy.NotificationChannel.TELEGRAM
)

if user_to_be_notified_in_slack and alert_group.notify_in_slack_enabled is False:
log_record = UserNotificationPolicyLogRecord(
author=user,
Expand All @@ -178,18 +174,6 @@ def notify_user_task(
notification_channel=notification_policy.notify_by,
notification_error_code=UserNotificationPolicyLogRecord.ERROR_NOTIFICATION_POSTING_TO_SLACK_IS_DISABLED,
)
elif user_to_be_notified_in_telegram and alert_group.notify_in_telegram_enabled is False:
log_record = UserNotificationPolicyLogRecord(
author=user,
type=UserNotificationPolicyLogRecord.TYPE_PERSONAL_NOTIFICATION_FAILED,
notification_policy=notification_policy,
alert_group=alert_group,
reason=reason,
slack_prevent_posting=prevent_posting_to_thread,
notification_step=notification_policy.step,
notification_channel=notification_policy.notify_by,
notification_error_code=UserNotificationPolicyLogRecord.ERROR_NOTIFICATION_POSTING_TO_TELEGRAM_IS_DISABLED,
)
else:
log_record = UserNotificationPolicyLogRecord(
author=user,
Expand Down Expand Up @@ -292,8 +276,7 @@ def perform_notification(log_record_pk):
)

elif notification_channel == UserNotificationPolicy.NotificationChannel.TELEGRAM:
if alert_group.notify_in_telegram_enabled is True:
TelegramToUserConnector.notify_user(user, alert_group, notification_policy)
TelegramToUserConnector.notify_user(user, alert_group, notification_policy)

# TODO: restore email notifications
# elif notification_channel == UserNotificationPolicy.NotificationChannel.EMAIL:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class UserNotificationPolicyLogRecord(models.Model):
ERROR_NOTIFICATION_MAIL_DELIVERY_FAILED,
ERROR_NOTIFICATION_TELEGRAM_BOT_IS_DELETED,
ERROR_NOTIFICATION_POSTING_TO_SLACK_IS_DISABLED,
ERROR_NOTIFICATION_POSTING_TO_TELEGRAM_IS_DISABLED,
ERROR_NOTIFICATION_POSTING_TO_TELEGRAM_IS_DISABLED, # deprecated
ERROR_NOTIFICATION_IN_SLACK,
ERROR_NOTIFICATION_IN_SLACK_TOKEN_ERROR,
ERROR_NOTIFICATION_IN_SLACK_USER_NOT_IN_SLACK,
Expand Down Expand Up @@ -213,6 +213,7 @@ def render_log_line_action(self, for_slack=False, substitute_author_with_tag=Fal
self.notification_error_code
== UserNotificationPolicyLogRecord.ERROR_NOTIFICATION_POSTING_TO_TELEGRAM_IS_DISABLED
):
# deprecated
result += f"failed to notify {user_verbal} in Telegram, because the incident is not posted to Telegram (reason: Telegram is disabled for the route)"
elif (
self.notification_error_code
Expand Down
21 changes: 0 additions & 21 deletions engine/apps/telegram/alert_group_representative.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ def on_alert_group_update_log_report(cls, **kwargs):
if not isinstance(alert_group, AlertGroup):
alert_group = AlertGroup.all_objects.get(pk=alert_group)

# telegram notification is disabled for channel filter
if alert_group.notify_in_telegram_enabled is False:
logger.debug(f"Skipping alert group with id {alert_group.pk} since notify_in_telegram is disabled")
return

messages_to_edit = alert_group.telegram_messages.filter(
message_type__in=(
TelegramMessage.LOG_MESSAGE,
Expand All @@ -90,30 +85,14 @@ def on_alert_group_action_triggered(cls, **kwargs):
if not isinstance(log_record, AlertGroupLogRecord):
log_record = AlertGroupLogRecord.objects.get(pk=log_record)

# telegram notification is disabled for channel filter
if log_record.alert_group.notify_in_telegram_enabled is False:
logger.debug(
f"Skipping alert group with id {log_record.alert_group.pk} since notify_in_telegram is disabled"
)
return

instance = cls(log_record)
if instance.is_applicable():
handler = instance.get_handler()
handler()

@staticmethod
def on_create_alert(**kwargs):
Alert = apps.get_model("alerts", "Alert")

alert_pk = kwargs["alert"]
alert = Alert.objects.get(pk=alert_pk)

# telegram notification is disabled for channel filter
if alert.group.notify_in_telegram_enabled is False:
logger.debug(f"Skipping alert with id {alert.pk} since notify_in_telegram is disabled")
return

on_create_alert_telegram_representative_async.apply_async((alert_pk,))

def get_handler(self):
Expand Down
3 changes: 3 additions & 0 deletions engine/apps/telegram/models/connectors/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ def get_channel_for_alert_group(cls, alert_group: AlertGroup) -> Optional["Teleg
if alert_group.channel_filter is None:
return default_channel

if not alert_group.channel_filter.notify_in_telegram:
return None

return alert_group.channel_filter.telegram_channel or default_channel

def make_channel_default(self, author):
Expand Down
66 changes: 66 additions & 0 deletions engine/apps/telegram/tests/test_channel_connector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import pytest

from apps.telegram.models import TelegramMessage, TelegramToOrganizationConnector


@pytest.mark.django_db
def test_get_channel_for_alert_group(
make_organization, make_alert_receive_channel, make_channel_filter, make_alert_group, make_telegram_channel
):
organization = make_organization()

make_telegram_channel(organization, is_default_channel=True)
telegram_channel = make_telegram_channel(organization)

alert_receive_channel = make_alert_receive_channel(organization)
channel_filter = make_channel_filter(
alert_receive_channel, notify_in_telegram=True, telegram_channel=telegram_channel
)

alert_group = make_alert_group(alert_receive_channel, channel_filter=channel_filter)

channel = TelegramToOrganizationConnector.get_channel_for_alert_group(alert_group)
assert channel is telegram_channel


@pytest.mark.django_db
def test_get_channel_telegram_disabled_for_route(
make_organization, make_alert_receive_channel, make_channel_filter, make_alert_group, make_telegram_channel
):
organization = make_organization()

telegram_channel = make_telegram_channel(organization)

alert_receive_channel = make_alert_receive_channel(organization)
channel_filter = make_channel_filter(
alert_receive_channel, notify_in_telegram=False, telegram_channel=telegram_channel
)

alert_group = make_alert_group(alert_receive_channel, channel_filter=channel_filter)

channel = TelegramToOrganizationConnector.get_channel_for_alert_group(alert_group)
assert channel is None


@pytest.mark.django_db
def test_get_channel_for_alert_group_dm_messages_exist(
make_organization,
make_alert_receive_channel,
make_channel_filter,
make_alert_group,
make_telegram_channel,
make_telegram_message,
):
organization = make_organization()

telegram_channel = make_telegram_channel(organization)
alert_receive_channel = make_alert_receive_channel(organization)
channel_filter = make_channel_filter(
alert_receive_channel, notify_in_telegram=True, telegram_channel=telegram_channel
)

alert_group = make_alert_group(alert_receive_channel, channel_filter=channel_filter)
make_telegram_message(alert_group=alert_group, message_type=TelegramMessage.PERSONAL_MESSAGE)

channel = TelegramToOrganizationConnector.get_channel_for_alert_group(alert_group)
assert channel is None
4 changes: 3 additions & 1 deletion helm/oncall/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ Create the name of the service account to use
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
command: ['sh', '-c', "until (python manage.py migrate --check); do echo Waiting for database migrations; sleep 2; done"]
securityContext:
{{ toYaml .Values.init.securityContext| nindent 4}}
env:
{{- include "snippet.oncall.env" . | nindent 12 }}
{{- include "snippet.mysql.env" . | nindent 12 }}
Expand All @@ -93,4 +95,4 @@ Create the name of the service account to use
{{- if .Values.env }}
{{- toYaml .Values.env | nindent 12 }}
{{- end }}
{{- end }}
{{- end }}
2 changes: 1 addition & 1 deletion helm/oncall/templates/cert-issuer.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{- if (index .Values "cert-manager") }}
{{- if (index .Values "cert-manager").enabled }}
apiVersion: cert-manager.io/v1
kind: Issuer
metadata:
Expand Down
2 changes: 2 additions & 0 deletions helm/oncall/templates/ingress-regular.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ spec:
name: {{ include "oncall.engine.fullname" . }}
port:
number: 8080
{{ if .Values.grafana.enabled }}
- path: /grafana
pathType: Prefix
backend:
service:
name: {{ include "oncall.grafana.fullname" . }}
port:
number: 80
{{- end }}
{{- end }}
12 changes: 12 additions & 0 deletions helm/oncall/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,15 @@ securityContext: {}
# readOnlyRootFilesystem: true
# runAsNonRoot: true
# runAsUser: 1000

init:
securityContext: {}
# allowPrivilegeEscalation: false
# capabilities:
# drop:
# - ALL
# privileged: false
# readOnlyRootFilesystem: true
# runAsGroup: 1337
# runAsNonRoot: true
# runAsUser: 1337

0 comments on commit d44518b

Please sign in to comment.