Skip to content

Commit

Permalink
Merge pull request #19 from SolarEdgeTech/amenezes/aiohttp
Browse files Browse the repository at this point in the history
Add support for monitoring aiohttp based applications
  • Loading branch information
MatanRubin authored Jul 8, 2020
2 parents b7a43ac + 93931ee commit 09d69bb
Show file tree
Hide file tree
Showing 20 changed files with 1,072 additions and 113 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/python_package_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ jobs:
steps:
- uses: actions/checkout@v2
- run: make bootstrap
- run: poetry update -vvv
- run: poetry build -vvv
- run: poetry install --extras flask --extras fastapi --extras db --extras redis
- run: poetry install --extras flask --extras fastapi --extras aiohttp --extras db --extras redis
- run: make coverage
- uses: actions/upload-artifact@v2
with:
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ bootstrap:
check: pylint mypy

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

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

pylint:
poetry run pylint --exit-zero pyctuator tests
Expand Down
38 changes: 34 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
Monitor Python web apps using
[Spring Boot Admin](https://github.com/codecentric/spring-boot-admin).

Pyctuator supports **Flask** and **FastAPI**. **Django** support is planned as well.
Pyctuator supports **Flask**, **FastAPI** and **aiohttp**. **Django** support is planned as well.

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

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

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

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

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

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

### aiohttp
The following example is complete and should run as is.

```python
from aiohttp import web
from pyctuator.pyctuator import Pyctuator

app = web.Application()
routes = web.RouteTableDef()

@routes.get("/")
def hello():
return web.Response(text="Hello World!")

Pyctuator(
app,
"aiohttp Pyctuator",
app_url="http://host.docker.internal:8888",
pyctuator_endpoint_url="http://host.docker.internal:8888/pyctuator",
registration_url="http://localhost:8080/instances"
)

app.add_routes(routes)
web.run_app(app, port=8888)
```

The application will automatically register with Spring Boot Admin upon start up.

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

### Registration Notes
When registering a service in Spring Boot Admin, note that:
* **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.
Expand Down Expand Up @@ -279,7 +309,7 @@ To run these examples, you'll need to have Spring Boot Admin running in a local

Unless the example includes a docker-compose file, you'll need to start Spring Boot Admin using docker directly:
```sh
docker run -p 8080:8080 michayaak/spring-boot-admin:2.2.3-1
docker run --rm -p 8080:8080 michayaak/spring-boot-admin:2.2.3-1
```
(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).

Expand Down
2 changes: 1 addition & 1 deletion examples/Advanced/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ python = "^3.7"
psutil = { version = "^5.6" }
fastapi = { version = "^0.41.0" }
uvicorn = { version = "^0.9.0" }
pyctuator = { version = "^0.11" }
pyctuator = { version = "^0.12" }
sqlalchemy = { version = "^1.3" }
PyMySQL = { version = "^0.9.3" }
cryptography = { version = "^2.8" }
Expand Down
2 changes: 1 addition & 1 deletion examples/FastAPI/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This example demonstrates the integration with the [FastAPI](https://fastapi.tia
## Running the example
1. Start an instance of SBA (Spring Boot Admin):
```sh
docker run -p 8082:8082 michayaak/spring-boot-admin:2.2.2
docker run --rm -p 8080:8080 michayaak/spring-boot-admin:2.2.3-1
```
2. Once Spring Boot Admin is running, you can run the examples as follow:
```sh
Expand Down
2 changes: 1 addition & 1 deletion examples/FastAPI/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ python = "^3.7"
psutil = { version = "^5.6" }
fastapi = { version = "^0.41.0" }
uvicorn = { version = "^0.9.0" }
pyctuator = { version = "^0.11" }
pyctuator = { version = "^0.12" }

[build-system]
requires = ["poetry>=0.12"]
Expand Down
6 changes: 3 additions & 3 deletions examples/Flask/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# FastAPI example
# Flask example
This example demonstrates the integration with the [Flask](https://flask.palletsprojects.com/) web-framework.

## Running the example
1. Start an instance of SBA (Spring Boot Admin):
```sh
docker run -p 8082:8082 michayaak/spring-boot-admin:2.2.2
docker run --rm -p 8080:8080 michayaak/spring-boot-admin:2.2.3-1
```
2. Once Spring Boot Admin is running, you can run the examples as follow:
```sh
Expand All @@ -19,4 +19,4 @@ This example demonstrates the integration with the [Flask](https://flask.pallets
* 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.
```Python
app.run(port=5000, host="0.0.0.0", debug=True)
```
```
2 changes: 1 addition & 1 deletion examples/Flask/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ authors = [
python = "^3.7"
psutil = { version = "^5.6" }
flask = { version = "^1.1" }
pyctuator = { version = "^0.11" }
pyctuator = { version = "^0.12" }

[build-system]
requires = ["poetry>=0.12"]
Expand Down
17 changes: 17 additions & 0 deletions examples/aiohttp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# aiohttp example
This example demonstrates the integration with the [aiohttp](https://docs.aiohttp.org).

## Running the example
1. Start an instance of SBA (Spring Boot Admin):
```sh
docker run --rm -p 8080:8080 michayaak/spring-boot-admin:2.2.3-1
```
2. Once Spring Boot Admin is running, you can run the examples as follow:
```sh
cd examples/aiohttp
poetry install
poetry run python -m aiohttp_example_app
```

![aiohttp Example](../images/aiohttp.png)

35 changes: 35 additions & 0 deletions examples/aiohttp/aiohttp_example_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import datetime
import logging
import random

from aiohttp import web

from pyctuator.pyctuator import Pyctuator

logging.basicConfig(level=logging.INFO)
my_logger = logging.getLogger("example")
app = web.Application()
routes = web.RouteTableDef()


@routes.get('/')
def home(request: web.Request) -> web.Response:
my_logger.debug(f"{datetime.datetime.now()} - {str(random.randint(0, 100))}")
print("Printing to STDOUT")
return web.Response(text="Hello World!")


example_app_address = "host.docker.internal"
example_sba_address = "localhost"

pyctuator = Pyctuator(
app,
"Example aiohttp",
app_url=f"http://{example_app_address}:8888",
pyctuator_endpoint_url=f"http://{example_app_address}:8888/pyctuator",
registration_url=f"http://{example_sba_address}:8080/instances",
app_description="Demonstrate Spring Boot Admin Integration with aiohttp",
)

app.add_routes(routes)
web.run_app(app, port=8888)
18 changes: 18 additions & 0 deletions examples/aiohttp/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[tool.poetry]
name = "aiohttp-pyctuator-example"
version = "1.0.0"
description = "Example of using Pyctuator"
authors = [
"Luke Skywalker <[email protected]>",
]

[tool.poetry.dependencies]
python = "^3.7"
psutil = { version = "^5.6" }
aiohttp = { version = "^3.5.4" }
pyctuator = { version = "^0.12" }

[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"

Binary file added examples/images/aiohttp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 09d69bb

Please sign in to comment.