This commit is contained in:
PhanThanhTrung 2024-10-29 14:43:48 +07:00
parent a5bd63df91
commit 90f959223c

View File

@ -11,16 +11,21 @@ from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor from opentelemetry.sdk.trace.export import BatchSpanProcessor
def setup_tracing(): def setup_tracing(tracer_endpoint, service_name):
# Set up a tracer provider # Set up a tracer provider
trace.set_tracer_provider(TracerProvider()) span_exporter = OTLPSpanExporter(endpoint=f"{tracer_endpoint}/v1/traces")
processor = BatchSpanProcessor(span_exporter=span_exporter)
# Configure the OTLP exporter attributes = {SERVICE_NAME: service_name}
otlp_exporter = OTLPSpanExporter(endpoint="http://localhost:4317", insecure=True) resource = Resource(attributes=attributes)
trace_provider = TracerProvider(resource=resource)
trace_provider.add_span_processor(span_processor=processor)
trace.set_tracer_provider(tracer_provider=trace_provider)
# Add a BatchSpanProcessor to handle traces reader = PeriodicExportingMetricReader(
span_processor = BatchSpanProcessor(otlp_exporter) OTLPMetricExporter(endpoint=f"{tracer_endpoint}/v1/metrics"))
trace.get_tracer_provider().add_span_processor(span_processor) meter_provider = MeterProvider(resource=resource, metric_readers=[reader])
metrics.set_meter_provider(meter_provider=meter_provider)
# Instrument Django # Instrument Django
DjangoInstrumentor().instrument() DjangoInstrumentor().instrument()