Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Brett Beutell committed May 9, 2023
0 parents commit a29a178
Show file tree
Hide file tree
Showing 7 changed files with 271 additions and 0 deletions.
147 changes: 147 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
env/
venv/
.venv/
.DS_Store

personal-notes.md

shame-deps/*.whl


#custom
data/
.vscode/
my_testing/
autometrics-venv/

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

.DS_Store
69 changes: 69 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
## Setup

### Setting up local prometheus

Follow instructions in autometrics-dev. I used Docker:

```sh
docker pull prom/prometheus
# I'm using port 9092 for prometheus because 9090 is already taken on my machine
docker run \
-p 9092:9090 \
-v /absolute/path/to/prometheus.yml:/etc/prometheus/prometheus.yml \
prom/prometheus
```

where the `prometheus.yml` file is:

```yaml
scrape_configs:
- job_name: python-autometrics-example-fastapi
metrics_path: /metrics
static_configs:
- targets: ["localhost:8000"]
# For a real deployment, you would want the scrape interval to be
# longer but for testing, you want the data to show up quickly
scrape_interval: 200ms
```
### Configure app to use prometheus
Configure `.env` to use the url to your local prometheus server:

```sh
# Again, I'm using 8063 because 9090 is already taken on my machine
PROMETHEUS_URL=http://localhost:8063
```

### Running the app

Create and activate virtual env

```sh
python3 -m venv env
source env/bin/activate
```

Install dependencies

```sh
pip install -r requirements.txt
```

Run app

```sh
uvicorn fastapi-example:app --reload
```

### Install and Configure Autometrics VSCode Extension

...

Update the VSCode Extension settings to use the url to your local prometheus server. Since I'm not using the default port, I need to update the settings:

```json
{
"autometrics.prometheusUrl": "http://localhost:8063"
}
```
6 changes: 6 additions & 0 deletions autometrics.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
app_name: "python-autometrics-example-fastapi" # Becomes the job name when pushing to aggregation gateway
publish_method: "push"
scrape_config:
scrape_interval: 500ms
static_configs: # ignored when "publish_method" is "push"
- targets: ["localhost:8080"] # or wherever your app runs
18 changes: 18 additions & 0 deletions fastapi-example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from fastapi import FastAPI
import uvicorn
from autometrics import autometrics

app = FastAPI()

@app.get("/")
@autometrics
def read_root():
do_something()
return {"Hello": "World"}

@autometrics
def do_something():
print("I did something")

if __name__ == "__main__":
uvicorn.run(app, host="localhost", port=8080)
15 changes: 15 additions & 0 deletions prometheus.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
scrape_configs:
- job_name: python-autometrics-example-fastapi
metrics_path: /metrics
static_configs:
- targets: ["host.docker.internal:8000"]
# For a real deployment, you would want the scrape interval to be
# longer but for testing, you want the data to show up quickly
scrape_interval: 500ms
- job_name: python-autometrics-example-example
metrics_path: /metrics
static_configs:
- targets: ["host.docker.internal:8080"]
# For a real deployment, you would want the scrape interval to be
# longer but for testing, you want the data to show up quickly
scrape_interval: 500ms
16 changes: 16 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
anyio==3.6.2
# autometrics==0.4
shame-deps/autometrics-0.5-py3-none-any.whl
# -e git+ssh://[email protected]/autometrics-dev/autometrics-py.git@0a11a33315bba5ba843b6b543e464b2871dd5206#egg=autometrics
# -e ../autometrics-py
click==8.1.3
fastapi==0.95.0
h11==0.14.0
idna==3.4
prometheus-client==0.16.0
pydantic==1.10.7
python-dotenv==1.0.0
sniffio==1.3.0
starlette==0.26.1
typing_extensions==4.5.0
uvicorn==0.21.1
Empty file added shame-deps/.gitkeep
Empty file.

0 comments on commit a29a178

Please sign in to comment.