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 f36f90f commit 89bd331
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,39 +78,44 @@ def wrapper(wrapped, instance, args, kwargs):
def _wrap_render(tracer, wrapped, instance, args, kwargs):
"""Wrap `Template.render()` or `Template.generate()`
"""
template_name = instance.name or DEFAULT_TEMPLATE_NAME
attributes = {ATTRIBUTE_JINJA2_TEMPLATE_NAME: template_name}
with tracer.start_as_current_span(
"jinja2.render", kind=SpanKind.INTERNAL, attributes=attributes
):
"jinja2.render", kind=SpanKind.INTERNAL,
) as span:
if span.is_recording():
template_name = instance.name or DEFAULT_TEMPLATE_NAME
span.set_attribute(ATTRIBUTE_JINJA2_TEMPLATE_NAME, template_name)
return wrapped(*args, **kwargs)


@_with_tracer_wrapper
def _wrap_compile(tracer, wrapped, _, args, kwargs):
template_name = (
args[1] if len(args) > 1 else kwargs.get("name", DEFAULT_TEMPLATE_NAME)
)
attributes = {ATTRIBUTE_JINJA2_TEMPLATE_NAME: template_name}
with tracer.start_as_current_span(
"jinja2.compile", kind=SpanKind.INTERNAL, attributes=attributes
):
"jinja2.compile", kind=SpanKind.INTERNAL,
) as span:
if span.is_recording():
template_name = (
args[1]
if len(args) > 1
else kwargs.get("name", DEFAULT_TEMPLATE_NAME)
)
span.set_attribute(ATTRIBUTE_JINJA2_TEMPLATE_NAME, template_name)
return wrapped(*args, **kwargs)


@_with_tracer_wrapper
def _wrap_load_template(tracer, wrapped, _, args, kwargs):
template_name = kwargs.get("name", args[0])
attributes = {ATTRIBUTE_JINJA2_TEMPLATE_NAME: template_name}
with tracer.start_as_current_span(
"jinja2.load", kind=SpanKind.INTERNAL, attributes=attributes
"jinja2.load", kind=SpanKind.INTERNAL,
) as span:
if span.is_recording():
template_name = kwargs.get("name", args[0])
span.set_attribute(ATTRIBUTE_JINJA2_TEMPLATE_NAME, template_name)
template = None
try:
template = wrapped(*args, **kwargs)
return template
finally:
if template:
if template and span.is_recording():
span.set_attribute(
ATTRIBUTE_JINJA2_TEMPLATE_PATH, template.filename
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import os
from unittest import mock

import jinja2

Expand Down Expand Up @@ -53,6 +54,21 @@ def test_render_inline_template_with_root(self):
self.assertIs(template.parent, root.get_span_context())
self.assertIsNone(root.parent)

def test_render_not_recording(self):
mock_tracer = mock.Mock()
mock_span = mock.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 mock.patch("opentelemetry.trace.get_tracer") as tracer:
tracer.return_value = mock_tracer
jinja2.environment.Template("Hello {{name}}!")
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_render_inline_template(self):
template = jinja2.environment.Template("Hello {{name}}!")
self.assertEqual(template.render(name="Jinja"), "Hello Jinja!")
Expand Down

0 comments on commit 89bd331

Please sign in to comment.