Skip to content

Commit 1bb0016

Browse files
committed
Added ability to extract span attributes from Tonado request objects.
OTEL_PYTHON_TONADO_TRACED_REQUEST_ATTRS env var can be set to a command separated list of attributes names that will be extracted from Tornado's request object and set as attributes on spans.
1 parent ddf0839 commit 1bb0016

File tree

5 files changed

+50
-5
lines changed

5 files changed

+50
-5
lines changed

Diff for: instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from opentelemetry.configuration import Configuration
1818
from opentelemetry.context import attach, detach
1919
from opentelemetry.instrumentation.django.version import __version__
20+
from opentelemetry.instrumentation.utils import extract_attributes_from_object
2021
from opentelemetry.instrumentation.wsgi import (
2122
add_response_attributes,
2223
collect_request_attributes,
@@ -102,10 +103,9 @@ def process_request(self, request):
102103
tracer = get_tracer(__name__, __version__)
103104

104105
attributes = collect_request_attributes(environ)
105-
for attr in self._traced_request_attrs:
106-
value = getattr(request, attr, None)
107-
if value is not None:
108-
attributes[attr] = str(value)
106+
attributes = extract_attributes_from_object(
107+
request, self._traced_request_attrs, attributes
108+
)
109109

110110
span = tracer.start_span(
111111
self._get_span_name(request),

Diff for: instrumentation/opentelemetry-instrumentation-tornado/README.rst

+12
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ A comma separated list of paths that should not be automatically traced. For exa
3131

3232
Then any requests made to ``/healthz`` and ``/ping`` will not be automatically traced.
3333

34+
Request attributes
35+
********************
36+
To extract certain attributes from Tornado's request object and use them as span attributes, set the environment variable ``OTEL_PYTHON_TORNADO_TRACED_REQUEST_ATTRS`` to a comma
37+
delimited list of request attribute names.
38+
39+
For example,
40+
41+
::
42+
43+
export OTEL_PYTHON_TONADO_TRACED_REQUEST_ATTRS='uri,query'
44+
45+
will extract path_info and content_type attributes from every traced request and add them as span attritbues.
3446

3547
References
3648
----------

Diff for: instrumentation/opentelemetry-instrumentation-tornado/src/opentelemetry/instrumentation/tornado/__init__.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def get(self):
5050
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
5151
from opentelemetry.instrumentation.tornado.version import __version__
5252
from opentelemetry.instrumentation.utils import (
53+
extract_attributes_from_object,
5354
http_status_to_canonical_code,
5455
unwrap,
5556
)
@@ -79,6 +80,7 @@ def get_traced_request_attrs():
7980
attrs = []
8081
return attrs
8182

83+
8284
_excluded_urls = get_excluded_urls()
8385
_traced_attrs = get_traced_request_attrs()
8486

@@ -205,7 +207,7 @@ def _get_attributes_from_request(request):
205207
if request.remote_ip:
206208
attrs["net.peer.ip"] = request.remote_ip
207209

208-
return attrs
210+
return extract_attributes_from_object(request, _traced_attrs, attrs)
209211

210212

211213
def _get_operation_name(handler, request):
@@ -220,6 +222,7 @@ def _start_span(tracer, handler, start_time) -> _TraceContext:
220222
_get_header_from_request_headers, handler.request.headers,
221223
)
222224
)
225+
223226
span = tracer.start_span(
224227
_get_operation_name(handler, handler.request),
225228
kind=trace.SpanKind.SERVER,

Diff for: instrumentation/opentelemetry-instrumentation-tornado/tests/test_instrumentation.py

+15
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,21 @@ def test_excluded(path):
339339
test_excluded("/healthz")
340340
test_excluded("/ping")
341341

342+
@patch(
343+
"opentelemetry.instrumentation.tornado._traced_attrs",
344+
["uri", "full_url", "query"],
345+
)
346+
def test_traced_attrs(self):
347+
self.fetch("/ping?q=abc&b=123")
348+
spans = self.sorted_spans(self.memory_exporter.get_finished_spans())
349+
self.assertEqual(len(spans), 2)
350+
server = spans[0]
351+
self.assertEqual(server.kind, SpanKind.SERVER)
352+
self.assert_span_has_attributes(
353+
server, {"uri": "/ping?q=abc&b=123", "query": "q=abc&b=123",}
354+
)
355+
self.memory_exporter.clear()
356+
342357

343358
class TestTornadoUninstrument(TornadoTest):
344359
def test_uninstrument(self):

Diff for: opentelemetry-instrumentation/src/opentelemetry/instrumentation/utils.py

+15
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,26 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from typing import Dict, Sequence
16+
1517
from wrapt import ObjectProxy
1618

1719
from opentelemetry.trace.status import StatusCanonicalCode
1820

1921

22+
def extract_attributes_from_object(
23+
obj: any, attributes: Sequence[str], existing: Dict[str, str] = None
24+
) -> Dict[str, str]:
25+
extracted = {}
26+
if existing:
27+
extracted.update(existing)
28+
for attr in attributes:
29+
value = getattr(obj, attr, None)
30+
if value is not None:
31+
extracted[attr] = str(value)
32+
return extracted
33+
34+
2035
def http_status_to_canonical_code(
2136
status: int, allow_redirect: bool = True
2237
) -> StatusCanonicalCode:

0 commit comments

Comments
 (0)