diff --git a/python/django/whatap_tutorial/README.md b/python/django/whatap_tutorial/README.md new file mode 100644 index 0000000..3b8a639 --- /dev/null +++ b/python/django/whatap_tutorial/README.md @@ -0,0 +1,14 @@ +## 빌드 +docker build -t whatap/kube_python_django:0.0.1 + +## 실행 +docker run -d -p 8000:8000 \ + -e license=x41pl22ek7jhv-z43cebasdv4il7-z62p3l35fj5502 \ + -e whatap_server_host=15.165.146.117 \ + -e app_name=test-django \ + -e app_process_name=python \ + whatap/kube_python_django:0.0.1 + +docker ps | grep whatap/kube_python_django:0.0.1 + +docker exec -it {container id} /bin/bash diff --git a/python/fastapi/entrypoint.sh b/python/fastapi/entrypoint.sh index d98a60f..b8a7b62 100755 --- a/python/fastapi/entrypoint.sh +++ b/python/fastapi/entrypoint.sh @@ -11,4 +11,5 @@ echo "logsink_enabled=true" >> whatap.conf echo "logsink_trace_enabled=true" >> whatap.conf echo "trace_logging_enabled=true" >> whatap.conf echo "log_unhandled_exception=true" >> whatap.conf -whatap-start-agent uvicorn server:app --host 0.0.0.0 --port 8000 \ No newline at end of file +#whatap-start-agent uvicorn server:app --host 0.0.0.0 --port 8000 +whatap-start-agent gunicorn --config config/gunicorn.py server:app \ No newline at end of file diff --git a/python/fastapi/log.py b/python/fastapi/log.py new file mode 100755 index 0000000..1c4896d --- /dev/null +++ b/python/fastapi/log.py @@ -0,0 +1,37 @@ +import logging +from src.utils import get_project_dir + +project_dir = get_project_dir() +try: + import whatap.trace.mod.logging as whatap_logging + if whatap_logging.loguru_injection_processed: + # whatap 모니터링 사용하는 경우 + FORMAT = '%(module)s.%(funcName)s - %(message)s - %(levelname)s - %(asctime)s %(lineno)s -- {{ "@txid" : "{txid}" }} -- ' + else: + # whatap 모니터링 사용, loguru injection (x) + FORMAT = '%(module)s.%(funcName)s - %(message)s - %(levelname)s - %(asctime)s %(lineno)s' +except: + # whatap 모니터링 사용하지 않는 경우 + FORMAT = '%(module)s.%(funcName)s - %(message)s - %(levelname)s - %(asctime)s %(lineno)s' + +logger = logging.getLogger(__name__) +logger.setLevel(logging.DEBUG) +logger.propagate = False + +# 로그를 콘솔에 출력하는 Handler +stream_handler = logging.StreamHandler() +stream_handler.setLevel(logging.DEBUG) +STREAM_FORMAT = FORMAT +stream_log_formatter = logging.Formatter(STREAM_FORMAT) +stream_handler.setFormatter(stream_log_formatter) +logger.addHandler(stream_handler) + + +##File handler +filename = f"{project_dir}/app_logs/app.log" +file_handler = logging.FileHandler(filename=filename, mode="a") +file_handler.setLevel(logging.INFO) +FORMAT = FORMAT +file_log_formatter = logging.Formatter(FORMAT) +file_handler.setFormatter(file_log_formatter) +logger.addHandler(file_handler) \ No newline at end of file diff --git a/python/fastapi/server.py b/python/fastapi/server.py index f1a6f81..269c14f 100755 --- a/python/fastapi/server.py +++ b/python/fastapi/server.py @@ -3,11 +3,10 @@ import hashlib from datetime import datetime, timedelta from loguru import logger as loguru_logger -from fastapi import FastAPI, HTTPException, Body +from fastapi import FastAPI, HTTPException, Request import uvicorn import requests from starlette.responses import HTMLResponse -from kubernetes import client from models import Result import csv @@ -31,6 +30,22 @@ async def health_check(): requests.get(url="https://fastapi.tiangolo.com/lo/") return {"message": "health_check"} +@app.get("/whatap_test") +async def whatap_test(req:Request): + logging_logger.info("start:health_test") + message = str(dict(req.headers)) + message = message.replace('{', '') + message = message.replace('}', '') + message = message.replace("'", '') + + logging_logger.info(f"message:{message}") + start_time = datetime.now() + while True: + if datetime.now() - start_time > timedelta(seconds=1): + break + res = requests.get(url="https://www.naver.com") + return {"test": "test"} + @app.get("/error_check", tags=["trace"]) async def error_check(): start_time = datetime.now()