Skip to content

Commit

Permalink
adding caller label to the counter
Browse files Browse the repository at this point in the history
  • Loading branch information
v-aparna committed Mar 24, 2023
1 parent 8bb51d5 commit 7f58877
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
16 changes: 16 additions & 0 deletions examples/caller-example.py
Original file line number Diff line number Diff line change
@@ -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")
14 changes: 10 additions & 4 deletions src/autometrics/autometrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
return docs

def get_caller_function(func):
caller_frame = inspect.stack()[2]
caller_function_name = caller_frame[3]
return caller_function_name

0 comments on commit 7f58877

Please sign in to comment.