Skip to content

Commit c8e762d

Browse files
committed
feat: add traceparent propagation via headers and meta tag
1 parent 9a05c70 commit c8e762d

File tree

4 files changed

+19
-0
lines changed

4 files changed

+19
-0
lines changed

ietf/context_processors.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from django.conf import settings
66
from django.utils import timezone
77
from ietf import __version__, __patch__, __release_branch__, __release_hash__
8+
from opentelemetry.propagate import inject
89

910
def server_mode(request):
1011
return {'server_mode': settings.SERVER_MODE}
@@ -51,3 +52,8 @@ def timezone_now(request):
5152
return {
5253
'timezone_now': timezone.now(),
5354
}
55+
56+
def traceparent_id(request):
57+
context_extras = {}
58+
inject(context_extras)
59+
return context_extras

ietf/middleware.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from django.http import HttpResponsePermanentRedirect
99
from ietf.utils.log import log, exc_parts
1010
from ietf.utils.mail import log_smtp_exception
11+
from opentelemetry.propagate import inject
1112
import re
1213
import smtplib
1314
import unicodedata
@@ -99,3 +100,12 @@ def add_header(request):
99100
return response
100101

101102
return add_header
103+
104+
def add_otel_traceparent_header(get_response):
105+
"""Middleware to add the OpenTelemetry traceparent id header to the response"""
106+
def add_header(request):
107+
response = get_response(request)
108+
inject(response)
109+
return response
110+
111+
return add_header

ietf/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ def skip_unreadable_post(record):
411411
],
412412
'OPTIONS': {
413413
'context_processors': [
414+
'ietf.context_processors.traceparent_id',
414415
'django.contrib.auth.context_processors.auth',
415416
'django.template.context_processors.debug', # makes 'sql_queries' available in templates
416417
'django.template.context_processors.i18n',
@@ -443,6 +444,7 @@ def skip_unreadable_post(record):
443444

444445

445446
MIDDLEWARE = [
447+
"ietf.middleware.add_otel_traceparent_header",
446448
"django.middleware.csrf.CsrfViewMiddleware",
447449
"corsheaders.middleware.CorsMiddleware", # see docs on CORS_REPLACE_HTTPS_REFERER before using it
448450
"django.middleware.common.CommonMiddleware",

ietf/templates/base.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
{% block title %}No title{% endblock %}
1616
</title>
1717
<meta name="viewport" content="width=device-width, initial-scale=1">
18+
<meta name="traceparent" content="{{ traceparent }}">
1819
<link href="{{ settings.STATIC_IETF_ORG }}/fonts/inter/import.css" rel="stylesheet">
1920
<link href="{{ settings.STATIC_IETF_ORG }}/fonts/noto-sans-mono/import.css" rel="stylesheet">
2021
<link rel="stylesheet" href="{% static 'ietf/css/ietf.css' %}">

0 commit comments

Comments
 (0)