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 jinja, celery, esearch, falcon instrumentations (#1241)
This commit is contained in:
@ -117,13 +117,16 @@ class _InstrumentedFalconAPI(falcon.API):
|
|||||||
token = context.attach(
|
token = context.attach(
|
||||||
propagators.extract(otel_wsgi.get_header_from_environ, env)
|
propagators.extract(otel_wsgi.get_header_from_environ, env)
|
||||||
)
|
)
|
||||||
attributes = otel_wsgi.collect_request_attributes(env)
|
|
||||||
span = self._tracer.start_span(
|
span = self._tracer.start_span(
|
||||||
otel_wsgi.get_default_span_name(env),
|
otel_wsgi.get_default_span_name(env),
|
||||||
kind=trace.SpanKind.SERVER,
|
kind=trace.SpanKind.SERVER,
|
||||||
attributes=attributes,
|
|
||||||
start_time=start_time,
|
start_time=start_time,
|
||||||
)
|
)
|
||||||
|
if span.is_recording():
|
||||||
|
attributes = otel_wsgi.collect_request_attributes(env)
|
||||||
|
for key, value in attributes.items():
|
||||||
|
span.set_attribute(key, value)
|
||||||
|
|
||||||
activation = self._tracer.use_span(span, end_on_exit=True)
|
activation = self._tracer.use_span(span, end_on_exit=True)
|
||||||
activation.__enter__()
|
activation.__enter__()
|
||||||
env[_ENVIRON_SPAN_KEY] = span
|
env[_ENVIRON_SPAN_KEY] = span
|
||||||
@ -162,7 +165,7 @@ class _TraceMiddleware:
|
|||||||
|
|
||||||
def process_request(self, req, resp):
|
def process_request(self, req, resp):
|
||||||
span = req.env.get(_ENVIRON_SPAN_KEY)
|
span = req.env.get(_ENVIRON_SPAN_KEY)
|
||||||
if not span:
|
if not span or not span.is_recording():
|
||||||
return
|
return
|
||||||
|
|
||||||
attributes = extract_attributes_from_object(
|
attributes = extract_attributes_from_object(
|
||||||
@ -173,7 +176,7 @@ class _TraceMiddleware:
|
|||||||
|
|
||||||
def process_resource(self, req, resp, resource, params):
|
def process_resource(self, req, resp, resource, params):
|
||||||
span = req.env.get(_ENVIRON_SPAN_KEY)
|
span = req.env.get(_ENVIRON_SPAN_KEY)
|
||||||
if not span:
|
if not span or not span.is_recording():
|
||||||
return
|
return
|
||||||
|
|
||||||
resource_name = resource.__class__.__name__
|
resource_name = resource.__class__.__name__
|
||||||
@ -186,7 +189,7 @@ class _TraceMiddleware:
|
|||||||
self, req, resp, resource, req_succeeded=None
|
self, req, resp, resource, req_succeeded=None
|
||||||
): # pylint:disable=R0201
|
): # pylint:disable=R0201
|
||||||
span = req.env.get(_ENVIRON_SPAN_KEY)
|
span = req.env.get(_ENVIRON_SPAN_KEY)
|
||||||
if not span:
|
if not span or not span.is_recording():
|
||||||
return
|
return
|
||||||
|
|
||||||
status = resp.status
|
status = resp.status
|
||||||
|
@ -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 falcon import testing
|
from falcon import testing
|
||||||
|
|
||||||
@ -188,3 +188,18 @@ class TestFalconInstrumentation(TestBase):
|
|||||||
span = self.memory_exporter.get_finished_spans()[0]
|
span = self.memory_exporter.get_finished_spans()[0]
|
||||||
self.assertIn("query_string", span.attributes)
|
self.assertIn("query_string", span.attributes)
|
||||||
self.assertEqual(span.attributes["query_string"], "q=abc")
|
self.assertEqual(span.attributes["query_string"], "q=abc")
|
||||||
|
|
||||||
|
def test_traced_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().simulate_get(path="/hello?q=abc")
|
||||||
|
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)
|
||||||
|
Reference in New Issue
Block a user