Pyctuator

repository·master·Indexed 41 days ago

https://github.com/solaredgetech/pyctuator

A Python implementation of the Spring Actuator API for popular web frameworks including Flask, FastAPI, aiohttp, and Tornado. It allows Python microservices to be monitored and controlled via Spring Boot Admin, providing insights into system metrics, environment variables, application configuration, and health monitoring via custom health-providers.

Tokens
6.5K
Snippets
31
Records
34
Agent score
52%

What's inside Pyctuator

  1. Configure Pyctuator authentication in FastAPI

    master

    When protecting the Pyctuator endpoint in a FastAPI application, you must perform two steps to ensure Spring Boot Admin (SBA) can still manage the application:

    1. Protect the API Router: Use a customizer to apply the required configuration changes to the FastAPI API router to enforce authentication.
    2. Update SBA Registration: Include the necessary credentials in the registration request sent to SBA. This allows SBA to authenticate itself when it attempts to query the Pyctuator API.
  2. Implement custom health monitoring with health-providers

    master

    Pyctuator allows SBA to monitor application-specific health aspects via custom health-providers. Health status can include multiple checks and detailed failure information.

    In the provided example, the application's health status is updated by posting a JSON payload formatted as pyctuator.health_provider.HealthDetails to a specific endpoint. This allows external triggers to change the reported health status (e.g., from UP to DOWN) and provide context in the details field.

    curl -X POST localhost:8000/health -d '{"status": "DOWN", "details": {"backend_connectivity": "Down", "available_resources": 41}}'
  3. Pyctuator Registration and Configuration Notes

    master

    When configuring Pyctuator for registration with Spring Boot Admin, keep the following requirements in mind:

    • Docker Networking: If Spring Boot Admin is in a container and your Python app is on the host, use host.docker.internal for app_url and pyctuator_endpoint_url.
    • HTTP Traces: To ensure the "Http Traces" tab correctly filters out requests sent by Spring Boot Admin, pyctuator_endpoint_url must use the same host and port as app_url.
    • HTTPS/SSL:
      • To use a custom SSL context, provide an ssl.SSLContext object via the ssl_context parameter in the Pyctuator constructor.
      • If Spring Boot Admin uses HTTPS with self-signed certificates, set the PYCTUATOR_REGISTRATION_NO_CERT environment variable to disable certificate validation during registration/deregistration.
  4. Run Spring Boot Admin via Docker for testing

    master

    To test Pyctuator examples locally, you can run Spring Boot Admin in a Docker container. Use the following command to start it on port 8080:

    docker run --rm -p 8080:8080 --add-host=host.docker.internal:host-gateway michayaak/spring-boot-admin:2.2.3-1
  5. Run the Tornado integration example

    master

    To run the Tornado integration example for Pyctuator, you must first have a running instance of Spring Boot Admin (SBA) to act as the actuator registry.

    1. Start Spring Boot Admin using Docker:

      docker run --rm -p 8080:8080 --add-host=host.docker.internal:host-gateway michayaak/spring-boot-admin:2.2.3-1
    2. Execute the Tornado example using Poetry:

      cd examples/tornado
      poetry install
      poetry run python -m tornado_example_app
    docker run --rm -p 8080:8080 --add-host=host.docker.internal:host-gateway michayaak/spring-boot-admin:2.2.3-1
    
    cd examples/tornado
    poetry install
    poetry run python -m tornado_example_app
  6. Run the Pyctuator Flask integration example

    master

    To run the Flask integration example, you must first have a running instance of Spring Boot Admin (SBA) to act as the actuator registry.

    1. Start Spring Boot Admin using Docker:

      docker run --rm -p 8080:8080 --add-host=host.docker.internal:host-gateway michayaak/spring-boot-admin:2.2.3-1
    2. Run the Flask application using Poetry:

      cd examples/Flask
      poetry install
      poetry run python -m flask_example_app
    docker run --rm -p 8080:8080 --add-host=host.docker.internal:host-gateway michayaak/spring-boot-admin:2.2.3-1
    
    cd examples/Flask
    poetry install
    poetry run python -m flask_example_app
  7. Run Spring Boot Admin via Docker

    master

    To use Pyctuator, you need a Spring Boot Admin instance to monitor your Python applications. You can launch a local instance using Docker:

    docker run --rm -p 8080:8080 --add-host=host.docker.internal:host-gateway michayaak/spring-boot-admin:2.2.3-1
  8. Run the aiohttp integration example

    master

    To see how pyctuator integrates with aiohttp, you must first have a Spring Boot Admin (SBA) instance running to act as the actuator registry. Follow these steps:

    1. Start Spring Boot Admin: Use Docker to run an SBA instance. Ensure you use the --add-host=host.docker.internal:host-gateway flag so the container can communicate with your local machine.
    2. Setup the example: Navigate to the examples/aiohttp directory, install dependencies using poetry, and execute the application module.

    Prerequisites:

    • Docker
    • Poetry
    • A running Spring Boot Admin instance
    # 1. Start Spring Boot Admin
    docker run --rm -p 8080:8080 --add-host=host.docker.internal:host-gateway michayaak/spring-boot-admin:2.2.3-1
    
    # 2. Run the aiohttp example
    cd examples/aiohttp
    poetry install
    poetry run python -m aiohttp_example_app
  9. Run the Advanced Pyctuator Example

    master

    To run the advanced example, you must have Spring Boot Admin (SBA), MySQL, and Redis running on the same machine. It is recommended to use the provided docker-compose.yml from the examples/Advanced directory to start these services.

    1. Start infrastructure services:
    docker-compose --project-name example --file docker-compose.yml up --detach --force-recreate
    1. Install dependencies and run the application using poetry:
    poetry install
    poetry run python advanced_example_app.py

    Once running, the application exposes APIs at http://localhost:8000 and you can connect to Spring Boot Admin at http://localhost:8082.

    docker-compose --project-name example --file docker-compose.yml up --detach --force-recreate
    
    poetry install
    poetry run python advanced_example_app.py
  10. Run the FastAPI Pyctuator example

    master

    To run the FastAPI integration example, you must first have a Spring Boot Admin (SBA) instance running to act as the management dashboard.

    1. Start Spring Boot Admin using Docker:

      docker run --rm -p 8080:8080 --add-host=host.docker.internal:host-gateway michayaak/spring-boot-admin:2.2.3-1
    2. Setup and run the FastAPI application: Navigate to the example directory, install dependencies with Poetry, and execute the module.

      cd examples/FastAPI
      poetry install
      poetry run python -m fastapi_example_app
    docker run --rm -p 8080:8080 --add-host=host.docker.internal:host-gateway michayaak/spring-boot-admin:2.2.3-1
    
    cd examples/FastAPI
    poetry install
    poetry run python -m fastapi_example_app