Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
""" Module for record constants """
import os
from sap.cf_logging import defaults
from sap.cf_logging.record import util
LAYER = 'python'
COMPONENT_ID = util.get_vcap_param('application_id', defaults.UNKNOWN)
COMPONENT_NAME = util.get_vcap_param('name', defaults.UNKNOWN)
COMPONENT_INSTANCE = os.getenv('CF_INSTANCE_INDEX', 0)
SPACE_ID = util.get_vcap_param('space_id', defaults.UNKNOWN)
SPACE_NAME = util.get_vcap_param('space_name', defaults.UNKNOWN)
CONTAINER_ID = os.getenv('CF_INSTANCE_IP', defaults.UNKNOWN)
COMPONENT_TYPE = 'application'
def get_remote_user(self, request):
if request.authorization is not None:
return request.authorization.username
return defaults.UNKNOWN
def __init__(self, extra, framework, *args, **kwargs):
super(SimpleLogRecord, self).__init__(*args, **kwargs)
utcnow = datetime.utcnow()
self.written_at = util.iso_time_format(utcnow)
self.written_ts = util.epoch_nano_second(utcnow)
request = extra[REQUEST_KEY] if extra and REQUEST_KEY in extra else None
self.correlation_id = framework.context.get_correlation_id(request) or defaults.UNKNOWN
self.extra = dict((key, value) for key, value in extra.items()
if key not in _SKIP_ATTRIBUTES) if extra else {}
for key, value in self.extra.items():
setattr(self, key, value)
def get_content_length(self, request):
return defaults.UNKNOWN
self.type = 'request'
self.direction = 'IN'
self.remote_user = request_reader.get_remote_user(request)
self.request = request_reader.get_path(request)
self.referer = request_reader.get_http_header(
request, 'referer', defaults.UNKNOWN)
self.x_forwarded_for = request_reader.get_http_header(
request, 'x-forwarded-for', defaults.UNKNOWN)
self.protocol = request_reader.get_protocol(request)
self.method = request_reader.get_method(request)
self.remote_ip = remote_ip
self.request_size_b = util.parse_int(length, -1)
self.remote_host = remote_ip
self.remote_port = request_reader.get_remote_port(
request) or defaults.UNKNOWN
request_start = context.get(
'request_started_at', request) or defaults.UNIX_EPOCH
self.request_received_at = util.iso_time_format(request_start)
# response related
response_sent_at = context.get(
'response_sent_at', request) or defaults.UNIX_EPOCH
self.response_sent_at = util.iso_time_format(response_sent_at)
self.response_time_ms = util.time_delta_ms(
request_start, response_sent_at)
self.response_status = util.parse_int(
response_reader.get_status_code(response), defaults.STATUS)
self.response_size_b = util.parse_int(
response_reader.get_response_size(response), defaults.RESPONSE_SIZE_B)
def get_remote_user(self, request):
return defaults.UNKNOWN
response = extra[RESPONSE_KEY]
props = dict((key, value) for key, value in extra.items()
if key not in [RESPONSE_KEY, REQUEST_KEY])
for key, value in props.items():
setattr(self, key, value)
length = request_reader.get_content_length(request)
remote_ip = request_reader.get_remote_ip(request)
self.type = 'request'
self.direction = 'IN'
self.remote_user = request_reader.get_remote_user(request)
self.request = request_reader.get_path(request)
self.referer = request_reader.get_http_header(
request, 'referer', defaults.UNKNOWN)
self.x_forwarded_for = request_reader.get_http_header(
request, 'x-forwarded-for', defaults.UNKNOWN)
self.protocol = request_reader.get_protocol(request)
self.method = request_reader.get_method(request)
self.remote_ip = remote_ip
self.request_size_b = util.parse_int(length, -1)
self.remote_host = remote_ip
self.remote_port = request_reader.get_remote_port(
request) or defaults.UNKNOWN
request_start = context.get(
'request_started_at', request) or defaults.UNIX_EPOCH
self.request_received_at = util.iso_time_format(request_start)
# response related
response_sent_at = context.get(