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
@@ -104,25 +118,19 @@ def SetupPrometheusExportsFromConfig():
104
118
port = getattr (settings , "PROMETHEUS_METRICS_EXPORT_PORT" , None )
105
119
port_range = getattr (settings , "PROMETHEUS_METRICS_EXPORT_PORT_RANGE" , None )
106
120
addr = getattr (settings , "PROMETHEUS_METRICS_EXPORT_ADDRESS" , "" )
121
+ registry = GetRegistry ()
107
122
if port_range :
108
- SetupPrometheusEndpointOnPortRange (port_range , addr )
123
+ SetupPrometheusEndpointOnPortRange (registry , port_range , addr )
109
124
elif port :
110
- SetupPrometheusEndpointOnPort (port , addr )
125
+ SetupPrometheusEndpointOnPort (registry , port , addr )
111
126
112
127
113
128
def ExportToDjangoView (request ):
114
129
"""Exports /metrics as a Django view.
115
130
116
131
You can use django_prometheus.urls to map /metrics to this view.
117
132
"""
118
- if (
119
- "PROMETHEUS_MULTIPROC_DIR" in os .environ
120
- or "prometheus_multiproc_dir" in os .environ
121
- ):
122
- registry = prometheus_client .CollectorRegistry ()
123
- multiprocess .MultiProcessCollector (registry )
124
- else :
125
- registry = prometheus_client .REGISTRY
133
+ registry = GetRegistry ()
126
134
metrics_page = prometheus_client .generate_latest (registry )
127
135
return HttpResponse (
128
136
metrics_page , content_type = prometheus_client .CONTENT_TYPE_LATEST
0 commit comments