Skip to content

Commit fb50ac0

Browse files
chore: optional opentelemetry (#10112)
* chore: optional opentelemetry * chore: add comment
1 parent e775af3 commit fb50ac0

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

dev/build/gunicorn.conf.py

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -135,21 +135,30 @@ def post_request(worker, req, environ, resp):
135135
def post_fork(server, worker):
136136
server.log.info("Worker spawned (pid: %s)", worker.pid)
137137

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()
138+
# Setting DATATRACKER_OPENTELEMETRY_ENABLE=all in the environment will enable all
139+
# opentelemetry instrumentations. Individual instrumentations can be selected by
140+
# using a space-separated list. See the code below for available instrumentations.
141+
telemetry_env = os.environ.get("DATATRACKER_OPENTELEMETRY_ENABLE", "").strip()
142+
if telemetry_env != "":
143+
enabled_telemetry = [tok.strip().lower() for tok in telemetry_env.split()]
144+
resource = Resource.create(attributes={
145+
"service.name": "datatracker",
146+
"service.version": ietf.__version__,
147+
"service.instance.id": worker.pid,
148+
"service.namespace": "datatracker",
149+
"deployment.environment.name": os.environ.get("DATATRACKER_SERVICE_ENV", "dev")
150+
})
151+
trace.set_tracer_provider(TracerProvider(resource=resource))
152+
otlp_exporter = OTLPSpanExporter(endpoint="https://heimdall-otlp.ietf.org/v1/traces")
153+
154+
trace.get_tracer_provider().add_span_processor(BatchSpanProcessor(otlp_exporter))
155+
156+
# Instrumentations
157+
if "all" in enabled_telemetry or "django" in enabled_telemetry:
158+
DjangoInstrumentor().instrument()
159+
if "all" in enabled_telemetry or "psycopg2" in enabled_telemetry:
160+
Psycopg2Instrumentor().instrument()
161+
if "all" in enabled_telemetry or "pymemcache" in enabled_telemetry:
162+
PymemcacheInstrumentor().instrument()
163+
if "all" in enabled_telemetry or "requests" in enabled_telemetry:
164+
RequestsInstrumentor().instrument()

0 commit comments

Comments
 (0)