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 authored Oct 14, 2020
1 parent d96d535 commit 44fbb71
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ def _uninstrument(self, **kwargs):


def _wrap_perform_request(tracer, span_name_prefix):
# pylint: disable=R0912
def wrapper(wrapped, _, args, kwargs):
method = url = None
try:
Expand All @@ -125,26 +126,27 @@ def wrapper(wrapped, _, args, kwargs):
params = kwargs.get("params", {})
body = kwargs.get("body", None)

attributes = {
"component": "elasticsearch-py",
"db.type": "elasticsearch",
}

if url:
attributes["elasticsearch.url"] = url
if method:
attributes["elasticsearch.method"] = method
if body:
attributes["db.statement"] = str(body)
if params:
attributes["elasticsearch.params"] = str(params)

with tracer.start_as_current_span(
op_name, kind=SpanKind.CLIENT, attributes=attributes
op_name, kind=SpanKind.CLIENT,
) as span:
if span.is_recording():
attributes = {
"component": "elasticsearch-py",
"db.type": "elasticsearch",
}
if url:
attributes["elasticsearch.url"] = url
if method:
attributes["elasticsearch.method"] = method
if body:
attributes["db.statement"] = str(body)
if params:
attributes["elasticsearch.params"] = str(params)
for key, value in attributes.items():
span.set_attribute(key, value)
try:
rv = wrapped(*args, **kwargs)
if isinstance(rv, dict):
if isinstance(rv, dict) and span.is_recording():
for member in _ATTRIBUTES_FROM_RESULT:
if member in rv:
span.set_attribute(
Expand All @@ -153,11 +155,12 @@ def wrapper(wrapped, _, args, kwargs):
)
return rv
except Exception as ex: # pylint: disable=broad-except
if isinstance(ex, elasticsearch.exceptions.NotFoundError):
status = StatusCanonicalCode.NOT_FOUND
else:
status = StatusCanonicalCode.UNKNOWN
span.set_status(Status(status, str(ex)))
if span.is_recording():
if isinstance(ex, elasticsearch.exceptions.NotFoundError):
status = StatusCanonicalCode.NOT_FOUND
else:
status = StatusCanonicalCode.UNKNOWN
span.set_status(Status(status, str(ex)))
raise ex

return wrapper
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,24 @@ def test_instrumentor(self, request_mock):
spans_list = self.get_ordered_finished_spans()
self.assertEqual(len(spans_list), 1)

def test_span_not_recording(self, request_mock):
request_mock.return_value = (1, {}, {})
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
Elasticsearch()
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)

ElasticsearchInstrumentor().uninstrument()

def test_prefix_arg(self, request_mock):
prefix = "prefix-from-env"
ElasticsearchInstrumentor().uninstrument()
Expand Down

0 comments on commit 44fbb71

Please sign in to comment.