Skip to content

Commit

Permalink
fixed urls.py
Browse files Browse the repository at this point in the history
  • Loading branch information
jaeeyoungkim committed Mar 11, 2024
1 parent 0ee1933 commit 7b14de3
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 3 deletions.
14 changes: 14 additions & 0 deletions python/django/whatap_tutorial/README.md
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion python/fastapi/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
#whatap-start-agent uvicorn server:app --host 0.0.0.0 --port 8000
whatap-start-agent gunicorn --config config/gunicorn.py server:app
37 changes: 37 additions & 0 deletions python/fastapi/log.py
Original file line number Diff line number Diff line change
@@ -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)
19 changes: 17 additions & 2 deletions python/fastapi/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()
Expand Down

0 comments on commit 7b14de3

Please sign in to comment.