Skip to content

Commit

Permalink
Use is_recording flag in jinja, celery, esearch, falcon instrumentati…
Browse files Browse the repository at this point in the history
…ons (#1241)
  • Loading branch information
lzchen committed Oct 14, 2020
1 parent e6c86d1 commit 6c7cdae
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,16 @@ def __call__(self, env, start_response):
token = context.attach(
propagators.extract(otel_wsgi.get_header_from_environ, env)
)
attributes = otel_wsgi.collect_request_attributes(env)
span = self._tracer.start_span(
otel_wsgi.get_default_span_name(env),
kind=trace.SpanKind.SERVER,
attributes=attributes,
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.__enter__()
env[_ENVIRON_SPAN_KEY] = span
Expand Down Expand Up @@ -162,7 +165,7 @@ def __init__(self, tracer=None, traced_request_attrs=None):

def process_request(self, req, resp):
span = req.env.get(_ENVIRON_SPAN_KEY)
if not span:
if not span or not span.is_recording():
return

attributes = extract_attributes_from_object(
Expand All @@ -173,7 +176,7 @@ def process_request(self, req, resp):

def process_resource(self, req, resp, resource, params):
span = req.env.get(_ENVIRON_SPAN_KEY)
if not span:
if not span or not span.is_recording():
return

resource_name = resource.__class__.__name__
Expand All @@ -186,7 +189,7 @@ def process_response(
self, req, resp, resource, req_succeeded=None
): # pylint:disable=R0201
span = req.env.get(_ENVIRON_SPAN_KEY)
if not span:
if not span or not span.is_recording():
return

status = resp.status
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from unittest.mock import patch
from unittest.mock import Mock, patch

from falcon import testing

Expand Down Expand Up @@ -188,3 +188,18 @@ def test_traced_request_attributes(self):
span = self.memory_exporter.get_finished_spans()[0]
self.assertIn("query_string", span.attributes)
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)

0 comments on commit 6c7cdae

Please sign in to comment.