18
18
logger = logging .getLogger (__name__ )
19
19
20
20
21
- def SetupPrometheusEndpointOnPort (port , addr = "" ):
21
+ def GetRegistry ():
22
+ if (
23
+ "PROMETHEUS_MULTIPROC_DIR" in os .environ
24
+ or "prometheus_multiproc_dir" in os .environ
25
+ ):
26
+ registry = prometheus_client .CollectorRegistry ()
27
+ multiprocess .MultiProcessCollector (registry )
28
+ else :
29
+ registry = prometheus_client .REGISTRY
30
+ return registry
31
+
32
+
33
+ def SetupPrometheusEndpointOnPort (registry , port , addr = "" ):
22
34
"""Exports Prometheus metrics on an HTTPServer running in its own thread.
23
35
24
36
The server runs on the given port and is by default listenning on
@@ -42,7 +54,7 @@ def SetupPrometheusEndpointOnPort(port, addr=""):
42
54
"autoreloader is active. Use the URL exporter, or start django "
43
55
"with --noreload. See documentation/exports.md."
44
56
)
45
- prometheus_client .start_http_server (port , addr = addr )
57
+ prometheus_client .start_http_server (port , addr = addr , registry = registry )
46
58
47
59
48
60
class PrometheusEndpointServer (threading .Thread ):
@@ -56,7 +68,7 @@ def run(self):
56
68
self .httpd .serve_forever ()
57
69
58
70
59
- def SetupPrometheusEndpointOnPortRange (port_range , addr = "" ):
71
+ def SetupPrometheusEndpointOnPortRange (registry , port_range , addr = "" ):
60
72
"""Like SetupPrometheusEndpointOnPort, but tries several ports.
61
73
62
74
This is useful when you're running Django as a WSGI application
@@ -82,8 +94,10 @@ def SetupPrometheusEndpointOnPortRange(port_range, addr=""):
82
94
"with --noreload. See documentation/exports.md."
83
95
)
84
96
for port in port_range :
97
+ handler = prometheus_client .MetricsHandler
98
+ handler .registry = registry
85
99
try :
86
- httpd = HTTPServer ((addr , port ), prometheus_client . MetricsHandler )
100
+ httpd = HTTPServer ((addr , port ), handler )
87
101
except OSError :
88
102
# Python 2 raises socket.error, in Python 3 socket.error is an
89
103
# alias for OSError
@@ -102,21 +116,18 @@ def SetupPrometheusExportsFromConfig():
102
116
port = getattr (settings , "PROMETHEUS_METRICS_EXPORT_PORT" , None )
103
117
port_range = getattr (settings , "PROMETHEUS_METRICS_EXPORT_PORT_RANGE" , None )
104
118
addr = getattr (settings , "PROMETHEUS_METRICS_EXPORT_ADDRESS" , "" )
119
+ registry = GetRegistry ()
105
120
if port_range :
106
- SetupPrometheusEndpointOnPortRange (port_range , addr )
121
+ SetupPrometheusEndpointOnPortRange (registry , port_range , addr )
107
122
elif port :
108
- SetupPrometheusEndpointOnPort (port , addr )
123
+ SetupPrometheusEndpointOnPort (registry , port , addr )
109
124
110
125
111
126
def ExportToDjangoView (request ):
112
127
"""Exports /metrics as a Django view.
113
128
114
129
You can use django_prometheus.urls to map /metrics to this view.
115
130
"""
116
- if "PROMETHEUS_MULTIPROC_DIR" in os .environ or "prometheus_multiproc_dir" in os .environ :
117
- registry = prometheus_client .CollectorRegistry ()
118
- multiprocess .MultiProcessCollector (registry )
119
- else :
120
- registry = prometheus_client .REGISTRY
131
+ registry = GetRegistry ()
121
132
metrics_page = prometheus_client .generate_latest (registry )
122
133
return HttpResponse (metrics_page , content_type = prometheus_client .CONTENT_TYPE_LATEST )
0 commit comments