-
Notifications
You must be signed in to change notification settings - Fork 2
Add deployment logs fetch support in SDK #132
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
08a5eb2
Add deployment logs fetch support in SDK
anandj91 e5adbfe
Add support for streaming mode in logs
anandj91 1fb56d3
Fix lint and mypy
anandj91 c2e114e
Fix example
anandj91 4ed0a63
Fix
anandj91 5dbb716
CHange deployment id back to 1234
anandj91 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,75 @@ | ||
| from datetime import datetime, timezone, timedelta | ||
|
|
||
| from centml.sdk.api import get_centml_client | ||
|
|
||
| # --- Configuration --- | ||
| DEPLOYMENT_ID = 1234 # Replace with your deployment ID | ||
| REVISION_NUMBER = 10 | ||
| HOURS_BACK = 1 # Fetch logs from the last N hours | ||
|
|
||
|
|
||
| def format_event(event: dict) -> str: | ||
| timestamp_ms = ( | ||
| event.get("timestamp") | ||
| or event.get("time") | ||
| or event.get("ts") | ||
| or "" | ||
| ) | ||
| message = ( | ||
| event.get("message") | ||
| or event.get("msg") | ||
| or event.get("log") | ||
| or str(event) | ||
| ) | ||
| if timestamp_ms: | ||
| ts = datetime.fromtimestamp(int(timestamp_ms) / 1000, tz=timezone.utc).isoformat() | ||
| return f"[{ts}] {message}" | ||
| return message | ||
|
|
||
|
|
||
| def main(): | ||
| stream = True | ||
| end_time = int(datetime.now(timezone.utc).timestamp() * 1000) | ||
| start_time = end_time - int(timedelta(hours=HOURS_BACK).total_seconds() * 1000) | ||
|
|
||
| print(f"Fetching logs for deployment {DEPLOYMENT_ID}") | ||
| print( | ||
| f"Time window: " | ||
| f"{datetime.fromtimestamp(start_time / 1000, tz=timezone.utc).isoformat()} → " | ||
| f"{datetime.fromtimestamp(end_time / 1000, tz=timezone.utc).isoformat()}" | ||
| ) | ||
| print() | ||
|
|
||
| with get_centml_client() as cclient: | ||
| if stream: | ||
| # Streaming: print events as each page arrives | ||
| for event in cclient.get_deployment_logs( | ||
| deployment_id=DEPLOYMENT_ID, | ||
| revision_number=REVISION_NUMBER, | ||
| start_time=start_time, | ||
| end_time=end_time, | ||
| start_from_head=False, | ||
| stream=stream, | ||
| ): | ||
| print(format_event(event)) | ||
| else: | ||
| # Batch: collect all events then process | ||
| events = cclient.get_deployment_logs( | ||
| deployment_id=DEPLOYMENT_ID, | ||
| revision_number=REVISION_NUMBER, | ||
| start_time=start_time, | ||
| end_time=end_time, | ||
| start_from_head=False, | ||
| ) | ||
|
|
||
| if not events: | ||
| print("No logs found in the given time window.") | ||
| return | ||
|
|
||
| print(f"Found {len(events)} log entries:\n") | ||
| for event in events: | ||
| print(format_event(event)) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
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.