Files
msa-django-blog/blog_prj/wsgi.py
icurfer 63c126077a
Some checks failed
Build And Test / build-and-push (push) Has been cancelled
feat: Presigned URL API 및 OpenTelemetry trace 추가
- Presigned URL API 추가 (MinIO/S3 private 버킷 지원)
- OpenTelemetry trace 설정 추가 (DEBUG=False 시 활성화)
- requirements.txt에 opentelemetry 패키지 추가

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-24 02:08:21 +09:00

85 lines
2.6 KiB
Python

"""
WSGI config for blog_prj project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/4.2/howto/deployment/wsgi/
"""
import os
# Django 설정을 미리 불러온다
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'blog_prj.settings')
from django.conf import settings
from django.core.wsgi import get_wsgi_application
# DEBUG 모드 아닐 때만 OpenTelemetry 활성
if not settings.DEBUG:
import grpc
from opentelemetry import trace
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.instrumentation.django import DjangoInstrumentor
from opentelemetry.instrumentation.requests import RequestsInstrumentor
from opentelemetry.instrumentation.logging import LoggingInstrumentor
from opentelemetry.instrumentation.dbapi import trace_integration
import MySQLdb
trace.set_tracer_provider(
TracerProvider(
resource=Resource.create({
"service.platform": settings.SERVICE_PLATFORM,
"service.name": settings.TRACE_SERVICE_NAME,
})
)
)
# TRACE_CA_CERT 설정에 따른 gRPC credentials 구성
credentials = None
ca_cert_path = os.getenv('TRACE_CA_CERT', '').strip()
if ca_cert_path and os.path.exists(ca_cert_path):
with open(ca_cert_path, 'rb') as f:
ca_cert = f.read()
credentials = grpc.ssl_channel_credentials(root_certificates=ca_cert)
insecure = False
else:
insecure = True
otlp_exporter = OTLPSpanExporter(
endpoint=settings.TRACE_ENDPOINT,
insecure=insecure,
credentials=credentials,
headers={
"x-scope-orgid": settings.SERVICE_PLATFORM,
"x-service": settings.TRACE_SERVICE_NAME
}
)
trace.get_tracer_provider().add_span_processor(
BatchSpanProcessor(otlp_exporter)
)
# Django 요청/응답 추적
DjangoInstrumentor().instrument()
# HTTP 클라이언트 요청 추적 (requests 라이브러리)
RequestsInstrumentor().instrument()
# 로그와 Trace 연동 (trace_id, span_id를 로그에 자동 추가)
LoggingInstrumentor().instrument(set_logging_format=True)
# MySQL DB 쿼리 추적
trace_integration(
MySQLdb,
"connect",
"mysql",
capture_parameters=True,
)
application = get_wsgi_application()