Skip to content

Commit 2089fdc

Browse files
ref(api): add server settings, allow self-hosted overrides through env (#7735)
SSIA Specifically, allow API server configuration through settings: - number of workers - number of threads - workers lifetime - workers max rss This is to allow self-hosted deployments to configure snuba-api server through env --------- Co-authored-by: getsantry[bot] <66042841+getsantry[bot]@users.noreply.github.com>
1 parent e965a53 commit 2089fdc

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

snuba/cli/api.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@
1111
@click.option("--bind", help="Address to listen on.")
1212
@click.option("--debug", is_flag=True)
1313
@click.option("--log-level", help="Logging level to use.")
14-
@click.option("--processes", type=click.IntRange(1), default=1)
14+
@click.option("--processes", type=click.IntRange(1))
1515
@click.option("--threads", type=click.IntRange(1))
16-
@click.option("--backlog", type=click.IntRange(128), default=128)
16+
@click.option("--backlog", type=click.IntRange(128))
1717
def api(
1818
*,
1919
bind: Optional[str],
2020
debug: bool,
2121
log_level: Optional[str],
22-
processes: int,
22+
processes: Optional[int],
2323
threads: Optional[int],
24-
backlog: int,
24+
backlog: Optional[int],
2525
) -> None:
2626
from snuba import settings
2727

@@ -35,6 +35,9 @@ def api(
3535
else:
3636
host, port = settings.HOST, settings.PORT
3737

38+
processes = processes or settings.API_WORKERS or 1
39+
threads = threads or settings.API_THREADS
40+
3841
if debug:
3942
if processes > 1 or (threads or 1) > 1:
4043
raise click.ClickException("processes/threads can only be 1 in debug")
@@ -51,11 +54,17 @@ def api(
5154
if log_level:
5255
os.environ["LOG_LEVEL"] = log_level
5356

57+
lifetime = settings.API_WORKERS_LIFETIME
58+
max_rss = settings.API_WORKERS_MAX_RSS
59+
backlog = backlog or max(128, 64 * processes)
60+
5461
server.serve(
5562
"snuba.web.wsgi:application",
5663
f"{host}:{port}",
5764
processes=processes,
5865
threads=threads,
5966
backlog=backlog,
67+
lifetime=lifetime,
68+
max_rss=max_rss,
6069
name="snuba-api",
6170
)

snuba/settings/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,19 @@
7070
# End Admin Settings #
7171
######################
7272

73+
################
74+
# Api Settings #
75+
################
76+
77+
API_WORKERS = 1
78+
API_THREADS = None
79+
API_WORKERS_LIFETIME = None
80+
API_WORKERS_MAX_RSS = None
81+
82+
####################
83+
# End Api Settings #
84+
####################
85+
7386
MAX_MIGRATIONS_REVERT_TIME_WINDOW_HRS = 24
7487

7588
ENABLE_DEV_FEATURES = os.environ.get("ENABLE_DEV_FEATURES", False)

snuba/settings/settings_self_hosted.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@
1212
VALID_RETENTION_DAYS = set([int(env("SENTRY_EVENT_RETENTION_DAYS", 90)), 30, 60])
1313
LOWER_RETENTION_DAYS = min(DEFAULT_RETENTION_DAYS, 30)
1414

15+
API_WORKERS = int(env("SNUBA_API_WORKERS", 1))
16+
API_THREADS = int(env("SNUBA_API_THREADS", 8))
17+
API_WORKERS_LIFETIME = (
18+
int(env("SNUBA_API_WORKERS_LIFETIME")) if env("SNUBA_API_WORKERS_LIFETIME") else None # type: ignore
19+
)
20+
API_WORKERS_MAX_RSS = (
21+
int(env("SNUBA_API_WORKERS_MAX_RSS")) if env("SNUBA_API_WORKERS_MAX_RSS") else None # type: ignore
22+
)
23+
1524
REDIS_HOST = env("REDIS_HOST", "127.0.0.1")
1625
REDIS_PORT = int(env("REDIS_PORT", 6379))
1726
REDIS_PASSWORD = env("REDIS_PASSWORD")

snuba/utils/server.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def serve(
1515
reload: bool = False,
1616
name: str | None = None,
1717
lifetime: int | None = None,
18+
max_rss: int | None = None,
1819
) -> None:
1920
host, port = bind.rsplit(":", maxsplit=1)
2021
server = Granian(
@@ -25,6 +26,7 @@ def serve(
2526
backlog=backlog,
2627
workers=processes,
2728
workers_lifetime=lifetime,
29+
workers_max_rss=max_rss,
2830
workers_kill_timeout=30,
2931
blocking_threads=threads,
3032
respawn_failed_workers=True,

0 commit comments

Comments
 (0)