def __init__(self) -> None:
        self.app = web.Application()
        self.routes = web.RouteTableDef()

        self.pyctuator = Pyctuator(
            self.app,
            "AIOHTTP Pyctuator",
            "http://*****:*****@self.routes.get("/logfile_test_repeater")
        async def logfile_test_repeater(request: web.Request) -> web.Response:
            repeated_string = request.query.get("repeated_string")
            logging.error(repeated_string)
            return web.Response(text=repeated_string)

        @self.routes.get("/httptrace_test_url")
        async def get_httptrace_test_url(request: web.Request) -> web.Response:
            # Sleep if requested to sleep - used for asserting httptraces timing
            sleep_sec = request.query.get("sleep_sec")
            if sleep_sec:
                logging.info("Sleeping %s seconds before replying", sleep_sec)
                time.sleep(int(sleep_sec))

            # Echo 'User-Data' header as 'resp-data' - used for asserting headers are captured properly
            return web.Response(headers={"resp-data": str(request.headers.get("User-Data"))}, body="my content")

        self.app.add_routes(self.routes)

        self.thread = threading.Thread(target=self._start_in_thread)
        self.should_stop_server = False
        self.server_started = False
示例#2
0
    def __init__(self) -> None:
        self.app = Flask("Flask Example Server")
        self.thread = threading.Thread(target=self.app.run)

        self.pyctuator = Pyctuator(self.app,
                                   "Flask Pyctuator",
                                   "http://*****:*****@self.app.route("/shutdown", methods=["POST"])
        # pylint: disable=unused-variable
        def shutdown() -> str:
            logging.info("Flask server shutting down...")
            func = request.environ.get("werkzeug.server.shutdown")
            if func is None:
                raise RuntimeError("Not running with the Werkzeug Server")
            func()
            return "Flask server off"

        @self.app.route("/logfile_test_repeater")
        # pylint: disable=unused-variable
        def logfile_test_repeater() -> str:
            repeated_string: str = str(request.args.get('repeated_string'))
            logging.error(repeated_string)
            return repeated_string

        @self.app.route("/httptrace_test_url", methods=["GET"])
        # pylint: disable=unused-variable
        def get_httptrace_test_url() -> Response:
            resp = Response()
            resp.headers["resp-data"] = str(request.headers.get('User-Data'))
            return resp
示例#3
0
    def __init__(self) -> None:
        self.app = FastAPI(
            title="FastAPI Example Server",
            description="Demonstrate Spring Boot Admin Integration with FastAPI",
            docs_url="/api",
        )

        self.pyctuator = Pyctuator(
            self.app,
            "FastAPI Pyctuator",
            "http://*****:*****@self.app.get("/logfile_test_repeater", tags=["pyctuator"])
        # pylint: disable=unused-variable
        def logfile_test_repeater(repeated_string: str) -> str:
            logging.error(repeated_string)
            return repeated_string

        self.server = CustomServer(config=(Config(app=self.app, loop="asyncio")))
        self.thread = threading.Thread(target=self.server.run)

        @self.app.get("/httptrace_test_url")
        # pylint: disable=unused-variable
        def get_httptrace_test_url(request: Request, sleep_sec: Optional[int]) -> Response:
            # Sleep if requested to sleep - used for asserting httptraces timing
            if sleep_sec:
                logging.info("Sleeping %s seconds before replying", sleep_sec)
                time.sleep(sleep_sec)

            # Echo 'User-Data' header as 'resp-data' - used for asserting headers are captured properly
            return Response(headers={"resp-data": str(request.headers.get("User-Data"))}, content="my content")
示例#4
0
    def __init__(self) -> None:
        self.app = FastAPI(
            title="FastAPI Example Server",
            description=
            "Demonstrate Spring Boot Admin Integration with FastAPI",
            docs_url="/api",
        )

        self.pyctuator = Pyctuator(
            self.app,
            "FastAPI Pyctuator",
            "http://*****:*****@self.app.get("/logfile_test_repeater", tags=["pyctuator"])
        # pylint: disable=unused-variable
        def logfile_test_repeater(repeated_string: str) -> str:
            logging.error(repeated_string)
            return repeated_string

        self.server = CustomServer(
            config=(Config(app=self.app, loop="asyncio")))
        self.thread = threading.Thread(target=self.server.run)

        @self.app.get("/httptrace_test_url")
        # pylint: disable=unused-variable
        def get_httptrace_test_url(request: Request) -> Response:
            return Response(
                headers={"resp-data": str(request.headers.get("User-Data"))},
                content="my content")
示例#5
0
class FlaskPyctuatorServer(PyctuatorServer):
    def __init__(self) -> None:
        self.app = Flask("Flask Example Server")
        self.thread = threading.Thread(target=self.app.run)

        self.pyctuator = Pyctuator(
            self.app,
            "Flask Pyctuator",
            "http://*****:*****@self.app.route("/shutdown", methods=["POST"])
        # pylint: disable=unused-variable
        def shutdown() -> str:
            logging.info("Flask server shutting down...")
            func = request.environ.get("werkzeug.server.shutdown")
            if func is None:
                raise RuntimeError("Not running with the Werkzeug Server")
            func()
            return "Flask server off"

        @self.app.route("/logfile_test_repeater")
        # pylint: disable=unused-variable
        def logfile_test_repeater() -> str:
            repeated_string: str = str(request.args.get("repeated_string"))
            logging.error(repeated_string)
            return repeated_string

        @self.app.route("/httptrace_test_url", methods=["GET"])
        # pylint: disable=unused-variable
        def get_httptrace_test_url() -> Response:
            # Sleep if requested to sleep - used for asserting httptraces timing
            sleep_sec = request.args.get("sleep_sec")
            if sleep_sec:
                logging.info("Sleeping %s seconds before replying", sleep_sec)
                time.sleep(int(sleep_sec))

            # Echo 'User-Data' header as 'resp-data' - used for asserting headers are captured properly
            resp = Response()
            resp.headers["resp-data"] = str(request.headers.get("User-Data"))
            return resp

    def start(self) -> None:
        self.thread.start()
        while True:
            time.sleep(0.5)
            try:
                requests.get("http://localhost:5000/pyctuator")
                return
            except requests.exceptions.RequestException:  # Catches all exceptions that Requests raises!
                pass

    def stop(self) -> None:
        self.pyctuator.stop()
        requests.post("http://localhost:5000/shutdown")
        self.thread.join()
    def __init__(self) -> None:

        # pylint: disable=abstract-method
        class LogfileTestRepeater(RequestHandler):
            def get(self) -> None:
                repeated_string = self.get_argument("repeated_string")
                logging.error(repeated_string)
                self.write(repeated_string)

        # pylint: disable=abstract-method
        class GetHttptraceTestUrl(RequestHandler):
            def get(self) -> None:
                sleep_sec: Optional[str] = self.get_argument("sleep_sec", None)
                # Sleep if requested to sleep - used for asserting httptraces timing
                if sleep_sec:
                    logging.info("Sleeping %s seconds before replying",
                                 sleep_sec)
                    time.sleep(int(sleep_sec))

                # Echo 'User-Data' header as 'resp-data' - used for asserting headers are captured properly
                self.add_header("resp-data",
                                str(self.request.headers.get("User-Data")))
                self.add_header("response-secret", "my password")
                self.write("my content")

        self.app = Application(
            [("/logfile_test_repeater", LogfileTestRepeater),
             ("/httptrace_test_url", GetHttptraceTestUrl)],
            debug=True)

        self.pyctuator = Pyctuator(
            self.app,
            "Tornado Pyctuator",
            app_url=f"http://localhost:5000",
            pyctuator_endpoint_url=f"http://localhost:5000/pyctuator",
            registration_url=f"http://localhost:8001/register",
            app_description=
            "Demonstrate Spring Boot Admin Integration with Tornado",
            registration_interval_sec=1,
            metadata=self.metadata,
        )

        self.io_loop: Optional[ioloop.IOLoop] = None
        self.http_server = HTTPServer(self.app, decompress_request=True)
        self.thread = threading.Thread(target=self._start_in_thread)
示例#7
0
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}:8082/instances",
    app_description="Demonstrate Spring Boot Admin Integration with aiohttp",
)

app.add_routes(routes)
web.run_app(app, port=8888)
class FastApiPyctuatorServer(PyctuatorServer):
    def __init__(self) -> None:
        self.app = FastAPI(
            title="FastAPI Example Server",
            description=
            "Demonstrate Spring Boot Admin Integration with FastAPI",
            docs_url="/api",
        )

        self.pyctuator = Pyctuator(
            self.app,
            "FastAPI Pyctuator",
            "http://*****:*****@self.app.get("/logfile_test_repeater", tags=["pyctuator"])
        # pylint: disable=unused-variable
        def logfile_test_repeater(repeated_string: str) -> str:
            logging.error(repeated_string)
            return repeated_string

        self.server = CustomServer(
            config=(Config(app=self.app, loop="asyncio")))
        self.thread = threading.Thread(target=self.server.run)

        @self.app.get("/httptrace_test_url")
        # pylint: disable=unused-variable
        def get_httptrace_test_url(request: Request,
                                   sleep_sec: Optional[int]) -> Response:
            # Sleep if requested to sleep - used for asserting httptraces timing
            if sleep_sec:
                logging.info("Sleeping %s seconds before replying", sleep_sec)
                time.sleep(sleep_sec)

            # Echo 'User-Data' header as 'resp-data' - used for asserting headers are captured properly
            return Response(
                headers={"resp-data": str(request.headers.get("User-Data"))},
                content="my content")

    def start(self) -> None:
        self.thread.start()
        while not self.server.started:
            time.sleep(0.01)

    def stop(self) -> None:
        logging.info("Stopping FastAPI server")
        self.pyctuator.stop()

        # Allow the recurring registration to complete any in-progress request before stopping FastAPI
        time.sleep(1)

        self.server.should_exit = True
        self.server.force_exit = True
        self.thread.join()
        logging.info("FastAPI server stopped")

    def atexit(self) -> None:
        if self.pyctuator.boot_admin_registration_handler:
            self.pyctuator.boot_admin_registration_handler.deregister_from_admin_server(
            )
示例#9
0
@app.get("/")
def read_root():
    my_logger.debug(
        f"{datetime.datetime.now()} - {str(random.randint(0, 100))}")
    print("Printing to STDOUT")
    return "Hello World!"


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

pyctuator = Pyctuator(
    app,
    "Example FastAPI",
    app_url=f"http://{example_app_address}:8000",
    pyctuator_endpoint_url=f"http://{example_app_address}:8000/pyctuator",
    registration_url=f"http://{example_sba_address}:8080/instances",
    app_description=app.description,
)

# Keep the console clear - configure uvicorn (FastAPI's WSGI web app) not to log the detail of every incoming request
uvicorn_logger = logging.getLogger("uvicorn")
uvicorn_logger.setLevel(logging.WARNING)

server = Server(config=(Config(
    app=app,
    loop="asyncio",
    host="0.0.0.0",
    logger=uvicorn_logger,
)))
server.run()
示例#10
0
# Workaround for starting Faust App without Kafka.
# See https://github.com/robinhood/faust/issues/234
class App(faust.App):

    producer_only = True

    class BootStrategy(BootStrategy):
        enable_kafka = False


app = App('demo1')

pyctuator = Pyctuator(app.web.web_app,
                      "aiohttp Pyctuator",
                      app_url="http://*****:*****@app.crontab('*/2 * * * *')
async def test_message():
    print('EVERY MINUTE I MESSAGE')


if __name__ == '__main__':
    app.main()
示例#11
0
@app.post("/health")
def health_up(request: Request,
              health: Dict) -> None:  # health should be of type HealthStatus
    global app_specific_health
    app_specific_health = HealthStatus(Status[health["status"]],
                                       details=health["details"])


# ----------------------------------------------------------------------------------------------------------------------
# Initialize Pyctuator with the SBA endpoint and all the extensions
# ----------------------------------------------------------------------------------------------------------------------

pyctuator = Pyctuator(
    app,
    get_conf("app.name"),
    get_conf("app.public_endpoint"),
    get_conf("monitoring.pyctuator_endpoint"),
    get_conf("monitoring.sba_registration_endpoint"),
    app_description=app.description,
)

# Provide app's build info
pyctuator.set_build_info(
    name=get_conf("app.name"),
    version=get_conf("app.version"),
    time=get_conf("app.build_time"),
)

# Provide git commit info
pyctuator.set_git_info(
    commit=get_conf("app.git.commit"),
    time=get_conf("app.git.time"),
示例#12
0
# Keep the console clear - configure werkzeug (flask's WSGI web app) not to log the detail of every incoming request
logging.getLogger("werkzeug").setLevel(logging.WARNING)

my_logger = logging.getLogger("example")

app = Flask("Flask Example Server")


@app.route("/")
def hello():
    my_logger.debug(
        f"{datetime.datetime.now()} - {str(random.randint(0, 100))}")
    print("Printing to STDOUT")
    return "Hello World!"


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

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

app.run(port=5000, host="0.0.0.0")
示例#13
0
app = FastAPI(
    title="FastAPI Example Server",
    description="Demonstrate Spring Boot Admin Integration with FastAPI",
    docs_url="/api",
)


@app.get("/")
def read_root():
    my_logger.debug(f"{datetime.datetime.now()} - {str(random.randint(0, 100))}")
    print("Printing to STDOUT")
    return "Hello World!"


example_app_public_address = socket.gethostbyname(socket.gethostname())
example_app_address_as_seen_from_sba_container = "host.docker.internal"
example_sba_address = "localhost"

pyctuator = Pyctuator(
    app,
    "Example FastAPI",
    f"http://{example_app_public_address}:8000",
    f"http://{example_app_address_as_seen_from_sba_container}:8000/pyctuator",
    f"http://{example_sba_address}:8082/instances",
    app_description=app.description,
)

server = Server(config=(Config(app=app, loop="asyncio", host="0.0.0.0")))
server.run()
示例#14
0
class TornadoPyctuatorServer(PyctuatorServer):
    def __init__(self) -> None:

        # pylint: disable=abstract-method
        class LogfileTestRepeater(RequestHandler):
            def get(self) -> None:
                repeated_string = self.get_argument("repeated_string")
                logging.error(repeated_string)
                self.write(repeated_string)

        # pylint: disable=abstract-method
        class GetHttptraceTestUrl(RequestHandler):
            def get(self) -> None:
                sleep_sec: Optional[str] = self.get_argument("sleep_sec", None)
                # Sleep if requested to sleep - used for asserting httptraces timing
                if sleep_sec:
                    logging.info("Sleeping %s seconds before replying",
                                 sleep_sec)
                    time.sleep(int(sleep_sec))

                # Echo 'User-Data' header as 'resp-data' - used for asserting headers are captured properly
                self.add_header("resp-data",
                                str(self.request.headers.get("User-Data")))
                self.write("my content")

        self.app = Application(
            [("/logfile_test_repeater", LogfileTestRepeater),
             ("/httptrace_test_url", GetHttptraceTestUrl)],
            debug=True)

        self.pyctuator = Pyctuator(
            self.app,
            "Tornado Pyctuator",
            app_url=f"http://localhost:5000",
            pyctuator_endpoint_url=f"http://localhost:5000/pyctuator",
            registration_url=f"http://localhost:8001/register",
            app_description=
            "Demonstrate Spring Boot Admin Integration with Tornado",
            registration_interval_sec=1,
        )

        self.io_loop: Optional[ioloop.IOLoop] = None
        self.http_server = HTTPServer(self.app, decompress_request=True)
        self.thread = threading.Thread(target=self._start_in_thread)

    def _start_in_thread(self) -> None:
        self.io_loop = ioloop.IOLoop()
        self.http_server.listen(5000)
        self.io_loop.start()

    def start(self) -> None:
        logging.info("Starting Tornado server")
        self.thread.start()
        time.sleep(0.5)

    def stop(self) -> None:
        logging.info("Stopping Tornado server")
        self.pyctuator.stop()

        # Allow the recurring registration to complete any in-progress request before stopping Tornado
        time.sleep(1)

        assert self.io_loop is not None

        self.http_server.stop()
        self.io_loop.add_callback(self.io_loop.stop)
        self.thread.join()
        self.io_loop.close(all_fds=True)

    def atexit(self) -> None:
        if self.pyctuator.boot_admin_registration_handler:
            self.pyctuator.boot_admin_registration_handler.deregister_from_admin_server(
            )
示例#15
0
from app.models import User, Paper

with app.app_context():
    db.create_all()
login_user = User.query.filter_by(username=cf.USERNAME).first()
if not login_user:
    u = User(username=cf.USERNAME)
    u.set_password(cf.PASSWORD)
    db.session.add(u)
    db.session.commit()

s3 = boto3.resource('s3',
                    aws_access_key_id=cf.ACCESS_KEY,
                    aws_secret_access_key=cf.SECRET_ACCESS_KEY)
ssapp_docs = s3.Bucket(cf.S3_BUCKET_NAME)

from app import routes, models

pyctuator = Pyctuator(
    app,
    "Semantic Segment Search",
    app_url=f"http://{cf.APP_ADDR}:8000",
    pyctuator_endpoint_url=f"http://{cf.APP_ADDR}:8000/pyctuator",
    registration_url=f"http://{cf.APP_ADDR}:8080/instances")

pyctuator.set_git_info(
    commit='2f03f40',
    time='Tue Jul 20 16:24:06 2021 -0700',
    branch='origin/master',
)
示例#16
0
# pip install pyctuator

import os
from flask import Flask
from pyctuator.auth import BasicAuth
from pyctuator.pyctuator import Pyctuator

app_name = "Flask App with Pyctuator"
app = Flask(app_name)


@app.route("/")
def hello():
    return "Hello World!"


Pyctuator(
    app,
    app_name,
    app_url="http://192.168.99.100:5000",
    pyctuator_endpoint_url="http://192.168.99.100:5000/pyctuator",
    registration_url="http://192.168.99.100:9600/instances",
    registration_auth=BasicAuth(username="******", password="******"),
)

app.run(host="0.0.0.0")
示例#17
0
# pip install pyctuator

import os
from flask import Flask
from pyctuator.pyctuator import Pyctuator

app_name = "Flask App with Pyctuator"
app = Flask(app_name)


@app.route("/")
def hello():
    return "Hello World!"


Pyctuator(app,
          app_name,
          app_url="http://localhost:5000",
          pyctuator_endpoint_url="http://localhost:5000/pyctuator",
          registration_url="http://localhost:9000/instances")

app.run()
class AiohttpPyctuatorServer(PyctuatorServer):

    def __init__(self) -> None:
        self.app = web.Application()
        self.routes = web.RouteTableDef()

        self.pyctuator = Pyctuator(
            self.app,
            "AIOHTTP Pyctuator",
            "http://*****:*****@self.routes.get("/logfile_test_repeater")
        async def logfile_test_repeater(request: web.Request) -> web.Response:
            repeated_string = request.query.get("repeated_string")
            logging.error(repeated_string)
            return web.Response(text=repeated_string)

        @self.routes.get("/httptrace_test_url")
        async def get_httptrace_test_url(request: web.Request) -> web.Response:
            # Sleep if requested to sleep - used for asserting httptraces timing
            sleep_sec = request.query.get("sleep_sec")
            if sleep_sec:
                logging.info("Sleeping %s seconds before replying", sleep_sec)
                time.sleep(int(sleep_sec))

            # Echo 'User-Data' header as 'resp-data' - used for asserting headers are captured properly
            headers = {
                "resp-data": str(request.headers.get("User-Data")),
                "response-secret": "my password"
            }
            return web.Response(headers=headers, body="my content")

        self.app.add_routes(self.routes)

        self.thread = threading.Thread(target=self._start_in_thread)
        self.should_stop_server = False
        self.server_started = False

    async def _run_server(self) -> None:
        logging.info("Preparing to start aiohttp server")
        runner = web.AppRunner(self.app)
        await runner.setup()

        logging.info("Starting aiohttp server")
        site = web.TCPSite(runner, port=8888)
        await site.start()
        self.server_started = True
        logging.info("aiohttp server started")

        while not self.should_stop_server:
            await asyncio.sleep(1)

        logging.info("Shutting down aiohttp server")
        await runner.shutdown()
        await runner.cleanup()
        logging.info("aiohttp server is shutdown")

    def _start_in_thread(self) -> None:
        loop = asyncio.new_event_loop()
        loop.run_until_complete(self._run_server())
        loop.stop()

    def start(self) -> None:
        self.thread.start()
        while not self.server_started:
            time.sleep(0.01)

    def stop(self) -> None:
        logging.info("Stopping aiohttp server")
        self.pyctuator.stop()
        self.should_stop_server = True
        self.thread.join()
        logging.info("aiohttp server stopped")

    def atexit(self) -> None:
        if self.pyctuator.boot_admin_registration_handler:
            self.pyctuator.boot_admin_registration_handler.deregister_from_admin_server()
示例#19
0
logging.basicConfig(level=logging.INFO)

my_logger = logging.getLogger("example")

app = Flask("Flask Example Server")


@app.route("/")
def hello():
    my_logger.debug(
        f"{datetime.datetime.now()} - {str(random.randint(0, 100))}")
    print("Printing to STDOUT")
    return "Hello World!"


example_app_public_address = socket.gethostbyname(socket.gethostname())
example_app_address_as_seen_from_sba_container = "host.docker.internal"
example_sba_address = "localhost"

Pyctuator(
    app,
    "Flask Pyctuator",
    f"http://{example_app_public_address}:5000",
    f"http://{example_app_address_as_seen_from_sba_container}:5000/pyctuator",
    f"http://{example_sba_address}:8082/instances",
    app_description="Demonstrate Spring Boot Admin Integration with Flask",
)

app.run(port=5000, host="0.0.0.0")