Skip to content
New issue

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

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

Already on GitHub? # to your account

Ft add sentry #306

Merged
merged 5 commits into from
Mar 8, 2022
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
11 changes: 10 additions & 1 deletion deploy/helm/bk-user/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,22 @@ global:
默认未开启,如需开启请将 `global.serviceMonitor.enabled` 设置为 true。

##### `values.yaml` 配置示例:

```yaml
global:
serviceMonitor:
enabled: true
```

### 9. 安装
### 9. 配置sentry

```yaml
global:
## sentry dsn
sentryDsn: "http://12927b5f211046b575ee51fd8b1ac34f@{SENTRY_DOMAIN}/{PROJECT_ID}"
```

### 10. 安装

如果你已经准备好了 `values.yaml`,就可以直接进行安装操作了

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ data:
# ESB Api 访问地址
BK_COMPONENT_API_URL: "{{ .Values.bkComponentApiUrl }}"
# 由于用户管理先于权限中心拉起,所以默认禁用,后期所有产品就绪后,可手动开启
ENABLE_IAM: "{{ .Values.global.enableIAM }}"
ENABLE_IAM: "{{ .Values.global.enableIAM }}"
# Sentry DSN配置, 非空则开启
SENTRY_DSN: "{{ .Values.global.sentryDsn }}"
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ data:
ENCRYPT_SECRET_KEY: {{ .Values.encryptSecretKey }}
# 用于拼接修改密码 URL
BK_USERMGR_SAAS_URL: {{ .Values.bkUserUrl }}
# Sentry DSN配置, 非空则开启
SENTRY_DSN: "{{ .Values.global.sentryDsn }}"
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ data:
# ESB Api 访问地址
BK_COMPONENT_API_URL: "{{ .Values.bkComponentApiUrl }}"
# 由于用户管理先于权限中心拉起,所以默认禁用,后期所有产品就绪后,可手动开启
ENABLE_IAM: "{{ .Values.global.enableIAM }}"
ENABLE_IAM: "{{ .Values.global.enableIAM }}"
# Sentry DSN配置, 非空则开启
SENTRY_DSN: "{{ .Values.global.sentryDsn }}"
4 changes: 3 additions & 1 deletion deploy/helm/bk-user/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ global:
bkDomainScheme: "http"
## 是否开启权限中心
enableIAM: false
## sentry dsn
sentryDsn: ""

hostAliases: []
# - ip: ""
Expand Down Expand Up @@ -91,4 +93,4 @@ redis:
replica:
replicaCount: 1
persistence:
enabled: false
enabled: false
5 changes: 5 additions & 0 deletions src/api/bkuser_core/common/exception_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from rest_framework.response import Response
from rest_framework.status import HTTP_400_BAD_REQUEST
from rest_framework.views import exception_handler
from sentry_sdk import capture_exception

from .error_codes import CoreAPIError
from .http import exist_force_raw_header
Expand Down Expand Up @@ -68,7 +69,11 @@ def get_ee_exception_response(exc, context):
elif isinstance(exc, AuthenticationFailed):
data["message"] = "403, authentication failed"
else:
# log
logger.exception("unknown exception while handling the request")
# report to sentry
capture_exception(exc)
# build response
data["message"] = UNKNOWN_ERROR_HINT
data["code"] = -1

Expand Down
5 changes: 5 additions & 0 deletions src/api/bkuser_core/config/common/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
COMMON_HEALTHZ_TOKEN = "56f17d8034234e92801ab59479e1259d"
HEALTHZ_PROBES: list = []

# ==============================================================================
# Sentry
# ==============================================================================
SENTRY_DSN = env("SENTRY_DSN", default="")

# ==============================================================================
# 全局应用配置
# ==============================================================================
Expand Down
20 changes: 20 additions & 0 deletions src/api/bkuser_core/monitoring/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-用户管理(Bk-User) available.
Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""
from django.apps import AppConfig

from .sentry import init_sentry_sdk


class MonitoringConfig(AppConfig):
name = 'bkuser_core.monitoring'

def ready(self):
init_sentry_sdk()
43 changes: 43 additions & 0 deletions src/api/bkuser_core/monitoring/sentry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-用户管理(Bk-User) available.
Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""
import sentry_sdk
from sentry_sdk.integrations.celery import CeleryIntegration
from sentry_sdk.integrations.django import DjangoIntegration
from sentry_sdk.integrations.redis import RedisIntegration


def init_sentry_sdk():
"""Register celery error events to sentry"""
from django.conf import settings

if settings.SENTRY_DSN:
# 初始化 sentry_sdk
sentry_sdk.init(
# debug=True,
dsn=settings.SENTRY_DSN,
integrations=[DjangoIntegration(), CeleryIntegration(), RedisIntegration()],
# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for performance monitoring.
# We recommend adjusting this value in production,
traces_sample_rate=1.0,
# If you wish to associate users to errors (assuming you are using
# django.contrib.auth) you may enable sending PII data.
send_default_pii=True,
# By default the SDK will try to use the SENTRY_RELEASE
# environment variable, or infer a git commit
# SHA as release, however you may want to set
# something more human-readable.
# release="myapp@1.0.0",
# Can export the environment
# environment="production",
)
# set global tag
sentry_sdk.set_tag('service_name', 'bk-user-api')
41 changes: 40 additions & 1 deletion src/api/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/api/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pydantic = "^1.8.2"
django-environ = "^0.4.5"
django-prometheus = "^2.1.0"
pyyaml = "^6.0"
sentry-sdk = "1.5.6"

[tool.poetry.dev-dependencies]
ipython = "^7.15.0"
Expand Down Expand Up @@ -69,4 +70,4 @@ url = "https://mirrors.tencent.com/pypi/simple/"

[tool.pytest.ini_options]
DJANGO_SETTINGS_MODULE = "bkuser_core.config.overlays.dev"
addopts = "--disable-pytest-warnings --reuse-db"
addopts = "--disable-pytest-warnings --reuse-db"
1 change: 1 addition & 0 deletions src/#/bklogin/config/common/django_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"bklogin.bkaccount",
"bklogin.bkauth",
"bklogin.bk_i18n",
"bklogin.monitoring",
)

MIDDLEWARE = (
Expand Down
16 changes: 16 additions & 0 deletions src/#/bklogin/config/common/system.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-用户管理(Bk-User) available.
Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""
from . import env

# ==============================================================================
# Sentry
# ==============================================================================
SENTRY_DSN = env("SENTRY_DSN", default="")
1 change: 1 addition & 0 deletions src/#/bklogin/config/overlays/dev.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ from bklogin.config.common.logging import * # noqa
from bklogin.config.common.platform import * # noqa
from bklogin.config.common.plugin import * # noqa
from bklogin.config.common.storage import * # noqa
from bklogin.config.common.system import * # noqa

from bkuser_global.logging import LoggingType, get_logging

Expand Down
1 change: 1 addition & 0 deletions src/#/bklogin/config/overlays/prod.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from bklogin.config.common.platform import * # noqa
from bklogin.config.common.plugin import * # noqa
from bklogin.config.common.storage import * # noqa
from bklogin.config.common.system import * # noqa

from bkuser_global.logging import LoggingType, get_logging

Expand Down
10 changes: 10 additions & 0 deletions src/#/bklogin/monitoring/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-用户管理(Bk-User) available.
Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""
21 changes: 21 additions & 0 deletions src/#/bklogin/monitoring/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
"""
Tencent is pleased to support the open source community by making 蓝鲸智云PaaS平台社区版 (BlueKing PaaS
Community Edition) available.
Copyright (C) 2017-2018 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""
from django.apps import AppConfig

from .sentry import init_sentry_sdk


class MonitoringConfig(AppConfig):
name = 'bklogin.monitoring'

def ready(self):
init_sentry_sdk()
43 changes: 43 additions & 0 deletions src/#/bklogin/monitoring/sentry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# -*- coding: utf-8 -*-
"""
Tencent is pleased to support the open source community by making 蓝鲸智云PaaS平台社区版 (BlueKing PaaS
Community Edition) available.
Copyright (C) 2017-2018 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""
import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration
from sentry_sdk.integrations.redis import RedisIntegration


def init_sentry_sdk():
"""Register celery error events to sentry"""
from django.conf import settings

if settings.SENTRY_DSN:
# 初始化 sentry_sdk
sentry_sdk.init(
# debug=True,
dsn=settings.SENTRY_DSN,
integrations=[DjangoIntegration(), RedisIntegration()],
# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for performance monitoring.
# We recommend adjusting this value in production,
traces_sample_rate=1.0,
# If you wish to associate users to errors (assuming you are using
# django.contrib.auth) you may enable sending PII data.
send_default_pii=True,
# By default the SDK will try to use the SENTRY_RELEASE
# environment variable, or infer a git commit
# SHA as release, however you may want to set
# something more human-readable.
# release="myapp@1.0.0",
# Can export the environment
# environment="production",
)
# set global tag
sentry_sdk.set_tag('service_name', 'bk-login')
Loading