示例#1
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
    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
示例#3
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
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()
示例#5
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]
示例#6
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()
示例#7
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()
示例#8
0
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)
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')
示例#10
0
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