def upload(data_to_save): from applicationinsights import TelemetryClient from applicationinsights.exceptions import enable client = TelemetryClient(INSTRUMENTATION_KEY) enable(INSTRUMENTATION_KEY) if in_diagnostic_mode(): sys.stdout.write('Telemetry upload begins\n') 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('{}/n'.format(str(err))) sys.stdout.write('Raw [{}]/n'.format(data_to_save)) for record in data_to_save: client.track_event(record['name'], record['properties']) client.flush() if in_diagnostic_mode(): json.dump(data_to_save, sys.stdout, indent=2, sort_keys=True) sys.stdout.write('\nTelemetry upload completes\n')
def __init__(self, instrumentation_key, telemetry_channel=None): super(Telemetry, self).__init__(instrumentation_key, telemetry_channel) self.start_time = None self.end_time = None enable(instrumentation_key) # adding context self.context.application.id = 'Azure CLI Shell' self.context.application.ver = __version__ self.context.user.id = Profile().get_installation_id() self.context.instrumentation_key = INSTRUMENTATION_KEY
def __init__(self, cli_ctx): super(Telemetry, self).__init__(INSTRUMENTATION_KEY, None) self.start_time = None self.end_time = None enable(INSTRUMENTATION_KEY) # adding context self.context.application.id = 'Azure CLI Shell' self.context.application.ver = __version__ self.context.user.id = Profile(cli_ctx=cli_ctx).get_installation_id() self.context.instrumentation_key = INSTRUMENTATION_KEY
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 _get_telemetry_client() -> Optional[TelemetryClient]: if not APPINSIGHTS_KEY: return None telemetry_client = TelemetryClient(APPINSIGHTS_KEY) telemetry_client.channel.sender.send_interval_in_milliseconds = \ TELEMETRY_QUEUE_SECONDS * 1000 telemetry_client.channel.sender.max_queue_item_count = \ TELEMETRY_QUEUE_ITEMS exceptions.enable(APPINSIGHTS_KEY) return telemetry_client
def attach_appinsights(logger, instrumentation_key: str): if not instrumentation_key: logger.warning( "appinsights instrumentation key is null; not writing to app insights" ) return handler = LoggingHandler(instrumentation_key) # TODO: extend this collection to jobid, pipelineid or farmid etc. handler.client._context.properties['Collection'] = 'ADF_LOGS' # Removing all PIO information from context. # Due to a bug in LoggingHanlder constructor, its not honoring context passed. # so trying to set values once handler is created. Overriding with 'None' string # instead of None as for later value, its taking default value from constructor. handler.client._context.device = contracts.Device() handler.client._context.device.os_version = NONE_STRING handler.client._context.device.locale = NONE_STRING handler.client._context.device.id = NONE_STRING handler.client._context.device.type = NONE_STRING handler.client._context.device.oem_name = NONE_STRING handler.client._context.device.model = NONE_STRING handler.client._context.location = contracts.Location() handler.client._context.location.ip = NONE_STRING handler.client._context.user = contracts.User() handler.client._context.user.id = NONE_STRING handler.client._context.user.account_id = NONE_STRING handler.client._context.user.auth_user_id = NONE_STRING handler.client._context.session = contracts.Session() handler.client._context.session.id = NONE_STRING handler.client._context.cloud = contracts.Cloud() handler.client._context.cloud.role = NONE_STRING handler.setLevel(Logger.get_logging_level()) # Log formatter looks similar to default python logging statement. handler.setFormatter( logging.Formatter( '[%(asctime)s %(levelname)s %(filename)s:%(lineno)d %(process)d:%(thread)d]: %(message)s' )) # enable exceptions to app insights enable(instrumentation_key) logger.addHandler(handler) logger.info("Attached app insights handler to logger")
def init_telemetry(): try: instrumentation_key = '02b91c82-6729-4241-befc-e6d02ca4fbba' global client #pylint: disable=global-statement client = TelemetryClient(instrumentation_key) client.context.application.id = 'Azure CLI' client.context.application.ver = cli.__version__ client.context.user.id = hash(getpass.getuser()) enable(instrumentation_key) except Exception: #pylint: disable=broad-except # Never fail the command because of telemetry pass
def test_enable(self): original = sys.excepthook sys.excepthook = mock_excepthook sender = MockSynchronousSender() queue = channel.SynchronousQueue(sender) telemetry_channel = channel.TelemetryChannel(None, queue) exceptions.enable('foo', telemetry_channel=telemetry_channel) try: raise Exception('Boom') except: sys.excepthook(*sys.exc_info()) sys.excepthook = original data = sender.data[0][0] self.assertIsNotNone(data) self.assertEqual('foo', data.ikey) self.assertEqual('Microsoft.ApplicationInsights.Exception', data.name)
def init_telemetry(): try: instrumentation_key = '02b91c82-6729-4241-befc-e6d02ca4fbba' global client #pylint: disable=global-statement client = TelemetryClient(instrumentation_key) client.context.application.id = 'Azure CLI' client.context.application.ver = core_version client.context.user.id = _get_user_machine_id() client.context.device.type = 'az' enable(instrumentation_key) except Exception as ex: #pylint: disable=broad-except # Never fail the command because of telemetry, unless debugging if _debugging(): raise ex
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()
def init_telemetry(): from applicationinsights import TelemetryClient from applicationinsights.exceptions import enable try: instrumentation_key = '02b91c82-6729-4241-befc-e6d02ca4fbba' global client #pylint: disable=global-statement client = TelemetryClient(instrumentation_key) client.context.application.id = 'Azure CLI' client.context.application.ver = core_version client.context.user.id = _get_user_machine_id() client.context.device.type = 'az' enable(instrumentation_key) except Exception as ex: #pylint: disable=broad-except # Never fail the command because of telemetry, unless debugging if _debugging(): raise ex
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')
def upload(data_to_save): from applicationinsights import TelemetryClient from applicationinsights.exceptions import enable client = TelemetryClient(INSTRUMENTATION_KEY) enable(INSTRUMENTATION_KEY) 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('{}/n'.format(str(err))) sys.stdout.write('Raw [{}]/n'.format(data_to_save)) for record in data_to_save: name = record['name'] raw_properties = record['properties'] properties = {} measurements = {} for k in raw_properties: v = raw_properties[k] 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')
def _setup_telemetry_channel(self): """Create telemetry_channel object. Instantiates a telemetry channel that collects unhandled exceptions. Return: telemetry_channel """ from applicationinsights.exceptions import enable from applicationinsights import channel # set up channel with context telemetry_channel = channel.TelemetryChannel() telemetry_channel.context.application.ver = get_version() # set up exception capture telemetry_channel.context.properties['capture'] = 'exceptions' enable(APP_INSIGHTS_KEY, telemetry_channel=telemetry_channel) return telemetry_channel
def test_enable(self): original = sys.excepthook sys.excepthook = mock_excepthook sender = MockSynchronousSender() queue = channel.SynchronousQueue(sender) telemetry_channel = channel.TelemetryChannel(None, queue) telemetry_channel.context.properties["foo"] = "bar" telemetry_channel.context.operation.id = 1001 exceptions.enable('foo', telemetry_channel=telemetry_channel) try: raise Exception('Boom') except: sys.excepthook(*sys.exc_info()) sys.excepthook = original data = sender.data[0][0] self.assertIsNotNone(data) self.assertEqual('foo', data.ikey) self.assertEqual('Microsoft.ApplicationInsights.Exception', data.name) self.assertEqual('bar', data.data.base_data.properties['foo']) self.assertEqual(1001, data.tags.get('ai.operation.id'))
def upload(data_to_save): from applicationinsights import TelemetryClient from applicationinsights.exceptions import enable client = TelemetryClient(INSTRUMENTATION_KEY) enable(INSTRUMENTATION_KEY) 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('{}/n'.format(str(err))) sys.stdout.write('Raw [{}]/n'.format(data_to_save)) for record in data_to_save: name = record['name'] raw_properties = record['properties'] properties = {} measurements = {} for k in raw_properties: v = raw_properties[k] 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')
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')
""" Package for the application. """ from applicationinsights.exceptions import enable enable('104f9dca-6034-42a1-a646-7c66230710e7')
"limited": lambda: count < NUMBER_OF_ITERATIONS, "unlimited": lambda: True } # Sysout Logging Setup logger = logging.getLogger("monitofi") logger.setLevel(logging.INFO) syshandler = logging.StreamHandler(sys.stdout) syshandler.setLevel(logging.INFO) formatter = json_log_formatter.JSONFormatter() syshandler.setFormatter(formatter) logger.addHandler(syshandler) if IKEY != "REPLACE_ME": # Logging unhandled exceptions with Appinsights enable(IKEY) # Applications Insights Logging Setup handler = LoggingHandler(IKEY) handler.setFormatter(formatter) logger.addHandler(handler) iclient = InfluxDBClient(INFLUXDB_SERVER, INFLUXDB_PORT, INFLUXDB_USERNAME, INFLUXDB_PASSWORD, INFLUXDB_DATABASE) iclient.create_database(INFLUXDB_DATABASE) def flattening(nested, prefix, ignore_list): field = {} flatten(True, nested, field, prefix, ignore_list)
import os import time import random from eval import * from flask import Flask, jsonify, request, abort from applicationinsights import TelemetryClient from applicationinsights.requests import WSGIApplication from applicationinsights.exceptions import enable from src.miscc.config import cfg #from werkzeug.contrib.profiler import ProfilerMiddleware enable(os.environ["TELEMETRY"]) app = Flask(__name__) app.wsgi_app = WSGIApplication(os.environ["TELEMETRY"], app.wsgi_app) @app.route('/api/v1.0/bird', methods=['POST']) def create_bird(): if not request.json or not 'caption' in request.json: abort(400) caption = request.json['caption'] t0 = time.time() urls = generate(caption, wordtoix, ixtoword, text_encoder, netG, blob_service) t1 = time.time() response = { 'small': urls[0], 'medium': urls[1],
class TelThread(threading.Thread): """ telemetry thread for exiting """ def __init__(self, threadfunc): threading.Thread.__init__(self) self.threadfunc = threadfunc def run(self): self.threadfunc() def scrub(text): """ scrubs the parameter values from args """ args = parse_quotes(text) next_scrub = False values = [] for arg in args: if arg.startswith('-'): next_scrub = True values.append(arg) elif next_scrub: values.append('*****') else: values.append(arg) return ' '.join(values) TC = Telemetry(INSTRUMENTATION_KEY) enable(INSTRUMENTATION_KEY) my_context(TC)
_STDERR_FORMAT = '%(asctime)s\t%(levelname)s\t%(message)s' _STDERR = StreamHandler() _STDERR.setFormatter(Formatter(_STDERR_FORMAT)) _LOG = getLogger() _LOG.addHandler(_STDERR) _LOG.setLevel(LOG_LEVEL) _APPINSIGHTS = None # type: TelemetryClient if APPINSIGHTS_KEY: _APPINSIGHTS = TelemetryClient(APPINSIGHTS_KEY) _APPINSIGHTS.channel.sender.send_interval_in_milliseconds = 30 * 1000 _APPINSIGHTS.channel.sender.max_queue_item_count = 10 exceptions.enable(APPINSIGHTS_KEY) class LogMixin(object): info_separator = '|' def log_debug(self, message: str, *args: Any): self._log('debug', message, args) def log_info(self, message: str, *args: Any): self._log('info', message, args) def log_exception(self, message: str, *args: Any): self._log('exception', message, args) def _log(self, level: str, log_message: str, log_args: Iterable[Any]):