-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(common/request_id): add request_id support (#444)
close #360
- Loading branch information
Showing
15 changed files
with
232 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# -*- 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 uuid | ||
|
||
from werkzeug.local import Local as _Local | ||
from werkzeug.local import release_local | ||
|
||
_local = _Local() | ||
|
||
|
||
def new_request_id(): | ||
return uuid.uuid4().hex | ||
|
||
|
||
class Singleton(object): | ||
_instance = None | ||
|
||
def __new__(cls, *args, **kwargs): | ||
if not isinstance(cls._instance, cls): | ||
cls._instance = object.__new__(cls, *args, **kwargs) | ||
return cls._instance | ||
|
||
|
||
class Local(Singleton): | ||
"""local对象 | ||
必须配合中间件RequestProvider使用 | ||
""" | ||
|
||
@property | ||
def request(self): | ||
"""获取全局request对象""" | ||
request = getattr(_local, "request", None) | ||
# if not request: | ||
# raise RuntimeError("request object not in local") | ||
return request | ||
|
||
@request.setter | ||
def request(self, value): | ||
"""设置全局request对象""" | ||
_local.request = value | ||
|
||
@property | ||
def request_id(self): | ||
# celery后台没有request对象 | ||
if self.request: | ||
return self.request.request_id | ||
|
||
return new_request_id() | ||
|
||
def get_http_request_id(self): | ||
"""从接入层获取request_id,或者生成一个新的request_id""" | ||
# 在从header中获取 | ||
request_id = self.request.META.get("HTTP_X_REQUEST_ID") or self.request.META.get("HTTP_X_BKAPI_REQUEST_ID", "") | ||
|
||
if request_id: | ||
return request_id | ||
|
||
# 最后主动生成一个 | ||
return new_request_id() | ||
|
||
@property | ||
def request_username(self) -> str: | ||
try: | ||
# celery后台,openAPI都可能没有user,需要判断 | ||
if self.request and hasattr(self.request, "user"): | ||
return self.request.user.username | ||
except Exception: # pylint: disable=broad-except | ||
return "" | ||
|
||
return "" | ||
|
||
def release(self): | ||
release_local(_local) | ||
|
||
|
||
local = Local() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.