8000 Add separate handling of the /favicon.ico path in the default wsgi im… · ethervoid/client_python@5dffc77 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5dffc77

Browse files
ngehrsitzNorman Gehrsitz
and
Norman Gehrsitz
authored
Add separate handling of the /favicon.ico path in the default wsgi implementation (prometheus#631)
Signed-off-by: Norman Gehrsitz <gehrsitz.norman@student.dhbw-kahlsruhe.de> Co-authored-by: Norman Gehrsitz <gehrsitz.norman@student.dhbw-kahlsruhe.de>
1 parent 5e3674a commit 5dffc77

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

prometheus_client/exposition.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,14 @@ def prometheus_app(environ, start_response):
113113
# Prepare parameters
114114
accept_header = environ.get('HTTP_ACCEPT')
115115
params = parse_qs(environ.get('QUERY_STRING', ''))
116-
# Bake output
117-
status, header, output = _bake_output(registry, accept_header, params)
116+
if environ['PATH_INFO'] == '/favicon.ico':
117+
# Serve empty response for browsers
118+
status = '200 OK'
119+
header = ('', '')
120+
output = b''
121+
else:
122+
# Bake output
123+
status, header, output = _bake_output(registry, accept_header, params)
118124
# Return output
119125
start_response(status, [header])
120126
return [output]

tests/test_wsgi.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from __future__ import absolute_import, unicode_literals
22

3+
import sys
4+
import unittest
35
from unittest import TestCase
46
from wsgiref.util import setup_testing_defaults
57

68
from prometheus_client import CollectorRegistry, Counter, make_wsgi_app
7-
from prometheus_client.exposition import CONTENT_TYPE_LATEST
9+
from prometheus_client.exposition import _bake_output, CONTENT_TYPE_LATEST
810

911

1012
class WSGITest(TestCase):
@@ -65,3 +67,22 @@ def test_report_metrics_3(self):
6567

6668
def test_report_metrics_4(self):
6769
self.validate_metrics("failed_requests", "Number of failed requests", 7)
70+
71+
@unittest.skipIf(sys.version_info < (3, 3), "Test requires Python 3.3+.")
72+
def test_favicon_path(self):
73+
from unittest.mock import patch
74+
75+
# Create mock to enable counting access of _bake_output
76+
with patch("prometheus_client.exposition._bake_output", side_effect=_bake_output) as mock:
77+
# Create and run WSGI app
78+
app = make_wsgi_app(self.registry)
79+
# Try accessing the favicon path
80+
favicon_environ = dict(self.environ)
81+
favicon_environ['PATH_INFO'] = '/favicon.ico'
82+
outputs = app(favicon_environ, self.capture)
83+
# Test empty response
84+
self.assertEqual(outputs, [b''])
85+
self.assertEqual(mock.call_count, 0)
86+
# Try accessing normal paths
87+
app(self.environ, self.capture)
88+
self.assertEqual(mock.call_count, 1)

0 commit comments

Comments
 (0)
0