0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-11-22 17:00:52 +01:00
posthog/gunicorn.config.py
Harry Waye a70b4b28c6
chore(web): add django-prometheus exposed on /_metrics (#11000)
* chore(web): add django-prometheus exposed on /_metrics

This exposes a number of metrics, see
97d5748664/documentation/exports.md
for details. It includes histogram of timings by viewname before and
after middleware.

I'm not particularly interested in these right now, but rather would
like to expose Kafka Producer metrics as per
https://github.com/PostHog/posthog/pull/10997

* Refactor to use gunicorn server hooks

* also add expose to dockerfile

* wip
2022-07-27 20:37:44 +01:00

56 lines
1.5 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
from prometheus_client import CollectorRegistry, multiprocess, start_http_server
loglevel = "error"
keepalive = 120
timeout = 90
grateful_timeout = 120
def on_starting(server):
print(
"""
\x1b[1;34m"""
+ r"""
_____ _ _ _
| __ \ | | | | | |
| |__) |__ ___| |_| |__| | ___ __ _
| ___/ _ \/ __| __| __ |/ _ \ / _` |
| | | (_) \__ \ |_| | | | (_) | (_| |
|_| \___/|___/\__|_| |_|\___/ \__, |
__/ |
|___/
"""
+ """
\x1b[0m
"""
)
print("Server running on \x1b[4mhttp://{}:{}\x1b[0m".format(*server.address[0]))
print("Questions? Please shoot us an email at \x1b[4mhey@posthog.com\x1b[0m")
print("\nTo stop, press CTRL + C")
def when_ready(server):
"""
To ease being able to hide the /metrics endpoint when running in production,
we serve the metrics on a separate port, using the
prometheus_client.multiprocess Collector to pull in data from the worker
processes.
"""
registry = CollectorRegistry()
multiprocess.MultiProcessCollector(registry)
port = int(os.environ.get("PROMETHEUS_METRICS_EXPORT_PORT", 8001))
start_http_server(port=port, registry=registry)
def worker_exit(server, worker):
"""
Ensure that we mark workers as dead with the prometheus_client such that
any cleanup can happen.
"""
multiprocess.mark_process_dead(worker.pid)