Skip to content

Commit f3ca1a1

Browse files
authored
Merge pull request #9 from autometrics-dev/caller
adding caller label to the counter
2 parents 8bb51d5 + 7f58877 commit f3ca1a1

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

examples/caller-example.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
print("hello")
2+
from prometheus_client import start_http_server
3+
4+
@autometrics
5+
def message():
6+
return "hello"
7+
8+
@autometrics
9+
def greet(name):
10+
m = message()
11+
greeting = f"hello {name}, {m}"
12+
return greeting
13+
14+
start_http_server(8080)
15+
while True:
16+
greet("john")

src/autometrics/autometrics.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import os
66
from functools import wraps
77

8-
prom_counter = Counter('function_calls_count', 'query??', ['function', 'module', 'result'])
8+
prom_counter = Counter('function_calls_count', 'query??', ['function', 'module', 'result', 'caller'])
99
prom_histogram = Histogram('function_calls_duration', 'query??', ['function', 'module'])
1010
# prom_guage = Gauge('function_calls_concurrent', 'query??', ['function', 'module']) # we are not doing gauge atm
1111

@@ -22,12 +22,13 @@ def autometrics(func):
2222
def wrapper(*args, **kwargs):
2323
func_name = func.__name__
2424
start_time = time.time()
25+
caller = get_caller_function(func)
2526
try:
2627
result = func(*args, **kwargs)
27-
prom_counter.labels(func_name, module_name, 'ok').inc()
28+
prom_counter.labels(func_name, module_name, 'ok', caller).inc()
2829
except Exception as e:
2930
result = e.__class__.__name__
30-
prom_counter.labels(func_name, module_name, 'error').inc()
31+
prom_counter.labels(func_name, module_name, 'error', caller).inc()
3132
duration = time.time() - start_time
3233
prom_histogram.labels(func_name, module_name).observe(duration)
3334
return result
@@ -50,4 +51,9 @@ def write_docs(func_name, module_name):
5051
for key, value in urls.items():
5152
docs = f"{docs}{key} : {value} \n\n"
5253
docs = f"{docs}-------------------------------------------\n"
53-
return docs
54+
return docs
55+
56+
def get_caller_function(func):
57+
caller_frame = inspect.stack()[2]
58+
caller_function_name = caller_frame[3]
59+
return caller_function_name

0 commit comments

Comments
 (0)