All checks were successful
Build And Test / build-and-push (push) Successful in 2m8s
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
90 lines
3.0 KiB
Python
90 lines
3.0 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 활성
|
|
print(f"[OTEL] DEBUG={settings.DEBUG}, TRACE_ENDPOINT={settings.TRACE_ENDPOINT}")
|
|
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()
|
|
print(f"[OTEL] CA_CERT_PATH={ca_cert_path}, exists={os.path.exists(ca_cert_path) if ca_cert_path else False}")
|
|
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
|
|
print(f"[OTEL] Using TLS with CA cert")
|
|
else:
|
|
insecure = True
|
|
print(f"[OTEL] Using insecure mode")
|
|
|
|
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)
|
|
)
|
|
print(f"[OTEL] OpenTelemetry initialized: service={settings.TRACE_SERVICE_NAME}, endpoint={settings.TRACE_ENDPOINT}")
|
|
|
|
# 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()
|