Skip to content

Commit

Permalink
added logging to Veilid Containers
Browse files Browse the repository at this point in the history
  • Loading branch information
rasswanth-s committed Feb 25, 2024
1 parent 6bfe9e4 commit 3587751
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
10 changes: 9 additions & 1 deletion packages/grid/veilid/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ cd packages/grid/veilid && \
docker run --rm -e DEV_MODE=True -p 4000:4000 -p 5959:5959 -p 5959:5959/udp -v $(pwd)/server:/app/server veilid:0.1
```

#### 2. Production Mode
##### 2. Additional Flags for Development

```
a. VEILID_FLAGS="--debug" (For Veilid Debug logs)
b. APP_LOG_LEVEL="debug" (For changing logging method inside the application could be info, debug, warning, critical)
c. UVICORN_LOG_LEVEL="debug" (For setting logging method for uvicorn)
```

#### 3. Production Mode

```sh
cd packages/grid/veilid && \
Expand Down
1 change: 1 addition & 0 deletions packages/grid/veilid/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
fastapi==0.103.2
loguru==0.7.2
uvicorn[standard]==0.24.0.post1
13 changes: 11 additions & 2 deletions packages/grid/veilid/server/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# stdlib
import os
import sys

# third party
from fastapi import Body
from fastapi import FastAPI
from fastapi import Request
from loguru import logger
from typing_extensions import Annotated

# relative
Expand All @@ -11,6 +16,11 @@
from .veilid_core import get_veilid_conn
from .veilid_core import retrieve_dht_key

# Logging Configuration
log_level = os.getenv("APP_LOG_LEVEL", "INFO").upper()
logger.remove()
logger.add(sys.stderr, colorize=True, level=log_level)

app = FastAPI(title="Veilid")
veilid_conn = VeilidConnectionSingleton()

Expand Down Expand Up @@ -52,8 +62,7 @@ async def startup_event() -> None:
try:
await veilid_conn.initialize_connection()
except Exception as e:
# TODO: Shift to Logging Module
print(e)
logger.exception(f"Failed to connect to Veilid: {e}")
raise e


Expand Down
12 changes: 7 additions & 5 deletions packages/grid/veilid/server/veilid_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Union

# third party
from loguru import logger
import veilid
from veilid import KeyPair
from veilid import TypedKey
Expand Down Expand Up @@ -55,26 +56,27 @@ def connection(self) -> Optional[_JsonVeilidAPI]:
async def initialize_connection(self) -> None:
if self._connection is None:
self._connection = await get_veilid_conn(update_callback=main_callback)
# TODO: Shift to Logging Module
print("Connected to Veilid")
logger.info("Connected to Veilid")

async def release_connection(self) -> None:
if self._connection is not None:
await self._connection.release()
# TODO: Shift to Logging Module
print("Disconnected from Veilid")
logger.info("Disconnected from Veilid")
self._connection = None


async def generate_dht_key() -> dict[str, str]:
logger.info("Generating DHT Key")
conn = await get_veilid_conn()

if await load_dht_key(conn):
return {"message": "DHT Key already exists"}

router = await (await conn.new_routing_context()).with_default_safety()

dht_record = await router.create_dht_record(veilid.DHTSchema.dflt(1))
async with router:
dht_record = await router.create_dht_record(veilid.DHTSchema.dflt(1))

keypair = KeyPair.from_parts(key=dht_record.owner, secret=dht_record.owner_secret)

await store_dht_key(conn, dht_record.key)
Expand Down
8 changes: 4 additions & 4 deletions packages/grid/veilid/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ set -e
export PATH="/root/.local/bin:${PATH}"

APP_MODULE=server.main:app
LOG_LEVEL=${LOG_LEVEL:-info}
APP_LOG_LEVEL=${APP_LOG_LEVEL:-info}
UVICORN_LOG_LEVEL=${UVICORN_LOG_LEVEL:-info}
HOST=${HOST:-0.0.0.0}
PORT=${PORT:-4000}
RELOAD=""
VEILID_FLAGS=""
VEILID_FLAGS=${VEILID_FLAGS:-""}

if [[ ${DEV_MODE} == "True" ]];
then
echo "DEV_MODE Enabled"
RELOAD="--reload"
VEILID_FLAGS="--debug"
fi

/veilid/veilid-server -c /veilid/veilid-server.conf $VEILID_FLAGS &

exec uvicorn $RELOAD --host $HOST --port $PORT --log-level $LOG_LEVEL "$APP_MODULE"
exec uvicorn $RELOAD --host $HOST --port $PORT --log-level $UVICORN_LOG_LEVEL "$APP_MODULE"

0 comments on commit 3587751

Please sign in to comment.