diff --git a/CHANGELOG.rst b/CHANGELOG.rst index af0d38d2..f94ac598 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -4,6 +4,7 @@ CHANGELOG Unreleased ========== +* bugfix: Handle missing segment gracefully in pymongo patch `https://github.com/aws/aws-xray-sdk-python/pull/475` 2.15.0 ========== diff --git a/aws_xray_sdk/ext/pymongo/patch.py b/aws_xray_sdk/ext/pymongo/patch.py index cd8df5d9..d82479ff 100644 --- a/aws_xray_sdk/ext/pymongo/patch.py +++ b/aws_xray_sdk/ext/pymongo/patch.py @@ -24,6 +24,8 @@ def started(self, event): subsegment = xray_recorder.begin_subsegment( f'{event.database_name}@{host_and_port_str}', 'remote') + if not subsegment: + return subsegment.put_annotation('mongodb_command_name', event.command_name) subsegment.put_annotation('mongodb_connection_id', host_and_port_str) subsegment.put_annotation('mongodb_database_name', event.database_name) @@ -34,6 +36,8 @@ def started(self, event): def succeeded(self, event): subsegment = xray_recorder.current_subsegment() + if not subsegment: + return subsegment.put_annotation('mongodb_duration_micros', event.duration_micros) if self.record_full_documents: subsegment.put_metadata('mongodb_reply', event.reply) @@ -41,6 +45,8 @@ def succeeded(self, event): def failed(self, event): subsegment = xray_recorder.current_subsegment() + if not subsegment: + return subsegment.add_fault_flag() subsegment.put_annotation('mongodb_duration_micros', event.duration_micros) subsegment.put_metadata('failure', event.failure)