Skip to content

Commit

Permalink
Add Monitoring.http_address to expose Prometheus stats server (#993)
Browse files Browse the repository at this point in the history
* Allow stats server to listen on TCP port

This commit allows the stats server to bind to the
port for outside network sources to use (Kubernetes Service).

* Add configuration item to bind monitoring address

PR revision: Instead of exposing the monitoring address we opt
to have users indicate via configuration option
`Monitoring.http_address` the server address to bind to.
  • Loading branch information
coperni authored Sep 9, 2022
1 parent d07f93e commit 66026df
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* Introduced Server.grr_binaries_readonly configuration option (set to False
by default). When set to True, binaries and python hacks can't be overriden
or deleted.
* Added configuration option Monitoring.http_address to specify server address
of stats server. Default value will remain 127.0.0.1.

## 3.4.3.1

Expand Down
5 changes: 4 additions & 1 deletion grr/core/grr_response_core/config/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@
description="The email address to notify in an emergency.",
default="grr-emergency@localhost"))

config_lib.DEFINE_string("Monitoring.http_address", "::1",
"IP address for stats monitoring server.")

config_lib.DEFINE_integer("Monitoring.http_port", 0,
"Port for stats monitoring server.")
"Port for stats monitoring server.")

config_lib.DEFINE_integer(
"Monitoring.http_port_max", None,
Expand Down
4 changes: 3 additions & 1 deletion grr/server/grr_response_server/base_stats_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ class BaseStatsServer(metaclass=abc.ABCMeta):
port: The TCP port that the server should listen to.
"""

def __init__(self, port):
def __init__(self, address, port):
"""Instantiates a new BaseStatsServer.
Args:
address: The IP address of the server to bind.
port: The TCP port that the server should listen to.
"""
self.address = address
self.port = port

@abc.abstractmethod
Expand Down
13 changes: 8 additions & 5 deletions grr/server/grr_response_server/stats_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,21 @@ class IPv6HTTPServer(http_server.HTTPServer):
class StatsServer(base_stats_server.BaseStatsServer):
"""A statistics server that exposes a minimal, custom /varz route."""

def __init__(self, port):
def __init__(self, address, port):
"""Instantiates a new StatsServer.
Args:
address: The IP address of the server to bind.
port: The TCP port that the server should listen to.
"""
super().__init__(port)
super().__init__(address, port)
self._http_server = None
self._server_thread = None

def Start(self):
"""Start HTTPServer."""
try:
self._http_server = IPv6HTTPServer(("::1", self.port), StatsServerHandler)
self._http_server = IPv6HTTPServer((self.address, self.port), StatsServerHandler)
except socket.error as e:
if e.errno == errno.EADDRINUSE:
raise base_stats_server.PortInUseError(self.port)
Expand Down Expand Up @@ -72,6 +73,8 @@ def InitializeStatsServerOnce():
a default one.
"""

address = config.CONFIG["Monitoring.http_address"]

# Figure out which port to use.
port = config.CONFIG["Monitoring.http_port"]
if not port:
Expand Down Expand Up @@ -99,8 +102,8 @@ def InitializeStatsServerOnce():

for port in range(port, max_port + 1):
try:
logging.info("Starting monitoring server on port %d.", port)
server_obj = server_cls(port)
logging.info("Starting monitoring server on address %s and port %d.", address, port)
server_obj = server_cls(address, port)
server_obj.Start()
return
except base_stats_server.PortInUseError as e:
Expand Down
4 changes: 2 additions & 2 deletions grr/server/grr_response_server/stats_server_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class StatsServerTest(base_stats_server_test.StatsServerTestMixin,
test_lib.GRRBaseTest):

def setUpStatsServer(self, port):
return stats_server.StatsServer(port)
return stats_server.StatsServer("::1", port)

def testPrometheusIntegration(self):
registry = prometheus_client.CollectorRegistry(auto_describe=True)
Expand All @@ -38,7 +38,7 @@ def testPrometheusIntegration(self):

with mock.patch.object(stats_server.StatsServerHandler, "registry",
registry):
server = stats_server.StatsServer(port)
server = stats_server.StatsServer("::1", port)
server.Start()
self.addCleanup(server.Stop)
res = requests.get("http://[::1]:{}/metrics".format(port))
Expand Down

0 comments on commit 66026df

Please sign in to comment.