def test_track_exception_works_as_expected(self):
     sender = MockTelemetrySender()
     queue = channel.SynchronousQueue(sender)
     client = TelemetryClient('99999999-9999-9999-9999-999999999999', channel.TelemetryChannel(context=None, queue=queue))
     client.context.device = None
     try:
         raise Exception("blah")
     except Exception as e:
         client.track_exception(*sys.exc_info(), properties={}, measurements={ 'x': 42 })
         client.flush()
     expected = '{"ver": 1, "name": "Microsoft.ApplicationInsights.Exception", "time": "TIME_PLACEHOLDER", "sampleRate": 100.0, "iKey": "99999999-9999-9999-9999-999999999999", "tags": {"ai.internal.sdkVersion": "SDK_VERSION_PLACEHOLDER"}, "data": {"baseType": "ExceptionData", "baseData": {"ver": 2, "handledAt": "UserCode", "exceptions": [{"id": 1, "outerId": 0, "typeName": "Exception", "message": "blah", "hasFullStack": true, "parsedStack": [{"level": 0, "method": "test_track_exception_works_as_expected", "assembly": "Unknown", "fileName": "TestTelemetryClient.py", "line": 0}]}], "measurements": {"x": 42}}}}'
     sender.data.time = 'TIME_PLACEHOLDER'
     sender.data.tags['ai.internal.sdkVersion'] = 'SDK_VERSION_PLACEHOLDER'
     for item in sender.data.data.base_data.exceptions:
         for frame in item.parsed_stack:
             frame.file_name = os.path.basename(frame.file_name)
             frame.line = 0
     actual = json.dumps(sender.data.write())
     self.assertEqual(expected, actual)
     try:
         raise Exception("blah")
     except Exception as e:
         client.track_exception()
         client.flush()
     expected = '{"ver": 1, "name": "Microsoft.ApplicationInsights.Exception", "time": "TIME_PLACEHOLDER", "sampleRate": 100.0, "iKey": "99999999-9999-9999-9999-999999999999", "tags": {"ai.internal.sdkVersion": "SDK_VERSION_PLACEHOLDER"}, "data": {"baseType": "ExceptionData", "baseData": {"ver": 2, "handledAt": "UserCode", "exceptions": [{"id": 1, "outerId": 0, "typeName": "Exception", "message": "blah", "hasFullStack": true, "parsedStack": [{"level": 0, "method": "test_track_exception_works_as_expected", "assembly": "Unknown", "fileName": "TestTelemetryClient.py", "line": 0}]}]}}}'
     sender.data.time = 'TIME_PLACEHOLDER'
     sender.data.tags['ai.internal.sdkVersion'] = 'SDK_VERSION_PLACEHOLDER'
     for item in sender.data.data.base_data.exceptions:
         for frame in item.parsed_stack:
             frame.file_name = os.path.basename(frame.file_name)
             frame.line = 0
     actual = json.dumps(sender.data.write())
     self.assertEqual(expected, actual)
예제 #2
0
def intercept_excepthook(type, value, traceback):
    client = TelemetryClient('temp_key', telemetry_channel)
    for instrumentation_key in enabled_instrumentation_keys:
        client.context.instrumentation_key = instrumentation_key
        client.track_exception(type, value, traceback)
    client.flush()
    original_excepthook(type, value, traceback)
def intercept_excepthook(type, value, traceback):
    client = TelemetryClient('temp_key', telemetry_channel)
    for instrumentation_key in enabled_instrumentation_keys:
        client.context.instrumentation_key = instrumentation_key
        client.track_exception(type, value, traceback)
    client.flush()
    original_excepthook(type, value, traceback)
 def test_track_exception_works_as_expected(self):
     sender = MockTelemetrySender()
     queue = channel.SynchronousQueue(sender)
     client = TelemetryClient('99999999-9999-9999-9999-999999999999', channel.TelemetryChannel(context=None, queue=queue))
     client.context.device = None
     try:
         raise Exception("blah")
     except Exception as e:
         client.track_exception(*sys.exc_info(), properties={}, measurements={ 'x': 42 })
         client.flush()
     expected = '{"ver": 1, "name": "Microsoft.ApplicationInsights.Exception", "time": "TIME_PLACEHOLDER", "sampleRate": 100.0, "iKey": "99999999-9999-9999-9999-999999999999", "tags": {"ai.internal.sdkVersion": "SDK_VERSION_PLACEHOLDER"}, "data": {"baseType": "ExceptionData", "baseData": {"ver": 2, "handledAt": "UserCode", "exceptions": [{"id": 1, "outerId": 0, "typeName": "Exception", "message": "blah", "hasFullStack": true, "parsedStack": [{"level": 0, "method": "test_track_exception_works_as_expected", "assembly": "Unknown", "fileName": "TestTelemetryClient.py", "line": 0}]}], "measurements": {"x": 42}}}}'
     sender.data.time = 'TIME_PLACEHOLDER'
     sender.data.tags['ai.internal.sdkVersion'] = 'SDK_VERSION_PLACEHOLDER'
     for item in sender.data.data.base_data.exceptions:
         for frame in item.parsed_stack:
             frame.file_name = os.path.basename(frame.file_name)
             frame.line = 0
     actual = json.dumps(sender.data.write())
     self.assertEqual(expected, actual)
     try:
         raise Exception("blah")
     except Exception as e:
         client.track_exception()
         client.flush()
     expected = '{"ver": 1, "name": "Microsoft.ApplicationInsights.Exception", "time": "TIME_PLACEHOLDER", "sampleRate": 100.0, "iKey": "99999999-9999-9999-9999-999999999999", "tags": {"ai.internal.sdkVersion": "SDK_VERSION_PLACEHOLDER"}, "data": {"baseType": "ExceptionData", "baseData": {"ver": 2, "handledAt": "UserCode", "exceptions": [{"id": 1, "outerId": 0, "typeName": "Exception", "message": "blah", "hasFullStack": true, "parsedStack": [{"level": 0, "method": "test_track_exception_works_as_expected", "assembly": "Unknown", "fileName": "TestTelemetryClient.py", "line": 0}]}]}}}'
     sender.data.time = 'TIME_PLACEHOLDER'
     sender.data.tags['ai.internal.sdkVersion'] = 'SDK_VERSION_PLACEHOLDER'
     for item in sender.data.data.base_data.exceptions:
         for frame in item.parsed_stack:
             frame.file_name = os.path.basename(frame.file_name)
             frame.line = 0
     actual = json.dumps(sender.data.write())
     self.assertEqual(expected, actual)
def send_exceptions(client: TelemetryClient, num_exceptions: int):
    for _ in range(num_exceptions):
        exception = generate_exception()
        # noinspection PyBroadException
        try:
            raise exception
        except Exception:
            client.track_exception()
            LOG.info('sent exception %s', exception)
    def test_track_exception_works_as_expected(self):
        def process(data, context):
            data.properties["NEW_PROP"] = "MYPROP"
            context.user.id = "BOTUSER"
            context.session.id = "BOTSESSION"
            return True

        sender = MockTelemetrySender()
        queue = channel.SynchronousQueue(sender)
        client = TelemetryClient(
            '99999999-9999-9999-9999-999999999999',
            channel.TelemetryChannel(context=None, queue=queue))
        client.add_telemetry_processor(process)
        client.context.device = None
        try:
            raise Exception("blah")
        except Exception as e:
            client.track_exception(*sys.exc_info(),
                                   properties={},
                                   measurements={'x': 42})
            client.flush()
        expected = '{"ver": 1, "name": "Microsoft.ApplicationInsights.Exception", "time": "TIME_PLACEHOLDER", "sampleRate": 100.0, "iKey": "99999999-9999-9999-9999-999999999999", "tags": {"ai.device.id": "DEVICE_ID_PLACEHOLDER", "ai.device.locale": "DEVICE_LOCALE_PLACEHOLDER", "ai.device.osVersion": "DEVICE_OS_VERSION_PLACEHOLDER", "ai.device.type": "DEVICE_TYPE_PLACEHOLDER", "ai.internal.sdkVersion": "SDK_VERSION_PLACEHOLDER", "ai.session.id": "BOTSESSION", "ai.user.id": "BOTUSER"}, "data": {"baseType": "ExceptionData", "baseData": {"ver": 2, "exceptions": [{"id": 1, "outerId": 0, "typeName": "Exception", "message": "blah", "hasFullStack": true, "parsedStack": [{"level": 0, "method": "test_track_exception_works_as_expected", "assembly": "Unknown", "fileName": "TestTelemetryProcessor.py", "line": 0}]}], "properties": {"NEW_PROP": "MYPROP"}, "measurements": {"x": 42}}}}'
        sender.data.time = 'TIME_PLACEHOLDER'
        sender.data.tags['ai.internal.sdkVersion'] = 'SDK_VERSION_PLACEHOLDER'
        sender.data.tags['ai.device.id'] = "DEVICE_ID_PLACEHOLDER"
        sender.data.tags['ai.device.locale'] = "DEVICE_LOCALE_PLACEHOLDER"
        sender.data.tags[
            'ai.device.osVersion'] = "DEVICE_OS_VERSION_PLACEHOLDER"
        sender.data.tags['ai.device.type'] = "DEVICE_TYPE_PLACEHOLDER"
        for item in sender.data.data.base_data.exceptions:
            for frame in item.parsed_stack:
                frame.file_name = os.path.basename(frame.file_name)
                frame.line = 0
        actual = json.dumps(sender.data.write())
        self.maxDiff = None
        self.assertEqual(expected, actual)
        try:
            raise Exception("blah")
        except Exception as e:
            client.track_exception()
            client.flush()
        expected = '{"ver": 1, "name": "Microsoft.ApplicationInsights.Exception", "time": "TIME_PLACEHOLDER", "sampleRate": 100.0, "iKey": "99999999-9999-9999-9999-999999999999", "tags": {"ai.device.id": "DEVICE_ID_PLACEHOLDER", "ai.device.locale": "DEVICE_LOCALE_PLACEHOLDER", "ai.device.osVersion": "DEVICE_OS_VERSION_PLACEHOLDER", "ai.device.type": "DEVICE_TYPE_PLACEHOLDER", "ai.internal.sdkVersion": "SDK_VERSION_PLACEHOLDER", "ai.session.id": "BOTSESSION", "ai.user.id": "BOTUSER"}, "data": {"baseType": "ExceptionData", "baseData": {"ver": 2, "exceptions": [{"id": 1, "outerId": 0, "typeName": "Exception", "message": "blah", "hasFullStack": true, "parsedStack": [{"level": 0, "method": "test_track_exception_works_as_expected", "assembly": "Unknown", "fileName": "TestTelemetryProcessor.py", "line": 0}]}], "properties": {"NEW_PROP": "MYPROP"}}}}'
        sender.data.time = 'TIME_PLACEHOLDER'
        sender.data.tags['ai.internal.sdkVersion'] = 'SDK_VERSION_PLACEHOLDER'
        sender.data.tags['ai.device.id'] = "DEVICE_ID_PLACEHOLDER"
        sender.data.tags['ai.device.locale'] = "DEVICE_LOCALE_PLACEHOLDER"
        sender.data.tags[
            'ai.device.osVersion'] = "DEVICE_OS_VERSION_PLACEHOLDER"
        sender.data.tags['ai.device.type'] = "DEVICE_TYPE_PLACEHOLDER"
        for item in sender.data.data.base_data.exceptions:
            for frame in item.parsed_stack:
                frame.file_name = os.path.basename(frame.file_name)
                frame.line = 0
        actual = json.dumps(sender.data.write())
        self.assertEqual(expected, actual)
예제 #7
0
def TrackEvent(File, Event, startTime,endTime):
    
    try:

        tc = TelemetryClient(Constants.AppInsight_Key)
        host_name = socket.gethostname() 
        host_ip = socket.gethostbyname(host_name) 
        tc.track_event(Event, { 'Server': host_name , 'IP': host_ip ,'Event':Event,'File':File}, { 'TimeTaken': TimeDiffMinutes(startTime,endTime)})
        tc.flush()
       
    except:
        tc.track_exception()
예제 #8
0
def TrackException(exp,File, Event, startTime,endTime):
    try:
        raise exp
    except:
        tc = TelemetryClient(Constants.AppInsight_Key)
        host_name = socket.gethostname() 
        host_ip = socket.gethostbyname(host_name) 
        tc.track_exception(*sys.exc_info(), { 'Server': host_name , 'IP': host_ip ,'Event':Event,'File':File}, { 'TimeTaken': TimeDiffMinutes(startTime,endTime) })
        tc.flush()

# Used for Debugging Purpose. Uncomment during debug and comment after fixing.
#if __name__ == '__main__': TestException()
    def test_track_exception_works_as_expected(self):
        def process(data, context):
            data.properties["NEW_PROP"] = "MYPROP"
            context.user.id = "BOTUSER"
            context.session.id = "BOTSESSION"
            return True

        sender = MockTelemetrySender()
        queue = channel.SynchronousQueue(sender)
        client = TelemetryClient('99999999-9999-9999-9999-999999999999', channel.TelemetryChannel(context=None, queue=queue))
        client.add_telemetry_processor(process)
        client.context.device = None
        try:
            raise Exception("blah")
        except Exception as e:
            client.track_exception(*sys.exc_info(), properties={}, measurements={ 'x': 42 })
            client.flush()
        expected = '{"ver": 1, "name": "Microsoft.ApplicationInsights.Exception", "time": "TIME_PLACEHOLDER", "sampleRate": 100.0, "iKey": "99999999-9999-9999-9999-999999999999", "tags": {"ai.device.id": "DEVICE_ID_PLACEHOLDER", "ai.device.locale": "DEVICE_LOCALE_PLACEHOLDER", "ai.device.osVersion": "DEVICE_OS_VERSION_PLACEHOLDER", "ai.device.type": "DEVICE_TYPE_PLACEHOLDER", "ai.internal.sdkVersion": "SDK_VERSION_PLACEHOLDER", "ai.session.id": "BOTSESSION", "ai.user.id": "BOTUSER"}, "data": {"baseType": "ExceptionData", "baseData": {"ver": 2, "exceptions": [{"id": 1, "outerId": 0, "typeName": "Exception", "message": "blah", "hasFullStack": true, "parsedStack": [{"level": 0, "method": "test_track_exception_works_as_expected", "assembly": "Unknown", "fileName": "TestTelemetryProcessor.py", "line": 0}]}], "properties": {"NEW_PROP": "MYPROP"}, "measurements": {"x": 42}}}}'
        sender.data.time = 'TIME_PLACEHOLDER'
        sender.data.tags['ai.internal.sdkVersion'] = 'SDK_VERSION_PLACEHOLDER'
        sender.data.tags['ai.device.id'] = "DEVICE_ID_PLACEHOLDER"
        sender.data.tags['ai.device.locale'] = "DEVICE_LOCALE_PLACEHOLDER"
        sender.data.tags['ai.device.osVersion'] = "DEVICE_OS_VERSION_PLACEHOLDER"
        sender.data.tags['ai.device.type'] = "DEVICE_TYPE_PLACEHOLDER"
        for item in sender.data.data.base_data.exceptions:
            for frame in item.parsed_stack:
                frame.file_name = os.path.basename(frame.file_name)
                frame.line = 0
        actual = json.dumps(sender.data.write())
        self.maxDiff = None
        self.assertEqual(expected, actual)
        try:
            raise Exception("blah")
        except Exception as e:
            client.track_exception()
            client.flush()
        expected = '{"ver": 1, "name": "Microsoft.ApplicationInsights.Exception", "time": "TIME_PLACEHOLDER", "sampleRate": 100.0, "iKey": "99999999-9999-9999-9999-999999999999", "tags": {"ai.device.id": "DEVICE_ID_PLACEHOLDER", "ai.device.locale": "DEVICE_LOCALE_PLACEHOLDER", "ai.device.osVersion": "DEVICE_OS_VERSION_PLACEHOLDER", "ai.device.type": "DEVICE_TYPE_PLACEHOLDER", "ai.internal.sdkVersion": "SDK_VERSION_PLACEHOLDER", "ai.session.id": "BOTSESSION", "ai.user.id": "BOTUSER"}, "data": {"baseType": "ExceptionData", "baseData": {"ver": 2, "exceptions": [{"id": 1, "outerId": 0, "typeName": "Exception", "message": "blah", "hasFullStack": true, "parsedStack": [{"level": 0, "method": "test_track_exception_works_as_expected", "assembly": "Unknown", "fileName": "TestTelemetryProcessor.py", "line": 0}]}], "properties": {"NEW_PROP": "MYPROP"}}}}'
        sender.data.time = 'TIME_PLACEHOLDER'
        sender.data.tags['ai.internal.sdkVersion'] = 'SDK_VERSION_PLACEHOLDER'
        sender.data.tags['ai.device.id'] = "DEVICE_ID_PLACEHOLDER"
        sender.data.tags['ai.device.locale'] = "DEVICE_LOCALE_PLACEHOLDER"
        sender.data.tags['ai.device.osVersion'] = "DEVICE_OS_VERSION_PLACEHOLDER"
        sender.data.tags['ai.device.type'] = "DEVICE_TYPE_PLACEHOLDER"
        for item in sender.data.data.base_data.exceptions:
            for frame in item.parsed_stack:
                frame.file_name = os.path.basename(frame.file_name)
                frame.line = 0
        actual = json.dumps(sender.data.write())
        self.assertEqual(expected, actual)
def get_instrumentation_key():
    """
        Gets an application Insight's instrumentation key from Azure Key Vault.
    """
    instrumentation_key = None

    try:
        instrumentation_key = get_key_vault_secret('APPINSIGHTSKEY')

    except Exception as exception:
        if instrumentation_key:
            telemetry_client = TelemetryClient(instrumentation_key)
            telemetry_client.track_exception()

    return instrumentation_key
예제 #11
0
class AppInsights(object):
    """This class represents a Flask extension that enables request telemetry,
    trace logging and exception logging for a Flask application. The telemetry
    will be sent to Application Insights service using the supplied
    instrumentation key.

    The following Flask config variables can be used to configure the extension:

    - Set ``APPINSIGHTS_INSTRUMENTATIONKEY`` to a string to provide the
      instrumentation key to send telemetry to application insights.
      Alternatively, this value can also be provided via an environment variable
      of the same name.

    - Set ``APPINSIGHTS_ENDPOINT_URI`` to a string to customize the telemetry
      endpoint to which Application Insights will send the telemetry.

    - Set ``APPINSIGHTS_DISABLE_REQUEST_LOGGING`` to ``False`` to disable
      logging of Flask requests to Application Insights.

    - Set ``APPINSIGHTS_DISABLE_TRACE_LOGGING`` to ``False`` to disable logging
      of all log traces to Application Insights.

    - Set ``APPINSIGHTS_DISABLE_EXCEPTION_LOGGING`` to ``False`` to disable
      logging of all exceptions to Application Insights.

    .. code:: python

            from flask import Flask
            from ai4e_app_insights import AppInsights

            # instantiate the Flask application
            app = Flask(__name__)
            app.config['APPINSIGHTS_INSTRUMENTATIONKEY'] = '<YOUR INSTRUMENTATION KEY GOES HERE>'

            # log requests, traces and exceptions to the Application Insights service
            appinsights = AppInsights(app)

            # define a simple route
            @app.route('/')
            def hello_world():
                return 'Hello World!'

            # run the application
            if __name__ == '__main__':
                app.run()
    """
    def __init__(self, app=None, context=None):
        """
        Initialize a new instance of the extension.

        Args:
            app (flask.Flask). the Flask application for which to initialize the extension.
        """
        socket.setdefaulttimeout(30)
        self._appinsights_key = None
        self._endpoint_uri = None
        self._channel = None
        self._requests_middleware = None
        self._trace_log_handler_grantee = None
        self._trace_log_handler_ai4e = None
        self._exception_telemetry_client_grantee = None
        self._exception_telemetry_client_ai4e = None

        if app:
            self.init_app(app, context)

    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)

    def _init_request_logging(self, app):
        """
        Sets up request logging unless ``APPINSIGHTS_DISABLE_REQUEST_LOGGING``
        is set in the Flask config.

        Args:
            app (flask.Flask). the Flask application for which to initialize the extension.
        """
        enabled = not app.config.get(CONF_DISABLE_REQUEST_LOGGING, False)

        if not enabled:
            return

        wsgi_key = self._appinsights_key

        self._requests_middleware = WSGIApplication(
            wsgi_key, app.wsgi_app, telemetry_channel=self._channel)

        app.wsgi_app = self._requests_middleware

    def _init_trace_logging(self, app):
        """
        Sets up trace logging unless ``APPINSIGHTS_DISABLE_TRACE_LOGGING`` is
        set in the Flask config.

        Args:
            app (flask.Flask). the Flask application for which to initialize the extension.
        """
        enabled = not app.config.get(CONF_DISABLE_TRACE_LOGGING, False)

        if not enabled:
            return

        if self._appinsights_key:
            self._trace_log_handler_grantee = LoggingHandler(
                self._appinsights_key, telemetry_channel=self._channel)

            app.logger.addHandler(self._trace_log_handler_grantee)

    def _init_exception_logging(self, app):
        """
        Sets up exception logging unless ``APPINSIGHTS_DISABLE_EXCEPTION_LOGGING``
        is set in the Flask config.

        Args:
            app (flask.Flask). the Flask application for which to initialize the extension.
        """
        enabled = not app.config.get(CONF_DISABLE_EXCEPTION_LOGGING, False)

        if not enabled:
            return

        if self._appinsights_key:
            self._exception_telemetry_client_grantee = TelemetryClient(
                self._appinsights_key, telemetry_channel=self._channel)

        @app.errorhandler(Exception)
        def exception_handler(exception):
            try:
                raise exception
            except Exception:
                if self._exception_telemetry_client_grantee:
                    self._exception_telemetry_client_grantee.track_exception()

                if self._exception_telemetry_client_ai4e:
                    self._exception_telemetry_client_ai4e.track_exception()
            finally:
                raise exception

    def flush(self):
        """Flushes the queued up telemetry to the service."""
        print("trying all flush")
        if self._requests_middleware:
            self._requests_middleware.flush()

        if self._trace_log_handler_grantee:
            self._trace_log_handler_grantee.flush()

        if self._trace_log_handler_ai4e:
            print("Trying trace flush...")
            self._trace_log_handler_ai4e.flush()
            print("Trace flush finsihed.")

        if self._exception_telemetry_client_grantee:
            self._exception_telemetry_client_grantee.flush()

        if self._exception_telemetry_client_ai4e:
            self._exception_telemetry_client_ai4e.flush()
예제 #12
0
tc.flush()

#Sending a metric telemetry item
from applicationinsights import TelemetryClient
tc = TelemetryClient('<YOUR INSTRUMENTATION KEY GOES HERE>')
tc.track_metric('My Metric', 42)
tc.flush()

#Sending an exception telemetry item with custom properties and measurements
import sys
from applicationinsights import TelemetryClient
tc = TelemetryClient('<YOUR INSTRUMENTATION KEY GOES HERE>')
try:
    raise Exception('blah')
except:
    tc.track_exception()

try:
    raise Exception("blah")
except:
    tc.track_exception(*sys.exc_info(), properties={ 'foo': 'bar' }, measurements={ 'x': 42 })
tc.flush()
Configuring context for a telemetry client instance

from applicationinsights import TelemetryClient
tc = TelemetryClient('<YOUR INSTRUMENTATION KEY GOES HERE>')
tc.context.application.ver = '1.2.3'
tc.context.device.id = 'My current device'
tc.context.device.oem_name = 'Asus'
tc.context.device.model = 'X31A'
tc.context.device.type = "Other"
class AppinsightsBotTelemetryClient(BotTelemetryClient):
    def __init__(self, instrumentation_key: str):
        self._instrumentation_key = instrumentation_key

        self._context = TelemetryContext()
        context.instrumentation_key = self._instrumentation_key
        # context.user.id = 'BOTID'        # telemetry_channel.context.session.
        # context.session.id = 'BOTSESSION'

        # set up channel with context
        self._channel = TelemetryChannel(context)
        # telemetry_channel.context.properties['my_property'] = 'my_value'

        self._client = TelemetryClient(self._instrumentation_key,
                                       self._channel)

    def track_pageview(self,
                       name: str,
                       url: str,
                       duration: int = 0,
                       properties: Dict[str, object] = None,
                       measurements: Dict[str, object] = None) -> None:
        """
        Send information about the page viewed in the application (a web page for instance).
        :param name: the name of the page that was viewed.
        :param url: the URL of the page that was viewed.
        :param duration: the duration of the page view in milliseconds. (defaults to: 0)
        :param properties: the set of custom properties the client wants attached to this data item. (defaults to: None)
        :param measurements: the set of custom measurements the client wants to attach to this data item. (defaults to: None)
        """
        self._client.track_pageview(name, url, duration, properties,
                                    measurements)

    def track_exception(self,
                        type_exception: type = None,
                        value: Exception = None,
                        tb: traceback = None,
                        properties: Dict[str, object] = None,
                        measurements: Dict[str, object] = None) -> None:
        """ 
        Send information about a single exception that occurred in the application.
        :param type_exception: the type of the exception that was thrown.
        :param value: the exception that the client wants to send.
        :param tb: the traceback information as returned by :func:`sys.exc_info`.
        :param properties: the set of custom properties the client wants attached to this data item. (defaults to: None)
        :param measurements: the set of custom measurements the client wants to attach to this data item. (defaults to: None)
        """
        self._client.track_exception(type_exception, value, tb, properties,
                                     measurements)

    def track_event(self,
                    name: str,
                    properties: Dict[str, object] = None,
                    measurements: Dict[str, object] = None) -> None:
        """ 
        Send information about a single event that has occurred in the context of the application.
        :param name: the data to associate to this event.
        :param properties: the set of custom properties the client wants attached to this data item. (defaults to: None)
        :param measurements: the set of custom measurements the client wants to attach to this data item. (defaults to: None)
        """
        self._client.track_event(name, properties, measurements)

    def track_metric(self,
                     name: str,
                     value: float,
                     type: TelemetryDataPointType = None,
                     count: int = None,
                     min: float = None,
                     max: float = None,
                     std_dev: float = None,
                     properties: Dict[str, object] = None) -> NotImplemented:
        """
        Send information about a single metric data point that was captured for the application.
        :param name: The name of the metric that was captured.
        :param value: The value of the metric that was captured.
        :param type: The type of the metric. (defaults to: TelemetryDataPointType.aggregation`)
        :param count: the number of metrics that were aggregated into this data point. (defaults to: None)
        :param min: the minimum of all metrics collected that were aggregated into this data point. (defaults to: None)
        :param max: the maximum of all metrics collected that were aggregated into this data point. (defaults to: None)
        :param std_dev: the standard deviation of all metrics collected that were aggregated into this data point. (defaults to: None)
        :param properties: the set of custom properties the client wants attached to this data item. (defaults to: None)
        """
        self._client.track_metric(name, value, type, count, min, max, std_dev,
                                  properties)

    def track_trace(self,
                    name: str,
                    properties: Dict[str, object] = None,
                    severity=None):
        """
        Sends a single trace statement.
        :param name: the trace statement.\n
        :param properties: the set of custom properties the client wants attached to this data item. (defaults to: None)\n
        :param severity: the severity level of this trace, one of DEBUG, INFO, WARNING, ERROR, CRITICAL
        """
        self._client.track_trace(name, properties, severity)

    def track_request(self,
                      name: str,
                      url: str,
                      success: bool,
                      start_time: str = None,
                      duration: int = None,
                      response_code: str = None,
                      http_method: str = None,
                      properties: Dict[str, object] = None,
                      measurements: Dict[str, object] = None,
                      request_id: str = None):
        """
        Sends a single request that was captured for the application.
        :param name: The name for this request. All requests with the same name will be grouped together.
        :param url: The actual URL for this request (to show in individual request instances).
        :param success: True if the request ended in success, False otherwise.
        :param start_time: the start time of the request. The value should look the same as the one returned by :func:`datetime.isoformat()` (defaults to: None)
        :param duration: the number of milliseconds that this request lasted. (defaults to: None)
        :param response_code: the response code that this request returned. (defaults to: None)
        :param http_method: the HTTP method that triggered this request. (defaults to: None)
        :param properties: the set of custom properties the client wants attached to this data item. (defaults to: None)
        :param measurements: the set of custom measurements the client wants to attach to this data item. (defaults to: None)
        :param request_id: the id for this request. If None, a new uuid will be generated. (defaults to: None)
        """
        self._client.track_request(name, url, success, start_time, duration,
                                   response_code, http_method, properties,
                                   measurements, request_id)

    def track_dependency(self,
                         name: str,
                         data: str,
                         type: str = None,
                         target: str = None,
                         duration: int = None,
                         success: bool = None,
                         result_code: str = None,
                         properties: Dict[str, object] = None,
                         measurements: Dict[str, object] = None,
                         dependency_id: str = None):
        """
        Sends a single dependency telemetry that was captured for the application.
        :param name: the name of the command initiated with this dependency call. Low cardinality value. Examples are stored procedure name and URL path template.
        :param data: the command initiated by this dependency call. Examples are SQL statement and HTTP URL with all query parameters.
        :param type: the dependency type name. Low cardinality value for logical grouping of dependencies and interpretation of other fields like commandName and resultCode. Examples are SQL, Azure table, and HTTP. (default to: None)
        :param target: the target site of a dependency call. Examples are server name, host address. (default to: None)
        :param duration: the number of milliseconds that this dependency call lasted. (defaults to: None)
        :param success: true if the dependency call ended in success, false otherwise. (defaults to: None)
        :param result_code: the result code of a dependency call. Examples are SQL error code and HTTP status code. (defaults to: None)
        :param properties: the set of custom properties the client wants attached to this data item. (defaults to: None)
        :param measurements: the set of custom measurements the client wants to attach to this data item. (defaults to: None)
        :param id: the id for this dependency call. If None, a new uuid will be generated. (defaults to: None)
        """
        self._client.track_dependency(name, data, type, target, duration,
                                      success, result_code, properties,
                                      measurements, dependency_id)
예제 #14
0
class RaftUtils():
    def __init__(self):
        work_directory = os.environ['RAFT_WORK_DIRECTORY']
        with open(os.path.join(work_directory, 'task-config.json'),
                  'r') as task_config:
            self.config = json.load(task_config)

        connection_str = os.environ['RAFT_SB_OUT_SAS']
        self.topic_client = TopicClient.from_connection_string(connection_str)

        self.telemetry_client = TelemetryClient(
            instrumentation_key=os.environ['RAFT_APP_INSIGHTS_KEY'])

        self.job_id = os.environ['RAFT_JOB_ID']
        self.container_name = os.environ['RAFT_CONTAINER_NAME']

        self.telemetry_properties = {
            "jobId": self.job_id,
            "taskIndex": os.environ['RAFT_TASK_INDEX'],
            "containerName": self.container_name
        }

    def report_status(self, state, details):
        m = {
            'eventType': 'JobStatus',
            'message': {
                'tool': 'ZAP',
                'jobId': self.job_id,
                'agentName': self.container_name,
                'details': details,
                'utcEventTime': time.strftime('%Y-%m-%d %H:%M:%S',
                                              time.gmtime()),
                'state': state
            }
        }
        msg = Message(str.encode(json.dumps(m)))
        self.topic_client.send(msg)

    def report_status_created(self, details=None):
        self.report_status('Created', details)

    def report_status_running(self, details=None):
        self.report_status('Running', details)

    def report_status_error(self, details=None):
        self.report_status('Error', details)

    def report_status_completed(self, details=None):
        self.report_status('Completed', details)

    def log_trace(self, trace):
        self.telemetry_client.track_trace(trace,
                                          properties=self.telemetry_properties)

    def log_exception(self):
        self.telemetry_client.track_exception(
            properties=self.telemetry_properties)

    def flush(self):
        self.telemetry_client.flush()

    def get_swagger_target(self):
        swagger = self.config.get("swaggerLocation")
        if swagger and swagger.get("url"):
            return swagger["url"]
        elif swagger.get("filePath"):
            return swagger["filePath"]
예제 #15
0
    print('Loading Keys')
    client = SecretClient(vault_url=KVUri, credential=credential)
    print(client.get_secret("test").value)
    AUTH_TOKEN = client.get_secret("TokenEH").value
    DSN_HOST = client.get_secret("DNSHostEH").value
    DB_NAME = client.get_secret("DbNameEH").value
    DB_USER = client.get_secret("DbUserEH").value
    DB_PASSWORD = client.get_secret("DbPasswordEH").value
    DB_PORT = client.get_secret("DNSPortEH").value
    # print(AUTH_TOKEN)
except:
    print('Error Occured in Fetching Keys')
    print(*sys.exc_info())
    tc.track_exception(*sys.exc_info(),
                       properties={
                           "type": "Error Occured in Fetching Keys",
                           "function": "getKeys"
                       })
    tc.flush()

test_db = dict(host=DSN_HOST,
               database=DB_NAME,
               user=DB_USER,
               password=DB_PASSWORD,
               port=DB_PORT)

static_db = PgsqlExecutor(test_db)
db = PGExecutor(username=DB_USER,
                password=DB_PASSWORD,
                host=DSN_HOST,
                port=DB_PORT,
예제 #16
0
class RaftUtils():
    def __init__(self, tool_name):
        from applicationinsights import TelemetryClient
        from azure.servicebus import ServiceBusClient, ServiceBusMessage
        self.config = task_config()

        connection_str = os.environ['RAFT_SB_OUT_SAS']

        self.sb_client = ServiceBusClient.from_connection_string(
            connection_str)
        self.topic_client = self.sb_client.get_topic_sender(
            self.sb_client._entity_name)

        self.telemetry_client = TelemetryClient(
            instrumentation_key=os.environ['RAFT_APP_INSIGHTS_KEY'])

        self.job_id = os.environ['RAFT_JOB_ID']
        self.container_name = os.environ['RAFT_CONTAINER_NAME']
        self.tool_name = tool_name

        self.telemetry_properties = {
            "jobId": self.job_id,
            "taskIndex": os.environ['RAFT_TASK_INDEX'],
            "containerName": self.container_name
        }

        self.newSbMessage = ServiceBusMessage

    def report_bug(self, bugDetails):
        m = {
            'eventType': 'BugFound',
            'message': {
                'tool': self.tool_name,
                'jobId': self.job_id,
                'agentName': self.container_name,
                'bugDetails': bugDetails
            }
        }
        msg = self.newSbMessage(str.encode(json.dumps(m)))
        self.topic_client.send_messages([msg])

    def report_status(self, state, details):
        m = {
            'eventType': 'JobStatus',
            'message': {
                'tool': self.tool_name,
                'jobId': self.job_id,
                'agentName': self.container_name,
                'details': details,
                'utcEventTime': time.strftime('%Y-%m-%d %H:%M:%S',
                                              time.gmtime()),
                'state': state
            }
        }
        msg = self.newSbMessage(str.encode(json.dumps(m)))
        self.topic_client.send_messages([msg])

    def report_status_created(self, details=None):
        self.report_status('Created', details)

    def report_status_running(self, details=None):
        self.report_status('Running', details)

    def report_status_error(self, details=None):
        self.report_status('Error', details)

    def report_status_completed(self, details=None):
        self.report_status('Completed', details)

    def log_trace(self, trace):
        self.telemetry_client.track_trace(trace,
                                          properties=self.telemetry_properties)

    def log_exception(self):
        self.telemetry_client.track_exception(
            properties=self.telemetry_properties)

    def flush(self):
        self.telemetry_client.flush()
        self.sb_client.close()
예제 #17
0
class ApplicationInsightsTelemetryClient(BotTelemetryClient):
    def __init__(self, instrumentation_key: str):
        self._instrumentation_key = instrumentation_key
        self._client = TelemetryClient(self._instrumentation_key)

        # Telemetry Processor
        def telemetry_processor(data, context):
            post_data = IntegrationPostData().activity_json
            # Override session and user id
            from_prop = post_data['from'] if 'from' in post_data else None
            user_id = from_prop['id'] if from_prop != None else None
            channel_id = post_data[
                'channelId'] if 'channelId' in post_data else None
            conversation = post_data[
                'conversation'] if 'conversation' in post_data else None
            conversation_id = conversation[
                'id'] if 'id' in conversation else None
            context.user.id = channel_id + user_id
            context.session.id = conversation_id

            # Additional bot-specific properties
            if 'activityId' in post_data:
                data.properties["activityId"] = post_data['activityId']
            if 'channelId' in post_data:
                data.properties["channelId"] = post_data['channelId']
            if 'activityType' in post_data:
                data.properties["activityType"] = post_data['activityType']

        self._client.add_telemetry_processor(telemetry_processor)

    def track_pageview(self,
                       name: str,
                       url: str,
                       duration: int = 0,
                       properties: Dict[str, object] = None,
                       measurements: Dict[str, object] = None) -> None:
        """
        Send information about the page viewed in the application (a web page for instance).
        :param name: the name of the page that was viewed.
        :param url: the URL of the page that was viewed.
        :param duration: the duration of the page view in milliseconds. (defaults to: 0)
        :param properties: the set of custom properties the client wants attached to this data item. (defaults to: None)
        :param measurements: the set of custom measurements the client wants to attach to this data item. (defaults to: None)
        """
        self._client.track_pageview(name, url, duration, properties,
                                    measurements)

    def track_exception(self,
                        type_exception: type = None,
                        value: Exception = None,
                        tb: traceback = None,
                        properties: Dict[str, object] = None,
                        measurements: Dict[str, object] = None) -> None:
        """ 
        Send information about a single exception that occurred in the application.
        :param type_exception: the type of the exception that was thrown.
        :param value: the exception that the client wants to send.
        :param tb: the traceback information as returned by :func:`sys.exc_info`.
        :param properties: the set of custom properties the client wants attached to this data item. (defaults to: None)
        :param measurements: the set of custom measurements the client wants to attach to this data item. (defaults to: None)
        """
        self._client.track_exception(type_exception, value, tb, properties,
                                     measurements)

    def track_event(self,
                    name: str,
                    properties: Dict[str, object] = None,
                    measurements: Dict[str, object] = None) -> None:
        """ 
        Send information about a single event that has occurred in the context of the application.
        :param name: the data to associate to this event.
        :param properties: the set of custom properties the client wants attached to this data item. (defaults to: None)
        :param measurements: the set of custom measurements the client wants to attach to this data item. (defaults to: None)
        """
        self._client.track_event(name, properties, measurements)

    def track_metric(self,
                     name: str,
                     value: float,
                     type: TelemetryDataPointType = None,
                     count: int = None,
                     min: float = None,
                     max: float = None,
                     std_dev: float = None,
                     properties: Dict[str, object] = None) -> NotImplemented:
        """
        Send information about a single metric data point that was captured for the application.
        :param name: The name of the metric that was captured.
        :param value: The value of the metric that was captured.
        :param type: The type of the metric. (defaults to: TelemetryDataPointType.aggregation`)
        :param count: the number of metrics that were aggregated into this data point. (defaults to: None)
        :param min: the minimum of all metrics collected that were aggregated into this data point. (defaults to: None)
        :param max: the maximum of all metrics collected that were aggregated into this data point. (defaults to: None)
        :param std_dev: the standard deviation of all metrics collected that were aggregated into this data point. (defaults to: None)
        :param properties: the set of custom properties the client wants attached to this data item. (defaults to: None)
        """
        self._client.track_metric(name, value, type, count, min, max, std_dev,
                                  properties)

    def track_trace(self,
                    name: str,
                    properties: Dict[str, object] = None,
                    severity=None):
        """
        Sends a single trace statement.
        :param name: the trace statement.\n
        :param properties: the set of custom properties the client wants attached to this data item. (defaults to: None)\n
        :param severity: the severity level of this trace, one of DEBUG, INFO, WARNING, ERROR, CRITICAL
        """
        self._client.track_trace(name, properties, severity)

    def track_request(self,
                      name: str,
                      url: str,
                      success: bool,
                      start_time: str = None,
                      duration: int = None,
                      response_code: str = None,
                      http_method: str = None,
                      properties: Dict[str, object] = None,
                      measurements: Dict[str, object] = None,
                      request_id: str = None):
        """
        Sends a single request that was captured for the application.
        :param name: The name for this request. All requests with the same name will be grouped together.
        :param url: The actual URL for this request (to show in individual request instances).
        :param success: True if the request ended in success, False otherwise.
        :param start_time: the start time of the request. The value should look the same as the one returned by :func:`datetime.isoformat()` (defaults to: None)
        :param duration: the number of milliseconds that this request lasted. (defaults to: None)
        :param response_code: the response code that this request returned. (defaults to: None)
        :param http_method: the HTTP method that triggered this request. (defaults to: None)
        :param properties: the set of custom properties the client wants attached to this data item. (defaults to: None)
        :param measurements: the set of custom measurements the client wants to attach to this data item. (defaults to: None)
        :param request_id: the id for this request. If None, a new uuid will be generated. (defaults to: None)
        """
        self._client.track_request(name, url, success, start_time, duration,
                                   response_code, http_method, properties,
                                   measurements, request_id)

    def track_dependency(self,
                         name: str,
                         data: str,
                         type: str = None,
                         target: str = None,
                         duration: int = None,
                         success: bool = None,
                         result_code: str = None,
                         properties: Dict[str, object] = None,
                         measurements: Dict[str, object] = None,
                         dependency_id: str = None):
        """
        Sends a single dependency telemetry that was captured for the application.
        :param name: the name of the command initiated with this dependency call. Low cardinality value. Examples are stored procedure name and URL path template.
        :param data: the command initiated by this dependency call. Examples are SQL statement and HTTP URL with all query parameters.
        :param type: the dependency type name. Low cardinality value for logical grouping of dependencies and interpretation of other fields like commandName and resultCode. Examples are SQL, Azure table, and HTTP. (default to: None)
        :param target: the target site of a dependency call. Examples are server name, host address. (default to: None)
        :param duration: the number of milliseconds that this dependency call lasted. (defaults to: None)
        :param success: true if the dependency call ended in success, false otherwise. (defaults to: None)
        :param result_code: the result code of a dependency call. Examples are SQL error code and HTTP status code. (defaults to: None)
        :param properties: the set of custom properties the client wants attached to this data item. (defaults to: None)
        :param measurements: the set of custom measurements the client wants to attach to this data item. (defaults to: None)
        :param id: the id for this dependency call. If None, a new uuid will be generated. (defaults to: None)
        """
        self._client.track_dependency(name, data, type, target, duration,
                                      success, result_code, properties,
                                      measurements, dependency_id)

    def flush(self):
        """Flushes data in the queue. Data in the queue will be sent either immediately irrespective of what sender is
        being used.
        """
        self._client.flush()