Skip to content

Commit

Permalink
Improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
celsiusnarhwal committed Jul 19, 2024
1 parent 5cd3018 commit 2da72e1
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 21 deletions.
35 changes: 21 additions & 14 deletions firewhale/_internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys
from pathlib import Path

import inflect as ifl
import pendulum
from docker import DockerClient
from jinja2 import Environment, FileSystemLoader
Expand All @@ -21,6 +22,8 @@
lstrip_blocks=True,
)

inflect = ifl.engine()


def generate():
"""
Expand All @@ -37,7 +40,7 @@ def generate():
matchers = []

for container in dc.containers.list():
logger.debug(f'Determining access for {container.name}')
logger.debug(f"Determining access for {container.name}")

read_label = container.labels.get(f"{settings.label_prefix}.read")
write_label = container.labels.get(f"{settings.label_prefix}.write")
Expand All @@ -56,13 +59,15 @@ def generate():

if "all" not in readable_endpoints:
logger.debug(
f'Granting {container.name} read access to: '
+ ", ".join([f"/{endpoint}" for endpoint in readable_endpoints])
f"Granting {container.name} read access to "
+ inflect.join(
[f"/{endpoint}" for endpoint in readable_endpoints]
)
)
rules.append("vars {endpoint} " + " ".join(readable_endpoints))
else:
logger.debug(
f'Granting {container.name} read access to all endpoints'
f"Granting {container.name} read access to all endpoints"
)

matchers.append(Matcher(name=f"{container.name}_read", rules=rules))
Expand All @@ -78,28 +83,26 @@ def generate():

if "all" not in writeable_endpoints:
logger.debug(
f'Granting "{container.name}" read access to: '
+ ", ".join(
f"Granting {container.name} write access to "
+ inflect.join(
[f"/{endpoint}" for endpoint in writeable_endpoints]
)
)

rules.append("vars {endpoint} " + " ".join(writeable_endpoints))
else:
logger.debug(
f'Granting {container.name} write access to all endpoints'
f"Granting {container.name} write access to all endpoints"
)

matchers.append(Matcher(name=f"{container.name}_write", rules=rules))
else:
logger.debug(f'No access granted to {container.name}')
logger.debug(f"No access granted to {container.name}")

# Write a request matcher for /events, /_ping, and /version
if allowed_containers:
container_names = [ctr.name for ctr in allowed_containers]
logger.debug(
"Granting read access to /events, /_ping, and /version to: "
+ ", ".join(container_names)
f"Granting read access to /events, /_ping, and /version to {inflect.join(container_names)}"
)

matchers.append(
Expand All @@ -121,9 +124,10 @@ def generate():


def log_sink(log: str):
# Since Caddy is the only other thing logging to the container, we try our best to mimic its log structure.
record = json.loads(log)["record"]

level: str = record["level"]["name"]
level: LogLevel = LogLevel(record["level"]["name"])
timestamp: float = record["time"]["timestamp"]
name: str = "firewhale"
message: str = record["message"]
Expand All @@ -148,13 +152,16 @@ def log_sink(log: str):
LogLevel.ERROR: "red",
}[level]

table = Table().grid()
table = Table.grid()
table.add_row(
Padding(time, (0, 1, 0, 0)),
Padding(f"[{level_color}]{level}[/]", (0, 8 - len(level), 0, 0)),
Padding(name, (0, 3, 0, 0)),
message,
)

console = Console(stderr=True, width=16 * 1024)
console = Console(stderr=True, width=10**100)
console.print(table)

if level is LogLevel.ERROR:
sys.exit(1)
20 changes: 14 additions & 6 deletions firewhale/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,25 @@ def _start():
)

try:
httpx.post(
resp = httpx.post(
f"http://{settings.caddy_admin_address}/load",
headers={"Content-Type": "text/caddyfile"},
content=_internal.generate(),
).raise_for_status()
)
except httpx.ConnectError:
logger.error(
f"There was an error communicating with Caddy's admin API at {settings.caddy_admin_address}. Please restart Firewhale."
f"There was an error communicating with Caddy's admin API at {settings.caddy_admin_address}. "
f"Please restart Firewhale."
)
exit(1)
else:
try:
resp.raise_for_status()
except httpx.HTTPStatusError:
logger.error(
f"Firewhale received an unexpected status code "
f"({resp.status_code} {httpx.codes.get_reason_phrase(resp.status_code)}) while communicating with "
f"Caddy's admin API. Please restart Firewhale."
)

time.sleep(settings.reload_interval_seconds)
logger.info("Reloading configuration")
Expand All @@ -72,11 +81,10 @@ def _start():
def main():
logger.remove()
logger.add(
level=settings.log_level,
level="WARNING" if settings.log_level is LogLevel.WARN else settings.log_level,
sink=_internal.log_sink,
serialize=True,
)
logger.level(LogLevel.WARN, no=logger.level("WARNING").no)


if __name__ == "__main__":
Expand Down
50 changes: 49 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ loguru = "^0.7.2"
pydantic-settings = "^2.3.4"
dict-deep = "^4.1.2"
pendulum = "^3.0.0"
inflect = "^7.3.1"

[tool.poetry.scripts]
firewhale = "firewhale.cli:app"
Expand Down

0 comments on commit 2da72e1

Please sign in to comment.