-
Notifications
You must be signed in to change notification settings - Fork 186
Feat/logger #1353
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
Merged
Merged
Feat/logger #1353
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
60ccab4
Add LOG_LEVEL env var for log level configuration
SAKURA-CAT 82e82e4
Refactor metrics upload logic and add batch uploader
SAKURA-CAT 4f3422e
Improve logger level handling and add tests
SAKURA-CAT c6505f8
Refactor batch uploader to simplify request handling
SAKURA-CAT cb80473
Refactor SwanLog tests for better isolation and flexibility
SAKURA-CAT 1932db0
Remove LOG_LEVEL from environment in reset_some_env
SAKURA-CAT 2e5d32f
Improve log level handling and test cleanup
SAKURA-CAT ed445e9
Refactor uploader URL usage and update tests
SAKURA-CAT d8bcab1
Fix handling of None data in trace_metrics
SAKURA-CAT File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,108 @@ | ||
| """ | ||
| @author: cunyue | ||
| @file: batch.py | ||
| @time: 2025/12/4 13:50 | ||
| @description: 分批上传函数 | ||
| """ | ||
|
|
||
| import time | ||
| from typing import Union, Literal, List, TypedDict, Dict | ||
|
|
||
| from swanlab.core_python import get_client | ||
| from swanlab.log import swanlog | ||
|
|
||
|
|
||
| # 上传指标数据 | ||
| class MetricDict(TypedDict): | ||
| projectId: str | ||
| experimentId: str | ||
| type: str | ||
| metrics: List[dict] | ||
| flagId: Union[str, None] | ||
|
|
||
|
|
||
| def create_data(metrics: List[dict], metrics_type: str) -> MetricDict: | ||
| """ | ||
| 携带上传日志的指标信息 | ||
| """ | ||
| client = get_client() | ||
| # Move 等实验需要将数据上传到根实验上 | ||
| exp_id = client.exp.root_exp_cuid or client.exp.cuid | ||
| proj_id = client.exp.root_proj_cuid or client.proj.cuid | ||
| assert proj_id is not None, "Project ID is empty." | ||
| assert exp_id is not None, "Experiment ID is empty." | ||
| flag_id = client.exp.flag_id | ||
| return { | ||
| "projectId": proj_id, | ||
| "experimentId": exp_id, | ||
| "type": metrics_type, | ||
| "metrics": metrics, | ||
| "flagId": flag_id, | ||
| } | ||
|
|
||
|
|
||
| def _generate_chunks(data: Union[MetricDict, Dict, List], per_request_len: int): | ||
| """ | ||
| 生成器:统一处理字典和列表的分片逻辑 | ||
| yield: 分片后的数据块 | ||
| """ | ||
| # 情况1: 不分批 | ||
| if per_request_len == -1: | ||
| yield data | ||
| return | ||
|
|
||
| # 情况2: 字典分批 | ||
| if isinstance(data, dict): | ||
| metrics = data.get('metrics', []) | ||
| # 如果 metrics 为空或长度不足,视为不需要分片的一整块 | ||
| if len(metrics) <= per_request_len: | ||
| yield data | ||
| else: | ||
| for i in range(0, len(metrics), per_request_len): | ||
| yield { | ||
| **data, | ||
| "metrics": metrics[i : i + per_request_len], | ||
| } | ||
|
|
||
| # 情况3: 列表分批 | ||
| elif isinstance(data, list): | ||
| if len(data) <= per_request_len: | ||
| yield data | ||
| else: | ||
| for i in range(0, len(data), per_request_len): | ||
| yield data[i : i + per_request_len] | ||
|
|
||
|
|
||
| def trace_metrics( | ||
| url: str, | ||
| data: Union[MetricDict, list] = None, | ||
| method: Literal['post', 'put'] = 'post', | ||
| per_request_len: int = 1000, | ||
| ): | ||
| """ | ||
| 分片指标上传方法 | ||
| """ | ||
| # 判断是否开启了分片模式(用于决定是否 sleep) | ||
| # 这里的逻辑是:如果 per_request_len 不是 -1,且数据量确实超过了限制,则认为是分片模式 | ||
| is_split_mode = False | ||
| if per_request_len != -1: | ||
| total_len = len(data.get('metrics', [])) if isinstance(data, dict) else len(data or []) | ||
| if total_len == 0: | ||
| return | ||
| is_split_mode = total_len > per_request_len | ||
| client = get_client() | ||
| # 遍历生成器产生的每一个数据块 | ||
| for chunk in _generate_chunks(data, per_request_len): | ||
| # TODO: 暂时注释掉前置检查 | ||
| # 如果在发送过程中 client 变成了 pending,则中断后续发送 | ||
| # if client.pending: | ||
| # break | ||
|
|
||
| # 调用被装饰的发送函数 | ||
| _, resp = getattr(client, method)(url, chunk) | ||
| # 后置检查 | ||
| if resp and resp.status_code == 202: | ||
| client.pending = True | ||
| swanlog.warning(f"Client set to pending due to 202 response: {url}") | ||
| # 分批发送时需要 sleep | ||
| is_split_mode and time.sleep(1) | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.