def info(self, msg, *args, **kwargs): """Wrapper method for log.info is called """ if self.isEnabledFor(logging.INFO): MDC.put('customField2', mdc_mapper()) self._log(logging.INFO, no_sep(msg), args, **kwargs)
def _handler( servicer: "ArtifactManagerServicer", request: Union[BluePrintDownloadInput, BluePrintRemoveInput, BluePrintUploadInput], context: ServicerContext, ) -> BluePrintManagementOutput: try: output: BluePrintManagementOutput = func(servicer, request, context) output.status.code = 200 output.status.message = "success" except ArtifactManagerError as error: # If ArtifactManagerError is raises one of defined error occurs. # Every ArtifactManagerError based exception has status_code paramenter # which has to be set in output. Use also exception's message to # set errorMessage of the output. output: BluePrintManagementOutput = BluePrintManagementOutput() output.status.code = error.status_code output.status.message = "failure" output.status.errorMessage = str(error.message) servicer.fill_MDC_timestamps() servicer.logger.error( "Error while processing the message - blueprintName={} blueprintVersion={}" .format(request.actionIdentifiers.blueprintName, request.actionIdentifiers.blueprintVersion), extra={"mdc": MDC.result()}, ) MDC.clear() return output
def process_pci_optimation(request_json, osdf_config, flat_policies): """Process a PCI request from a Client (build config-db, policy and API call, make the call, return result) :param req_object: Request parameters from the client :param osdf_config: Configuration specific to OSDF application (core + deployment) :param flat_policies: policies related to pci (fetched based on request) :return: response from PCI Opt """ try: mdc_from_json(request_json) rc = get_rest_client(request_json, service="pcih") req_id = request_json["requestInfo"]["requestId"] cell_info_list, network_cell_info = config_request(request_json, osdf_config, flat_policies) pci_response = get_solutions(cell_info_list, network_cell_info, request_json) metrics_log.info(MH.inside_worker_thread(req_id)) except Exception as err: error_log.error("Error for {} {}".format(req_id, traceback.format_exc())) try: body = build_json_error_body(err) metrics_log.info(MH.sending_response(req_id, "ERROR")) rc.request(json=body, noresponse=True) except RequestException: MDC.put('requestID', req_id) error_log.error("Error sending asynchronous notification for {} {}".format(req_id, traceback.format_exc())) raise err try: metrics_log.info(MH.calling_back_with_body(req_id, rc.url, pci_response)) error_log.error("pci response: {}".format(str(pci_response))) rc.request(json=pci_response, noresponse=True) except RequestException: # can't do much here but log it and move on error_log.error("Error sending asynchronous notification for {} {}".format(req_id, traceback.format_exc()))
def mdc_from_json(request_json): """Populate MDC fields given a request in json format """ if MDC.get("instanceUUID") is None: default_mdc() MDC.put('requestID', get_request_id(request_json)) MDC.put('partnerName', get_partner_name(request_json))
def error(self, msg, *args, **kwargs): """Wrapper method for log.error is called msg: log message args: logging args kwargs: all the optional args """ if self.isEnabledFor(logging.ERROR): if not MDC.get('errorCode'): set_error_details(400, 'Internal Error') MDC.put('customField2', mdc_mapper()) self._log(logging.ERROR, no_sep(msg), args, **kwargs)
def populate_mdc(request): """Populate MDC fields from the request headers """ populate_default_mdc(request) req_id = request.headers.get('X-ONAP-RequestID', g.empty_value) request_json = request.get_json() if req_id == g.empty_value: req_id = get_request_id(request_json) g.request_id = req_id MDC.put('requestID', req_id) MDC.put('partnerName', get_partner_name(request_json))
def mdc_mapper(): """Convert the MDC dict into comma separated, name=value string :return: string format """ return ','.join(f'{k}={v}' for (k, v) in MDC.result().items() if k not in ['customField2'])
def process_request(self, request): # fetch propageted Id from other component. if do not fetch id, # generate one. ReqeustID = request.META.get("HTTP_X_TRANSACTIONID", None) if ReqeustID is None: ReqeustID = str(uuid.uuid3(uuid.NAMESPACE_URL, settings.MULTIVIM_VERSION)) MDC.put("requestID", ReqeustID) # generate the reqeust id InvocationID = str(uuid.uuid4()) MDC.put("invocationID", InvocationID) MDC.put("serviceName", settings.MULTIVIM_VERSION) MDC.put("serviceIP", self._getLastIp(request)) return None
def process_request(self, request): # Fetch TRANSACTIONID Id and pass to plugin server ReqeustID = request.META.get("HTTP_X_ONAP-RequestID", None) if ReqeustID is None: ReqeustID = uuid.uuid3(uuid.NAMESPACE_URL, SERVICE_NAME) request.META["HTTP_X_ONAP-RequestID"] = ReqeustID MDC.put("requestID", ReqeustID) # generate the unique id InovocationID = uuid.uuid3(uuid.NAMESPACE_DNS, SERVICE_NAME) MDC.put("invocationID", InovocationID) MDC.put("serviceName", SERVICE_NAME) # access ip MDC.put("serviceIP", self._getLastIp(request)) return None
def process_request(self, request): ''' process request ''' # fetch propageted Id from other component. if do not fetch id, # generate one. reqeust_id = request.META.get("HTTP_X_TRANSACTIONID", None) if reqeust_id is None: reqeust_id = str(uuid.uuid3(uuid.NAMESPACE_URL, settings.MULTIVIM_VERSION)) MDC.put("requestID", reqeust_id) # generate the reqeust id invocation_id = str(uuid.uuid4()) MDC.put("invocationID", invocation_id) MDC.put("serviceName", settings.MULTIVIM_VERSION) MDC.put("serviceIP", self._get_last_ip(request)) return None
def wrapper(*args, **kwargs): request_id = str(uuid.uuid1()) invocation_id = str(uuid.uuid1()) MDC.put('ServiceName', getenv('HOSTNAME')) MDC.put('RequestID', request_id) MDC.put('InvocationID', invocation_id) kwargs['request_id'] = request_id kwargs['invocation_id'] = invocation_id return func(*args, **kwargs)
def populate_default_mdc(request): """Populate default MDC fields given the request """ if MDC.get("instanceUUID") is None: default_mdc() g.request_start = time.process_time() g.empty_value = "EMPTY" g.request_id = MDC.get("requestID") MDC.put('serviceName', request.path) MDC.put('IPAddress', request.headers.get('X-Forwarded-For', request.remote_addr))
def fill_MDC_timestamps(self, status_code: int = 200) -> NoReturn: """Add MDC context timestamps "in place". :param status_code: int with expected response status. Default: 200 (success) """ now = datetime.utcnow().replace(tzinfo=timezone.utc) MDC.put("EndTimestamp", now.strftime(MDC_DATETIME_FORMAT)) # Elapsed time measured in miliseconds MDC.put("ElapsedTime", (now - self.processing_started_at).total_seconds() * 1000) MDC.put("StatusCode", status_code)
def removeBlueprint(self, request: BlueprintRemoveInput, context: ServicerContext) -> BlueprintManagementOutput: """Remove blueprint file request method. Currently it only logs when is called and all base class method. :param request: BlueprintRemoveInput :param context: ServicerContext :return: BlueprintManagementOutput """ self.repository.remove_blueprint( request.actionIdentifiers.blueprintName, request.actionIdentifiers.blueprintVersion ) self.fill_MDC_timestamps() self.logger.info( "Blueprint removal successfuly processed - blueprintName={} blueprintVersion={}".format( request.actionIdentifiers.blueprintName, request.actionIdentifiers.blueprintVersion ), extra={"mdc": MDC.result()}, ) return BlueprintManagementOutput()
def downloadBlueprint(self, request: BlueprintDownloadInput, context: ServicerContext) -> BlueprintManagementOutput: """Download blueprint file request method. Currently it only logs when is called and all base class method. :param request: BlueprintDownloadInput :param context: ServicerContext :return: BlueprintManagementOutput """ output: BlueprintManagementOutput = BlueprintManagementOutput() output.fileChunk.chunk = self.repository.download_blueprint( request.actionIdentifiers.blueprintName, request.actionIdentifiers.blueprintVersion ) self.fill_MDC_timestamps() self.logger.info( "Blueprint download successfuly processed - blueprintName={} blueprintVersion={}".format( request.actionIdentifiers.blueprintName, request.actionIdentifiers.blueprintVersion ), extra={"mdc": MDC.result()}, ) return output
def default_server_info(): """Populate server & server_ip_address MDC fields """ # If not set or purposely set = None, then set default if MDC.get('server') is None: try: server = socket.getfqdn() except Exception: try: server = socket.gethostname() except Exception: server = '' MDC.put('server', server) if MDC.get('serverIPAddress') is None: try: server_ip_address = socket.gethostbyname(MDC.get('server')) except Exception: server_ip_address = "" MDC.put('serverIPAddress', server_ip_address)
def process_response(self, request, response): MDC.clear() return response
def clear_mdc(): """Clear MDC """ MDC.clear()
def process_response(self, response): ''' process response ''' MDC.clear() return response
def set_error_details(code, desc): """set errorCode and description """ MDC.put('errorCode', code) MDC.put('errorDescription', desc)
def default_mdc(): """Populate default MDC fields """ MDC.put('instanceUUID', generate_uuid()) MDC.put('InvocationID', generate_uuid()) MDC.put('serviceName', 'OOF_OSDF') MDC.put('threadID', threading.currentThread().getName()) default_server_info() MDC.put('requestID', generate_uuid()) MDC.put('partnerName', 'N/A') MDC.put('entryTimestamp', get_time())
def augment_record(*args, **kwargs): new_record = old_record(*args, **kwargs) new_record.mdc = MDC.result() return new_record
def log_message(message, status_value, p_marker='INVOKE'): MDC.put('statusCode', status_value) MDC.put('customField1', p_marker) MDC.put('timer', int((time.process_time() - g.request_start) * 1000)) audit_log.info(message)
def set_default_audit_mdc(request, status_value, p_marker): MDC.put('partnerName', 'internal') MDC.put('serviceName', request.path) MDC.put('statusCode', status_value) MDC.put('requestID', 'internal') MDC.put('timer', int((time.process_time() - g.request_start) * 1000)) MDC.put('customField1', p_marker)
def _decorator( servicer: "ArtifactManagerServicer", request: Union[BluePrintDownloadInput, BluePrintRemoveInput, BluePrintUploadInput], context: ServicerContext, ) -> BluePrintManagementOutput: MDC.put("RequestID", request.commonHeader.requestId) MDC.put("InvocationID", request.commonHeader.subRequestId) MDC.put("ServiceName", servicer.__class__.__name__) MDC.put("PartnerName", request.commonHeader.originatorId) started_at = datetime.utcnow().replace(tzinfo=timezone.utc) MDC.put("BeginTimestamp", started_at.strftime(MDC_DATETIME_FORMAT)) # Adding processing_started_at to the servicer so later we'll have the data to calculate elapsed time. servicer.processing_started_at = started_at MDC.put("TargetEntity", "py-executor") MDC.put("TargetServiceName", func.__name__) MDC.put("Server", socket.getfqdn()) output: BluePrintManagementOutput = func(servicer, request, context) MDC.clear() return output