def test_failed_existing_span(self):
        tracer = MockTracer()
        tracing = CommandTracing(tracer, span_tags=dict(one=123))
        event = MockEvent()
        tracing.started(event)
        scope = tracing._scopes.get('request_id')
        tracing.failed(event)
        assert tracing._scopes.get('request_id') is None

        tags = scope.span.tags
        assert tags['event.failure'] == json.dumps(event.failure)
        assert tags['reported_duration'] == event.duration_micros
 def test_started(self):
     tracer = MockTracer()
     tracing = CommandTracing(tracer, span_tags=dict(one=123))
     event = MockEvent()
     tracing.started(event)
     scope = tracing._scopes.get('request_id')
     span = scope.span
     assert span.operation_name == 'someOperation'
     tags = span.tags
     assert tags['one'] == 123
     assert tags['command.name'] == 'someOperation'
     assert tags['command'] == json.dumps(event.command)
     assert tags['namespace'] == 'someDatabase.someCollection'
     assert tags[ext_tags.COMPONENT] == 'PyMongo'
    def test_failed_existing_span(self):
        tracer = MockTracer()
        tracing = CommandTracing(tracer, span_tags=dict(one=123))
        event = MockEvent()
        event.failure = {'errmsg': 'error message', 'codeName': 'SomeError'}
        tracing.started(event)
        scope = tracing._scopes.get('request_id')
        tracing.failed(event)
        assert tracing._scopes.get('request_id') is None

        tags = scope.span.tags
        assert tags['event.failure'] == json.dumps(event.failure)
        assert tags['reported_duration'] == event.duration_micros
        assert tags['sfx.error.message'] == event.failure['errmsg']
        assert tags['sfx.error.kind'] == event.failure['codeName']