[Python] Digital Ocean Function returning generic error code after adding log forwarding

Discussão em 'Python' iniciado por Stack, Junho 22, 2025.

  1. Stack

    Stack Membro Participativo

    I'm testing a simple function (python) in Digital Ocean App Platform. The function imports psycopg2, connects to an attached Postgres DB, and returns the DB's version. This function was working until I added log forwarding to my DO App and function, to forward logs to Betterstack (Logtail). I did this, because within DO App Platform it is not possible get function logs (I verified this with Digital Ocean support: function logs are only available via their command line tool doctl if the function is a stand alone function, NOT if the function is part of an App. If the function is part of an App, it is necessary to forward logs to a logging service). Now, instead of returning a success message:

    {
    "message": "Successfully imported dependencies and found DB version RealDictRow([('version', 'PostgreSQL 16.9 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 14.2.1 20240912 (Red Hat 14.2.1-3), 64-bit')]).",
    "psycopg2_version": "2.9.10 (dt dec pq3 ext lo64)"
    }


    It returns this error message which is coming from Digital Ocean, not my function:

    {
    "code": "abc123581b123ea5e3d73848",
    "error": "There was an error processing your request."
    }


    This is my project.yml file. Note that DO Support recommended I specify LOG_DESTINATION in this way:

    environment:
    DB_HOST: "${DB_HOST}"
    DB_PORT: "${DB_PORT}"
    DB_USER: "${DB_USER}"
    DB_PASSWORD: "${DB_PASSWORD}"
    DB_NAME: "${DB_NAME}"
    DB_SSL_MODE: "${DB_SSL_MODE}"
    LOG_DESTINATIONS: "[{'logtail':{'token':'${LOGTAIL_TOKEN}'}}]"
    packages:
    - name: db-test
    actions:
    - name: db-tester
    runtime: 'python:default'
    web: true


    This is my function:

    import os
    import psycopg2
    from psycopg2.extras import RealDictCursor

    def get_db_connection():
    """Create a connection to the PostgreSQL database"""
    print("Connecting to the database...")
    connection = psycopg2.connect(
    host=os.environ.get("DB_HOST"),
    port=os.environ.get("DB_PORT"),
    dbname=os.environ.get("DB_NAME"),
    user=os.environ.get("DB_USER"),
    password=os.environ.get("DB_PASSWORD"),
    sslmode=os.environ.get("DB_SSL_MODE"),
    cursor_factory=RealDictCursor
    )
    return connection

    def main(args):
    try:
    print("Starting the receipt-submitter function...")
    version = psycopg2.__version__
    conn = get_db_connection()
    cursor = conn.cursor()
    cursor.execute("SELECT version();")
    db_version = cursor.fetchone()
    cursor.close()
    conn.close()

    # Test imports were successful
    return {
    "body": {
    "message": f"Successfully imported dependencies and found DB version {db_version}.",
    "psycopg2_version": version
    }
    }
    except Exception as e:
    print("Exception thrown.")
    return {
    "body": {
    "error": f"{str(e)}"
    },
    "statusCode": 500
    }

    Continue reading...

Compartilhe esta Página