Beispiel #1
0
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)
Beispiel #2
0
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()))
Beispiel #3
0
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))
Beispiel #4
0
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))
Beispiel #5
0
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))
Beispiel #6
0
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)
Beispiel #7
0
 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
Beispiel #8
0
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)
Beispiel #9
0
    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
Beispiel #11
0
    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)
Beispiel #12
0
    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)
Beispiel #13
0
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())
Beispiel #14
0
def set_error_details(code, desc):
    """set errorCode and description

    """
    MDC.put('errorCode', code)
    MDC.put('errorDescription', desc)
Beispiel #15
0
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)
Beispiel #16
0
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)
Beispiel #17
0
    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