API based logs

Hello I want to get machine or apps logs using API
insterd of using

fly logs -a Myapp 

if possible streaming the logs output would be amazing to show users the status of there app whats happining

Howdy!

we use NATS for logs streaming:

For older logs we use quickwit tho, you could look at your organization grafana to check how it’s done by inspecting the panels (maybe even the browser console).

hey thanks for the reply I have gone through this but its saying we need to run the nats in our machine but how can i do that i am using api to create a machine here is my sample code

def update_streamlit_code(new_code, dependencies=None):
    """Update the Streamlit app code in the deployed machine"""
    list_url = f"{FLY_API_HOSTNAME}/v1/apps/{APP_NAME}/machines"
    install_cmd = install_dependencies(dependencies)
    
    try:
        # First, get the current machine
        response = requests.get(list_url, headers=headers)
        response.raise_for_status()
        machines = response.json()
        
        if not machines:
            print("No machines found for the app")
            return False
        
        machine_id = machines[0]['id']
        print(f"Using machine ID: {machine_id}")
        
        # Suspend the machine before updating
        suspend_url = f"{FLY_API_HOSTNAME}/v1/apps/{APP_NAME}/machines/{machine_id}/suspend"
        print(f"Suspending machine {machine_id}...")
        
        suspend_response = requests.post(suspend_url, headers=headers)
        suspend_response.raise_for_status()
        
        # Wait for the machine to be suspended
        max_suspend_attempts = 20
        suspend_attempt = 0
        while suspend_attempt < max_suspend_attempts:
            status_response = requests.get(f"{FLY_API_HOSTNAME}/v1/apps/{APP_NAME}/machines/{machine_id}", headers=headers)
            if status_response.status_code == 200:
                status = status_response.json().get('state', '')
                print(f"Machine status: {status}")
                if status == 'suspended':
                    break
            time.sleep(0.5)
            suspend_attempt += 1
        
        if suspend_attempt >= max_suspend_attempts:
            print("Timeout waiting for machine to suspend")
            return False
        
        # Get the current configuration
        current_config = machines[0]['config']
        
        # Base64 encode the new code
        encoded_code = base64.b64encode(new_code.encode('utf-8')).decode('utf-8')
        print("Code encoded successfully")
        
        # Prepare the update configuration
        update_config = {
            "config": {
                "image": "registry.fly.io/ssss",
                "env": {
                    "PORT": "8505"
                },
                "files": [
                    {
                        "guest_path": "/app/streamlit_app.py",
                        "raw_value": encoded_code  # Use base64 encoded content
                    }
                ],
                "services": [
                    {
                        "ports": [
                            {
                                "port": 443,
                                "handlers": ["tls", "http"]
                            },
                            {
                                "port": 80,
                                "handlers": ["http"]
                            }
                        ],
                        "protocol": "tcp",
                        "internal_port": 8505,
                        "autostop": "suspend",
                        "autostart": True
                    }
                ],
                "processes": [
                    {
                        "cmd": [
                            "sh",
                            "-c",
                            f"{install_cmd} && streamlit run streamlit_app.py --server.port=8505 --server.address=0.0.0.0"
                        ]
                    }
                ]
            }
        }
        
        # Update the specific machine
        update_url = f"{FLY_API_HOSTNAME}/v1/apps/{APP_NAME}/machines/{machine_id}"
        print(f"Updating machine {machine_id}...")
        
        response = requests.post(update_url, headers=headers, json=update_config)
        response.raise_for_status()
        
        print(f"Response status code: {response.status_code}")
        print(f"Response body: {response.text}")
        

        
        # Wait for the machine to start
        print("Waiting for machine to restart...")
        max_attempts = 20
        attempt = 0
        while attempt < max_attempts:
            status_response = requests.get(update_url, headers=headers)
            if status_response.status_code == 200:
                status = status_response.json().get('state', '')
                print(f"Machine status: {status}")
                if status == 'started':
                    # Wait a bit to ensure the machine is fully ready
                    time.sleep(0.5)
                    break
            time.sleep(0.5)
            attempt += 1
        
        if attempt >= max_attempts:
            print("Timeout waiting for machine to restart")
            return False
        
        return True
        

It’s quite easy and fun tbh

Then you can listen to logs as if you’re inside an app running in fly.io

You can use a NATS library in python to connect to it more easily.

hey thanks I was able to receive logs this are the the steps i have followed and for the users who wants to try it

fly wireguard create
fly wireguard create personal iad test test.conf
wg-quick up test.conf
ping6 _api.internal -c4

./nats sub "logs.>" --server 'nats://[DNS]:4223' --user personal --password "FlyV *************"

so the we are gettign the logs of each machine of that region is it possible to get logs of all the region ?

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.