def __init__(self, flask_app, logger): self.app = flask_app self.log = logger self.api = Api(self.app) self.is_terminating = False self.func_properties = {} self.func_request_counts = {} self.api_prefix = getenv('API_PREFIX') self.tracer = None self.api_task_manager = TaskManager() signal.signal(signal.SIGINT, self.initialize_term) # Add health check endpoint self.app.add_url_rule(self.api_prefix + '/', view_func=self.health_check, methods=['GET']) print("Adding url rule: " + self.api_prefix + '/') # Add task endpoint self.api.add_resource( Task, self.api_prefix + '/task/<id>', resource_class_kwargs={'task_manager': self.api_task_manager}) print("Adding url rule: " + self.api_prefix + '/task/<int:taskId>') if getenv('APPINSIGHTS_INSTRUMENTATIONKEY', None): azure_exporter = AzureExporter( connection_string='InstrumentationKey=' + str(getenv('APPINSIGHTS_INSTRUMENTATIONKEY')), timeout=getenv('APPINSIGHTS_TIMEOUT', 30.0)) sampling_rate = getenv('TRACE_SAMPLING_RATE', None) if not sampling_rate: sampling_rate = 1.0 self.middleware = FlaskMiddleware( self.app, exporter=azure_exporter, sampler=ProbabilitySampler(rate=float(sampling_rate)), ) self.tracer = Tracer( exporter=AzureExporter( connection_string='InstrumentationKey=' + str(getenv('APPINSIGHTS_INSTRUMENTATIONKEY')), timeout=getenv('APPINSIGHTS_TIMEOUT', 30.0)), sampler=ProbabilitySampler(rate=float(sampling_rate)), ) self.app.before_request(self.before_request)
def __init__(self, run): print('Initializing the AppInsightsLogger') self.env = Env() self.run_id = self.get_run_id_and_set_context(run) # Prepare integrations and initialize tracer config_integration.trace_integrations(['httplib', 'logging']) texporter = AzureExporter( connection_string=self.env.app_insights_connection_string) texporter.add_telemetry_processor(self.callback_function) self.tracer = Tracer(exporter=texporter, sampler=ProbabilitySampler( self.env.trace_sampling_rate)) # Create AppInsights Handler and set log format self.logger = logging.getLogger(__name__) self.logger.setLevel( getattr(logging, self.env.log_level.upper(), "WARNING")) handler = AzureLogHandler( connection_string=self.env.app_insights_connection_string, logging_sampling_rate=self.env.log_sampling_rate, ) handler.add_telemetry_processor(self.callback_function) self.logger.addHandler(handler) # initializes metric exporter mexporter = metrics_exporter.new_metrics_exporter( enable_standard_metrics=self.env.enable_standard_metrics, export_interval=self.env.metrics_export_interval, connection_string=self.env.app_insights_connection_string, ) mexporter.add_telemetry_processor(self.callback_function) stats_module.stats.view_manager.register_exporter(mexporter)
def get_application() -> FastAPI: application = FastAPI(title=config.PROJECT_NAME, debug=config.DEBUG, description=config.API_DESCRIPTION, version=config.VERSION, docs_url=None, redoc_url=None, openapi_url=None) application.add_event_handler("startup", create_start_app_handler(application)) application.add_event_handler("shutdown", create_stop_app_handler(application)) try: application.add_middleware( RequestTracerMiddleware, exporter=AzureExporter( connection_string= f'InstrumentationKey={os.getenv("APPINSIGHTS_INSTRUMENTATIONKEY")}', sampler=ProbabilitySampler(1.0))) except Exception as e: logging.error(f"Failed to add RequestTracerMiddleware: {e}") application.add_middleware(ServerErrorMiddleware, handler=generic_error_handler) application.add_exception_handler(HTTPException, http_error_handler) application.add_exception_handler(RequestValidationError, http422_error_handler) application.include_router(api_router) return application
async def middlewareOpencensus( request: Request, call_next ): # The middleware function recieves: The request, A function call_next that will recieve the request as a parameter tracer = Tracer(exporter=AzureExporter( connection_string=f'InstrumentationKey={APPINSIGHTS_INSTRUMENTATIONKEY}' ), sampler=ProbabilitySampler(1.0)) # https://opencensus.io/tracing/span/kind/#2 # SpanKind details the relationships between spans in addition to the parent/child relationship. # Spand kind: 0 = UNSEPCIFIED, 1 = SERVER, 2 = CLIENT # Detail explaination of Span : https://opencensus.io/tracing/span/ with tracer.span("main") as span: span.span_kind = SpanKind.SERVER response = await call_next( request ) # call next will pass the request to the corresponding path operation, then returns the response genrated by the corresponding path operation tracer.add_attribute_to_current_span( # current span is a SERVER attribute_key=HTTP_STATUS_CODE, # E.g. 202, 201, 404, 405, 505 attribute_value=response.status_code) tracer.add_attribute_to_current_span(attribute_key=HTTP_URL, attribute_value=str(request.url)) return response
def pre_invocation_app_level(cls, logger, context, func_args={}, *args, **kwargs): """An implementation of pre invocation hooks on Function App's level. The Python Worker Extension Interface is defined in https://github.com/Azure/azure-functions-python-library/ blob/dev/azure/functions/extension/app_extension_base.py """ if not cls._exporter: logger.warning( 'Please call OpenCensusExtension.configure() after the import ' 'statement to ensure AzureExporter is setup correctly.') return span_context = TraceContextPropagator().from_headers({ "traceparent": context.trace_context.Traceparent, "tracestate": context.trace_context.Tracestate }) tracer = Tracer(span_context=span_context, exporter=cls._exporter, sampler=ProbabilitySampler(1.0)) setattr(context, 'tracer', tracer)
def _setup_azure_logging(logger: logging.Logger, app: Flask, connection_string: str): #:https://docs.microsoft.com/en-us/azure/azure-monitor/app/opencensus-python #:param logger: Logging instance for azure opencensus stream handler. #:param app: Flask app instance for azure opencensus handler. #:param connection_string: Azure Application Insight connection string. # Setup trace handler. Handles normal logging output: # >>> logger.info("Info message") azure_handler = AzureLogHandler(connection_string=connection_string) logger.addHandler(azure_handler) FlaskMiddleware( app, exporter=AzureExporter(connection_string=connection_string), sampler=ProbabilitySampler(rate=1.0), )
def create_trace_dependency(name=name, message=message, message_before=message_before, message_after=message_after): config_integration.trace_integrations(['logging']) logger = logging.getLogger(__name__) handler = AzureLogHandler(connection_string='InstrumentationKey=%s' % (instrument_key)) handler.setFormatter( logging.Formatter('%(traceId)s %(spanId)s %(message)s')) logger.addHandler(handler) tracer = Tracer(exporter=AzureExporter( connection_string='InstrumentationKey=%s' % (instrument_key)), sampler=ProbabilitySampler(1.0)) logger.warning(message_before) with tracer.span(name=name): logger.warning(message) logger.warning(message_after)
def _setup_azure_logging(logger: logging.Logger, app: Flask, connection_string: str): """Setup logging into Azure Application Insights. :see: https://docs.microsoft.com/en-us/azure/azure-monitor/app/opencensus-python :param logger: Logging instance to a ssign azure opencensus stream handler. :param app: Flask app instance to assing azure opencensus handler. :param connection_string: Azure Application Insight connection string. """ # Setup trace handler. Handles normal logging output: # >>> logger.info("Info message") azure_handler = AzureLogHandler( connection_string=connection_string ) logger.addHandler(azure_handler) # Setup flask middleware, so pageview metrics are stored in azure. FlaskMiddleware( app, exporter=AzureExporter(connection_string=connection_string), sampler=ProbabilitySampler(rate=1.0), )
def pre_invocation_app_level(cls, logger: Logger, context: Context, func_args: Dict[str, object] = {}, *args, **kwargs) -> None: if not cls._has_configure_called: raise FunctionExtensionException( 'Please ensure OpenCensusExtension.configure() is called ' 'after the import OpenCensusExtension statement.') span_context = TraceContextPropagator().from_headers({ "traceparent": context.trace_context.Traceparent, "tracestate": context.trace_context.Tracestate }) tracer = Tracer(span_context=span_context, exporter=cls._exporter, sampler=ProbabilitySampler(1.0)) cls._tracers[context.function_name] = tracer setattr(context, 'tracer', tracer)
logger.setLevel(logging.INFO) # Metrics exporter = metrics_exporter.new_metrics_exporter( enable_standard_metrics=True, connection_string= 'InstrumentationKey=bdba70b7-8e6e-4adb-adb5-8fd02df32aea;IngestionEndpoint=https://westus2-1.in.applicationinsights.azure.com/' ) # Tracing tracer = Tracer( exporter=AzureExporter( connection_string= 'InstrumentationKey=bdba70b7-8e6e-4adb-adb5-8fd02df32aea;IngestionEndpoint=https://westus2-1.in.applicationinsights.azure.com/' ), sampler=ProbabilitySampler(1.0), ) app = Flask(__name__) # Requests middleware = FlaskMiddleware( app, exporter=AzureExporter( connection_string= "InstrumentationKey=bdba70b7-8e6e-4adb-adb5-8fd02df32aea;IngestionEndpoint=https://westus2-1.in.applicationinsights.azure.com/" ), sampler=ProbabilitySampler(rate=1.0), ) # Load configurations from environment or config file
from functools import wraps from werkzeug.exceptions import HTTPException from opencensus.trace.tracer import Tracer from opencensus.trace.samplers import ProbabilitySampler if not getenv('APPINSIGHTS_INSTRUMENTATIONKEY', None): tracer = Tracer() else: sampling_rate = getenv('TRACE_SAMPLING_RATE', None) if not sampling_rate: sampling_rate = 1.0 tracer = Tracer( exporter=AzureExporter(), sampler=ProbabilitySampler(float(sampling_rate)), ) disable_request_metric = getenv('DISABLE_CURRENT_REQUEST_METRIC', False) MAX_REQUESTS_KEY_NAME = 'max_requests' CONTENT_TYPE_KEY_NAME = 'content_type' CONTENT_MAX_KEY_NAME = 'content_max_length' APP_INSIGHTS_REQUESTS_KEY_NAME = 'CURRENT_REQUESTS' class Task(Resource): def __init__(self, **kwargs): self.task_mgr = kwargs['task_manager']
# You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from azure.identity import ClientSecretCredential from opencensus.ext.azure.trace_exporter import AzureExporter from opencensus.trace.samplers import ProbabilitySampler from opencensus.trace.tracer import Tracer tenant_id = "<tenant-id>" client_id = "<client-id>" client_secret = "<client-secret>" credential = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret) tracer = Tracer(exporter=AzureExporter( credential=credential, connection_string="<your-connection-string>"), sampler=ProbabilitySampler(1.0)) with tracer.span(name='foo'): print('Hello, World!') input(...)
# Copyright 2019, OpenCensus Authors # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from opencensus.ext.azure.trace_exporter import AzureExporter from opencensus.trace.samplers import ProbabilitySampler from opencensus.trace.tracer import Tracer # TODO: you need to specify the instrumentation key in the # APPINSIGHTS_INSTRUMENTATIONKEY environment variable. tracer = Tracer(exporter=AzureExporter(), sampler=ProbabilitySampler(1.0)) with tracer.span(name='foo'): print('Hello, World!')
'{{ project_name }}.settings') try: from django.core.management import execute_from_command_line import django import django.conf except ImportError as exc: raise ImportError( "Couldn't import Django. Are you sure it's installed and " "available on your PYTHONPATH environment variable? Did you " "forget to activate a virtual environment?") from exc # Get OpenCensus settings from settings.py SETTINGS = getattr(django.conf.settings, 'OPENCENSUS', {}) SETTINGS = SETTINGS.get('TRACE', {}) SAMPLER = (SETTINGS.get('SAMPLER', None) or ProbabilitySampler(rate=1.0)) if isinstance(SAMPLER, six.string_types): SAMPLER = configuration.load(SAMPLER) EXPORTER = SETTINGS.get('EXPORTER', None) or PrintExporter() if isinstance(EXPORTER, six.string_types): EXPORTER = configuration.load(EXPORTER) TRACER = Tracer( exporter=EXPORTER, sampler=SAMPLER, ) # Add tracing for PostgreSQL config_integration.trace_integrations(['postgresql'])
def __init__(self, logger, flask_app=None): self._properties = { "service_name": getenv(CONF_SERVICE_NAME), "service_version": getenv(CONF_SERVICE_VERSION), "service_cluster": getenv(CONF_SERVICE_CLUSTER), "service_model_name": getenv(CONF_SERVICE_MODEL_NAME), "service_model_version": getenv(CONF_SERVICE_MODEL_VERSION), "service_container_version": getenv(CONF_SERVICE_CONTAINER_VERSION), "service_container_name": getenv(CONF_SERVICE_CONTAINER_NAME), "task_id": "none", } self.logger = logger self.metrics = {} self.tracer = None self.appinsights_key = getenv("APPINSIGHTS_INSTRUMENTATIONKEY", None) if self.appinsights_key: try: print("Setting up Azure Monitor with Application Insights.") config_integration.trace_integrations(["logging"]) # self.logger = logging.getLogger(getenv(CONF_SERVICE_NAME)) self.logger.setLevel(logging.INFO) handler = AzureLogHandler( connection_string="InstrumentationKey=" + self.appinsights_key) self.logger.addHandler(handler) self.azure_exporter = AzureExporter( connection_string="InstrumentationKey=" + self.appinsights_key, timeout=getenv("APPINSIGHTS_TIMEOUT", 30.0), ) sampling_rate = getenv("TRACE_SAMPLING_RATE", None) if not sampling_rate: sampling_rate = 1.0 self.middleware = None if flask_app: self.middleware = FlaskMiddleware( flask_app, exporter=self.azure_exporter, sampler=ProbabilitySampler(rate=float(sampling_rate)), ) # self.tracer = Tracer( # exporter=self.azure_exporter, # sampler=ProbabilitySampler(rate=float(sampling_rate)), # ) self.tracer = Tracer(exporter=self.azure_exporter, sampler=AlwaysOnSampler()) self.metrics_exporter = metrics_exporter.new_metrics_exporter( connection_string="InstrumentationKey=" + self.appinsights_key) stats = stats_module.stats self.view_manager = stats.view_manager self.view_manager.register_exporter(self.metrics_exporter) self.stats_recorder = stats.stats_recorder except Exception as e: print("Exception in setting up the Azure Monitor:") print(e)
def create_dependency(name=name, message=message): tracer = Tracer(exporter=AzureExporter( connection_string='InstrumentationKey=%s' % (instrument_key)), sampler=ProbabilitySampler(1.0)) with tracer.span(name=name): print(message)
def __init__(self, **kwargs): super().__init__(**kwargs) self.sampler = ProbabilitySampler(1) self.exporter = print_exporter.PrintExporter() self.propagator = TraceContextPropagator()
from opencensus.tags import tag_map as tag_map_module from opencensus.ext.azure.trace_exporter import AzureExporter from opencensus.trace import config_integration from opencensus.trace.samplers import ProbabilitySampler from opencensus.trace.tracer import Tracer from opencensus.ext.flask.flask_middleware import FlaskMiddleware from applicationinsights import TelemetryClient instrumentation_key='InstrumentationKey=d6f3db6b-e6dc-4fd9-a9c5-65b3679df64f' # Logging config_integration.trace_integrations(['logging']) logger = logging.getLogger(__name__) logHandler = AzureLogHandler( connection_string=instrumentation_key, sampler=ProbabilitySampler(rate=1.0) ) logger.addHandler(logHandler) logger.setLevel(logging.DEBUG) # Metrics exporter = metrics_exporter.new_metrics_exporter( enable_standard_metrics=True, connection_string=instrumentation_key) # Tracing # tracer = Tracer( # exporter=AzureExporter( # connection_string=instrumentation_key), # sampler=ProbabilitySampler(rate=1.0) # )