From 03a206d7566f8b5de15d7ff275e23891e736f601 Mon Sep 17 00:00:00 2001 From: Aparna Valsala Date: Tue, 21 Mar 2023 15:54:00 +0000 Subject: [PATCH 1/3] fastapi example initial commit --- examples/fastapi-example.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 examples/fastapi-example.py diff --git a/examples/fastapi-example.py b/examples/fastapi-example.py new file mode 100644 index 0000000..8c6e138 --- /dev/null +++ b/examples/fastapi-example.py @@ -0,0 +1,27 @@ +from fastapi import FastAPI, Response +import uvicorn +from autometrics.autometrics import autometrics +from prometheus_client import generate_latest + +app = FastAPI() + +@app.get("/metrics") +async def metrics(): + return Response(generate_latest()) + +@autometrics +@app.get("/") +async def read_root(): + print("This is inside the read_root") + do_something() + return {"Hello": "World"} + +@autometrics +def do_something(): + print("done") + +print(read_root.__doc__) +print(do_something.__doc__) + +if __name__ == "__main__": + uvicorn.run(app, host="localhost", port=8080) \ No newline at end of file From 9a5e2cd22ec4bea4a575a1001663d64c0de648d7 Mon Sep 17 00:00:00 2001 From: Aparna Valsala Date: Wed, 22 Mar 2023 16:28:09 +0000 Subject: [PATCH 2/3] added fastapi support and corresponding example --- .gitignore | 1 + examples/fastapi-example.py | 12 ++++-------- requirements.txt | 19 ++++++++++++++----- setup.cfg | 4 +--- setup.py | 1 - src/autometrics/autometrics.py | 2 ++ 6 files changed, 22 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index ea5e707..0f3420a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ data/ .vscode/ my_testing/ +autometrics-venv/ # Byte-compiled / optimized / DLL files __pycache__/ diff --git a/examples/fastapi-example.py b/examples/fastapi-example.py index 8c6e138..c3d4859 100644 --- a/examples/fastapi-example.py +++ b/examples/fastapi-example.py @@ -6,13 +6,12 @@ app = FastAPI() @app.get("/metrics") -async def metrics(): +def metrics(): return Response(generate_latest()) -@autometrics @app.get("/") -async def read_root(): - print("This is inside the read_root") +@autometrics +def read_root(): do_something() return {"Hello": "World"} @@ -20,8 +19,5 @@ async def read_root(): def do_something(): print("done") -print(read_root.__doc__) -print(do_something.__doc__) - -if __name__ == "__main__": +if __name__ == "__main__": uvicorn.run(app, host="localhost", port=8080) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index c9ded23..378631b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,16 @@ -certifi==2022.12.7 -charset-normalizer==2.1.1 +anyio==3.6.2 +build==0.10.0 +click==8.1.3 +fastapi==0.95.0 +h11==0.14.0 idna==3.4 +packaging==23.0 prometheus-client==0.16.0 -requests==2.28.1 -urllib3==1.26.13 -python-dotenv==0.21.1 +pydantic==1.10.6 +pyproject_hooks==1.0.0 +python-dotenv==1.0.0 +sniffio==1.3.0 +starlette==0.26.1 +typing_extensions==4.5.0 +urllib3==1.26.15 +uvicorn==0.21.1 diff --git a/setup.cfg b/setup.cfg index 59ce26e..8835496 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,7 +1,5 @@ [metadata] -author = fiberplane -author_email = hello@fiberplane.com -description = Autometrics package for python + description = Autometrics package for python long_description = file: README.md long_description_content_type = text/markdown url = https://github.com/autometrics-dev/autometrics-py diff --git a/setup.py b/setup.py index a36d521..97613c4 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,6 @@ packages=find_packages(), install_requires=[ 'prometheus_client', - 'prometheus_api_client', 'python-dotenv' ] ) \ No newline at end of file diff --git a/src/autometrics/autometrics.py b/src/autometrics/autometrics.py index 9e88250..28e2c9f 100644 --- a/src/autometrics/autometrics.py +++ b/src/autometrics/autometrics.py @@ -3,6 +3,7 @@ import inspect from .prometheus_url import Generator import os +from functools import wraps prom_counter = Counter('function_calls_count', 'query??', ['function', 'module', 'result']) prom_histogram = Histogram('function_calls_duration', 'query??', ['function', 'module']) @@ -17,6 +18,7 @@ def autometrics(func): else: classname = func.__qualname__.rsplit('.', 1)[0] module_name = f"{filename}.{classname}" + @wraps(func) def wrapper(*args, **kwargs): func_name = func.__name__ start_time = time.time() From 0b4e97fcc631542e0ea36e1003731cfc2d37a40e Mon Sep 17 00:00:00 2001 From: Aparna Valsala Date: Wed, 22 Mar 2023 17:13:48 +0000 Subject: [PATCH 3/3] minor corrections --- requirements.txt | 21 +++++++++++++++++++++ setup.cfg | 2 +- setup.py | 2 +- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index 378631b..00130ae 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,16 +1,37 @@ anyio==3.6.2 +bleach==6.0.0 build==0.10.0 +certifi==2022.12.7 +charset-normalizer==3.1.0 click==8.1.3 +docutils==0.19 fastapi==0.95.0 h11==0.14.0 idna==3.4 +importlib-metadata==6.1.0 +jaraco.classes==3.2.3 +keyring==23.13.1 +markdown-it-py==2.2.0 +mdurl==0.1.2 +more-itertools==9.1.0 packaging==23.0 +pkginfo==1.9.6 prometheus-client==0.16.0 pydantic==1.10.6 +Pygments==2.14.0 pyproject_hooks==1.0.0 python-dotenv==1.0.0 +readme-renderer==37.3 +requests==2.28.2 +requests-toolbelt==0.10.1 +rfc3986==2.0.0 +rich==13.3.2 +six==1.16.0 sniffio==1.3.0 starlette==0.26.1 +twine==4.0.2 typing_extensions==4.5.0 urllib3==1.26.15 uvicorn==0.21.1 +webencodings==0.5.1 +zipp==3.15.0 diff --git a/setup.cfg b/setup.cfg index 8835496..6c1f319 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,5 +1,5 @@ [metadata] - description = Autometrics package for python +description = Autometrics package for python long_description = file: README.md long_description_content_type = text/markdown url = https://github.com/autometrics-dev/autometrics-py diff --git a/setup.py b/setup.py index 97613c4..66be6bf 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name='autometrics', - version='0.1', + version='0.2', packages=find_packages(), install_requires=[ 'prometheus_client',