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