diff --git a/.gitignore b/.gitignore index b6e4761..92c1127 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,8 @@ +#custom +test.py +data/ +autometrics/ + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/examples/example.py b/examples/example.py new file mode 100644 index 0000000..f69d329 --- /dev/null +++ b/examples/example.py @@ -0,0 +1,20 @@ +from prometheus_client import start_http_server +import time +import autometrics + +@autometrics.function_calls_count +@autometrics.function_calls_duration +@autometrics.function_calls_concurrent + +def example(): + start_time = time.time() + print('This is an example function') + end_time = time.time() + duration = end_time - start_time + print('Duration:', duration) + +start_http_server(8080) + +while True: + example() + time.sleep(1) \ No newline at end of file diff --git a/prometheus.yaml b/prometheus.yaml new file mode 100644 index 0000000..2dd330e --- /dev/null +++ b/prometheus.yaml @@ -0,0 +1,14 @@ +global: + scrape_interval: 15s + evaluation_interval: 15s + +scrape_configs: + - job_name: 'prometheus' + scrape_interval: 5s + static_configs: + - targets: ['localhost:9090'] + + - job_name: 'myservice' + scrape_interval: 5s + static_configs: + - targets: ['localhost:8080'] diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..c25a6cc --- /dev/null +++ b/requirements.txt @@ -0,0 +1,6 @@ +certifi==2022.12.7 +charset-normalizer==2.1.1 +idna==3.4 +prometheus-client==0.16.0 +requests==2.28.1 +urllib3==1.26.13 diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/autometrics.py b/src/autometrics.py new file mode 100644 index 0000000..4d1e7ff --- /dev/null +++ b/src/autometrics.py @@ -0,0 +1,39 @@ +from prometheus_client import Counter, Histogram, Gauge +import time +import inspect +import functools + +function_calls_counter = Counter('function_calls_count', 'query??', ['function', 'module']) +function_calls_histogram = Histogram('function_calls_duration', 'query??', ['function', 'module']) +function_calls_gauge = Gauge('function_calls_concurrent', 'query??', ['function', 'module']) + +def function_calls_count(func): + @functools.wraps(func) + def wrapper(*args, **kwargs): + func_name = func.__name__ + module_name = inspect.getmodule(func).__name__ + function_calls_counter.labels(func_name, module_name).inc() + result = func(*args, **kwargs) + return result + return wrapper + +def function_calls_duration(func): + def wrapper(*args, **kwargs): + func_name = func.__name__ + module_name = inspect.getmodule(func).__name__ + start_time = time.time() + result = func(*args, **kwargs) + duration = time.time() - start_time + function_calls_histogram.labels(func_name, module_name).observe(duration) + return result + return wrapper + +def function_calls_concurrent(func): + @functools.wraps(func) + def wrapper(*args, **kwargs): + func_name = func.__name__ + module_name = inspect.getmodule(func).__name__ + function_calls_gauge.labels(func_name, module_name).set(time.time()) + result = func(*args, **kwargs) + return result + return wrapper