def InitializeAppInsights(self): # AppInsights initialization cur_thread = threading.current_thread() logFormatter = pylogging.Formatter( "%(asctime)s [%(threadName)-12.12s] [%(levelname)-7.7s]: %(message)s" ) appInsightsKey = os.environ['APPINSIGHTS_KEY'] if 'APPINSIGHTS_ENDPOINTURL' in os.environ and os.environ[ 'APPINSIGHTS_ENDPOINTURL']: appInsightsEndPointUrl = os.environ['APPINSIGHTS_ENDPOINTURL'] else: appInsightsEndPointUrl = 'https://dc.services.visualstudio.com/v2/track' self.rootLogger.info("AppInsights key: '" + appInsightsKey + "'") # log locally self.rootLogger.info("AppInsights endpoint: '" + appInsightsEndPointUrl + "'") # create a child logger per thread so that we can set the SessionId without collision during concurrent execution # by default logging will propagate to the parent rootLogger self.telemetryLogger = self.rootLogger.getChild( 'AppInsights.{0}'.format(cur_thread)) telemetryhandler = logging.enable(appInsightsKey, endpoint=appInsightsEndPointUrl) telemetryhandler.setFormatter(logFormatter) telemetryhandler.client.context.application.id = "DiskInspect-Service" telemetryhandler.client.context.application.ver = self.containerVersion telemetryhandler.client.context.properties[ 'releaseName'] = self.releaseName self.telemetryLogger.addHandler(telemetryhandler) self.telemetryClient = telemetryhandler.client
def test_enable(self): handler1 = logging.enable('foo') self.assertIsNotNone(handler1) self.assertEqual('LoggingHandler', handler1.__class__.__name__) self.assertEqual('foo', handler1.client.context.instrumentation_key) handler2 = logging.enable('foo') self.assertEqual('LoggingHandler', handler2.__class__.__name__) self.assertEqual('foo', handler2.client.context.instrumentation_key) channel = MockChannel() handler3 = logging.enable('bar', telemetry_channel=channel) self.assertIsNotNone(handler1) self.assertEqual('LoggingHandler', handler3.__class__.__name__) self.assertEqual('bar', handler3.client.context.instrumentation_key) self.assertEqual(channel, handler3.client.channel) all_handlers = pylogging.getLogger().handlers self.assertIn(handler2, all_handlers) self.assertIn(handler3, all_handlers) pylogging.getLogger().removeHandler(handler2) pylogging.getLogger().removeHandler(handler3)
def enable_telemetry(): logging.enable('key1')
def test_enable_with_level(self): handler = logging.enable('foo', level='DEBUG') self.assertIsNotNone(handler) self.assertEqual(handler.level, pylogging.DEBUG) pylogging.getLogger().removeHandler(handler)
def test_enable_raises_exception_on_endpoint_with_telemetry_channel(self): with self.assertRaises(Exception): logging.enable('foo', endpoint='http://bar', telemetry_channel=MockChannel())
def test_enable_raises_exception_on_async_with_telemetry_channel(self): with self.assertRaises(Exception): logging.enable('foo', async_=True, telemetry_channel=MockChannel())
def test_enable_with_async(self): handler = logging.enable('foo', async_=True) self.assertIsInstance(handler.client.channel.queue, AsynchronousQueue) self.assertIsInstance(handler.client.channel.sender, AsynchronousSender) pylogging.getLogger().removeHandler(handler)
def test_enable_with_endpoint(self): handler = logging.enable('foo', endpoint='http://bar') self.assertEqual(handler.client.channel.sender.service_endpoint_uri, 'http://bar') pylogging.getLogger().removeHandler(handler)
def start_logger() -> None: """ Start logging of messages passed through the python logging module. This sets up logging to a time based logging. This means that all logging messages on or above ``filelogginglevel`` will be written to `pythonlog.log` All logging messages on or above ``consolelogginglevel`` will be written to stderr. ``filelogginglevel`` and ``consolelogginglevel`` are defined in the ``qcodesrc.json`` file. """ global console_handler global file_handler # set loggers to the supplied levels for name, level in qc.config.logger.logger_levels.items(): logging.getLogger(name).setLevel(level) root_logger = logging.getLogger() root_logger.setLevel(logging.DEBUG) # remove previously set handlers for handler in (console_handler, file_handler): if handler is not None: handler.close() root_logger.removeHandler(handler) # add qcodes handlers # console console_handler = logging.StreamHandler() console_handler.setLevel(qc.config.logger.console_level) console_handler.setFormatter(get_formatter()) root_logger.addHandler(console_handler) # file filename = get_log_file_name() os.makedirs(os.path.dirname(filename), exist_ok=True) file_handler = logging.handlers.TimedRotatingFileHandler(filename, when='midnight') file_handler.setLevel(qc.config.logger.file_level) file_handler.setFormatter(get_formatter()) root_logger.addHandler(file_handler) # capture any warnings from the warnings module logging.captureWarnings(capture=True) if qc.config.telemetry.enabled: from applicationinsights import channel from applicationinsights.logging import enable # the telemetry_handler can be flushed global telemetry_handler loc = qc.config.GUID_components.location stat = qc.config.GUID_components.work_station sender = channel.AsynchronousSender() queue = channel.AsynchronousQueue(sender) appin_channel = channel.TelemetryChannel(context=None, queue=queue) appin_channel.context.user.id = f'{loc:02x}-{stat:06x}' # it is not completely clear which context fields get sent up. # Here we shuffle some info from one field to another. acc_name = appin_channel.context.device.id appin_channel.context.user.account_id = acc_name # note that the following function will simply silently fail if an # invalid instrumentation key is used. There is thus no exception to # catch telemetry_handler = enable(qc.config.telemetry.instrumentation_key, telemetry_channel=appin_channel) log.info("QCoDes logger setup completed") log_qcodes_versions(log) print(f'Qcodes Logfile : {filename}')
#!/usr/local/bin/python3 import logging from applicationinsights.logging import enable from flask import Flask from flask import request as freq from src.api.common.utils import get_azure_token from src.api.speach2code.speach2text import speach2text from src.api.speach2code.text2code import text2code from src.api.speach_rec.voice_recognition import voice_rec from src.keys import keys key = keys['insights'] enable(key) app = Flask(__name__) @app.route('/') def index(): return 'Speach to Text api! ;-)' @app.route('/speachToText/v1.0/toCode', methods=['POST']) def to_code(): logging.error('speach to code end point') data = freq.data logging.error('%s', str(data)) key = keys['speachToText'] token = get_azure_token(key)
def enable_logging(): key = get_instrumentation_key() enable(key)
if __name__ == '__main__': run_flask_in_debug_mode = True try: application_insights_instrumentation_key = os.environ[ ENV_VAR_APPLICATION_INSIGHTS_INSTRUMENTATION_KEY] except: logging.warning( f'No environment variable "{ENV_VAR_APPLICATION_INSIGHTS_INSTRUMENTATION_KEY}" set' ) if application_insights_instrumentation_key: telemetry_client = TelemetryClient( application_insights_instrumentation_key) telemetry_client.context.device.role_name = APPLICATION_NAME application_insights_handler = enable( application_insights_instrumentation_key) logging.basicConfig(handlers=[application_insights_handler], format='%(levelname)s: %(message)s') try: flask_port = int(os.environ[ENV_VAR_FLASK_PORT]) except: logging.warning( f'No environment variable "{ENV_VAR_FLASK_PORT}" set; using default port {flask_port}' ) try: run_flask_in_debug_mode = int(os.environ[ENV_VAR_FLASK_DEBUG_MODE]) except: logging.warning( f'No environment variable "{ENV_VAR_FLASK_DEBUG_MODE}" set; will not run Flask in debug mode'
# Databricks notebook source # Import Necessary Libraries import datetime from applicationinsights import TelemetryClient import logging from applicationinsights.logging import enable import time from applicationinsights.exceptions import enable from applicationinsights import channel from applicationinsights.logging import LoggingHandler instrumentation_key = '<Instrumentation key>' #Enable unhandled exception logging enable(instrumentation_key) #setup other needed variables tc = TelemetryClient(instrumentation_key) tc.context.application.ver = '0.0.1' tc.context.device.id = 'Sample notebook' telemetry_channel = channel.TelemetryChannel() telemetry_channel.context.application.ver = '1.0.0' telemetry_channel.context.properties['application_name'] = 'sample_notebook' telemetry_channel.context.properties['application_id'] = sc.applicationId handler = LoggingHandler(instrumentation_key, telemetry_channel=telemetry_channel) handler.setLevel(logging.DEBUG) handler.setFormatter(logging.Formatter('%(levelname)s: %(message)s')) logger = logging.getLogger('simple_logger')
from applicationinsights import TelemetryClient def process(data, context): data.properties["NEW_PROP"] = "MYPROP" # Add property context.user.id = "MYID" # Change ID return True # Not filtered tc = TelemetryClient('<YOUR INSTRUMENTATION KEY GOES HERE>') tc.add_telemetry_processor(process) #Basic logging configuration (first option) 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 logging.shutdown() Basic logging configuration (second option) import logging from applicationinsights.logging import LoggingHandler # set up logging handler = LoggingHandler('<YOUR INSTRUMENTATION KEY GOES HERE>') logging.basicConfig(handlers=[ handler ], format='%(levelname)s: %(message)s', level=logging.DEBUG)