-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
154 lines (125 loc) · 5.21 KB
/
Makefile
File metadata and controls
154 lines (125 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# Python Logging with Loki + Grafana Stack
# Author: Generated by AI Assistant
# Description: Comprehensive logging setup for Python applications
DOCKER_IMAGE = python-logging-loki:latest
PROJECT_NAME = python-logging-loki
.PHONY: help build run up down logs test clean status grafana loki promtail dev
help: ## Show this help message
@echo "🚀 Python Logging with Loki + Grafana"
@echo "======================================"
@echo "Available commands:"
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf " ✅ \033[36m%-15s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
build: ## Build Docker app images
@echo "🔨 Building Docker image $(DOCKER_IMAGE)"
docker build \
-f Dockerfile \
--build-arg USER_UID=$$(id -u) \
--build-arg USER_GID=$$(id -g) \
-t $(DOCKER_IMAGE) .
run: ## Run Python app standalone (without stack)
@echo "🐍 Running Python app standalone"
docker run --rm \
--name python-logging-app \
$(DOCKER_IMAGE)
up: ## Start the complete logging stack (app + loki + grafana)
@echo "🚀 Starting the complete logging stack"
DOCKER_BUILT=1 docker-compose up -d --build
@echo "✅ Stack started successfully!"
@echo "📊 Grafana: http://localhost:3000"
@echo "🗄️ Loki API: http://localhost:3100"
@echo "📋 Promtail: http://localhost:9080"
up-logs: ## Start stack and follow logs
@echo "🚀 Starting stack with live logs"
docker-compose up --build
down: ## Stop and remove all containers
@echo "🛑 Stopping the logging stack"
docker-compose down
@echo "✅ Stack stopped"
restart: ## Restart the entire stack
@echo "🔄 Restarting the logging stack"
$(MAKE) down
$(MAKE) up
logs: ## Show logs from Python app
@echo "📋 Following application logs"
docker-compose logs -f app
logs-all: ## Show logs from all services
@echo "📋 Following all service logs"
docker-compose logs -f
test: ## Run logging tests locally
@echo "🧪 Running logging tests"
python3 test_logging.py
test-docker: ## Run tests inside Docker
@echo "🧪 Running tests in Docker environment"
docker run --rm -v $(PWD):/app -w /app python:3.12-slim bash -c "pip install structlog && python test_logging.py"
status: ## Check status of all services
@echo "📊 Service Status:"
@docker-compose ps
health: ## Check health of all services
@echo "🏥 Health Check:"
@echo "Testing Grafana..."
@curl -s http://localhost:3000/api/health || echo "❌ Grafana not responding"
@echo "Testing Loki..."
@curl -s http://localhost:3100/ready || echo "❌ Loki not responding"
@echo "Testing Promtail..."
@curl -s http://localhost:9080/ready || echo "❌ Promtail not responding"
grafana: ## Open Grafana in browser
@echo "🌐 Opening Grafana dashboard"
@open http://localhost:3000 || echo "Please open http://localhost:3000 manually"
loki: ## Query Loki API directly
@echo "🗄️ Querying Loki for recent logs"
@curl -s "http://localhost:3100/loki/api/v1/query_range?query={job=\"python_app_logs\"}&limit=10" | jq '.data.result[0].values[-5:][]' || echo "Install jq for formatted output"
promtail: ## Check Promtail metrics
@echo "📋 Promtail metrics"
@curl -s http://localhost:9080/metrics | grep promtail || echo "❌ Promtail metrics not available"
dev: ## Start development environment with hot reload
@echo "🔧 Starting development environment"
@echo "Note: Modify app/main.py for hot reload"
docker-compose -f docker-compose.yml up --build
clean: ## Clean up Docker resources
@echo "🧹 Cleaning up Docker resources"
docker-compose down -v
docker system prune -f
docker volume prune -f
@echo "✅ Cleanup completed"
backup-logs: ## Backup current logs
@echo "💾 Backing up logs"
@mkdir -p backups
@tar -czf backups/logs_$(shell date +%Y%m%d_%H%M%S).tar.gz app/logs/ || echo "No logs to backup"
install-deps: ## Install local development dependencies
@echo "📦 Installing dependencies"
@command -v uv >/dev/null 2>&1 || pip install uv
@uv sync
format: ## Format code with black
@echo "🎨 Formatting code"
@uv run black app/ *.py
lint: ## Lint code with ruff
@echo "🔍 Linting code"
@uv run ruff check app/ *.py
security: ## Security scan with bandit
@echo "🔒 Running security scan"
@uv run bandit -r app/
dashboard-export: ## Export current Grafana dashboard
@echo "📊 Exporting Grafana dashboard"
@curl -s "http://localhost:3000/api/dashboards/uid/python-logging-loki" | jq '.dashboard' > dashboards/structlog-dashboard-backup.json || echo "Dashboard export failed"
quick-start: ## Quick start guide
@echo "🚀 Quick Start Guide"
@echo "=================="
@echo "1. Run: make up"
@echo "2. Wait 30 seconds for services to start"
@echo "3. Open: http://localhost:3000"
@echo "4. View logs in Grafana dashboard"
@echo "5. To stop: make down"
demo: ## Run demo with explanation
@echo "🎬 Running demo..."
$(MAKE) up
@echo ""
@echo "🎯 Demo is running! Here's what's happening:"
@echo " 📱 Python app is generating realistic business logs"
@echo " 📋 Promtail is collecting logs from Docker"
@echo " 🗄️ Loki is storing and indexing the logs"
@echo " 📊 Grafana is visualizing everything in real-time"
@echo ""
@echo "🌐 Open your browser to: http://localhost:3000"
@echo "⏱️ Give it 30 seconds to see data flowing"
@echo ""
@echo "🛑 To stop demo: make down"