def test_mk_sdkopts_alldefaults():
    oldargs = sys.argv
    newargs = ['--dt_foo=bar', 'qu', '--dt_bla=off', 'dt_quak=on']
    try:
        sys.argv = list(newargs)  # copy
        opts = oneagent.sdkopts_from_commandline()
        assert sys.argv == newargs  # Nothing should be removed
    finally:
        sys.argv = oldargs
    assert opts == ['foo=bar', 'bla=off']
Пример #2
0
def main():
    # This gathers arguments prefixed with '--dt_' from sys.argv into the
    # returned list. See also the basic-sdk-sample.
    sdk_options = oneagent.sdkopts_from_commandline(remove=True)

    # Before using the SDK you have to initialize the OneAgent. In this scenario, we
    # initialize the SDK and prepare it for forking.
    #
    # Passing in the sdk_options is entirely optional and usually not required
    # as all settings will be automatically provided by the Dynatrace OneAgent
    # that is installed on the host.
    #
    # To activate the forking support add the optional 'forkable' parameter and set it to True.
    #
    # If you run this example on Windows then you'll get an "Invalid Argument" error back
    # because there's no forking support for Windows available.
    init_result = oneagent.initialize(sdk_options, forkable=True)
    try:
        if init_result.error is not None:
            print('Error during SDK initialization:', init_result.error)

        # While not by much, it is a bit faster to cache the result of
        # oneagent.get_sdk() instead of calling the function multiple times.
        sdk = getsdk()

        # The agent state is one of the integers in oneagent.sdk.AgentState.
        # Since we're using the 'forkable' mode the state will be TEMPORARILY_INACTIVE (1) on Linux.
        print('Agent state (parent process):', sdk.agent_state)

        # In the parent, the state will be PARENT_INITIALIZED (1).
        print('Agent fork state (parent process):', sdk.agent_fork_state)

        # The instance attribute 'agent_found' indicates whether an agent could be found or not.
        print('Agent found:', sdk.agent_found)

        # If an agent was found but it is incompatible with this version of the SDK for Python
        # then 'agent_is_compatible' would be set to false.
        print('Agent is compatible:', sdk.agent_is_compatible)

        # The agent version is a string holding both the OneAgent version and the
        # OneAgent SDK for C/C++ version separated by a '/'.
        print('Agent version:', sdk.agent_version_string)

        if init_result.error is None:
            fork_children()
            input('Now wait until the path appears in the UI ...')
    finally:
        shutdown_error = oneagent.shutdown()
        if shutdown_error:
            print('Error shutting down SDK:', shutdown_error)
def main():
    print('+main')

    oneagent.logger.setLevel(1)
    oneagent.logger.addHandler(logging.StreamHandler())

    # This gathers arguments prefixed with '--dt_' from sys.argv into the
    # returned list. See try_init below.
    sdk_options = oneagent.sdkopts_from_commandline(remove=True)

    # If you do not call try_init() manually, the first call to
    # oneagent.sdk.SDK.get() will attempt to initialize the SDK with default
    # options, swallowing any errors, which is why manually calling try_init()
    # is recommended.
    # Passing in the sdk_options is entirely optional and usually not required
    # as all settings will be automatically provided by the Dynatrace OneAgent
    # that is installed on the host.
    init_result = oneagent.try_init(sdk_options)
    try:
        if init_result.error is not None:
            print('Error during SDK initialization:', init_result.error)

        # While not by much, it is a bit faster to cache the result of
        # oneagent.sdk.SDK.get() instead of calling the function multiple times.
        sdk = getsdk()

        # The agent state is one of the integers in oneagent.sdk.AgentState.
        print('Agent state:', sdk.agent_state)

        # The agent version is the version of the installed OneAgent, not the
        # version of the SDK.
        print('Agent version:', sdk.agent_version_string)

        mock_incoming_web_request()

        # We use trace_incoming_remote_call here, because it is one of the few
        # calls that create a new path if none is running yet.
        with sdk.trace_incoming_remote_call('main', 'main', 'main'):
            # Simulate some remote calls
            outgoing_remote_call(success=True)
            outgoing_remote_call(success=True)
            outgoing_remote_call(success=False)
        print('-main')
        input('Now wait until the path appears in the UI...')
    finally:
        shutdown_error = oneagent.shutdown()
        if shutdown_error:
            print('Error shutting down SDK:', shutdown_error)
def test_load_old_agent():
    saved_path = os.environ.get('DT_AGENTLIBRARY', '')
    try:
        assert os.environ['DT_OLDAGENTLIBRARY'] is not None
        assert os.environ['DT_OLDAGENTLIBRARY'] != ''
        os.environ['DT_AGENTLIBRARY'] = os.environ.get('DT_OLDAGENTLIBRARY', '')

        sdk_options = oneagent.sdkopts_from_commandline(remove=True)
        init_result = oneagent.initialize(sdk_options)
        assert init_result.error is not None
        assert init_result.status == InitResult.STATUS_INIT_ERROR

        sdk = oneagent.get_sdk()

        assert sdk.agent_state == AgentState.NOT_INITIALIZED
        assert sdk.agent_found
        assert not sdk.agent_is_compatible

        assert sdk.agent_version_string == '1.141.246.20180604-140607/' + shipped_c_stub_version
    finally:
        oneagent.shutdown()
        os.environ['DT_AGENTLIBRARY'] = saved_path
Пример #5
0
def sdk_init():
    ''' Initialize OneAgent Python SDK '''
    sdk_options = oneagent.sdkopts_from_commandline(remove=True)
    init_result = oneagent.initialize(sdk_options)
    if init_result:
        print('SDK should work (but agent might be inactive).',
              file=sys.stderr)
    else:
        print(
            'SDK will definitely not work (i.e. functions will be no-ops); init_result =',
            init_result,
            file=sys.stderr)
        return False
    try:
        getsdk().set_diagnostic_callback(_diag_callback)
    finally:
        print('Agent state:', getsdk().agent_state, file=sys.stderr)
        print('Agent found:', getsdk().agent_found, file=sys.stderr)
        print('Agent is compatible:',
              getsdk().agent_is_compatible,
              file=sys.stderr)
        print('Agent version:', getsdk().agent_version_string, file=sys.stderr)

    return True
def test_mk_sdkopts_remove():
    argv_orig = ['/SDKfoo=bar', 'qu', '/SDKbla=off', '--SDKquak=on']
    argv = list(argv_orig)  # copy
    opts = oneagent.sdkopts_from_commandline(argv, remove=True, prefix='/SDK')
    assert argv == [argv_orig[1], argv_orig[3]]
    assert opts == ['foo=bar', 'bla=off']
def test_mk_sdkopts_customprefix():
    argv_orig = ['/SDKfoo=bar', 'qu', '/SDKbla=off', '/sdkquak=on']
    argv = list(argv_orig)  # copy
    opts = oneagent.sdkopts_from_commandline(argv, prefix='/SDK')
    assert argv == argv_orig
    assert opts == ['foo=bar', 'bla=off']
def main():
    print('+main')

    # This gathers arguments prefixed with '--dt_' from sys.argv into the
    # returned list. See initialize below.
    sdk_options = oneagent.sdkopts_from_commandline(remove=True)

    # Before using the SDK you have to initialize the OneAgent. You can call oneagent.initialize()
    # as often as you want, but you also have to call oneagent.shutdown() for every call to
    # initialize() as well.
    #
    # Passing in the sdk_options is entirely optional and usually not required
    # as all settings will be automatically provided by the Dynatrace OneAgent
    # that is installed on the host.
    init_result = oneagent.initialize(sdk_options)
    try:
        if init_result.error is not None:
            print('Error during SDK initialization:', init_result.error)

        # While not by much, it is a bit faster to cache the result of
        # oneagent.get_sdk() instead of calling the function multiple times.
        sdk = getsdk()

        # Set the diagnostic callback. Strongly recommended.
        sdk.set_diagnostic_callback(_diag_callback)

        # Set the verbose callback.
        # Not recommended in production as lots of messages can be emitted.
        if IN_DEV_ENVIRONMENT:
            sdk.set_verbose_callback(_diag_callback)

        # The agent state is one of the integers in oneagent.sdk.AgentState.
        print('Agent state:', sdk.agent_state)

        # The instance attribute 'agent_found' indicates whether an agent could be found or not.
        print('Agent found:', sdk.agent_found)

        # If an agent was found but it is incompatible with this version of the SDK for Python
        # then 'agent_is_compatible' would be set to false.
        print('Agent is compatible:', sdk.agent_is_compatible)

        # The agent version is a string holding both the OneAgent version and the
        # OneAgent SDK for C/C++ version separated by a '/'.
        print('Agent version:', sdk.agent_version_string)

        mock_incoming_web_request()

        mock_outgoing_web_request()

        mock_outgoing_message()

        mock_custom_service()

        # We use trace_incoming_remote_call here, because it is one of the few
        # calls that create a new path if none is running yet.
        with sdk.trace_incoming_remote_call('main', 'main', 'main'):
            # We want to start an asynchronous execution at this time, so we create an
            # in-process link which we will use afterwards (or in a different thread).
            link = sdk.create_in_process_link()

            # Simulate some remote calls
            outgoing_remote_call(success=True)
            outgoing_remote_call(success=True)
            outgoing_remote_call(success=False)

        # Now the asynchronous execution starts. So we create an in-process tracer. We're using
        # the in-process link which we've created above. This link specifies where the traced
        # actions below will show up in the path.
        with sdk.trace_in_process_link(link):
            outgoing_remote_call(success=True)

        print('-main')
        input('Now wait until the path appears in the UI...')
    finally:
        shutdown_error = oneagent.shutdown()
        if shutdown_error:
            print('Error shutting down SDK:', shutdown_error)