Use is_recording flag in asgi, pyramid, aiohttp instrumentation (#1142)
This commit is contained in:
parent
7bb8ce1552
commit
3fe8225e85
|
|
@ -74,21 +74,22 @@ def _before_traversal(event):
|
||||||
)
|
)
|
||||||
tracer = trace.get_tracer(__name__, __version__)
|
tracer = trace.get_tracer(__name__, __version__)
|
||||||
|
|
||||||
attributes = otel_wsgi.collect_request_attributes(environ)
|
|
||||||
|
|
||||||
if request.matched_route:
|
if request.matched_route:
|
||||||
span_name = request.matched_route.pattern
|
span_name = request.matched_route.pattern
|
||||||
attributes["http.route"] = request.matched_route.pattern
|
|
||||||
else:
|
else:
|
||||||
span_name = otel_wsgi.get_default_span_name(environ)
|
span_name = otel_wsgi.get_default_span_name(environ)
|
||||||
|
|
||||||
span = tracer.start_span(
|
span = tracer.start_span(
|
||||||
span_name,
|
span_name, kind=trace.SpanKind.SERVER, start_time=start_time,
|
||||||
kind=trace.SpanKind.SERVER,
|
|
||||||
attributes=attributes,
|
|
||||||
start_time=start_time,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if span.is_recording():
|
||||||
|
attributes = otel_wsgi.collect_request_attributes(environ)
|
||||||
|
if request.matched_route:
|
||||||
|
attributes["http.route"] = request.matched_route.pattern
|
||||||
|
for key, value in attributes.items():
|
||||||
|
span.set_attribute(key, value)
|
||||||
|
|
||||||
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 pyramid.config import Configurator
|
from pyramid.config import Configurator
|
||||||
|
|
||||||
|
|
@ -87,6 +87,22 @@ 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"):
|
||||||
|
self.client.get("/hello/123")
|
||||||
|
span_list = self.memory_exporter.get_finished_spans()
|
||||||
|
self.assertEqual(len(span_list), 0)
|
||||||
|
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(
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue