Beispiel #1
0
def create_client(aisettings=None, telemetry_processor: TelemetryProcessor = None):
    global saved_clients, saved_channels  # pylint: disable=invalid-name, global-statement

    if aisettings is None:
        aisettings = load_settings()

    if aisettings in saved_clients:
        return saved_clients[aisettings]

    channel_settings = aisettings.channel_settings

    if channel_settings in saved_channels:
        channel = saved_channels[channel_settings]
    else:
        sender = AsynchronousSender(service_endpoint_uri=channel_settings.endpoint)

        if channel_settings.send_time is not None:
            sender.send_time = channel_settings.send_time
        if channel_settings.send_interval is not None:
            sender.send_interval = channel_settings.send_interval

        queue = AsynchronousQueue(sender)
        channel = TelemetryChannel(None, queue)
        saved_channels[channel_settings] = channel

    ikey = aisettings.ikey
    if ikey is None:
        return dummy_client("No ikey specified", telemetry_processor)

    client = get_telemetry_client_with_processor(
        aisettings.ikey, channel, telemetry_processor
    )
    saved_clients[aisettings] = client
    return client
    def init_app(self, app):
        """
        Initializes the extension for the provided Flask application.

        Args:
            app (flask.Flask). the Flask application for which to initialize the extension.
        """
        self._key = app.config.get(CONF_KEY) or getenv(CONF_KEY)

        if not self._key:
            return

        self._endpoint_uri = app.config.get(CONF_ENDPOINT_URI)

        if self._endpoint_uri:
            sender = AsynchronousSender(self._endpoint_uri)
        else:
            sender = AsynchronousSender()

        queue = AsynchronousQueue(sender)
        self._channel = TelemetryChannel(None, queue)

        self._init_request_logging(app)
        self._init_trace_logging(app)
        self._init_exception_logging(app)
Beispiel #3
0
    def _telemetry(self) -> Union[TelemetryClient, NullTelemetryClient]:
        """Create the telemetry client."""
        if not self.ikey:
            return NullTelemetryClient()

        if self.endpoint:
            sender = AsynchronousSender(self.endpoint)
        else:
            sender = AsynchronousSender()
        queue = AsynchronousQueue(sender)
        context = TelemetryContext()
        context.instrumentation_key = self.ikey
        channel = TelemetryChannel(context, queue)
        return TelemetryClient(self.ikey, telemetry_channel=channel)
Beispiel #4
0
    def init_app(self, app, context):
        """
        Initializes the extension for the provided Flask application.

        Args:
            app (flask.Flask). the Flask application for which to initialize the extension.
        """
        print("Starting application insights module.")
        self._key_grantee = app.config.get(CONF_KEY_GRANTEE) or getenv(
            CONF_KEY_GRANTEE)
        self._key_ai4e = app.config.get(CONF_KEY_AI4E) or getenv(CONF_KEY_AI4E)

        if (self._key_grantee and len(self._key_grantee.strip()) > 0):
            self._key_grantee = self._key_grantee.strip()
        else:
            self._key_grantee = None

        if (self._key_ai4e and len(self._key_ai4e.strip()) > 0):
            self._key_ai4e = self._key_ai4e.strip()
        else:
            self._key_ai4e = None

        if self._key_grantee:
            print("Grantee application insights key set.")

        if self._key_ai4e:
            print("AI4E application insights key set: " + str(self._key_ai4e))

        if not self._key_grantee and not self._key_ai4e:
            return

        self._endpoint_uri = app.config.get(CONF_ENDPOINT_URI)

        if self._endpoint_uri:
            sender = AsynchronousSender(self._endpoint_uri)
        else:
            sender = AsynchronousSender()

        queue = AsynchronousQueue(sender)

        if not context:
            context = AI4ETelemetryContext()

        self._channel = TelemetryChannel(context, queue)

        self._init_request_logging(app)
        self._init_trace_logging(app)
        self._init_exception_logging(app)
 def client(self):
     ctx = _app_ctx_stack.top
     if ctx is not None:
         if not hasattr(ctx, 'appinsight_client'):
             ctx.appinsight_client = TelemetryClient(
                 current_app.config[CONFIG_KEY_INSTRUMENTATION_KEY],
                 telemetry_channel=TelemetryChannel(
                     queue=AsynchronousQueue(AsynchronousSender())))
         return ctx.appinsight_client
Beispiel #6
0
 def _telemetry(self) -> TelemetryClient:
     """Create the telemetry client."""
     if not self.ikey:
         sender = NullSender()
     else:
         sender = AsynchronousSender(self.endpoint)
     ikey = self.ikey or '00000000-0000-0000-0000-000000000000'
     queue = AsynchronousQueue(sender)
     context = TelemetryContext()
     context.instrumentation_key = ikey
     channel = TelemetryChannel(context, queue)
     return TelemetryClient(ikey, telemetry_channel=channel)
    def init_app(self, app, context):
        """
        Initializes the extension for the provided Flask application.

        Args:
            app (flask.Flask). the Flask application for which to initialize the extension.
        """
        print("Starting application insights module.")
        self._appinsights_key = app.config.get(
            APPINSIGHTS_INSTRUMENTATIONKEY) or getenv(
                APPINSIGHTS_INSTRUMENTATIONKEY)

        if self._appinsights_key and len(self._appinsights_key.strip()) > 0:
            self._appinsights_key = self._appinsights_key.strip()
        else:
            self._appinsights_key = None

        # Set the application insights key for production use.
        if self._appinsights_key:
            print("Application insights key set.")

        self._endpoint_uri = app.config.get(CONF_ENDPOINT_URI)

        if self._endpoint_uri:
            sender = AsynchronousSender(self._endpoint_uri)
        else:
            sender = AsynchronousSender()

        queue = AsynchronousQueue(sender)

        if not context:
            context = AI4ETelemetryContext()

        self._channel = TelemetryChannel(context, queue)

        self._init_request_logging(app)
        self._init_trace_logging(app)
        self._init_exception_logging(app)
Beispiel #8
0
    def __init__(self):
        self.grantee_key = None
        raw_key = getenv(CONF_KEY_GRANTEE, None)
        if (raw_key and len(raw_key.strip()) > 0):
            self.grantee_key = raw_key.strip()

        if (self.grantee_key):
            self.sender = AsynchronousSender()
            self.r_queue = AsynchronousQueue(self.sender)
            self.r_context = AI4ETelemetryContext()
            self.r_channel = TelemetryChannel(self.r_context, self.r_queue)

            self.appinsights_grantee_client = TelemetryClient(
                getenv(CONF_KEY_GRANTEE), self.r_channel)
            self.appinsights_ai4e_client = None

            if (getenv(CONF_KEY_AI4E)):
                self.appinsights_ai4e_client = TelemetryClient(
                    getenv(CONF_KEY_AI4E), self.r_channel)
Beispiel #9
0
    def setup(self, app):
        """
        Initializes the plugin for the provided Bottle application.

        Args:
            app (bottle.Bottle). the Bottle application for which to initialize the extension.
        """
        self._key = app.config.get(CONF_KEY) or getenv(CONF_KEY)

        if not self._key:
            return

        self._endpoint_uri = app.config.get(CONF_ENDPOINT_URI)
        sender = AsynchronousSender(self._endpoint_uri)

        queue = AsynchronousQueue(sender)
        self._channel = TelemetryChannel(None, queue)
        self._tc = TelemetryClient(self._key, self._channel)

        self.context.cloud.role_instance = platform.node()
Beispiel #10
0
def create_telemetrie_client():
    key = get_instrumentation_key()

    #https://microsoft.github.io/ApplicationInsights-Python/
    queue = AsynchronousQueue(AsynchronousSender())
    telemetrie_channel = channel.TelemetryChannel(None, queue)
    telemetrie = TelemetryClient(key, telemetry_channel = telemetrie_channel)

    # flush telemetry if we have 10 or more telemetry items in our queue
    telemetrie.channel.queue.max_queue_length = 10
    # send telemetry to the service in batches of 5
    telemetrie.channel.sender.send_buffer_size = 1
    # the background worker thread will be active for 5 seconds before it shuts down. if
    # during this time items are picked up from the queue, the timer is reset.
    telemetrie.channel.sender.send_time = 5
    # the background worker thread will poll the queue every 0.5 seconds for new items
    telemetrie.channel.sender.send_interval = 0.5

    telemetrie.context.operation.name = 'neural-image-tensorflow'

    return telemetrie
 def test_default_send_interval(self):
     """Tests that send_interval is equal to the default when it is unspecified"""
     client = common.create_client()
     self.assertEqual(client.channel.sender.send_interval,
                      AsynchronousSender().send_interval)
import django
from django.test import TestCase, Client, modify_settings, override_settings

from applicationinsights import TelemetryClient
from applicationinsights.channel import TelemetryChannel, SynchronousQueue, SenderBase, NullSender, AsynchronousSender
from applicationinsights.django import common

if django.VERSION > (1, 10):
    MIDDLEWARE_NAME = "MIDDLEWARE"
else:
    MIDDLEWARE_NAME = "MIDDLEWARE_CLASSES"

TEST_IKEY = '12345678-1234-5678-9012-123456789abc'
TEST_ENDPOINT = 'https://test.endpoint/v2/track'
DEFAULT_ENDPOINT = AsynchronousSender().service_endpoint_uri
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))


class AITestCase(TestCase):
    def plug_sender(self):
        # Reset saved objects
        common.saved_clients = {}
        common.saved_channels = {}

        # Create a client and mock out the sender
        client = common.create_client()
        sender = MockSender()
        client._channel = TelemetryChannel(None, SynchronousQueue(sender))
        self.events = sender.events
        self.channel = client.channel
Beispiel #13
0
def _create_telemetry_channel() -> TelemetryChannel:
    sender = AsynchronousSender(
        APPINSIGHTS_HOST) if APPINSIGHTS_HOST else NullSender()
    queue = AsynchronousQueue(sender)
    return TelemetryChannel(queue=queue)