Skip to content

Commit 09d69bb

Browse files
authored
Merge pull request #19 from SolarEdgeTech/amenezes/aiohttp
Add support for monitoring aiohttp based applications
2 parents b7a43ac + 93931ee commit 09d69bb

File tree

20 files changed

+1072
-113
lines changed

20 files changed

+1072
-113
lines changed

.github/workflows/python_package_build.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ jobs:
1515
steps:
1616
- uses: actions/checkout@v2
1717
- run: make bootstrap
18-
- run: poetry update -vvv
1918
- run: poetry build -vvv
20-
- run: poetry install --extras flask --extras fastapi --extras db --extras redis
19+
- run: poetry install --extras flask --extras fastapi --extras aiohttp --extras db --extras redis
2120
- run: make coverage
2221
- uses: actions/upload-artifact@v2
2322
with:

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ bootstrap:
1616
check: pylint mypy
1717

1818
test:
19-
poetry run pytest --log-cli-level=4 --tb=no -v tests
19+
poetry run pytest --log-cli-level=4 -vv tests
2020

2121
coverage:
22-
poetry run pytest --cov-report xml:./coverage.xml --cov-report html --cov-report term --cov=pyctuator --log-cli-level=4 --tb=no -v tests
22+
poetry run pytest --cov-report xml:./coverage.xml --cov-report html --cov-report term --cov=pyctuator --log-cli-level=4 -vv tests
2323

2424
pylint:
2525
poetry run pylint --exit-zero pyctuator tests

README.md

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
Monitor Python web apps using
88
[Spring Boot Admin](https://github.com/codecentric/spring-boot-admin).
99

10-
Pyctuator supports **Flask** and **FastAPI**. **Django** support is planned as well.
10+
Pyctuator supports **Flask**, **FastAPI** and **aiohttp**. **Django** support is planned as well.
1111

1212
The following video shows a FastAPI web app being monitored and controled using Spring Boot Admin.
1313

@@ -63,9 +63,9 @@ It currently supports the following Actuator features:
6363
* **HTTP traces** - Tail recent HTTP requests, including status codes and latency
6464

6565
## Quickstart
66-
The examples below show a minimal integration of **FastAPI** and **Flask** applications with **Pyctuator**.
66+
The examples below show a minimal integration of **FastAPI**, **Flask** and **aiohttp** applications with **Pyctuator**.
6767

68-
After installing Flask/FastAPI and Pyctuator, start by launching a local Spring Boot Admin instance:
68+
After installing Flask/FastAPI/aiohttp and Pyctuator, start by launching a local Spring Boot Admin instance:
6969

7070
```sh
7171
docker run --rm --name spring-boot-admin -p 8080:8080 michayaak/spring-boot-admin:2.2.3-1
@@ -139,6 +139,36 @@ The application will automatically register with Spring Boot Admin upon start up
139139

140140
Log in to the Spring Boot Admin UI at `http://localhost:8080` to interact with the application.
141141

142+
### aiohttp
143+
The following example is complete and should run as is.
144+
145+
```python
146+
from aiohttp import web
147+
from pyctuator.pyctuator import Pyctuator
148+
149+
app = web.Application()
150+
routes = web.RouteTableDef()
151+
152+
@routes.get("/")
153+
def hello():
154+
return web.Response(text="Hello World!")
155+
156+
Pyctuator(
157+
app,
158+
"aiohttp Pyctuator",
159+
app_url="http://host.docker.internal:8888",
160+
pyctuator_endpoint_url="http://host.docker.internal:8888/pyctuator",
161+
registration_url="http://localhost:8080/instances"
162+
)
163+
164+
app.add_routes(routes)
165+
web.run_app(app, port=8888)
166+
```
167+
168+
The application will automatically register with Spring Boot Admin upon start up.
169+
170+
Log in to the Spring Boot Admin UI at `http://localhost:8080` to interact with the application.
171+
142172
### Registration Notes
143173
When registering a service in Spring Boot Admin, note that:
144174
* **Docker** - If the Spring Boot Admin is running in a container while the managed service is running in the docker-host directly, the `app_url` and `pyctuator_endpoint_url` should use `host.docker.internal` as the url's host so Spring Boot Admin will be able to connect to the monitored service.
@@ -279,7 +309,7 @@ To run these examples, you'll need to have Spring Boot Admin running in a local
279309

280310
Unless the example includes a docker-compose file, you'll need to start Spring Boot Admin using docker directly:
281311
```sh
282-
docker run -p 8080:8080 michayaak/spring-boot-admin:2.2.3-1
312+
docker run --rm -p 8080:8080 michayaak/spring-boot-admin:2.2.3-1
283313
```
284314
(the docker image's tag represents the version of Spring Boot Admin, so if you need to use version `2.0.0`, use `michayaak/spring-boot-admin:2.0.0` instead, note it accepts connections on port 8082).
285315

examples/Advanced/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ python = "^3.7"
1111
psutil = { version = "^5.6" }
1212
fastapi = { version = "^0.41.0" }
1313
uvicorn = { version = "^0.9.0" }
14-
pyctuator = { version = "^0.11" }
14+
pyctuator = { version = "^0.12" }
1515
sqlalchemy = { version = "^1.3" }
1616
PyMySQL = { version = "^0.9.3" }
1717
cryptography = { version = "^2.8" }

examples/FastAPI/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This example demonstrates the integration with the [FastAPI](https://fastapi.tia
44
## Running the example
55
1. Start an instance of SBA (Spring Boot Admin):
66
```sh
7-
docker run -p 8082:8082 michayaak/spring-boot-admin:2.2.2
7+
docker run --rm -p 8080:8080 michayaak/spring-boot-admin:2.2.3-1
88
```
99
2. Once Spring Boot Admin is running, you can run the examples as follow:
1010
```sh

examples/FastAPI/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ python = "^3.7"
1111
psutil = { version = "^5.6" }
1212
fastapi = { version = "^0.41.0" }
1313
uvicorn = { version = "^0.9.0" }
14-
pyctuator = { version = "^0.11" }
14+
pyctuator = { version = "^0.12" }
1515

1616
[build-system]
1717
requires = ["poetry>=0.12"]

examples/Flask/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# FastAPI example
1+
# Flask example
22
This example demonstrates the integration with the [Flask](https://flask.palletsprojects.com/) web-framework.
33

44
## Running the example
55
1. Start an instance of SBA (Spring Boot Admin):
66
```sh
7-
docker run -p 8082:8082 michayaak/spring-boot-admin:2.2.2
7+
docker run --rm -p 8080:8080 michayaak/spring-boot-admin:2.2.3-1
88
```
99
2. Once Spring Boot Admin is running, you can run the examples as follow:
1010
```sh
@@ -19,4 +19,4 @@ This example demonstrates the integration with the [Flask](https://flask.pallets
1919
* Note that when Flask debugging is enabled, Pyctuator and Flask are initialized twice because Flask reloads the script. This causes Pyctuator to register twice thus the `startup` time alternates between the time these instances started.
2020
```Python
2121
app.run(port=5000, host="0.0.0.0", debug=True)
22-
```
22+
```

examples/Flask/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ authors = [
1010
python = "^3.7"
1111
psutil = { version = "^5.6" }
1212
flask = { version = "^1.1" }
13-
pyctuator = { version = "^0.11" }
13+
pyctuator = { version = "^0.12" }
1414

1515
[build-system]
1616
requires = ["poetry>=0.12"]

examples/aiohttp/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# aiohttp example
2+
This example demonstrates the integration with the [aiohttp](https://docs.aiohttp.org).
3+
4+
## Running the example
5+
1. Start an instance of SBA (Spring Boot Admin):
6+
```sh
7+
docker run --rm -p 8080:8080 michayaak/spring-boot-admin:2.2.3-1
8+
```
9+
2. Once Spring Boot Admin is running, you can run the examples as follow:
10+
```sh
11+
cd examples/aiohttp
12+
poetry install
13+
poetry run python -m aiohttp_example_app
14+
```
15+
16+
![aiohttp Example](../images/aiohttp.png)
17+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import datetime
2+
import logging
3+
import random
4+
5+
from aiohttp import web
6+
7+
from pyctuator.pyctuator import Pyctuator
8+
9+
logging.basicConfig(level=logging.INFO)
10+
my_logger = logging.getLogger("example")
11+
app = web.Application()
12+
routes = web.RouteTableDef()
13+
14+
15+
@routes.get('/')
16+
def home(request: web.Request) -> web.Response:
17+
my_logger.debug(f"{datetime.datetime.now()} - {str(random.randint(0, 100))}")
18+
print("Printing to STDOUT")
19+
return web.Response(text="Hello World!")
20+
21+
22+
example_app_address = "host.docker.internal"
23+
example_sba_address = "localhost"
24+
25+
pyctuator = Pyctuator(
26+
app,
27+
"Example aiohttp",
28+
app_url=f"http://{example_app_address}:8888",
29+
pyctuator_endpoint_url=f"http://{example_app_address}:8888/pyctuator",
30+
registration_url=f"http://{example_sba_address}:8080/instances",
31+
app_description="Demonstrate Spring Boot Admin Integration with aiohttp",
32+
)
33+
34+
app.add_routes(routes)
35+
web.run_app(app, port=8888)

0 commit comments

Comments
 (0)