-
Notifications
You must be signed in to change notification settings - Fork 236
Per-backend log filtering #1312
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
Draft
danielholanda
wants to merge
4
commits into
main
Choose a base branch
from
dholanda/logs
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.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import React, { useEffect, useRef, useState } from 'react'; | ||
| import React, { useCallback, useEffect, useRef, useState } from 'react'; | ||
| import { getAPIKey, getServerBaseUrl, onServerUrlChange, serverConfig } from './utils/serverConfig'; | ||
| import {EventSource} from 'eventsource'; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
|
|
@@ -7,6 +7,11 @@ interface LogsWindowProps { | |
| height?: number; | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| interface LogSource { | ||
| name: string; | ||
| label: string; | ||
| } | ||
|
|
||
| const BOTTOM_FOLLOW_THRESHOLD_PX = 60; | ||
|
|
||
| const LogsWindow: React.FC<LogsWindowProps> = ({ isVisible, height }) => { | ||
|
|
@@ -23,6 +28,10 @@ const LogsWindow: React.FC<LogsWindowProps> = ({ isVisible, height }) => { | |
| const [apiKey, setAPIKey] = useState<string>(''); | ||
| const [isInitialized, setIsInitialized] = useState(false); | ||
|
|
||
| const [selectedSource, setSelectedSource] = useState<string>('all'); | ||
| const [availableSources, setAvailableSources] = useState<LogSource[]>([]); | ||
| const sourcePollRef = useRef<NodeJS.Timeout | null>(null); | ||
|
|
||
| const isNearBottom = () => { | ||
| const logsContent = logsContentRef.current; | ||
| if (!logsContent) return true; | ||
|
|
@@ -38,13 +47,11 @@ const LogsWindow: React.FC<LogsWindowProps> = ({ isVisible, height }) => { | |
| isProgrammaticScrollRef.current = true; | ||
| logsEndRef.current.scrollIntoView({ behavior: 'auto', block: 'end' }); | ||
|
|
||
| // Keep programmatic-scroll guard through the next paint. | ||
| requestAnimationFrame(() => { | ||
| isProgrammaticScrollRef.current = false; | ||
| }); | ||
| }; | ||
|
|
||
| // Wait for serverConfig to initialize and get the correct URL | ||
| useEffect(() => { | ||
| serverConfig.waitForInit().then(() => { | ||
| setServerUrl(getServerBaseUrl()); | ||
|
|
@@ -53,7 +60,6 @@ const LogsWindow: React.FC<LogsWindowProps> = ({ isVisible, height }) => { | |
| }); | ||
| }, []); | ||
|
|
||
| // Listen for URL changes (covers both port changes and explicit URL updates) | ||
| useEffect(() => { | ||
| const unsubscribe = onServerUrlChange((newUrl: string, newAPIKey: string) => { | ||
| console.log('Server URL changed, updating logs URL:', newUrl); | ||
|
|
@@ -66,6 +72,45 @@ const LogsWindow: React.FC<LogsWindowProps> = ({ isVisible, height }) => { | |
| }; | ||
| }, []); | ||
|
|
||
| // Poll for available log sources | ||
| const fetchSources = useCallback(async () => { | ||
| if (!serverUrl || !isInitialized) return; | ||
|
|
||
| try { | ||
| const headers: Record<string, string> = {}; | ||
| if (apiKey) { | ||
| headers['Authorization'] = `Bearer ${apiKey}`; | ||
| } | ||
| const resp = await fetch(`${serverUrl}/api/v1/logs/sources`, { headers }); | ||
| if (resp.ok) { | ||
| const data = await resp.json(); | ||
| setAvailableSources(data.sources || []); | ||
| } | ||
| } catch { | ||
| // Silently ignore — the sources list is a convenience, not critical | ||
| } | ||
| }, [serverUrl, apiKey, isInitialized]); | ||
|
|
||
| useEffect(() => { | ||
| if (!isVisible || !isInitialized || !serverUrl) { | ||
| if (sourcePollRef.current) { | ||
| clearInterval(sourcePollRef.current); | ||
| sourcePollRef.current = null; | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| fetchSources(); | ||
| sourcePollRef.current = setInterval(fetchSources, 10000); | ||
|
|
||
| return () => { | ||
| if (sourcePollRef.current) { | ||
| clearInterval(sourcePollRef.current); | ||
| sourcePollRef.current = null; | ||
| } | ||
| }; | ||
| }, [isVisible, isInitialized, serverUrl, apiKey, fetchSources]); | ||
|
|
||
| // Auto-scroll to bottom when new logs arrive (if auto-scroll is enabled) | ||
| useEffect(() => { | ||
| if (autoScroll) { | ||
|
|
@@ -95,11 +140,9 @@ const LogsWindow: React.FC<LogsWindowProps> = ({ isVisible, height }) => { | |
| return () => logsContent.removeEventListener('scroll', handleScroll); | ||
| }, []); | ||
|
|
||
| // Connect to SSE log stream | ||
| // Connect to SSE log stream — reconnects when selectedSource changes | ||
| useEffect(() => { | ||
| // Don't connect until we have the correct URL from initialization | ||
| if (!isVisible || !isInitialized || !serverUrl) { | ||
| // Clean up connection when logs window is hidden or not ready | ||
| if (eventSourceRef.current) { | ||
| eventSourceRef.current.close(); | ||
| eventSourceRef.current = null; | ||
|
|
@@ -115,7 +158,6 @@ const LogsWindow: React.FC<LogsWindowProps> = ({ isVisible, height }) => { | |
| try { | ||
| setConnectionStatus('connecting'); | ||
|
|
||
| // Close existing connection if any | ||
| if (eventSourceRef.current) { | ||
| eventSourceRef.current.close(); | ||
| } | ||
|
|
@@ -130,31 +172,31 @@ const LogsWindow: React.FC<LogsWindowProps> = ({ isVisible, height }) => { | |
| }, | ||
| })} : {}; | ||
|
|
||
| const eventSource = new EventSource(`${serverUrl}/api/v1/logs/stream`, options) | ||
| const streamUrl = selectedSource && selectedSource !== 'all' | ||
| ? `${serverUrl}/api/v1/logs/stream?source=${encodeURIComponent(selectedSource)}` | ||
| : `${serverUrl}/api/v1/logs/stream`; | ||
|
|
||
| const eventSource = new EventSource(streamUrl, options); | ||
| eventSourceRef.current = eventSource; | ||
|
|
||
| eventSource.onopen = () => { | ||
| console.log('Log stream connected to:', serverUrl); | ||
| console.log('Log stream connected to:', streamUrl); | ||
| setConnectionStatus('connected'); | ||
| }; | ||
|
|
||
| eventSource.onmessage = (event) => { | ||
| // SSE sends data as "data: <log line>" | ||
| const logLine = event.data; | ||
|
|
||
| // Skip heartbeat messages | ||
| if (logLine.trim() === '' || logLine === 'heartbeat') { | ||
| return; | ||
| } | ||
|
|
||
| // Keep follow mode sticky when user is effectively at bottom. | ||
| const shouldFollowNextLine = autoScrollRef.current || isNearBottom(); | ||
| if (shouldFollowNextLine && !autoScrollRef.current) { | ||
| setAutoScroll(true); | ||
| } | ||
|
|
||
| setLogs((prevLogs) => { | ||
| // Keep last 1000 lines to prevent memory issues | ||
| const newLogs = [...prevLogs, logLine]; | ||
| return newLogs.length > 1000 ? newLogs.slice(-1000) : newLogs; | ||
| }); | ||
|
|
@@ -165,7 +207,6 @@ const LogsWindow: React.FC<LogsWindowProps> = ({ isVisible, height }) => { | |
| setConnectionStatus('error'); | ||
| eventSource.close(); | ||
|
|
||
| // Reconnect after 5 seconds | ||
| reconnectTimeoutRef.current = setTimeout(() => { | ||
| console.log('Attempting to reconnect to log stream...'); | ||
| connectToLogStream(); | ||
|
|
@@ -181,10 +222,8 @@ const LogsWindow: React.FC<LogsWindowProps> = ({ isVisible, height }) => { | |
| } | ||
| }; | ||
|
|
||
| // Initial connection | ||
| connectToLogStream(); | ||
|
|
||
| // Cleanup on unmount or when visibility changes | ||
| return () => { | ||
| if (eventSourceRef.current) { | ||
| eventSourceRef.current.close(); | ||
|
|
@@ -195,7 +234,7 @@ const LogsWindow: React.FC<LogsWindowProps> = ({ isVisible, height }) => { | |
| reconnectTimeoutRef.current = null; | ||
| } | ||
| }; | ||
| }, [isVisible, serverUrl, apiKey, isInitialized]); | ||
| }, [isVisible, serverUrl, apiKey, isInitialized, selectedSource]); | ||
|
|
||
| const handleClearLogs = () => { | ||
| setLogs([]); | ||
|
|
@@ -206,13 +245,31 @@ const LogsWindow: React.FC<LogsWindowProps> = ({ isVisible, height }) => { | |
| scrollToBottom(); | ||
| }; | ||
|
|
||
| const handleSourceChange = (e: React.ChangeEvent<HTMLSelectElement>) => { | ||
| setLogs([]); | ||
| setSelectedSource(e.target.value); | ||
| }; | ||
|
|
||
| if (!isVisible) return null; | ||
|
|
||
| return ( | ||
| <div className="logs-window" style={height ? { height: `${height}px`, flex: 'none' } : undefined}> | ||
| <div className="logs-header"> | ||
| <h3>Server Logs</h3> | ||
| <div className="logs-controls"> | ||
| <select | ||
| className="logs-source-select" | ||
| value={selectedSource} | ||
| onChange={handleSourceChange} | ||
| title="Filter logs by source" | ||
| > | ||
| <option value="all">All Sources</option> | ||
| {availableSources.map((src) => ( | ||
| <option key={src.name} value={src.name}> | ||
| {src.label} | ||
| </option> | ||
| ))} | ||
| </select> | ||
| <span className={`connection-status status-${connectionStatus}`}> | ||
| {connectionStatus === 'connecting' && '⟳ Connecting...'} | ||
| {connectionStatus === 'connected' && '● Connected'} | ||
|
|
||
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
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.
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.



There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's up with all the
WebSocketServer::handleConnection() HTTP status: 400 error: Missing Sec-WebSocket-Key value? Never seen that before.