def test_add_exception_cause_resetting():
    segment = Segment('seg')
    subseg = Subsegment('subseg', 'remote', segment)
    exception = Exception("testException")
    stack = [['path', 'line', 'label']]
    subseg.add_exception(exception=exception, stack=stack)
    segment.add_exception(exception=exception, stack=stack)

    segment.add_exception(exception=Exception("newException"), stack=stack)
    subseg.close()
    segment.close()

    seg_cause = segment.cause
    assert isinstance(seg_cause, dict)
    assert 'newException' == seg_cause['exceptions'][0].message
def test_serialize_subsegment_with_aws():

    segment = Segment('test')
    subsegment = Subsegment('test', 'aws', segment)
    
    aws = {
        "bucket_name": "testbucket",
        "region": "us-east-1",
        "operation": "GetObject",
        "request_id": "0000000000000000",
        "key": "123",
        "resource_names": [
            "testbucket"
        ]
    }
    
    subsegment.set_aws(aws)
    
    subsegment.close()
    segment.close()
    
    expected_subsegment_dict = {
    "id": subsegment.id,
    "name": "test",
    "start_time": subsegment.start_time,
    "in_progress": False,
    "aws": {
        "bucket_name": "testbucket",
        "region": "us-east-1",
        "operation": "GetObject",
        "request_id": "0000000000000000",
        "key": "123",
        "resource_names": [
            "testbucket"
        ]
    },
    "trace_id": subsegment.trace_id,
    "type": "subsegment",
    "namespace": "aws",
    "end_time": subsegment.end_time
    }

    actual_subsegment_dict = entity_to_dict(subsegment)
    
    assert expected_subsegment_dict == actual_subsegment_dict
def test_add_exception_referencing():
    segment = Segment('seg')
    subseg = Subsegment('subseg', 'remote', segment)
    exception = Exception("testException")
    stack = [['path', 'line', 'label']]
    subseg.add_exception(exception=exception, stack=stack)
    segment.add_exception(exception=exception, stack=stack)
    subseg.close()
    segment.close()

    seg_cause = segment.cause
    subseg_cause = subseg.cause

    assert isinstance(subseg_cause, dict)
    if sys.version_info.major == 2:
        assert isinstance(seg_cause, basestring)
    else:
        assert isinstance(seg_cause, str)
    assert seg_cause == subseg_cause['exceptions'][0].id
def test_serialize_subsegment_with_sql():

    segment = Segment('test')
    subsegment = Subsegment('test', 'remote', segment)
    
    sql = {
        "url": "jdbc:postgresql://aawijb5u25wdoy.cpamxznpdoq8.us-west-2.rds.amazonaws.com:5432/ebdb",
        "preparation": "statement",
        "database_type": "PostgreSQL",
        "database_version": "9.5.4",
        "driver_version": "PostgreSQL 9.4.1211.jre7",
        "user" : "dbuser",
        "sanitized_query" : "SELECT  *  FROM  customers  WHERE  customer_id=?;"
    }

    subsegment.set_sql(sql)
    
    subsegment.close()  
    segment.close()

    expected_subsegment_dict = {
    "id": subsegment.id,
    "name": "test",
    "start_time": subsegment.start_time,
    "in_progress": False,
    "trace_id": subsegment.trace_id,
    "type": "subsegment",
    "namespace": "remote",
    "sql": {
        "url": "jdbc:postgresql://aawijb5u25wdoy.cpamxznpdoq8.us-west-2.rds.amazonaws.com:5432/ebdb",
        "preparation": "statement",
        "database_type": "PostgreSQL",
        "database_version": "9.5.4",
        "driver_version": "PostgreSQL 9.4.1211.jre7",
        "user": "******",
        "sanitized_query": "SELECT  *  FROM  customers  WHERE  customer_id=?;"
    },
    "end_time": subsegment.end_time
    }

    actual_subsegment_dict = entity_to_dict(subsegment)
    
    assert expected_subsegment_dict == actual_subsegment_dict
def test_serialize_subsegment():

    segment = Segment('test')
    subsegment = Subsegment('test', 'local', segment)
    
    subsegment.close()  
    segment.close()

    expected_subsegment_dict = {
    "id": subsegment.id,
    "name": "test",
    "start_time": subsegment.start_time,
    "in_progress": False,
    "trace_id": subsegment.trace_id,
    "type": "subsegment",
    "namespace": "local",
    "end_time": subsegment.end_time
    }

    actual_subsegment_dict = entity_to_dict(subsegment)
    
    assert expected_subsegment_dict == actual_subsegment_dict
def test_serialize_subsegment_with_http():

    segment = Segment('test')
    subsegment = Subsegment('test', 'remote', segment)
    
    subsegment.put_http_meta(http.URL, 'https://aws.amazon.com')
    subsegment.put_http_meta(http.METHOD, 'get')

    subsegment.put_http_meta(http.STATUS, 200)
    subsegment.put_http_meta(http.CONTENT_LENGTH, 0)
    
    subsegment.close()  
    segment.close()

    expected_subsegment_dict = {
    "id": subsegment.id,
    "name": "test",
    "start_time": subsegment.start_time,
    "in_progress": False,
    "http": {
        "request": {
            "url": "https://aws.amazon.com",
            "method": "get"
        },
        "response": {
            "status": 200,
            "content_length": 0
        }
    },
    "trace_id": subsegment.trace_id,
    "type": "subsegment",
    "namespace": "remote",
    "end_time": subsegment.end_time
    }

    actual_subsegment_dict = entity_to_dict(subsegment)
    
    assert expected_subsegment_dict == actual_subsegment_dict
예제 #7
0
def test_reference_counting():

    segment = Segment('seg')
    subsegment = Subsegment('sub', 'local', segment)
    segment.add_subsegment(subsegment)
    subsegment = Subsegment('sub', 'local', segment)
    subsubsegment = Subsegment('subsub', 'local', segment)
    subsegment.add_subsegment(subsubsegment)

    assert not segment.ready_to_send()
    assert segment.ref_counter.get_current() == 2

    subsubsegment.close()
    assert not segment.ready_to_send()
    assert segment.ref_counter.get_current() == 1

    subsegment.close()
    assert not segment.ready_to_send()
    assert segment.ref_counter.get_current() == 0

    segment.close()
    assert segment.ready_to_send()
    assert segment.get_total_subsegments_size() == 2
def test_serialize_segment_with_exception():

    class TestException(Exception):
        def __init__(self, message):
            super(TestException, self).__init__(message)

    segment_one = Segment('test')
    
    stack_one = [
        ('/path/to/test.py', 10, 'module', 'another_function()'),
        ('/path/to/test.py', 3, 'another_function', 'wrong syntax')
    ]
    
    stack_two = [
        ('/path/to/test.py', 11, 'module', 'another_function()'),
        ('/path/to/test.py', 4, 'another_function', 'wrong syntax')
    ]

    exception_one = TestException('test message one')
    exception_two = TestException('test message two')

    segment_one.add_exception(exception_one, stack_one, True)
    segment_one.add_exception(exception_two, stack_two, False)
    
    segment_one.close()
    
    expected_segment_one_dict = {
    "id": segment_one.id,
    "name": "test",
    "start_time": segment_one.start_time,
    "in_progress": False,
    "cause": {
        "working_directory": segment_one.cause['working_directory'],
        "exceptions": [
            {
                "id": exception_one._cause_id,
                "message": "test message one",
                "type": "TestException",
                "remote": True,
                "stack": [
                    {
                        "path": "test.py",
                        "line": 10,
                        "label": "module"
                    },
                    {
                        "path": "test.py",
                        "line": 3,
                        "label": "another_function"
                    }
                ]
            },
            {
                "id": exception_two._cause_id,
                "message": "test message two",
                "type": "TestException",
                "remote": False,
                "stack": [
                    {
                        "path": "test.py",
                        "line": 11,
                        "label": "module"
                    },
                    {
                        "path": "test.py",
                        "line": 4,
                        "label": "another_function"
                    }
                ]
            }
        ]
    },
    "trace_id": segment_one.trace_id,
    "fault": True,
    "end_time": segment_one.end_time
    }

    segment_two = Segment('test')
    subsegment = Subsegment('test', 'local', segment_two)

    subsegment.add_exception(exception_one, stack_one, True)
    subsegment.add_exception(exception_two, stack_two, False)
    subsegment.close()
    
    # will record cause id instead as same exception already recorded in its subsegment
    segment_two.add_exception(exception_one, stack_one, True)
    
    segment_two.close()
    
    expected_segment_two_dict = {
    "id": segment_two.id,
    "name": "test",
    "start_time": segment_two.start_time,
    "in_progress": False,
    "cause": exception_one._cause_id,
    "trace_id": segment_two.trace_id,
    "fault": True,
    "end_time": segment_two.end_time
    }

    actual_segment_one_dict = entity_to_dict(segment_one)
    actual_segment_two_dict = entity_to_dict(segment_two)
    
    assert expected_segment_one_dict == actual_segment_one_dict
    assert expected_segment_two_dict == actual_segment_two_dict