실시간 프록시 모니터링: Python 및 Grafana로 대시보드 구축
I’m ready to translate the article for you, but I need the actual text of the post. Could you please paste the content you’d like translated (excluding the source link you’ve already provided)? Once I have the text, I’ll keep the source line unchanged and translate everything else into Korean while preserving the original formatting and code blocks.
추적해야 할 주요 지표
- 성공률 – HTTP 200을 반환하는 요청의 비율
- 응답 시간 – 프록시당 평균 및 P95 지연 시간
- 대역폭 사용량 – 프록시당 및 전체 데이터 사용량
- 오류 분포 – 오류 유형 (타임아웃, 403, 429, CAPTCHA)
- IP 고유성 – 실제 사용 중인 고유 IP 수
- 풀 상태 – 활성 프록시와 실패 프록시의 비율
- 회전 빈도 – IP가 변경되는 빈도
- 지리적 분포 – 종료 IP가 위치한 지역
- 성공 요청당 비용 – 실제 비용 계산
- 블랙리스트 비율 – 현재 차단된 IP 수
아키텍처 개요
Your Application
|
v
Proxy Middleware (collects metrics)
|
v
Prometheus (stores time‑series data)
|
v
Grafana (visualizes dashboards)
프록시 래퍼와 메트릭스
import time
import requests
from prometheus_client import Counter, Histogram, Gauge, start_http_server
# Define metrics
REQUEST_COUNT = Counter(
"proxy_requests_total",
"Total proxy requests",
["proxy", "status", "target_domain"]
)
RESPONSE_TIME = Histogram(
"proxy_response_seconds",
"Response time in seconds",
["proxy"],
buckets=[0.1, 0.5, 1, 2, 5, 10, 30]
)
ACTIVE_PROXIES = Gauge(
"proxy_pool_active",
"Number of active proxies in pool"
)
BANDWIDTH = Counter(
"proxy_bandwidth_bytes",
"Bandwidth consumed in bytes",
["proxy", "direction"]
)
class MonitoredProxy:
def __init__(self, proxy_url):
self.proxy_url = proxy_url
self.proxy_dict = {"http": proxy_url, "https": proxy_url}
def request(self, url, **kwargs):
start = time.time()
domain = url.split("/")[2]
try:
response = requests.get(
url,
proxies=self.proxy_dict,
timeout=kwargs.get("timeout", 15),
**kwargs
)
duration = time.time() - start
# Record metrics
REQUEST_COUNT.labels(
proxy=self.proxy_url,
status=str(response.status_code),
target_domain=domain
).inc()
RESPONSE_TIME.labels(proxy=self.proxy_url).observe(duration)
BANDWIDTH.labels(
proxy=self.proxy_url, direction="response"
).inc(len(response.content))
return response
except Exception:
duration = time.time() - start
REQUEST_COUNT.labels(
proxy=self.proxy_url,
status="error",
target_domain=domain
).inc()
raise
Prometheus 구성 (prometheus.yml)
scrape_configs:
- job_name: "proxy_monitor"
scrape_interval: 15s
static_configs:
- targets: ["localhost:8000"]
핵심 대시보드 패널 (Grafana)
-
성공률
rate(proxy_requests_total{status="200"}[5m]) / rate(proxy_requests_total[5m]) * 100 -
평균 응답 시간
rate(proxy_response_seconds_sum[5m]) / rate(proxy_response_seconds_count[5m]) -
오류 분포
sum by (status) (rate(proxy_requests_total{status!="200"}[5m])) -
시간당 대역폭
sum(rate(proxy_bandwidth_bytes[1h])) * 3600
알림 규칙 (alert_rules.yml)
groups:
- name: proxy_alerts
rules:
- alert: LowSuccessRate
expr: |
rate(proxy_requests_total{status="200"}[5m]) /
rate(proxy_requests_total[5m]) 5
for: 5m
annotations:
summary: Average proxy latency above 5 seconds
경량 CSV 로거 (대안)
Prometheus와 Grafana가 과도하다면 CSV에 로그를 남길 수 있습니다:
import csv
from datetime import datetime
def log_request(proxy, url, status, latency, bytes_received):
with open("proxy_log.csv", "a", newline="") as f:
writer = csv.writer(f)
writer.writerow([
datetime.now().isoformat(),
proxy,
url,
status,
round(latency, 3),
bytes_received
])
나중에 pandas(또는 기타 데이터‑분석 도구)를 사용해 CSV를 분석하여 추세와 문제 있는 프록시를 식별할 수 있습니다.
Further Reading
보다 많은 프록시 모니터링 설정 및 인프라 가이드를 보려면 DataResearchTools를 방문하세요.