-
Notifications
You must be signed in to change notification settings - Fork 4
Add logging service for capturing and uploading console logs #9
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
Open
Dwij1704
wants to merge
4
commits into
main
Choose a base branch
from
upload-log
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9294810
Add logging service for capturing and uploading console logs
Dwij1704 7056bcd
Merge branch 'main' into upload-log
Dwij1704 5a0b2b2
Enhance Client class with flush method for log uploads. Refactor trac…
Dwij1704 2826fb2
Refactor logging functionality by moving logging service to console-l…
Dwij1704 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
This file was deleted.
Oops, something went wrong.
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,39 @@ | ||
| # Log Upload Functionality | ||
|
|
||
| Simple log capture and upload functionality for AgentOps TypeScript SDK, matching the Python SDK implementation. | ||
|
|
||
| ## How it works | ||
|
|
||
| 1. When the SDK is initialized, console methods (log, info, warn, error, debug) are automatically patched | ||
| 2. All console output is captured to an in-memory buffer with timestamps | ||
| 3. Logs can be uploaded to the API using `uploadLogFile(traceId)` | ||
| 4. Buffer is cleared after successful upload | ||
|
|
||
| ## Usage | ||
|
|
||
| ```typescript | ||
| import { agentops } from 'agentops'; | ||
|
|
||
| // Initialize SDK - starts capturing console output | ||
| await agentops.init({ apiKey: 'your-api-key' }); | ||
|
|
||
| // Your application code - all console output is captured | ||
| console.log('Application started'); | ||
| console.error('An error occurred'); | ||
|
|
||
| // Upload logs when needed | ||
| const result = await agentops.uploadLogFile('trace-123'); | ||
| if (result) { | ||
| console.log(`Logs uploaded: ${result.id}`); | ||
| } | ||
|
|
||
| // Shutdown SDK | ||
| await agentops.shutdown(); | ||
| ``` | ||
|
|
||
| ## Implementation Details | ||
|
|
||
| - **Buffer**: Simple array-based buffer that stores timestamped log entries | ||
| - **Format**: `YYYY-MM-DDTHH:mm:ss.sssZ - LEVEL - message` | ||
| - **API Endpoint**: POST to `/v4/logs/upload/` with trace ID in headers | ||
| - **Cleanup**: Original console methods restored on SDK shutdown | ||
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,39 @@ | ||
| /** | ||
| * Simple memory buffer for capturing console logs | ||
| */ | ||
| export class LogBuffer { | ||
| private buffer: string[] = []; | ||
|
|
||
| /** | ||
| * Append a log entry to the buffer | ||
| */ | ||
| append(entry: string): void { | ||
| const timestamp = new Date().toISOString(); | ||
| const formattedEntry = `${timestamp} - ${entry}`; | ||
| this.buffer.push(formattedEntry); | ||
| } | ||
|
|
||
| /** | ||
| * Get all buffer content as a single string | ||
| */ | ||
| getContent(): string { | ||
| return this.buffer.join('\n'); | ||
| } | ||
|
|
||
| /** | ||
| * Clear the buffer | ||
| */ | ||
| clear(): void { | ||
| this.buffer = []; | ||
| } | ||
|
|
||
| /** | ||
| * Check if buffer is empty | ||
| */ | ||
| isEmpty(): boolean { | ||
| return this.buffer.length === 0; | ||
| } | ||
| } | ||
|
|
||
| // Global log buffer instance | ||
| export const globalLogBuffer = new LogBuffer(); |
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,3 @@ | ||
| export { LogBuffer, globalLogBuffer } from './buffer'; | ||
| export { LoggingInstrumentor, loggingInstrumentor } from './instrumentor'; | ||
| export { LoggingService, loggingService } from './service'; |
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,82 @@ | ||
| import { globalLogBuffer } from './buffer'; | ||
|
|
||
| export class LoggingInstrumentor { | ||
Dwij1704 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| private originalMethods: Map<string, Function> = new Map(); | ||
| private isPatched: boolean = false; | ||
|
|
||
| /** | ||
| * Patch console methods to capture output to the log buffer | ||
| */ | ||
| patch(): void { | ||
| if (this.isPatched) { | ||
| return; | ||
| } | ||
|
|
||
| // List of console methods to patch | ||
| const methodsToPatch = ['log', 'info', 'warn', 'error', 'debug']; | ||
|
|
||
| methodsToPatch.forEach(method => { | ||
| const originalMethod = (console as any)[method]; | ||
| this.originalMethods.set(method, originalMethod); | ||
|
|
||
| // Create a patched version that logs to buffer and calls original | ||
| (console as any)[method] = (...args: any[]) => { | ||
| // Format the message | ||
| const message = args | ||
| .map(arg => { | ||
| if (typeof arg === 'object') { | ||
| try { | ||
| return JSON.stringify(arg); | ||
| } catch { | ||
| return String(arg); | ||
| } | ||
| } | ||
| return String(arg); | ||
| }) | ||
| .join(' '); | ||
|
|
||
| // Add level prefix and append to buffer | ||
| const levelPrefix = method.toUpperCase(); | ||
| globalLogBuffer.append(`${levelPrefix} - ${message}`); | ||
|
|
||
| // Call the original method | ||
| originalMethod.apply(console, args); | ||
| }; | ||
| }); | ||
|
|
||
| this.isPatched = true; | ||
| } | ||
|
|
||
| /** | ||
| * Restore original console methods | ||
| */ | ||
| unpatch(): void { | ||
Dwij1704 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (!this.isPatched) { | ||
| return; | ||
| } | ||
|
|
||
| this.originalMethods.forEach((originalMethod, method) => { | ||
| (console as any)[method] = originalMethod; | ||
| }); | ||
|
|
||
| this.originalMethods.clear(); | ||
| this.isPatched = false; | ||
| } | ||
|
|
||
| /** | ||
| * Setup cleanup handlers to restore console on exit | ||
| */ | ||
| setupCleanup(): void { | ||
| const cleanup = () => { | ||
| this.unpatch(); | ||
| globalLogBuffer.clear(); | ||
| }; | ||
|
|
||
| process.on('exit', cleanup); | ||
| process.on('SIGINT', cleanup); | ||
| process.on('SIGTERM', cleanup); | ||
| } | ||
| } | ||
|
|
||
| // Global logging instrumentor instance | ||
| export const loggingInstrumentor = new LoggingInstrumentor(); | ||
Dwij1704 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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.