|
1 | | -# Copyright The IETF Trust 2024, All Rights Reserved |
| 1 | +# Copyright The IETF Trust 2024-2025, All Rights Reserved |
| 2 | + |
| 3 | +import os |
| 4 | +import ietf |
| 5 | +from opentelemetry import trace |
| 6 | +from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter |
| 7 | +from opentelemetry.sdk.resources import Resource |
| 8 | +from opentelemetry.sdk.trace import TracerProvider |
| 9 | +from opentelemetry.sdk.trace.export import BatchSpanProcessor |
| 10 | +from opentelemetry.instrumentation.django import DjangoInstrumentor |
| 11 | +from opentelemetry.instrumentation.psycopg2 import Psycopg2Instrumentor |
| 12 | +from opentelemetry.instrumentation.pymemcache import PymemcacheInstrumentor |
| 13 | +from opentelemetry.instrumentation.requests import RequestsInstrumentor |
2 | 14 |
|
3 | 15 | # Configure security scheme headers for forwarded requests. Cloudflare sets X-Forwarded-Proto |
4 | 16 | # for us. Don't trust any of the other similar headers. Only trust the header if it's coming |
@@ -119,3 +131,25 @@ def post_request(worker, req, environ, resp): |
119 | 131 | in_flight = in_flight_by_pid.get(worker.pid, []) |
120 | 132 | if request_description in in_flight: |
121 | 133 | in_flight.remove(request_description) |
| 134 | + |
| 135 | +def post_fork(server, worker): |
| 136 | + server.log.info("Worker spawned (pid: %s)", worker.pid) |
| 137 | + |
| 138 | + resource = Resource.create(attributes={ |
| 139 | + "service.name": "datatracker", |
| 140 | + "service.version": ietf.__version__, |
| 141 | + "service.instance.id": worker.pid, |
| 142 | + "service.namespace": "datatracker", |
| 143 | + "deployment.environment.name": os.environ.get("DATATRACKER_SERVICE_ENV", "dev") |
| 144 | + }) |
| 145 | + |
| 146 | + trace.set_tracer_provider(TracerProvider(resource=resource)) |
| 147 | + otlp_exporter = OTLPSpanExporter(endpoint="https://heimdall-otlp.ietf.org/v1/traces") |
| 148 | + |
| 149 | + trace.get_tracer_provider().add_span_processor(BatchSpanProcessor(otlp_exporter)) |
| 150 | + |
| 151 | + # Instrumentations |
| 152 | + DjangoInstrumentor().instrument() |
| 153 | + Psycopg2Instrumentor().instrument() |
| 154 | + PymemcacheInstrumentor().instrument() |
| 155 | + RequestsInstrumentor().instrument() |
0 commit comments