hyperdashio / hyperdash-sdk-py

Official Python SDK for Hyperdash
https://hyperdash.io
199 stars 26 forks source link

Add HD client with logger #69

Closed richardartoul closed 7 years ago

richardartoul commented 7 years ago

Sample code:

# -*- coding: utf-8 -*-

import time
import sys

from hyperdash import monitor
from threading import Thread

def worker(thread_num, monitored):
    @monitor("test threading" + str(thread_num), capture_io=False)
    def monitored_func(hd_client):
        for x in range(5):
            hd_client.logger.info(
                "thread {} is doing some work".format(thread_num))
            time.sleep(1)

    def unmonitored_func():
        for x in range(5):
            print("thread {} is doing some work".format(thread_num))
            time.sleep(1)
    return monitored_func if monitored else unmonitored_func

t1 = Thread(target=worker(1, True))
t2 = Thread(target=worker(2, True))
t3 = Thread(target=worker(3, False))

t1.daemon = True
t2.daemon = True
t3.daemon = True

t1.start()
t2.start()
t3.start()

t1.join()
t2.join()
t3.join()

Terminal output

thread 3 is doing some work
thread 1 is doing some work
thread 2 is doing some work
thread 3 is doing some work
thread 1 is doing some work
thread 2 is doing some work
thread 3 is doing some work
thread 1 is doing some work
thread 2 is doing some work
thread 3 is doing some work
thread 1 is doing some work
thread 2 is doing some work
thread 3 is doing some work
thread 2 is doing some work
thread 1 is doing some work
Logs for this run of test threading1 are available locally at: /Users/richie/.hyperdash/logs/test-threading1/test-threading1_2017-08-12t22-59-03-117036.log
Logs for this run of test threading2 are available locally at: /Users/richie/.hyperdash/logs/test-threading2/test-threading2_2017-08-12t22-59-03-119680.log

^^^ Each of the log files above only contains print statements from the specific thread it was assigned to and also on the mobile app I got the correct output where there were two different ones being monitored and each one only had logs for its own thread