Skip to content

Commit

Permalink
added fastapi support and corresponding example
Browse files Browse the repository at this point in the history
  • Loading branch information
v-aparna committed Mar 22, 2023
1 parent 03a206d commit 9a5e2cd
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
data/
.vscode/
my_testing/
autometrics-venv/

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
12 changes: 4 additions & 8 deletions examples/fastapi-example.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,18 @@
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"}

@autometrics
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)
19 changes: 14 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
4 changes: 1 addition & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
[metadata]
author = fiberplane
author_email = [email protected]
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
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
packages=find_packages(),
install_requires=[
'prometheus_client',
'prometheus_api_client',
'python-dotenv'
]
)
2 changes: 2 additions & 0 deletions src/autometrics/autometrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand All @@ -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()
Expand Down

0 comments on commit 9a5e2cd

Please sign in to comment.