Skip to content

Commit

Permalink
#186810782 : replace print by logger and latest moesif packages (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
HzANut authored Jan 12, 2024
1 parent ec6b220 commit ab20fbe
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 130 deletions.
7 changes: 4 additions & 3 deletions examples/fastapi/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
moesifasgi
uvicorn
passlib==1.7.4
moesifapi==1.4.1
moesifpythonrequest==0.3.2
moesifapi==1.4.2
moesifpythonrequest==0.3.3
fastapi==0.108.0
jose==1.0.0
python-jose==3.3.0
Expand All @@ -14,4 +14,5 @@ cffi==1.15.0
six==1.16.0
pycparser==2.21
bcrypt
python-multipart
python-multipart
importlib-metadata
16 changes: 8 additions & 8 deletions moesifasgi/app_config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from datetime import datetime
from moesifapi.exceptions.api_exception import *
import json
import logging

logger = logging.getLogger(__name__)

# Application Configuration
class AppConfig:
Expand All @@ -16,14 +19,12 @@ def get_config(cls, api_client, debug):
return config_api_response
except APIException as inst:
if 401 <= inst.response_code <= 403:
print("Unauthorized access getting application configuration. Please check your Application Id.")
logger.error("Unauthorized access getting application configuration. Please check your Application Id.")
if debug:
print("Error getting application configuration, with status code:")
print(inst.response_code)
logger.info(f"Error getting application configuration, with status code: {inst.response_code}")
except Exception as ex:
if debug:
print("Error getting application configuration:")
print(str(ex))
logger.info(f"Error getting application configuration: {str(ex)}")

@classmethod
def parse_configuration(cls, config, debug):
Expand All @@ -32,7 +33,7 @@ def parse_configuration(cls, config, debug):
return config.headers.get("X-Moesif-Config-ETag"), json.loads(config.raw_body).get('sample_rate', 100), datetime.utcnow()
except:
if debug:
print('Error while parsing the configuration object, setting the sample rate to default')
logger.info('Error while parsing the configuration object, setting the sample rate to default')
return None, 100, datetime.utcnow()

@classmethod
Expand All @@ -55,8 +56,7 @@ def get_sampling_percentage(cls, config, user_id, company_id):

return config_body.get('sample_rate', 100)
except Exception as e:
print("Error while parsing user or company sample rate")
print(e)
logger.warning(f"Error while parsing user or company sample rate:{str(e)}")

# Use default
return 100
13 changes: 9 additions & 4 deletions moesifasgi/client_ip.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import re

import logging

logger = logging.getLogger(__name__)


class ClientIp:

Expand All @@ -13,14 +17,15 @@ def is_ip(cls, value):
ipv6 = r"^((?=.*::)(?!.*::.+::)(::)?([\dA-F]{1,4}:(:|\b)|){5}|([\dA-F]{1,4}:){6})((([\dA-F]{1,4}((?!\3)::|:\b|$))|(?!\2\3)){2}|(((2[0-4]|1\d|[1-9])?\d|25[0-5])\.?\b){4})$/i"
return re.match(ipv4, value) or re.match(ipv6, value)

def getClientIpFromXForwardedFor(self, value):
def getClientIpFromXForwardedFor(self, value, debug = False):
try:

if not value or value is None:
return None

if not isinstance(value, str):
print("Expected a string, got -" + str(type(value)))
if debug:
logger.info(f"Expected a string, got - {str(type(value))}")
else:
# x-forwarded-for may return multiple IP addresses in the format:
# "client IP, proxy 1 IP, proxy 2 IP"
Expand All @@ -45,7 +50,7 @@ def getClientIpFromXForwardedFor(self, value):
except StopIteration:
return value.encode('utf-8')

def get_client_address(self, request_headers, default_host):
def get_client_address(self, request_headers, default_host, debug = False):
try:
# Standard headers used by Amazon EC2, Heroku, and others.
if 'x-client-ip' in request_headers:
Expand All @@ -54,7 +59,7 @@ def get_client_address(self, request_headers, default_host):

# Load-balancers (AWS ELB) or proxies.
if 'x-forwarded-for' in request_headers:
xForwardedFor = self.getClientIpFromXForwardedFor(request_headers['x-forwarded-for'])
xForwardedFor = self.getClientIpFromXForwardedFor(request_headers['x-forwarded-for'], debug)
if self.is_ip(xForwardedFor):
return xForwardedFor

Expand Down
4 changes: 2 additions & 2 deletions moesifasgi/event_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async def to_event(self, request, response, event_req, event_rsp, moesif_setting
metadata=await self.logger_helper.get_metadata(moesif_settings, request, response, debug),
direction="Incoming")

def to_request(self, request, request_time, request_body, api_version, disable_capture_transaction_id):
def to_request(self, request, request_time, request_body, api_version, disable_capture_transaction_id, debug=False):
# Request URI
request_uri = request.url._url
# Request Verb
Expand All @@ -41,7 +41,7 @@ def to_request(self, request, request_time, request_body, api_version, disable_c
# Add transaction id to the request header
request_headers["X-Moesif-Transaction-Id"] = self.transaction_id
# Request Ip address
request_ip_address = self.client_ip.get_client_address(request_headers, request.client.host)
request_ip_address = self.client_ip.get_client_address(request_headers, request.client.host, debug)
# Request Body
req_body = None
req_transfer_encoding = None
Expand Down
22 changes: 10 additions & 12 deletions moesifasgi/logger_helper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import inspect
import json
import base64
import logging

logger = logging.getLogger(__name__)


class LoggerHelper:
Expand Down Expand Up @@ -44,8 +47,7 @@ def parse_authorization_header(self, token, field, debug):
return str(json_decode[field])
except Exception as e:
if debug:
print("Error while parsing authorization header to fetch user id.")
print(e)
logger.info(f"Error while parsing authorization header to fetch user id: {str(e)}")
return None

async def get_user_id(self, middleware_settings, request, response, request_headers, debug):
Expand Down Expand Up @@ -116,8 +118,7 @@ async def get_user_id(self, middleware_settings, request, response, request_head
user_id = self.parse_authorization_header(token, field, debug)
except Exception as e:
if debug:
print("can not execute identify_user function, please check moesif settings.")
print(e)
logger.info(f"can not execute identify_user function, please check moesif settings: {str(e)}")
return user_id

async def get_company_id(self, middleware_settings, request, response, debug):
Expand All @@ -131,8 +132,7 @@ async def get_company_id(self, middleware_settings, request, response, debug):
company_id = identify_company(request, response)
except Exception as e:
if debug:
print("can not execute identify_company function, please check moesif settings.")
print(e)
logger.info(f"can not execute identify_company function, please check moesif settings: {str(e)}")
return company_id

async def get_metadata(self, middleware_settings, request, response, debug):
Expand All @@ -146,8 +146,7 @@ async def get_metadata(self, middleware_settings, request, response, debug):
metadata = get_metadata(request, response)
except Exception as e:
if debug:
print("can not execute get_metadata function, please check moesif settings.")
print(e)
logger.info(f"can not execute get_metadata function, please check moesif settings: {str(e)}")
return metadata

async def get_session_token(self, middleware_settings, request, response, debug):
Expand All @@ -161,8 +160,7 @@ async def get_session_token(self, middleware_settings, request, response, debug)
session_token = get_session_token(request, response)
except Exception as e:
if debug:
print("Can not execute get_session_token function. Please check moesif settings.")
print(e)
logger.info(f"Can not execute get_session_token function. Please check moesif settings: {str(e)}")
return session_token

async def should_skip(self, middleware_settings, request, response, debug):
Expand All @@ -177,7 +175,7 @@ async def should_skip(self, middleware_settings, request, response, debug):
return False
except Exception as e:
if debug:
print("error trying to execute skip function.")
logger.info("error trying to execute skip function.")
return False

async def mask_event(self, event_model, middleware_settings, debug):
Expand All @@ -190,5 +188,5 @@ async def mask_event(self, event_model, middleware_settings, debug):
event_model = mask_event_model(event_model)
except Exception as e:
if debug:
print("Can not execute MASK_EVENT_MODEL function. Please check moesif settings.")
logger.info("Can not execute MASK_EVENT_MODEL function. Please check moesif settings.")
return event_model
39 changes: 18 additions & 21 deletions moesifasgi/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import random
import queue
import atexit
import logging

logger = logging.getLogger(__name__)


class MoesifMiddleware(BaseHTTPMiddleware):
Expand All @@ -44,11 +47,11 @@ def __init__(self, settings=None, *args, **kwargs):
if self.moesif_settings.get('CAPTURE_OUTGOING_REQUESTS', False):
try:
if self.DEBUG:
print('Start capturing outgoing requests')
logger.info('Start capturing outgoing requests')
# Start capturing outgoing requests
StartCapture().start_capture_outgoing(self.moesif_settings)
except:
print('Error while starting to capture the outgoing events')
logger.warning('Error while starting to capture the outgoing events')
self.api_client = self.client.api
self.app_config = AppConfig()
self.send_async_events = SendEventAsync()
Expand All @@ -75,14 +78,13 @@ def __init__(self, settings=None, *args, **kwargs):
self.config, self.DEBUG)
except Exception as ex:
if self.DEBUG:
print('Error while parsing application configuration on initialization')
print(str(ex))
logger.info(f'Error while parsing application configuration on initialization:{str(ex)}')

# Function to listen to the send event job response
def moesif_event_listener(self, event):
if event.exception:
if self.DEBUG:
print('Error reading response from the scheduled job')
logger.info('Error reading response from the scheduled job')
else:
if event.retval:
response_etag, self.last_event_job_run_time = event.retval
Expand All @@ -96,8 +98,7 @@ def moesif_event_listener(self, event):
self.config, self.DEBUG)
except Exception as ex:
if self.DEBUG:
print('Error while updating the application configuration')
print(str(ex))
logger.info(f'Error while updating the application configuration: {str(ex)}')

def schedule_background_job(self):
try:
Expand All @@ -122,8 +123,7 @@ def schedule_background_job(self):
atexit.register(lambda: self.send_async_events.exit_handler(self.scheduler, self.DEBUG))
except Exception as ex:
if self.DEBUG:
print("Error when scheduling the job")
print(str(ex))
logger.info(f"Error when scheduling the job: {str(ex)}")

def update_user(self, user_profile):
User().update_user(user_profile, self.api_client, self.DEBUG)
Expand Down Expand Up @@ -159,7 +159,7 @@ async def dispatch(self, request, call_next):
# request time
request_time = self.get_time()
if self.DEBUG:
print("event request time: ", request_time)
logger.info(f"event request time: {str(request_time)}")

# Read Request Body
request_body = None
Expand All @@ -172,7 +172,7 @@ async def dispatch(self, request, call_next):
# response time
response_time = self.get_time()
if self.DEBUG:
print("event response time: ", response_time)
logger.info(f"event response time: {str(response_time)}")

skip = await self.logger_helper.should_skip(self.moesif_settings, request, response, self.DEBUG)
if not skip:
Expand All @@ -184,7 +184,7 @@ async def dispatch(self, request, call_next):

if self.sampling_percentage >= random_percentage:
# Prepare Event Request Model
event_req = self.event_mapper.to_request(request, request_time, request_body, self.api_version, self.disable_transaction_id)
event_req = self.event_mapper.to_request(request, request_time, request_body, self.api_version, self.disable_transaction_id, self.DEBUG)

# Read Response Body
resp_body = None
Expand Down Expand Up @@ -219,25 +219,22 @@ async def dispatch(self, request, call_next):
except Exception as ex:
self.is_event_job_scheduled = False
if self.DEBUG:
print('Error while starting the event scheduler job in background')
print(str(ex))
logger.info(f'Error while starting the event scheduler job in background: {str(ex)}')
# Add Event to the queue
if self.DEBUG:
print('Add Event to the queue')
logger.info('Add Event to the queue')
self.moesif_events_queue.put(event_data)
except Exception as ex:
if self.DEBUG:
print("Error while adding event to the queue")
print(str(ex))
logger.info(f"Error while adding event to the queue: {str(ex)}")
else:
if self.DEBUG:
print('Skipped Event as the moesif event model is None')
logger.info('Skipped Event as the moesif event model is None')
else:
if self.DEBUG:
print("Skipped Event due to sampling percentage: " + str(self.sampling_percentage)
+ " and random percentage: " + str(random_percentage))
logger.info(f"Skipped Event due to sampling percentage: {str(self.sampling_percentage)} and random percentage: {str(random_percentage)}")
else:
if self.DEBUG:
print('Skipped Event using should_skip configuration option')
logger.info('Skipped Event using should_skip configuration option')

return response
16 changes: 8 additions & 8 deletions moesifasgi/send_batch_events.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from datetime import datetime
import logging

logger = logging.getLogger(__name__)

class SendEventAsync:

Expand All @@ -14,25 +16,23 @@ def exit_handler(cls, scheduler, debug):
scheduler.shutdown()
except Exception as ex:
if debug:
print("Error while closing the queue or scheduler shut down")
print(str(ex))
logger.info(f"Error while closing the queue or scheduler shut down: {str(ex)}")

@classmethod
def send_events(cls, api_client, batch_events, debug):
try:
if debug:
print("Sending events to Moesif")
logger.info("Sending events to Moesif")
batch_events_api_response = api_client.create_events_batch(batch_events)
if debug:
print("Events sent successfully")
logger.info("Events sent successfully")
# Fetch Config ETag from response header
batch_events_response_config_etag = batch_events_api_response.get("X-Moesif-Config-ETag")
# Return Config Etag
return batch_events_response_config_etag
except Exception as ex:
if debug:
print("Error sending event to Moesif")
print(str(ex))
logger.info(f"Error sending event to Moesif: {str(ex)}")
return None

def batch_events(self, api_client, moesif_events_queue, debug, batch_size):
Expand All @@ -49,11 +49,11 @@ def batch_events(self, api_client, moesif_events_queue, debug, batch_size):
return batch_response, datetime.utcnow()
else:
if debug:
print("No events to send")
logger.info("No events to send")
# Set the last time event job ran but no message to read from the queue
return None, datetime.utcnow()
except:
if debug:
print("No message to read from the queue")
logger.info("No message to read from the queue")
# Set the last time event job ran when exception occurred while sending event
return None, datetime.utcnow()
Loading

0 comments on commit ab20fbe

Please sign in to comment.