Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Inital commit #1

Merged
merged 5 commits into from
Feb 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#custom
test.py
data/
autometrics/

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
20 changes: 20 additions & 0 deletions examples/example.py
Original file line number Diff line number Diff line change
@@ -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)
14 changes: 14 additions & 0 deletions prometheus.yaml
Original file line number Diff line number Diff line change
@@ -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']
6 changes: 6 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
Empty file added src/__init__.py
Empty file.
39 changes: 39 additions & 0 deletions src/autometrics.py
Original file line number Diff line number Diff line change
@@ -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