Skip to content

Commit

Permalink
Fix single-tenant metrics not to filter by tenant (#7385)
Browse files Browse the repository at this point in the history
* Fix single-tenant metrics not to filter by tenant
* Always print all labels in /metrics
  • Loading branch information
fantix authored and msullivan committed Jul 1, 2024
1 parent 6b8143b commit 884ff25
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
4 changes: 0 additions & 4 deletions edb/common/prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,6 @@ def _generate(self, buffer: list[str], **label_filters: str) -> None:
fmt_label = ','.join(
f'{label}="{_format_label_val(label_val)}"'
for label, label_val in zip(self._labels, labels)
if label not in label_filters
)
buffer.append(
f'{self._name}{self._suffix}{{{fmt_label}}} {float(value)}'
Expand All @@ -425,7 +424,6 @@ def _generate(self, buffer: list[str], **label_filters: str) -> None:
fmt_label = ','.join(
f'{label}="{_format_label_val(label_val)}"'
for label, label_val in zip(self._labels, labels)
if label not in label_filters
)
buffer.append(
f'{self._name}_created{{{fmt_label}}} {float(value)}'
Expand Down Expand Up @@ -618,7 +616,6 @@ def _generate(self, buffer: list[str], **label_filters: str) -> None:
fmt_label = ','.join(
f'{label}="{_format_label_val(label_val)}"'
for label, label_val in zip(self._labels, labels)
if label not in label_filters
)
accum = 0.0
for buck, val in zip(self._buckets, values[1]): # type: ignore
Expand Down Expand Up @@ -648,7 +645,6 @@ def _generate(self, buffer: list[str], **label_filters: str) -> None:
fmt_label = ','.join(
f'{label}="{_format_label_val(label_val)}"'
for label, label_val in zip(self._labels, labels)
if label not in label_filters
)
buffer.append(
f'{self._name}_created{{{fmt_label}}} {float(value)}'
Expand Down
3 changes: 2 additions & 1 deletion edb/server/protocol/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

from edb import errors
from edb.server import metrics
from edb.server import server

from edb.common import debug
from edb.common import markup
Expand All @@ -32,7 +33,7 @@ async def handle_request(
tenant,
):
try:
if tenant is None:
if tenant is None or isinstance(tenant.server, server.Server):
output = metrics.registry.generate()
else:
output = metrics.registry.generate(
Expand Down
23 changes: 17 additions & 6 deletions edb/testbase/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,16 +660,23 @@ def _shutdown_cluster(cluster, *, destroy=True):
cluster.destroy()


def _fetch_metrics(host: str, port: int) -> str:
return _call_system_api(host, port, '/metrics', return_json=False)
def _fetch_metrics(host: str, port: int, sslctx=None) -> str:
return _call_system_api(
host, port, '/metrics', return_json=False, sslctx=sslctx
)


def _fetch_server_info(host: str, port: int) -> dict[str, Any]:
return _call_system_api(host, port, '/server-info')


def _call_system_api(host: str, port: int, path: str, return_json=True):
con = http.client.HTTPConnection(host, port)
def _call_system_api(
host: str, port: int, path: str, return_json=True, sslctx=None
):
if sslctx is None:
con = http.client.HTTPConnection(host, port)
else:
con = http.client.HTTPSConnection(host, port, context=sslctx)
con.connect()
try:
con.request(
Expand Down Expand Up @@ -773,7 +780,9 @@ def fetch_metrics(cls) -> str:
assert cls.cluster is not None
conargs = cls.cluster.get_connect_args()
host, port = conargs['host'], conargs['port']
return _fetch_metrics(host, port)
ctx = ssl.create_default_context()
ctx.load_verify_locations(conargs['tls_ca_file'])
return _fetch_metrics(host, port, sslctx=ctx)

@classmethod
def get_connect_args(
Expand Down Expand Up @@ -2060,7 +2069,9 @@ def get_connect_args(self, **kwargs) -> dict[str, str | int]:
return conn_args

def fetch_metrics(self) -> str:
return _fetch_metrics(self.host, self.port)
ctx = ssl.create_default_context()
ctx.load_verify_locations(self.tls_cert_file)
return _fetch_metrics(self.host, self.port, sslctx=ctx)

def fetch_server_info(self) -> dict[str, Any]:
return _fetch_server_info(self.host, self.port)
Expand Down
5 changes: 5 additions & 0 deletions tests/test_server_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,11 @@ async def test_server_compiler_pool_with_server(self):
self.assertEqual(sd.call_system_api('/server/status/ready'), 'OK')
pid1, pid2 = await self._get_worker_pids(sd)

data = sd.fetch_metrics()
self.assertRegex(
data, r'\nedgedb_server_compiler_processes_current 2.0\n'
)

# Terminate one worker, the server is still OK
self._kill_and_wait(pid1)
self.assertEqual(sd.call_system_api('/server/status/ready'), 'OK')
Expand Down

0 comments on commit 884ff25

Please sign in to comment.