A beautiful, real-time web interface for viewing Google Cloud Run logs. This application provides instant access to your Cloud Run service logs with search, filtering, and a modern dark theme interface.
- π Real-time log streaming via WebSockets
- π Search and filter logs by content and severity
- π¨ Beautiful dark theme interface
- π± Responsive design works on all devices
- β‘ Auto-reconnection when connection is lost
- π One-click deployment to Google Cloud Run
- π° Cost-effective with proper Cloud Run scaling
Before you begin, make sure you have:
- Google Cloud Account with billing enabled
- Google Cloud CLI installed (install here)
- Docker installed (install here)
- Node.js installed (install here)
# Clone this repository
git clone https://github.com/ShrishPremkrishna/website-for-cloud-run-logs.git
cd website-for-cloud-run-logs
# Install dependencies
npm install# Login to Google Cloud
gcloud auth login
# Set up your service account (this will guide you through the process)
npm run setupThis script will:
- Create a service account with the necessary permissions
- Download the service account key file
- Set up your project configuration
Create a .env file in the project root:
# Copy the example environment file
cp env.example .env
# Edit the .env file with your values
nano .env # or use your preferred editorUpdate the .env file with your actual values:
GCP_PROJECT_ID=your-actual-project-id
GCP_CLOUD_RUN_SERVICE_NAME=your-actual-service-name
GCP_KEY_PATH=./service-account.json
PORT=3000# Deploy with one command
npm run deployThat's it! Your log viewer will be available at the URL shown in the output.
π§ What This Application Does
This is a full-stack web application that:
-
Backend (Node.js/Express):
- Connects to Google Cloud Logging API
- Streams logs in real-time using WebSockets
- Serves the React frontend
- Filters logs by your specified Cloud Run service
-
Frontend (React + Tailwind CSS):
- Displays logs in real-time with a beautiful interface
- Provides search and filtering capabilities
- Auto-reconnects if the connection is lost
- Responsive design for all devices
-
Deployment (Docker + Cloud Run):
- Containerized for easy deployment
- Optimized for Cloud Run's scaling model
- Includes proper error handling and health checks
ποΈ Understanding the Architecture
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β Your Browser βββββΊβ Cloud Run App βββββΊβ Google Cloud β
β (Frontend) β β (Backend) β β Logging API β
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β
βΌ
ββββββββββββββββββββ
β WebSocket β
β Connection β
ββββββββββββββββββββ
π§ Local Development
To run the application locally for development:
# Start both frontend and backend in development mode
npm run devThis will start:
- Backend server on
http://localhost:5001 - Frontend development server on
http://localhost:5173 - Hot reloading for both frontend and backend
| Script | Description |
|---|---|
npm run dev |
Start both frontend and backend in development mode |
npm run dev:backend |
Start only the backend server |
npm run frontend |
Start only the frontend development server |
npm run frontend:build |
Build the frontend for production |
npm start |
Start the production server |
npm run setup |
Set up Google Cloud service account |
cloud-run-log-viewer/
βββ backend/
β βββ index.js # Express server + WebSocket + Log streaming
βββ frontend/
β βββ App.jsx # Main React component
β βββ index.jsx # React entry point
β βββ index.html # HTML template
β βββ index.css # Tailwind CSS styles
βββ scripts/
β βββ setup-service-account.sh # Google Cloud setup script
βββ dist/ # Built frontend (generated)
βββ package.json # Dependencies and scripts
βββ Dockerfile # Container configuration
βββ deploy.sh # Deployment script
βββ README.md # This file
The backend handles:
- Express Server: Serves the React app and API endpoints
- WebSocket Server: Real-time communication with frontend
- Google Cloud Logging: Streams logs from your Cloud Run service
- Health Check:
/healthendpoint for monitoring
Key functions:
startLogStream(): Connects to Google Cloud Logging APIbroadcastLogs(): Sends logs to all connected WebSocket clientsparseLogMessage(): Parses structured log messages
The frontend provides:
- WebSocket Connection: Real-time log streaming
- Search & Filtering: Find specific logs
- Severity Filtering: Filter by INFO, WARNING, ERROR
- Auto-reconnection: Handles connection drops
Key features:
- Real-time log display
- Search functionality
- Severity-based filtering
- Responsive design
# Start backend in development mode (auto-restart on changes)
npm run dev:backendThe backend will automatically restart when you make changes to backend/index.js.
# Start frontend in development mode (hot reload)
npm run frontendThe frontend will hot-reload when you make changes to React components.
# Start both servers
npm run dev
# Test the application
open http://localhost:5173# Build the frontend
npm run frontend:build
# Start production server
npm start- Check logs: Backend logs appear in the terminal
- Health check:
curl http://localhost:5001/health - WebSocket: Check browser console for connection status
- Browser DevTools: Check Console and Network tabs
- React DevTools: Install browser extension for React debugging
- WebSocket: Monitor WebSocket connection in Network tab
-
"Service account not found"
npm run setup
-
"Permission denied"
gcloud auth login
-
"No logs appearing"
- Check your Cloud Run service is running
- Verify service name is correct
- Check service account permissions
-
"Connection lost"
- Normal behavior, app will auto-reconnect
- Check backend is running
- Verify WebSocket URL
- Start the app:
npm run dev - Open browser: http://localhost:5173
- Generate logs: Make requests to your Cloud Run service
- Verify logs appear: Check the web interface
# Run health check
curl http://localhost:5001/health
# Check WebSocket connection
# Open browser console and look for "WebSocket connected"- Log Filtering: Only process relevant logs
- Connection Pooling: Reuse Google Cloud connections
- Memory Management: Clean up old log entries
- Virtual Scrolling: For large log volumes
- Debounced Search: Reduce API calls
- Log Retention: Limit displayed logs
- Backend: Add new endpoints in
backend/index.js - Frontend: Create new components in
frontend/ - Styling: Modify
frontend/index.css
Modify parseLogMessage() in backend/index.js to handle different log formats.
- Backend: Add auth middleware
- Frontend: Add login component
- WebSocket: Add authentication to WebSocket connection
# Build Docker image
docker build -t cloud-run-log-viewer .
# Run locally
docker run -p 3000:3000 \
-e GCP_PROJECT_ID=your-project-id \
-e GCP_CLOUD_RUN_SERVICE_NAME=your-service-name \
cloud-run-log-viewer# Build frontend
npm run frontend:build
# Start production server
npm start
# Test at http://localhost:3000βοΈ Environment Variables Explained
| Variable | Description | Required | Example |
|---|---|---|---|
GCP_PROJECT_ID |
Your Google Cloud project ID | Yes | my-project-123 |
GCP_CLOUD_RUN_SERVICE_NAME |
Name of the Cloud Run service to monitor | Yes | my-api-service |
GCP_KEY_PATH |
Path to service account key file | Yes | /app/service-account.json |
PORT |
Port for local development | No | 3000 |
π Deployment Options
The easiest way to deploy is using the provided script:
npm run deployThis will:
- Build a Docker image
- Push it to Google Container Registry
- Deploy to Cloud Run with proper configuration
- Set up auto-scaling and CPU allocation
# Enable required APIs
gcloud services enable run.googleapis.com
gcloud services enable containerregistry.googleapis.com
# Build and push Docker image
docker buildx build --platform linux/amd64 -t gcr.io/$PROJECT_ID/cloud-run-log-viewer:latest --push .
# Deploy to Cloud Run
gcloud run deploy cloud-run-log-viewer \
--image gcr.io/$PROJECT_ID/cloud-run-log-viewer:latest \
--platform managed \
--region us-central1 \
--allow-unauthenticated \
--set-env-vars="GCP_PROJECT_ID=$PROJECT_ID,GCP_CLOUD_RUN_SERVICE_NAME=$GCP_CLOUD_RUN_SERVICE_NAME,GCP_KEY_PATH=/app/service-account.json" \
--memory 512Mi \
--cpu 1 \
--cpu-boost \
--min-instances 1 \
--max-instances 10| Variable | Description | Required | Example |
|---|---|---|---|
GCP_PROJECT_ID |
Your Google Cloud project ID | Yes | my-project-123 |
GCP_CLOUD_RUN_SERVICE_NAME |
Name of the Cloud Run service to monitor | Yes | my-api-service |
GCP_KEY_PATH |
Path to service account key file | Yes | /app/service-account.json |
PORT |
Port for local development | No | 3000 |
For all deployment options, you need a Google Cloud service account:
# Run the setup script
npm run setup
# This will create:
# - Service account with proper permissions
# - service-account.json file
# - Required IAM bindings- Min 1 instance: ~$17-20/month
- Max 10 instances: Prevents runaway costs
- CPU allocation: Optimized for performance
- Railway: Pay-per-use, starts at $5/month
- Render: Free tier available, then $7/month
- Heroku: Free tier discontinued, starts at $7/month
- Vercel: Generous free tier
- Service Account: Use minimal required permissions
- HTTPS: All platforms provide HTTPS by default
- Authentication: Consider adding auth for production
- Key Management: Never commit service account keys
- Environment Variables: Use platform secrets management
π§ Troubleshooting
-
"Service account not found"
# Run the setup script again npm run setup -
"Permission denied"
# Check authentication gcloud auth list # Re-authenticate if needed gcloud auth login
-
"No logs appearing"
- Verify your Cloud Run service name is correct
- Check that your service is actually running
- Ensure the service account has proper permissions
-
"Connection lost"
- This is normal behavior, the app will auto-reconnect
- Check your internet connection
- Verify the Cloud Run service is running
- You might need to setup minimum scaling in your cloud run container to 1 so that this website stays live constantly.
π Security Considerations
- Service Account: The application uses a service account with minimal required permissions
- HTTPS: Cloud Run provides HTTPS by default
- Authentication: Consider adding authentication for production use
- Key Management: Never commit
service-account.jsonto version control
π° Cost Optimization
- Min Instances: Set to 1 to keep the connection alive (~$17-20/month)
- Max Instances: Limited to 10 to prevent runaway costs
- CPU Allocation: Uses CPU boost for better performance
- Memory: Optimized to 512Mi for cost efficiency
π Changing the Target Service
To monitor a different Cloud Run service:
# Update your .env file
GCP_CLOUD_RUN_SERVICE_NAME=your-new-service-name
# Redeploy
npm run deployπ¨ Modifying the UI
The frontend is built with React and Tailwind CSS. Key files:
frontend/App.jsx: Main application componentfrontend/index.css: Styling with Tailwindbackend/index.js: Backend API and WebSocket server
π Adding Authentication
For production use, consider adding authentication:
- Google OAuth: Use Google Cloud Identity
- API Keys: Implement API key authentication
- Custom Auth: Add your own authentication system
- Google Cloud Run Documentation
- Google Cloud Logging API
- WebSocket Protocol
- React Documentation
- Tailwind CSS
Contributions are welcome! Please feel free to submit a Pull Request.