mirror of
https://github.com/open-telemetry/opentelemetry-python-contrib.git
synced 2025-08-01 17:34:38 +08:00
Use is_recording flag in flask, django, tornado, boto, botocore instrumentations (#1164)
This commit is contained in:
@ -119,17 +119,20 @@ def _before_request():
|
|||||||
|
|
||||||
tracer = trace.get_tracer(__name__, __version__)
|
tracer = trace.get_tracer(__name__, __version__)
|
||||||
|
|
||||||
|
span = tracer.start_span(
|
||||||
|
span_name,
|
||||||
|
kind=trace.SpanKind.SERVER,
|
||||||
|
start_time=environ.get(_ENVIRON_STARTTIME_KEY),
|
||||||
|
)
|
||||||
|
if span.is_recording():
|
||||||
attributes = otel_wsgi.collect_request_attributes(environ)
|
attributes = otel_wsgi.collect_request_attributes(environ)
|
||||||
if flask.request.url_rule:
|
if flask.request.url_rule:
|
||||||
# For 404 that result from no route found, etc, we
|
# For 404 that result from no route found, etc, we
|
||||||
# don't have a url_rule.
|
# don't have a url_rule.
|
||||||
attributes["http.route"] = flask.request.url_rule.rule
|
attributes["http.route"] = flask.request.url_rule.rule
|
||||||
span = tracer.start_span(
|
for key, value in attributes.items():
|
||||||
span_name,
|
span.set_attribute(key, value)
|
||||||
kind=trace.SpanKind.SERVER,
|
|
||||||
attributes=attributes,
|
|
||||||
start_time=environ.get(_ENVIRON_STARTTIME_KEY),
|
|
||||||
)
|
|
||||||
activation = tracer.use_span(span, end_on_exit=True)
|
activation = tracer.use_span(span, end_on_exit=True)
|
||||||
activation.__enter__()
|
activation.__enter__()
|
||||||
environ[_ENVIRON_ACTIVATION_KEY] = activation
|
environ[_ENVIRON_ACTIVATION_KEY] = activation
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
from unittest.mock import patch
|
from unittest.mock import Mock, patch
|
||||||
|
|
||||||
from flask import Flask, request
|
from flask import Flask, request
|
||||||
|
|
||||||
@ -106,6 +106,21 @@ class TestProgrammatic(InstrumentationTest, TestBase, WsgiTestBase):
|
|||||||
self.assertEqual(span_list[0].kind, trace.SpanKind.SERVER)
|
self.assertEqual(span_list[0].kind, trace.SpanKind.SERVER)
|
||||||
self.assertEqual(span_list[0].attributes, expected_attrs)
|
self.assertEqual(span_list[0].attributes, expected_attrs)
|
||||||
|
|
||||||
|
def test_not_recording(self):
|
||||||
|
mock_tracer = Mock()
|
||||||
|
mock_span = Mock()
|
||||||
|
mock_span.is_recording.return_value = False
|
||||||
|
mock_tracer.start_span.return_value = mock_span
|
||||||
|
mock_tracer.use_span.return_value.__enter__ = mock_span
|
||||||
|
mock_tracer.use_span.return_value.__exit__ = mock_span
|
||||||
|
with patch("opentelemetry.trace.get_tracer") as tracer:
|
||||||
|
tracer.return_value = mock_tracer
|
||||||
|
self.client.get("/hello/123")
|
||||||
|
self.assertFalse(mock_span.is_recording())
|
||||||
|
self.assertTrue(mock_span.is_recording.called)
|
||||||
|
self.assertFalse(mock_span.set_attribute.called)
|
||||||
|
self.assertFalse(mock_span.set_status.called)
|
||||||
|
|
||||||
def test_404(self):
|
def test_404(self):
|
||||||
expected_attrs = expected_attributes(
|
expected_attrs = expected_attributes(
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user