fastapi

fastapi

  • Expondo logs com python, fastapi and uvicorn

    Essa semana recebi uma missão: permitir acesso aos logs de um container numa VM em que a pessoa não pode conectar.

    Pense em algumas opções e a que me pareceu mais apropriada foi criar um pequeno serviço com python, fastapi e uvicorn. E deixar disponível como acesso http.

    Então fiz um programa bem simples:

      
    #! /usr/bin/env python3
    
    import uvicorn
    from fastapi import FastAPI
    from fastapi.responses import PlainTextResponse, StreamingResponse
    import subprocess
    
    # https://stackoverflow.com/questions/4417546/constantly-print-subprocess-output-while-process-is-running
    def shellExec(command: list[str]):
        popen = subprocess.Popen(command, stdout=subprocess.PIPE, universal_newlines=True)
        for stdout_line in iter(popen.stdout.readline, ""):
            yield stdout_line
        popen.stdout.close()
        return_code = popen.wait()
        if return_code:
            raise subprocess.CalledProcessError(return_code, command)
    
    def getContainerLogs():
        for line in shellExec(["docker", "logs", "ubuntu", "-f"]):
            yield  line
    
    app = FastAPI()
    @app.get("/logs", response_class=PlainTextResponse)
    async def getLogs():
        return StreamingResponse(getContainerLogs(), media_type="text/plain")
    
    
    if __name__ == '__main__':
        uvicorn.run(app, host="0.0.0.0", port=8080)    
     
    

    O programa então roda o comando docker logs ubuntu -f pra ficar lendo os logs vindo do container "ubuntu". Nada muito fantástico.

    E como deixar rodando?

    Eu podia criar um container que pudesse acessar /var/run/docker.socket pra ler info dos containers rodando. E os logs. Mas fui pela simplicidade e só criei um serviço do systemd mesmo.

      
    [Unit]
    Description=Stream logs from ubuntu container
    Wants=network-online.target
    After=network-online.target docker.service
    
    [Service]
    User=helio
    Group=hackerz
    Restart=always
    WorkingDirectory=/home/helio/bin
    ExecStart=/home/helio/bin/stream-logs-container.py
    # If running the Agent in scraping service mode, you will want to override this value with
    # something larger to allow the Agent to gracefully leave the cluster. 4800s is recommend.
    TimeoutStopSec=5s
    
    [Install]
    WantedBy=multi-user.target    
     
    

    Daí bastou ativar e partir pro abraço.

      
    ❯ sudo systemctl enable --now stream-logs-container.service
    Password:
    ❯ curl localhost:8080/logs
    mariadb 12:38:12.20 INFO  ==> 
    mariadb 12:38:12.21 INFO  ==> Welcome to the Bitnami mariadb container
    mariadb 12:38:12.21 INFO  ==> Subscribe to project updates by watching https://github.com/bitnami/containers
    mariadb 12:38:12.21 INFO  ==> Did you know there are enterprise versions of the Bitnami catalog? For enhanced secure software supply chain features, unlimited pulls from Docker, LTS support, or application customization, see Bitnami Premium or Tanzu Application Catalog. See https://www.arrow.com/globalecs/na/vendors/bitnami/ for more information.
    mariadb 12:38:12.22 INFO  ==> 
    mariadb 12:38:12.22 INFO  ==> ** Starting MariaDB setup **
    mariadb 12:38:12.25 INFO  ==> Validating settings in MYSQL_*/MARIADB_* env vars
    mariadb 12:38:12.26 INFO  ==> Initializing mariadb database
    mariadb 12:38:12.28 INFO  ==> Updating 'my.cnf' with custom configuration
    mariadb 12:38:12.29 INFO  ==> Setting slow_query_log option
    mariadb 12:38:12.35 INFO  ==> Setting long_query_time option
    mariadb 12:38:12.37 INFO  ==> Installing database
    mariadb 12:38:13.91 INFO  ==> Starting mariadb in background
    2025-10-02 12:38:13 0 [Note] Starting MariaDB 10.11.11-MariaDB source revision e69f8cae1a15e15b9e4f5e0f8497e1f17bdc81a4 server_uid RV0GswTTbCaNJgiFfL+XFbloFPM= as process 98
    2025-10-02 12:38:13 0 [Note] InnoDB: Compressed tables use zlib 1.2.13
    2025-10-02 12:38:13 0 [Note] InnoDB: Number of transaction pools: 1
    2025-10-02 12:38:13 0 [Note] InnoDB: Using crc32 + pclmulqdq instructions
    2025-10-02 12:38:14 0 [Note] InnoDB: Using Linux native AIO
    2025-10-02 12:38:14 0 [Note] InnoDB: Initializing buffer pool, total size = 128.000MiB, chunk size = 2.000MiB
    2025-10-02 12:38:14 0 [Note] InnoDB: Completed initialization of buffer pool
    2025-10-02 12:38:14 0 [Note] InnoDB: Buffered log writes (block size=512 bytes)
    2025-10-02 12:38:14 0 [Note] InnoDB: End of log at LSN=45502
    2025-10-02 12:38:14 0 [Note] InnoDB: 128 rollback segments are active.
    2025-10-02 12:38:14 0 [Note] InnoDB: Setting file './ibtmp1' size to 12.000MiB. Physically writing the file full; Please wait...
    2025-10-02 12:38:14 0 [Note] InnoDB: File './ibtmp1' size is now 12.000MiB.
    ^C
     
    

    Como ele fica lendo sem parar os logs, é preciso um "ctrl+c" pra sair.

We use cookies

We use cookies on our website. Some of them are essential for the operation of the site, while others help us to improve this site and the user experience (tracking cookies). You can decide for yourself whether you want to allow cookies or not. Please note that if you reject them, you may not be able to use all the functionalities of the site.