Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions systemd/torch.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[Unit]
Description=Torch prometheus proxy

[Service]
ExecStart=/usr/bin/python -m torch
User=torch
Restart=always

# Sandboxing:
PrivateTmp=true
DevicePolicy=closed
ProtectSystem=full
ProtectHome=true
InaccessibleDirectories=/var /sysroot /run
ReadOnlyDirectories=/
PrivateNetwork=true
NoNewPrivileges=true

[Install]
WantedBy=multi-user.target
8 changes: 8 additions & 0 deletions systemd/torch.socket
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[Unit]
Description=Torch prometheus proxy socket

[Socket]
ListenStream=/run/torch.sock

[Install]
WantedBy=sockets.target
10 changes: 8 additions & 2 deletions torch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,25 @@ def main():
import os
from datetime import timedelta
from gevent import wsgi
import gevent.socket as socket
from .collector import PrometheusMetricCollector
class QuietWSGIHandler(wsgi.WSGIHandler):
"""WSGIHandler subclass that will not create an access log"""
def log_request(self, *args):
pass

port = int(os.environ['SERVICE_PORT'])
if os.environ.get('LISTEN_FDS') == '1' and \
os.environ.get('LISTEN_PID') == str(os.getpid()):
listener = socket.fromfd(3, socket.AF_UNIX, socket.SOCK_STREAM)
else:
listener = ('0.0.0.0', int(os.environ['SERVICE_PORT']))

ttl = timedelta(hours=int(os.environ.get('TORCH_TTL', 24)))
metrics_prefix = '/metrics'

application = PrometheusMetricCollector(prefix=metrics_prefix, ttl=ttl)

httpd = wsgi.WSGIServer(('0.0.0.0', port), application, handler_class=QuietWSGIHandler)
httpd = wsgi.WSGIServer(listener, application, handler_class=QuietWSGIHandler)
try:
httpd.serve_forever()
except:
Expand Down