Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ dist
docs/_build/
htmlcov
pip-wheel-metadata
.vscode
.venv
6 changes: 6 additions & 0 deletions src/kubernetes_wsgi/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ def parse_args(argv: Sequence[Text]) -> Dict[str, Any]:
default=20,
help="Maximum number of threads to run",
)
parser.add_argument(
"--hide-server",
action="store_true",
help="Hides server version in HTTP header"
)
args = parser.parse_args(argv)
return {
"application": args.application,
Expand All @@ -68,6 +73,7 @@ def parse_args(argv: Sequence[Text]) -> Dict[str, Any]:
"health_check_path": args.health_check_path,
"min_threads": args.min_threads,
"max_threads": args.max_threads,
"hide_server": args.hide_server,
}


Expand Down
16 changes: 13 additions & 3 deletions src/kubernetes_wsgi/server.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import logging


# This is a fake import, only used during type checking.
from typing import TYPE_CHECKING, Callable

from prometheus_client import REGISTRY # type: ignore
from prometheus_client.twisted import MetricsResource # type: ignore
from twisted import logger # type: ignore
Expand All @@ -12,12 +12,12 @@
from twisted.logger import STDLibLogObserver # type: ignore
from twisted.python import threadpool # type: ignore
from twisted.web.http import Request, proxiedLogFormatter # type: ignore
import twisted.web.http

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appears to be unused.

from twisted.web.server import Site # type: ignore
from twisted.web.wsgi import WSGIResource # type: ignore

from .metrics import TwistedThreadPoolCollector


if TYPE_CHECKING:
from wsgiref.types import WSGIApplication

Expand All @@ -28,14 +28,21 @@
class KubernetesWSGISite(Site):
"""Extension to Site to ignore heath checks for access logging."""

def __init__(self, health_check_path: str, *args, **kwargs):
def __init__(self, health_check_path: str, hide_server: bool = False, *args, **kwargs):
self.hide_server = hide_server
self.__health_check_path = health_check_path
super().__init__(*args, **kwargs)

def log(self, request):
path = request.path.decode()
if path != self.__health_check_path:
return super().log(request)

def getResourceFor(self, request):
if self.hide_server:
request.setHeader(b'server', b"")

return super().getResourceFor(request)


class MetricsSite(Site):
Expand All @@ -53,6 +60,7 @@ def serve(
health_check_path: str = "/healthz",
min_threads: int = 5,
max_threads: int = 20,
hide_server: bool = True,
):
# Quiet the Twisted factory logging.
Factory.noisy = False
Expand All @@ -76,6 +84,7 @@ def serve(
port,
access_log_formatter,
health_check_path,
hide_server,
)
_listen_metrics(reactor, metrics_port)

Expand All @@ -96,6 +105,7 @@ def _listen_wsgi(
port: int,
access_log_formatter: LogFormatter,
health_check_path: str,
hide_server: bool,
) -> None:
"""Listen for the WSGI application."""
wsgi_resource = WSGIResource(reactor, pool, application)
Expand Down