Ejemplo n.º 1
0
    def client(self):
        if self._client is None:
            # Create a client that submits telemetry parented to the request.
            self._client = applicationinsights.TelemetryClient(self.context.instrumentation_key, self._baseclient.channel)
            self._client.context.operation.parent_id = self.context.operation.id

        return self._client
Ejemplo n.º 2
0
def create_client(aisettings=None):
    global saved_clients, saved_channels

    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 = applicationinsights.channel.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 = applicationinsights.channel.AsynchronousQueue(sender)
        channel = applicationinsights.channel.TelemetryChannel(None, queue)
        saved_channels[channel_settings] = channel

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

    client = applicationinsights.TelemetryClient(aisettings.ikey, channel)
    saved_clients[aisettings] = client
    return client
Ejemplo n.º 3
0
def dummy_client(reason):
    """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 = applicationinsights.channel.NullSender()
    queue = applicationinsights.channel.SynchronousQueue(sender)
    channel = applicationinsights.channel.TelemetryChannel(None, queue)
    return applicationinsights.TelemetryClient("00000000-0000-0000-0000-000000000000", channel)
Ejemplo n.º 4
0
 def __init__(self):
     self.enabled = False
     if os.getenv('AML_APP_INSIGHTS_ENABLED'
                  ) == 'true' and 'AML_APP_INSIGHTS_KEY' in os.environ:
         instrumentation_key = os.getenv('AML_APP_INSIGHTS_KEY')
         exception_channel = self._make_telemetry_channel()
         self.telemetry_client = appinsights.TelemetryClient(
             instrumentation_key, exception_channel)
         self._request_channel = self._make_telemetry_channel()
         self._container_id = os.getenv('HOSTNAME', 'Unknown')
         self.enabled = True
Ejemplo n.º 5
0
    def __init__(self, instrumentation_key, *args, **kwargs):
        """
        Initialize a new instance of the class.

        Args:
            instrumentation_key (str). the instrumentation key to use while sending telemetry to the service.
        """
        if not instrumentation_key:
            raise Exception('Instrumentation key was required but not provided')
        telemetry_channel = kwargs.get('telemetry_channel')
        if 'telemetry_channel' in kwargs:
            del kwargs['telemetry_channel']
        self.client = applicationinsights.TelemetryClient(instrumentation_key, telemetry_channel)
        super(LoggingHandler, self).__init__(*args, **kwargs)
Ejemplo n.º 6
0
    def process_exception(self, request, exception):
        if not self._settings.log_exceptions:
            return None

        if type(exception) is Http404:
            return None

        _, _, tb = sys.exc_info()
        if tb is None or exception is None:
            # No actual traceback or exception info, don't bother logging.
            return None

        client = applicationinsights.TelemetryClient(self._client.context.instrumentation_key, self._client.channel)
        if hasattr(request, 'appinsights'):
            client.context.operation.parent_id = request.appinsights.request.id

        client.track_exception(type(exception), exception, tb)

        return None
Ejemplo n.º 7
0
    def __init__(self, instrumentation_key, wsgi_application, *args, **kwargs):
        """
        Initialize a new instance of the class.

        Args:
            instrumentation_key (str). the instrumentation key to use while sending telemetry to the service.\n
            wsgi_application (func). the WSGI application that we're wrapping.
        """
        if not instrumentation_key:
            raise Exception('Instrumentation key was required but not provided')
        if not wsgi_application:
            raise Exception('WSGI application was required but not provided')
        telemetry_channel = kwargs.pop('telemetry_channel', None)
        if not telemetry_channel:
            sender = applicationinsights.channel.AsynchronousSender()
            queue = applicationinsights.channel.AsynchronousQueue(sender)
            telemetry_channel = applicationinsights.channel.TelemetryChannel(None, queue)
        self.client = applicationinsights.TelemetryClient(instrumentation_key, telemetry_channel)
        self.client.context.device.type = "PC"
        self._wsgi_application = wsgi_application
        self._common_properties = kwargs.pop('common_properties', {})
Ejemplo n.º 8
0
LOGGER.setLevel(logging.INFO)
FORMATTER = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
#FORMATTER = logging.Formatter("%(asctime)s - %(levelname)s - " +
#                              "%(pathname)s:%(lineno)s - %(message)s")
STREAM_HANDLER = logging.StreamHandler(stream=sys.stdout)
STREAM_HANDLER.setFormatter(FORMATTER)
LOGGER.addHandler(STREAM_HANDLER)

TELEMETRY_CLIENT = None
INSTRUMENTATION_KEY = os.environ.get("INSTRUMENTATION_KEY")
if INSTRUMENTATION_KEY:
    TELEMETRY_CHANNEL = applicationinsights.channel.TelemetryChannel(
        None,
        applicationinsights.channel.AsynchronousQueue(
            applicationinsights.channel.AsynchronousSender()))
    TELEMETRY_CLIENT = applicationinsights.TelemetryClient(
        INSTRUMENTATION_KEY, TELEMETRY_CHANNEL)

STORAGE_CONNECTION_STRING = os.environ.get("STORAGE_CONNECTION_STRING")
if not STORAGE_CONNECTION_STRING:
    raise Exception(
        "Please set environment variable STORAGE_CONNECTION_STRING")
STORAGE = dict(
    token.split("=", 1) for token in STORAGE_CONNECTION_STRING.split(";"))

# Storage Account Credentials
STORAGE_ACCOUNT_NAME = STORAGE["AccountName"]
STORAGE_KEY = STORAGE["AccountKey"]
LEASE_CONTAINER_NAME = "python-leases"

EVENT_HUB_CONNECTION_STRING = os.environ.get("EVENT_HUB_CONNECTION_STRING")
if not EVENT_HUB_CONNECTION_STRING: