1 # -------------------------------------------------------------------------
2 # Copyright (c) 2020 AT&T Intellectual Property
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 # -------------------------------------------------------------------------
25 from onaplogging.mdcContext import MDC
29 DATE_FMT = '%Y-%m-%dT%H:%M:%S'
32 def default_server_info():
33 """Populate server & server_ip_address MDC fields
36 # If not set or purposely set = None, then set default
37 if MDC.get('server') is None:
39 server = socket.getfqdn()
42 server = socket.gethostname()
45 MDC.put('server', server)
46 if MDC.get('serverIPAddress') is None:
48 server_ip_address = socket.gethostbyname(MDC.get('server'))
50 server_ip_address = ""
51 MDC.put('serverIPAddress', server_ip_address)
55 """Populate default MDC fields
58 MDC.put('instanceUUID', generate_uuid())
59 MDC.put('InvocationID', generate_uuid())
60 MDC.put('serviceName', 'OOF_OSDF')
61 MDC.put('threadID', threading.currentThread().getName())
63 MDC.put('requestID', generate_uuid())
64 MDC.put('partnerName', 'N/A')
65 MDC.put('entryTimestamp', get_time())
68 def mdc_from_json(request_json):
69 """Populate MDC fields given a request in json format
72 if MDC.get("instanceUUID") is None:
74 MDC.put('requestID', get_request_id(request_json))
75 MDC.put('partnerName', get_partner_name(request_json))
78 def populate_default_mdc(request):
79 """Populate default MDC fields given the request
82 if MDC.get("instanceUUID") is None:
84 g.request_start = time.process_time()
85 g.empty_value = "EMPTY"
86 g.request_id = MDC.get("requestID")
87 MDC.put('serviceName', request.path)
88 MDC.put('IPAddress', request.headers.get('X-Forwarded-For', request.remote_addr))
91 def populate_mdc(request):
92 """Populate MDC fields from the request headers
95 populate_default_mdc(request)
96 req_id = request.headers.get('X-ONAP-RequestID', g.empty_value)
97 request_json = request.get_json()
98 if req_id == g.empty_value:
99 req_id = get_request_id(request_json)
100 g.request_id = req_id
101 MDC.put('requestID', req_id)
102 MDC.put('partnerName', get_partner_name(request_json))
105 def get_request_id(request_json):
106 """Get the request_id from the request
109 request_id = request_json['requestInfo'].get('requestId')
111 request_id = request_json['requestInfo'].get('requestID')
116 """Generate an unique uuid
119 return f'{uuid.uuid1()}'
122 def get_partner_name(request_json):
123 """Get the partnerName
127 if 'requestInfo' in request_json:
128 partner_name = request_json['requestInfo'].get('sourceId', EMPTY)
140 """Generate the invocation time string
142 The dateformat is %Y-%m-%dT%H:%M:%S.sss
146 msec = int((ct - int(ct)) * 1000)
147 return f'{time.strftime(DATE_FMT, lt)}.{msec:0>3}'
150 def set_error_details(code, desc):
151 """set errorCode and description
154 MDC.put('errorCode', code)
155 MDC.put('errorDescription', desc)