Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Greenhost
isitup
Commits
55fb9ec2
Commit
55fb9ec2
authored
Jan 20, 2021
by
Mark Swillus
Browse files
Add url and change to POST
parent
47484cbc
Pipeline
#5972
passed with stage
in 37 seconds
Changes
4
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
isitup/__init__.py
View file @
55fb9ec2
...
...
@@ -69,7 +69,7 @@ async def all_url_checks(check: Union[http.CheckUrl, dns.CheckDomain]) -> list[d
return
results
@
app
.
ge
t
(
"/check/prometheus"
)
async
def
prometheus_checks
()
->
None
:
@
app
.
pos
t
(
"/check/prometheus"
)
async
def
prometheus_checks
(
prometheusUrl
:
prometheus
.
PrometheusUrl
)
->
None
:
checker
=
retrieve_check
(
"prometheus"
)
return
await
checker
()
return
await
checker
(
prometheusUrl
)
isitup/prometheus.py
View file @
55fb9ec2
async
def
check_prometheus
()
->
None
:
import
httpx
import
pydantic
from
httpx
import
AsyncClient
from
isitup.checks
import
CheckErrorException
class
PrometheusUrl
(
pydantic
.
BaseModel
):
prometheusUrl
:
pydantic
.
AnyHttpUrl
=
pydantic
.
Field
(
...,
title
=
"Prometheus Url"
,
description
=
"Prometheus server to query"
)
class
Config
:
schema_extra
=
{
"example"
:
{
"PrometheusUrl"
:
"https://prometheus.example.com"
,}}
async
def
check_prometheus
(
prometheusUrl
:
PrometheusUrl
)
->
None
:
healthy
=
await
prometheus_is_healthy
(
prometheusUrl
.
prometheusUrl
)
if
not
healthy
:
raise
CheckErrorException
(
status_code
=
502
,
detail
=
"Prometheus is not reachable"
)
return
1
def
get_client
():
return
AsyncClient
()
async
def
prometheus_is_healthy
(
url
:
str
)
->
bool
:
healthcheck_url
=
url
+
"/-/healthy"
ready_url
=
url
+
"/-/ready"
async
with
get_client
()
as
client
:
response
=
await
client
.
get
(
healthcheck_url
)
if
response
.
status_code
==
200
and
b
"Prometheus is Healthy."
in
response
.
read
():
response
=
await
client
.
get
(
ready_url
)
if
(
response
.
status_code
==
200
and
b
"Prometheus is Ready."
in
response
.
read
()
):
return
True
return
False
def
register
(
register_callback
:
callable
)
->
None
:
register_callback
(
check_prometheus
,
"prometheus"
)
requirements-dev.txt
View file @
55fb9ec2
...
...
@@ -3,7 +3,9 @@ black==20.8b1
asgi_lifespan
pytest==6.2.1
pytest-asyncio
pytest-httpserver
pytest-mock
pytest-raises
pre-commit
pytest-watch
pytest-cov
...
...
tests/test_api.py
View file @
55fb9ec2
import
re
from
unittest
import
mock
import
pytest
from
httpx
import
AsyncClient
from
pytest_httpserver
import
HTTPServer
from
pytest_mock
import
MockerFixture
from
starlette.status
import
(
HTTP_200_OK
,
...
...
@@ -10,6 +12,8 @@ from starlette.status import (
)
import
isitup
from
isitup.checks
import
CheckErrorException
from
isitup.prometheus
import
PrometheusUrl
,
check_prometheus
,
prometheus_is_healthy
class
TestHttpChecks
:
...
...
@@ -59,10 +63,20 @@ class TestDnsChecks:
class
TestPrometheusChecks
:
@
pytest
.
mark
.
asyncio
async
def
test_check_prometheus_is_reachable
(
self
,
api
:
AsyncClient
,
base_url
:
str
async
def
test_check_prometheus_requires_post
(
self
,
api
:
AsyncClient
,
httpserver
:
HTTPServer
,
mocker
:
MockerFixture
,
base_url
:
str
,
)
->
None
:
res
=
await
api
.
get
(
"/check/prometheus/"
)
assert
res
.
status_code
==
405
mock_function
=
mock
.
AsyncMock
(
return_value
=
True
)
mocker
.
patch
(
"isitup.prometheus.prometheus_is_healthy"
,
mock_function
)
res
=
await
api
.
post
(
"/check/prometheus/"
,
json
=
{
"prometheusUrl"
:
"https://ignored.net"
}
)
assert
res
.
status_code
==
200
@
pytest
.
mark
.
asyncio
...
...
@@ -71,5 +85,34 @@ class TestPrometheusChecks:
)
->
None
:
mock_function
=
mock
.
AsyncMock
()
mocker
.
patch
(
"isitup.prometheus.check_prometheus"
,
mock_function
)
res
=
await
api
.
ge
t
(
"/check/prometheus/"
)
res
=
await
api
.
pos
t
(
"/check/prometheus/"
)
mock_function
.
call_count
==
1
@
pytest
.
mark
.
asyncio
async
def
test_prometheus_check_function_executes_healthcheck
(
self
,
api
:
AsyncClient
,
httpserver
:
HTTPServer
,
base_url
:
str
)
->
None
:
health
=
await
prometheus_is_healthy
(
httpserver
.
url_for
(
""
))
# returns 500
assert
health
==
False
httpserver
.
expect_request
(
"/-/healthy"
).
respond_with_data
(
"Prometheus is Healthy."
)
httpserver
.
expect_oneshot_request
(
"/-/ready"
).
respond_with_data
(
"Prometheus is Ready."
)
health
=
await
prometheus_is_healthy
(
httpserver
.
url_for
(
""
))
assert
health
==
True
health
=
await
prometheus_is_healthy
(
httpserver
.
url_for
(
""
)
)
# ready returns 500 now
assert
health
==
False
@
pytest
.
mark
.
asyncio
@
pytest
.
mark
.
raises
(
exception
=
CheckErrorException
)
async
def
test_check_prometheus_raises_exception_when_prometheus_not_healthy
(
self
,
api
:
AsyncClient
,
mocker
:
MockerFixture
,
base_url
:
str
)
->
None
:
mocker
.
patch
(
"isitup.prometheus.prometheus_is_healthy"
,
return_value
=
False
)
ignored
=
PrometheusUrl
ignored
.
prometheusUrl
=
"ignored"
await
check_prometheus
(
ignored
)
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment