コード例 #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
コード例 #2
0
    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)
コード例 #3
0
 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
コード例 #4
0
def dummy_client(reason: str, telemetry_processor: TelemetryProcessor = None):  # pylint: disable=unused-argument
    """Creates a dummy channel so even if we're not logging telemetry, we can still send
    along the real object to things that depend on it to exist"""

    sender = NullSender()
    queue = SynchronousQueue(sender)
    channel = TelemetryChannel(None, queue)
    client = get_telemetry_client_with_processor(
        "00000000-0000-0000-0000-000000000000", channel, telemetry_processor)
    return client
コード例 #5
0
def enable(instrumentation_key, *args, **kwargs):
    """Enables the Application Insights logging handler for the root logger for the supplied instrumentation key.
    Multiple calls to this function with different instrumentation keys result in multiple handler instances.

    .. code:: python

        import logging
        from applicationinsights.logging import enable

        # set up logging
        enable('<YOUR INSTRUMENTATION KEY GOES HERE>')

        # log something (this will be sent to the Application Insights service as a trace)
        logging.info('This is a message')

        # logging shutdown will cause a flush of all un-sent telemetry items
        # alternatively set up an async channel via enable('<YOUR INSTRUMENTATION KEY GOES HERE>', async_=True)

    Args:
        instrumentation_key (str). the instrumentation key to use while sending telemetry to the service.

    Keyword Args:
        async_ (bool): Whether to use an async channel for the telemetry. Defaults to False.
        endpoint (str): The custom endpoint to which to send the telemetry. Defaults to None.
        level (Union[int, str]): The level to set for the logger. Defaults to INFO.

    Returns:
        :class:`ApplicationInsightsHandler`. the newly created or existing handler.
    """
    if not instrumentation_key:
        raise Exception('Instrumentation key was required but not provided')
    if instrumentation_key in enabled_instrumentation_keys:
        logging.getLogger().removeHandler(
            enabled_instrumentation_keys[instrumentation_key])
    async_ = kwargs.pop('async_', False)
    endpoint = kwargs.pop('endpoint', None)
    telemetry_channel = kwargs.get('telemetry_channel')
    if telemetry_channel and async_:
        raise Exception('Incompatible arguments async_ and telemetry_channel')
    if telemetry_channel and endpoint:
        raise Exception(
            'Incompatible arguments endpoint and telemetry_channel')
    if not telemetry_channel:
        if async_:
            sender, queue = AsynchronousSender, AsynchronousQueue
        else:
            sender, queue = SynchronousSender, SynchronousQueue
        kwargs['telemetry_channel'] = TelemetryChannel(
            queue=queue(sender(endpoint)))
    log_level = kwargs.pop('level', logging.INFO)
    handler = LoggingHandler(instrumentation_key, *args, **kwargs)
    handler.setLevel(log_level)
    enabled_instrumentation_keys[instrumentation_key] = handler
    logging.getLogger().addHandler(handler)
    return handler
コード例 #6
0
def build_vortex_telemetry_client(service_endpoint_uri):
    """
        Build vortex telemetry client.
    """
    vortex_sender = VortexSynchronousSender(service_endpoint_uri)
    sync_queue = SynchronousQueue(vortex_sender)
    channel = TelemetryChannel(None, queue=sync_queue)

    client = TelemetryClient(INSTRUMENTATION_KEY, channel)
    enable(INSTRUMENTATION_KEY)
    return client
コード例 #7
0
    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
コード例 #8
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)
コード例 #9
0
def main(endpoint: str, ikey: str, send_config: SendConfig):
    sender = NoRetrySender(endpoint)
    queue = SynchronousQueue(sender)
    context = TelemetryContext()
    context.instrumentation_key = ikey
    channel = TelemetryChannel(context, queue)
    client = TelemetryClient(ikey, telemetry_channel=channel)

    send_events(client, send_config.num_events)
    send_logs(client, send_config.num_traces)
    send_exceptions(client, send_config.num_exceptions)

    client.flush()
コード例 #10
0
    def _get_telemetry_client(self, instrumentation_key):
        if not instrumentation_key:
            return None

        if instrumentation_key not in self._clients:
            # In the original implementation there is a line of code enable the exception hook. Removed.
            # enable(instrumentation_key)

            channel = TelemetryChannel(queue=SynchronousQueue(self._sender()))
            client = TelemetryClient(instrumentation_key=instrumentation_key, telemetry_channel=channel)
            self._clients[instrumentation_key] = client

        return self._clients[instrumentation_key]
コード例 #11
0
ファイル: logger.py プロジェクト: cicorias/agogosml
    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)
コード例 #12
0
def main(endpoint: furl, ikey: str, send_config: SendConfig):
    wait_for_port(port=endpoint.port, host=endpoint.host)

    sender = NoRetrySender(endpoint)
    queue = SynchronousQueue(sender)
    channel = TelemetryChannel(queue=queue)
    client = TelemetryClient(ikey, channel)

    send_events(client, send_config.num_events)
    send_logs(client, send_config.num_traces)
    send_exceptions(client, send_config.num_exceptions)
    send_requests(client, send_config.num_requests)

    client.flush()
コード例 #13
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)
コード例 #14
0
ファイル: enable.py プロジェクト: brennerm/python-appinsights
def __enable_for_urllib3(http_connection_pool_class,
                         https_connection_pool_class, instrumentation_key,
                         telemetry_channel, always_flush):
    if not instrumentation_key:
        raise Exception('Instrumentation key was required but not provided')

    if telemetry_channel is None:
        sender = SynchronousSender()
        queue = SynchronousQueue(sender)
        telemetry_channel = TelemetryChannel(None, queue)

    client = TelemetryClient(instrumentation_key, telemetry_channel)

    orig_http_urlopen_method = http_connection_pool_class.urlopen
    orig_https_urlopen_method = https_connection_pool_class.urlopen

    def custom_urlopen_wrapper(urlopen_func):
        def custom_urlopen(*args, **kwargs):
            start_time = current_milli_time()
            response = urlopen_func(*args, **kwargs)
            try:  # make sure to always return the response
                duration = current_milli_time() - start_time
                try:
                    method = args[1]
                except IndexError:
                    method = kwargs['method']

                try:
                    url = args[2]
                except IndexError:
                    url = kwargs['url']

                _track_dependency(client, always_flush, args[0].host, method,
                                  url, duration, response.status)
            finally:
                return response

        return custom_urlopen

    http_connection_pool_class.urlopen = custom_urlopen_wrapper(
        orig_http_urlopen_method)
    https_connection_pool_class.urlopen = custom_urlopen_wrapper(
        orig_https_urlopen_method)
コード例 #15
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)
コード例 #16
0
def upload(data_to_save):
    data_to_save = json.loads(data_to_save)

    for instrumentation_key in data_to_save:
        client = TelemetryClient(instrumentation_key=instrumentation_key,
                                 telemetry_channel=TelemetryChannel(queue=SynchronousQueue(LimitedRetrySender())))
        enable(instrumentation_key)
        for record in data_to_save[instrumentation_key]:
            name = record['name']
            raw_properties = record['properties']
            properties = {}
            measurements = {}
            for k, v in raw_properties.items():
                if isinstance(v, str):
                    properties[k] = v
                else:
                    measurements[k] = v
            client.track_event(name, properties, measurements)
        client.flush()
コード例 #17
0
def upload(data_to_save):
    if in_diagnostic_mode():
        sys.stdout.write('Telemetry upload begins\n')
        sys.stdout.write('Got data {}\n'.format(
            json.dumps(json.loads(data_to_save), indent=2)))

    try:
        data_to_save = json.loads(data_to_save.replace("'", '"'))
    except Exception as err:  # pylint: disable=broad-except
        if in_diagnostic_mode():
            sys.stdout.write('ERROR: {}/n'.format(str(err)))
            sys.stdout.write('Raw [{}]/n'.format(data_to_save))

    for instrumentation_key in data_to_save:
        client = TelemetryClient(
            instrumentation_key=instrumentation_key,
            telemetry_channel=TelemetryChannel(
                queue=SynchronousQueue(LimitedRetrySender())))
        enable(instrumentation_key)

        for record in data_to_save[instrumentation_key]:
            name = record['name']
            raw_properties = record['properties']
            properties = {}
            measurements = {}
            for k, v in raw_properties.items():
                if isinstance(v, six.string_types):
                    properties[k] = v
                else:
                    measurements[k] = v
            client.track_event(record['name'], properties, measurements)

            if in_diagnostic_mode():
                sys.stdout.write(
                    '\nTrack Event: {}\nProperties: {}\nMeasurements: {}'.
                    format(name, json.dumps(properties, indent=2),
                           json.dumps(measurements, indent=2)))

        client.flush()

    if in_diagnostic_mode():
        sys.stdout.write('\nTelemetry upload completes\n')
コード例 #18
0
ファイル: plugin.py プロジェクト: brennerm/python-appinsights
    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()
コード例 #19
0
ファイル: enable.py プロジェクト: brennerm/python-appinsights
def __enable_for_urllib(base_http_handler_class,
                        base_https_handler_class,
                        instrumentation_key,
                        telemetry_channel=None,
                        always_flush=False):
    pass
    if not instrumentation_key:
        raise Exception('Instrumentation key was required but not provided')

    if telemetry_channel is None:
        sender = SynchronousSender()
        queue = SynchronousQueue(sender)
        telemetry_channel = TelemetryChannel(None, queue)

    client = TelemetryClient(instrumentation_key, telemetry_channel)

    class AppInsightsHTTPHandler(base_http_handler_class, object):
        def http_open(self, req):
            start_time = current_milli_time()
            response = super(AppInsightsHTTPHandler, self).http_open(req)

            try:
                _track_for_urllib(client, always_flush, start_time, req,
                                  response)
            finally:
                return response

    class AppInsightsHTTPSHandler(base_https_handler_class, object):
        def https_open(self, req):
            start_time = current_milli_time()
            response = super(AppInsightsHTTPSHandler, self).https_open(req)

            try:
                _track_for_urllib(client, always_flush, start_time, req,
                                  response)
            finally:
                return response

    return AppInsightsHTTPHandler, AppInsightsHTTPSHandler
コード例 #20
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._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)
コード例 #21
0
ファイル: log.py プロジェクト: tezzytezzy/opwen-cloudserver
def _create_telemetry_channel() -> TelemetryChannel:
    sender = AsynchronousSender(
        APPINSIGHTS_HOST) if APPINSIGHTS_HOST else NullSender()
    queue = AsynchronousQueue(sender)
    return TelemetryChannel(queue=queue)
コード例 #22
0
 def __init__(self, context=None, queue=None):
     TelemetryChannel.__init__(self, context, queue)