Skip to content

Commit

Permalink
[MDS-5437] keycloak log client (#2777)
Browse files Browse the repository at this point in the history
* wip

* handle inevitable error.

* update log format.

* update comment.
  • Loading branch information
henryoforeh-dev authored Nov 3, 2023
1 parent e051d33 commit 6fbc584
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
9 changes: 9 additions & 0 deletions services/core-api/app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import datetime

import requests
import os
from logging.config import dictConfig

from flask import Flask, request, current_app
from flask_cors import CORS
from flask_restplus import Resource, apidoc
from opentelemetry.instrumentation.flask import FlaskInstrumentor
from opentelemetry.instrumentation.logging import LoggingInstrumentor
from sqlalchemy.exc import SQLAlchemyError
from app.flask_jwt_oidc_local.exceptions import AuthError
from werkzeug.exceptions import Forbidden
Expand Down Expand Up @@ -44,13 +47,19 @@
from app.tasks.celery import celery
from app.tasks.celery_health_check import HealthCheckProbe

from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider

def create_app(test_config=None):
"""Create and configure an instance of the Flask application."""
if test_config is None:
dictConfig(Config.LOGGING_DICT_CONFIG)
app = Flask(__name__)

trace.set_tracer_provider(TracerProvider())

FlaskInstrumentor().instrument_app(app)

if test_config is None:
# load the instance config, if it exists, when not testing
app.config.from_object(Config)
Expand Down
49 changes: 47 additions & 2 deletions services/core-api/app/config.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,58 @@
import logging
import os

from logging.handlers import SysLogHandler
from dotenv import load_dotenv, find_dotenv
from celery.schedules import crontab
from opentelemetry import trace
import requests


ENV_FILE = find_dotenv()
if ENV_FILE:
load_dotenv(ENV_FILE)



class CustomFormatter(logging.Formatter):
def format(self, record):
KEY_CLOAK_CLIENT_ID = None
def get_key_cloak_client_id():
try:
# Check if the request is a valid HTTP request
if requests:
from app.extensions import getJwtManager
if getJwtManager().audience:
return getJwtManager().audience
except Exception as e:
# Handle the exception here (e.g., log it)
print(f"An error occurred: {e}")

return None

def get_traceid_from_telemetry():
current_span = trace.get_current_span()
if current_span:
traceid = current_span.get_span_context().trace_id
return traceid
return None

# Get the traceid from the telemetry
traceid = get_traceid_from_telemetry()

# Add the traceid to the log message
record.traceid = traceid
if get_key_cloak_client_id() and not KEY_CLOAK_CLIENT_ID:
KEY_CLOAK_CLIENT_ID = get_key_cloak_client_id()

# Call the parent formatter to format the log message
formatted_message = super().format(record)

# Add the traceid, keycloak client id and message to the formatted log message
formatted_message = f'{formatted_message} [trace_id={traceid} client={KEY_CLOAK_CLIENT_ID}]: {record.message}'

return formatted_message


class Config(object):
# Environment config
FLASK_LOGGING_LEVEL = os.environ.get('FLASK_LOGGING_LEVEL',
Expand All @@ -18,7 +62,8 @@ class Config(object):
'version': 1,
'formatters': {
'default': {
'format': '[%(asctime)s] %(levelname)s in %(module)s: %(message)s',
'()': CustomFormatter,
'format': '%(asctime)s %(levelname)s [%(name)s] [%(filename)s:%(lineno)d]',
}
},
'handlers': {
Expand Down

0 comments on commit 6fbc584

Please sign in to comment.