diff --git a/examples/caller-example.py b/examples/caller-example.py new file mode 100644 index 0000000..0062a82 --- /dev/null +++ b/examples/caller-example.py @@ -0,0 +1,16 @@ +print("hello") +from prometheus_client import start_http_server + +@autometrics +def message(): + return "hello" + +@autometrics +def greet(name): + m = message() + greeting = f"hello {name}, {m}" + return greeting + +start_http_server(8080) +while True: + greet("john") \ No newline at end of file diff --git a/src/autometrics/autometrics.py b/src/autometrics/autometrics.py index 28e2c9f..575d289 100644 --- a/src/autometrics/autometrics.py +++ b/src/autometrics/autometrics.py @@ -5,7 +5,7 @@ import os from functools import wraps -prom_counter = Counter('function_calls_count', 'query??', ['function', 'module', 'result']) +prom_counter = Counter('function_calls_count', 'query??', ['function', 'module', 'result', 'caller']) prom_histogram = Histogram('function_calls_duration', 'query??', ['function', 'module']) # prom_guage = Gauge('function_calls_concurrent', 'query??', ['function', 'module']) # we are not doing gauge atm @@ -22,12 +22,13 @@ def autometrics(func): def wrapper(*args, **kwargs): func_name = func.__name__ start_time = time.time() + caller = get_caller_function(func) try: result = func(*args, **kwargs) - prom_counter.labels(func_name, module_name, 'ok').inc() + prom_counter.labels(func_name, module_name, 'ok', caller).inc() except Exception as e: result = e.__class__.__name__ - prom_counter.labels(func_name, module_name, 'error').inc() + prom_counter.labels(func_name, module_name, 'error', caller).inc() duration = time.time() - start_time prom_histogram.labels(func_name, module_name).observe(duration) return result @@ -50,4 +51,9 @@ def write_docs(func_name, module_name): for key, value in urls.items(): docs = f"{docs}{key} : {value} \n\n" docs = f"{docs}-------------------------------------------\n" - return docs \ No newline at end of file + return docs + +def get_caller_function(func): + caller_frame = inspect.stack()[2] + caller_function_name = caller_frame[3] + return caller_function_name \ No newline at end of file