def main():
    call_msg = []

    def diag_cb(msg):
        sys.stderr.flush()
        call_msg.append(msg)

    sdk = onesdk.SDK.get()
    try:
        sdk.set_diagnostic_callback(diag_cb)
        sdk.create_database_info(None, None, onesdk.Channel(0))
        gc.collect()
        gc.collect()
        gc.collect()
        print(call_msg)
        n_msgs = len(call_msg)

        # Database name must not be null (from CSDK), leaked db info handle
        assert n_msgs == 2

        assert all(isinstance(m, six.text_type) for m in call_msg)
        sdk.set_diagnostic_callback(None)
        sdk.create_database_info(None, None, onesdk.Channel(0))
        print(call_msg[n_msgs:])
        assert len(call_msg) == n_msgs
    finally:
        oneagent.shutdown()
def do_remote_call_thread_func(strtag, success):
    try:
        print('+thread')
        # We use positional arguments to specify required values and named
        # arguments to specify optional values.
        incall = getsdk().trace_incoming_remote_call(
            'dummyPyMethod',
            'DummyPyService',
            'dupypr://localhost/dummyEndpoint',
            protocol_name='DUMMY_PY_PROTOCOL',
            str_tag=strtag)
        with incall:
            if not success:
                raise RuntimeError('Remote call failed on the server side.')
            dbinfo = getsdk().create_database_info(
                'Northwind', onesdk.DatabaseVendor.SQLSERVER,
                onesdk.Channel(onesdk.ChannelType.TCP_IP, '10.0.0.42:6666'))

            # This with-block will automatically free the database info handle
            # at the end. Note that the handle is used for multiple tracers. In
            # general, it is recommended to reuse database (and web application)
            # info handles as often as possible (for efficiency reasons).
            with dbinfo:
                traced_db_operation(dbinfo, "BEGIN TRAN;")
                traced_db_operation(
                    dbinfo, "SELECT TOP 1 qux FROM baz ORDER BY quux;")
                traced_db_operation(dbinfo,
                                    "SELECT foo, bar FROM baz WHERE qux = 23")
                traced_db_operation(
                    dbinfo, "UPDATE baz SET foo = foo + 1 WHERE qux = 23;")
                traced_db_operation(dbinfo, "COMMIT;")
        print('-thread')
    except Exception as e:
        failed[0] = e
        raise
def mock_process_incoming_message():
    sdk = getsdk()

    # Create the messaging system info object.
    msi_handle = sdk.create_messaging_system_info(
        'MyPythonSenderVendor', 'MyPythonDestination',
        MessagingDestinationType.QUEUE,
        onesdk.Channel(onesdk.ChannelType.UNIX_DOMAIN_SOCKET,
                       'MyPythonChannelEndpoint'))

    with msi_handle:
        # Create the receive tracer for incoming messages.
        with sdk.trace_incoming_message_receive(msi_handle):
            print('here we wait for incoming messages ...')

            # Create the tracer for processing incoming messages.
            tracer = sdk.trace_incoming_message_process(msi_handle)

            # Now we can set the vendor message and correlation IDs. It's possible to set them
            # either before the tracer is started or afterwards. But they have to be set before
            # the tracer ends.
            tracer.set_vendor_message_id('message_id')
            with tracer:
                print('handle incoming message')
                tracer.set_correlation_id('correlation_id')
def mock_outgoing_message():
    sdk = getsdk()

    # Create the messaging system info object.
    msi_handle = sdk.create_messaging_system_info(
        'MyPythonReceiverVendor', 'MyPythonDestination',
        MessagingDestinationType.TOPIC,
        onesdk.Channel(onesdk.ChannelType.TCP_IP, '10.11.12.13:1415'))

    with msi_handle:
        # Create the outgoing message tracer;
        with sdk.trace_outgoing_message(msi_handle) as tracer:
            # Set the message and correlation IDs.
            tracer.set_vendor_message_id('msgId')
            tracer.set_correlation_id('corrId')

            print('handle outgoing message')
def test_trace_sql_database_request(sdk):
    with create_dummy_entrypoint(sdk):
        dbi = sdk.create_database_info(
            'dbn', 'dbv', onesdk.Channel(onesdk.ChannelType.OTHER, 'ce'))
        hdbi = dbi.handle
        with dbi:
            tracer = sdk.trace_sql_database_request(dbi, DUMMY_SQL)
            with tracer:
                tracer.set_round_trip_count(1)
                tracer.set_rows_returned(42)
    nsdk = get_nsdk(sdk)
    assert len(nsdk.finished_paths) == 1
    _, root = nsdk.finished_paths[0].children[0]  # Strip dummy entrypoint
    assert isinstance(root, sdkmockiface.DbRequestHandle)
    assert root.vals[0] is hdbi
    assert root.vals[1] == DUMMY_SQL
    assert root.round_trip_count == 1
    assert root.returned_row_count == 42
def test_trace_out_remote_call(sdk):
    print('SDK:', sdk)
    with create_dummy_entrypoint(sdk):
        print('SDK2:', sdk)
        tracer = sdk.trace_outgoing_remote_call('a',
                                                'b',
                                                'c',
                                                onesdk.Channel(
                                                    onesdk.ChannelType.OTHER,
                                                    'e'),
                                                protocol_name='foo')
        tracer.start()
        tracer.end()
    nsdk = get_nsdk(sdk)
    assert len(nsdk.finished_paths) == 1
    _, root = nsdk.finished_paths[0].children[0]
    assert isinstance(root, sdkmockiface.OutRemoteCallHandle)
    assert root.vals == ('a', 'b', 'c', onesdk.ChannelType.OTHER, 'e')
    assert root.protocol_name == 'foo'
def outgoing_remote_call(success):
    print('+remote')

    # We use positional arguments to specify required values and named arguments
    # to specify optional values.
    call = getsdk().trace_outgoing_remote_call(
        'dummyPyMethod', 'DummyPyService', 'dupypr://localhost/dummyEndpoint',
        onesdk.Channel(onesdk.ChannelType.IN_PROCESS, 'localhost'),
        protocol_name='DUMMY_PY_PROTOCOL')
    try:
        with call:

            # Note that this property can only be accessed after starting the
            # tracer. See the documentation on tagging for more information.
            strtag = call.outgoing_dynatrace_string_tag
            do_remote_call(strtag, success)
    except RuntimeError: # Swallow the exception raised above.
        pass
    print('-remote')