Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 监控告警内容注入自定义文档链接 #1339

Merged
merged 5 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apiserver/paasng/paasng/accessories/smart_advisor/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def __init__(self, tag_value):
AppFeatureTag = type("AppFeatureTag", (BaseFixedTypeTag,), {"tag_type": "app-feature"})
DeploymentFailureTag = type("DeploymentFailureTag", (BaseFixedTypeTag,), {"tag_type": "deploy-failure"})
DeployPhaseTag = type("DeployPhaseTag", (BaseFixedTypeTag,), {"tag_type": "deploy_phase"})
SaasMonitorTag = type("SaasMonitorTag", (BaseFixedTypeTag,), {"tag_type": "saas_monitor"})


TAG_TYPES: List[Type[BaseFixedTypeTag]] = [
Expand All @@ -73,6 +74,7 @@ def __init__(self, tag_value):
AppFeatureTag,
DeploymentFailureTag,
DeployPhaseTag,
SaasMonitorTag,
]
TAG_TYPES_MAP = {t.tag_type: t for t in TAG_TYPES}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
from django.conf import settings
from django.utils.translation import gettext_lazy as _

from paasng.accessories.smart_advisor.advisor import DocumentaryLinkAdvisor
from paasng.accessories.smart_advisor.tags import get_dynamic_tag
from paasng.infras.bkmonitorv3.client import make_bk_monitor_client
from paasng.infras.bkmonitorv3.shim import get_or_create_bk_monitor_space
from paasng.misc.monitoring.monitor.alert_rules.config import RuleConfig
Expand Down Expand Up @@ -73,6 +75,24 @@ def _validate(self, rule_configs: List[RuleConfig]):
f"app_code({self.app_code})"
)

def _update_metric_name_prefixes(self, ctx: dict, alert_code: str):
"""根据告警代码添加需要的指标前缀到上下文中"""
alert_config_mapping = {
"rabbitmq": settings.RABBITMQ_MONITOR_CONF,
"bkrepo": settings.BKREPO_MONITOR_CONF,
"gcs_mysql": settings.GCS_MYSQL_MONITOR_CONF,
}
for code_snippet, config in alert_config_mapping.items():
if code_snippet in alert_code:
ctx[f"{code_snippet}_metric_name_prefix"] = config.get("metric_name_prefix", "")

def _update_docs_url(self, ctx: dict, alert_code: str):
"""根据告警代码添加需要的文档信息"""
tag = get_dynamic_tag(f"saas_monitor:{alert_code}")
links = DocumentaryLinkAdvisor().search_by_tags([tag], limit=1)
if links:
ctx["doc_url"] = f"处理建议: {links[0].location}"

def _render_rule_configs(self, rule_configs: List[RuleConfig]) -> Dict:
"""按照 MonitorAsCode 规则, 渲染出如下示例目录结构:

Expand All @@ -88,14 +108,10 @@ def _render_rule_configs(self, rule_configs: List[RuleConfig]) -> Dict:
for conf in rule_configs:
ctx = conf.to_dict()
ctx["notice_group_name"] = self.default_notice_group_name
# 涉及到 rabbitmq 的告警策略, 指标是通过 bkmonitor 配置的采集器采集, 需要添加指标前缀
if "rabbitmq" in conf.alert_code:
ctx["rabbitmq_metric_name_prefix"] = settings.RABBITMQ_MONITOR_CONF.get("metric_name_prefix", "")
if "bkrepo" in conf.alert_code:
ctx["bkrepo_metric_name_prefix"] = settings.BKREPO_MONITOR_CONF.get("metric_name_prefix", "")
if "gcs_mysql" in conf.alert_code:
ctx["gcs_mysql_metric_name_prefix"] = settings.GCS_MYSQL_MONITOR_CONF.get("metric_name_prefix", "")

# 涉及到增强服务的告警策略, 指标是通过 bkmonitor 配置的采集器采集, 需要添加指标前缀
self._update_metric_name_prefixes(ctx, conf.alert_code)
# 为通知添加操作文档链接,若未在'管理后台--智能顾问--文档管理'配置文档则不添加
self._update_docs_url(ctx, conf.alert_code)
configs[f"rule/{conf.alert_rule_name}.yaml"] = j2_env.get_template(f"{conf.alert_code}.yaml.j2").render(
**ctx
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,33 @@ notice: # 通知配置
interval: 30 # 通知收敛间隔(分钟)
user_groups: # 通知组配置
- '{{ notice_group_name }}'
template:
SheepSheepChen marked this conversation as resolved.
Show resolved Hide resolved
abnormal:
content: '{% raw %}{{content.level}}

{{content.begin_time}}

{{content.time}}

{{content.duration}}

{{content.target_type}}

{{content.data_source}}

{{content.content}}{% endraw %} {{ doc_url }}

{% raw %}{{content.current_value}}

{{content.biz}}

{{content.target}}

{{content.dimension}}

{{content.detail}}

{{content.assign_detail}}

{{content.related_info}}'
title: '{{business.bk_biz_name}} - {{alarm.name}}{{alarm.display_type}}{% endraw %}'
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,33 @@ notice: # 通知配置
interval: 30 # 通知收敛间隔(分钟)
user_groups: # 通知组配置
- '{{ notice_group_name }}'
template:
abnormal:
content: '{% raw %}{{content.level}}

{{content.begin_time}}

{{content.time}}

{{content.duration}}

{{content.target_type}}

{{content.data_source}}

{{content.content}}{% endraw %} {{ doc_url }}

{% raw %}{{content.current_value}}

{{content.biz}}

{{content.target}}

{{content.dimension}}

{{content.detail}}

{{content.assign_detail}}

{{content.related_info}}'
title: '{{business.bk_biz_name}} - {{alarm.name}}{{alarm.display_type}}{% endraw %}'
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,33 @@ notice: # 通知配置
interval: 120 # 通知收敛间隔(分钟)
user_groups: # 通知组配置
- '{{ notice_group_name }}'
template:
abnormal:
content: '{% raw %}{{content.level}}

{{content.begin_time}}

{{content.time}}

{{content.duration}}

{{content.target_type}}

{{content.data_source}}

{{content.content}}{% endraw %} {{ doc_url }}

{% raw %}{{content.current_value}}

{{content.biz}}

{{content.target}}

{{content.dimension}}

{{content.detail}}

{{content.assign_detail}}

{{content.related_info}}'
title: '{{business.bk_biz_name}} - {{alarm.name}}{{alarm.display_type}}{% endraw %}'
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,33 @@ notice: # 通知配置
interval: 120 # 通知收敛间隔(分钟)
user_groups: # 通知组配置
- '{{ notice_group_name }}'
template:
abnormal:
content: '{% raw %}{{content.level}}

{{content.begin_time}}

{{content.time}}

{{content.duration}}

{{content.target_type}}

{{content.data_source}}

{{content.content}}{% endraw %} {{ doc_url }}

{% raw %}{{content.current_value}}

{{content.biz}}

{{content.target}}

{{content.dimension}}

{{content.detail}}

{{content.assign_detail}}

{{content.related_info}}'
title: '{{business.bk_biz_name}} - {{alarm.name}}{{alarm.display_type}}{% endraw %}'
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,33 @@ notice: # 通知配置
interval: 30 # 通知收敛间隔(分钟)
user_groups: # 通知组配置
- '{{ notice_group_name }}'
template:
abnormal:
content: '{% raw %}{{content.level}}

{{content.begin_time}}

{{content.time}}

{{content.duration}}

{{content.target_type}}

{{content.data_source}}

{{content.content}}{% endraw %} {{ doc_url }}

{% raw %}{{content.current_value}}

{{content.biz}}

{{content.target}}

{{content.dimension}}

{{content.detail}}

{{content.assign_detail}}

{{content.related_info}}'
title: '{{business.bk_biz_name}} - {{alarm.name}}{{alarm.display_type}}{% endraw %}'
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,33 @@ notice: # 通知配置
interval: 120 # 通知收敛间隔(分钟)
user_groups: # 通知组配置
- '{{ notice_group_name }}'
template:
abnormal:
content: '{% raw %}{{content.level}}

{{content.begin_time}}

{{content.time}}

{{content.duration}}

{{content.target_type}}

{{content.data_source}}

{{content.content}}{% endraw %} {{ doc_url }}

{% raw %}{{content.current_value}}

{{content.biz}}

{{content.target}}

{{content.dimension}}

{{content.detail}}

{{content.assign_detail}}

{{content.related_info}}'
title: '{{business.bk_biz_name}} - {{alarm.name}}{{alarm.display_type}}{% endraw %}'
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,33 @@ notice: # 通知配置
interval: 30 # 通知收敛间隔(分钟)
user_groups: # 通知组配置
- '{{ notice_group_name }}'
template:
abnormal:
content: '{% raw %}{{content.level}}

{{content.begin_time}}

{{content.time}}

{{content.duration}}

{{content.target_type}}

{{content.data_source}}

{{content.content}}{% endraw %} {{ doc_url }}
SheepSheepChen marked this conversation as resolved.
Show resolved Hide resolved

{% raw %}{{content.current_value}}

{{content.biz}}

{{content.target}}

{{content.dimension}}

{{content.detail}}

{{content.assign_detail}}

{{content.related_info}}'
title: '{{business.bk_biz_name}} - {{alarm.name}}{{alarm.display_type}}{% endraw %}'