Skip to content

Commit dfe5ba3

Browse files
taimoorzaeemsteve-chavez
authored andcommitted
fix(admin): metrics endpoint not responding with Content-Type header
The prometheus metrics text format requires `Content-Type` header for correct scraping which fails otherwise. Closes #4271. Signed-off-by: Taimoor Zaeem <taimoorzaeem@gmail.com>
1 parent 7657607 commit dfe5ba3

File tree

4 files changed

+9
-1
lines changed

4 files changed

+9
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1818
- Fix OpenAPI broken docs link by @taimoorzaeem in #4080
1919
- Fix OpenAPI specification incorrectly exposing GET methods for volatile functions by @joelonsql in #4174
2020
- Fix empty spread embeddings return unexpected SQL error by @taimoorzaeem in #3887
21+
- Fix `/metrics` endpoint not responding with `Content-Type` header by @taimoorzaeem in #4271
2122

2223
### Changed
2324

docs/references/observability.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ The ``metrics`` endpoint on the :ref:`admin_server` endpoint provides metrics in
128128
129129
curl "http://localhost:3001/metrics"
130130
131+
.. code-block:: http
132+
133+
HTTP/1.1 200 OK
134+
Content-Type: text/plain; charset=utf-8
135+
131136
# HELP pgrst_schema_cache_query_time_seconds The query time in seconds of the last schema cache load
132137
# TYPE pgrst_schema_cache_query_time_seconds gauge
133138
pgrst_schema_cache_query_time_seconds 1.5937927e-2

src/PostgREST/Admin.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import Network.Socket.ByteString
1616

1717
import PostgREST.AppState (AppState)
1818
import PostgREST.Config (AppConfig (..))
19+
import PostgREST.MediaType (MediaType (..), toContentType)
1920
import PostgREST.Metrics (metricsToText)
2021
import PostgREST.Network (resolveHost)
2122
import PostgREST.Observation (Observation (..))
@@ -58,7 +59,7 @@ admin appState req respond = do
5859
respond $ Wai.responseLBS HTTP.status200 [] (maybe mempty JSON.encode sCache)
5960
["metrics"] -> do
6061
mets <- metricsToText
61-
respond $ Wai.responseLBS HTTP.status200 [] mets
62+
respond $ Wai.responseLBS HTTP.status200 [toContentType MTTextPlain] mets -- Content-Type is required for prometheus compliance
6263
_ ->
6364
respond $ Wai.responseLBS HTTP.status404 [] mempty
6465

test/io/test_io.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,6 +1730,7 @@ def test_admin_metrics(defaultenv):
17301730
with run(env=defaultenv, port=freeport()) as postgrest:
17311731
response = postgrest.admin.get("/metrics")
17321732
assert response.status_code == 200
1733+
assert response.headers["Content-Type"] == "text/plain; charset=utf-8"
17331734
assert "pgrst_schema_cache_query_time_seconds" in response.text
17341735
assert 'pgrst_schema_cache_loads_total{status="SUCCESS"}' in response.text
17351736
assert "pgrst_db_pool_max" in response.text

0 commit comments

Comments
 (0)