Skip to content

Commit

Permalink
Fixed #27: enable all services log to file for metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
rgaudin committed Dec 19, 2023
1 parent 61fe6e3 commit a0c0c24
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions reverse-proxy/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ COPY Caddyfile /etc/caddy/
COPY gen-caddyfile.py /src/

ENV FQDN "generic.hotspot"
ENV METRICS_LOGS_DIR "/var/log/metrics"

# store python bytecode in image
RUN python3 -m compileall /src/gen-caddyfile.py && mv /src/__pycache__/*.pyc /usr/local/lib/
Expand Down
20 changes: 20 additions & 0 deletions reverse-proxy/gen-caddyfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import dataclasses
import os
import pathlib
import subprocess
import sys
import traceback
Expand Down Expand Up @@ -100,6 +101,17 @@ def should_protect(self) -> bool:
local_certs
skip_install_trust
{% if debug %}debug{% endif %}
log metrics {
include http.log.access
output file {$METRICS_LOGS_DIR}/metrics.json {
roll_size 1MiB
roll_keep 2
roll_uncompressed
roll_keep_for 48h
}
format json
}
}
{% if with_contentfilter %}
Expand All @@ -111,6 +123,7 @@ def should_protect(self) -> bool:
# home page on domain, with prefix redirects
{$FQDN}:80, {$FQDN}:443 {
tls internal
log
{% if services %}
# redirect to services (convenience only){% for service in services.values() %}
Expand All @@ -134,6 +147,7 @@ def should_protect(self) -> bool:
{% for service in services.values() %}
{{service.name}}.{$FQDN}:80, {{service.name}}.{$FQDN}:443 {
tls internal
log
{% if service.should_protect %}
basicauth * {
Expand All @@ -156,6 +170,7 @@ def should_protect(self) -> bool:
{% for subdomain, folder in files_map.items() %}
{{subdomain}}.{$FQDN}:80, {{subdomain}}.{$FQDN}:443 {
tls internal
log
reverse_proxy files:80
rewrite * /{{folder}}/{path}?{query}
handle_errors {
Expand All @@ -169,6 +184,7 @@ def should_protect(self) -> bool:
# fallback for unhandled names/IP arriving here
:80, :443 {
tls internal
log
respond "Not Found! Oops" 404
}
Expand Down Expand Up @@ -197,6 +213,10 @@ def gen_caddyfile():


if __name__ == "__main__":
# create metrics logs dir on start in case it doesnt exist. (corner case as this
# would generally be mounted and thus created by Docker and caddy will ultimately
# create it upon first request)
pathlib.Path(os.getenv("METRICS_LOGS_DIR", "")).mkdir(parents=True, exist_ok=True)
gen_caddyfile()

if debug:
Expand Down

0 comments on commit a0c0c24

Please sign in to comment.