Skip to content

Commit e291965

Browse files
Add optional APITally monitoring support to API (#348)
* Add optional APITally monitoring support to API * Address review feedback: conditionally enable middleware, default env local, add logging options * Change default APITally environment to open_quartz_local
1 parent ee083b7 commit e291965

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,16 @@ and how we can make improvements in the future.
8989
Note that any latitudes and longitudes are rounded to 2 decimals places in order to anonymize the data.
9090
If you would like to disable this logging, you can do so by setting the environment variable `QUARTZ_SOLAR_FORECAST_LOGGING` to `False`.
9191

92+
### API Monitoring (APITally)
93+
94+
The FastAPI application supports optional monitoring via APITally.
95+
96+
To enable API request monitoring, set the following environment variables before starting the API:
97+
98+
```bash
99+
export APITALLY_CLIENT_ID=your_client_id
100+
export APITALLY_ENVIRONMENT=open_quartz_local
101+
```
92102

93103
## Model
94104

api/v1/api.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from importlib.metadata import version
66

77
import pandas as pd
8+
from apitally.fastapi import ApitallyMiddleware
89
from fastapi import FastAPI
910
from fastapi.middleware.cors import CORSMiddleware
1011
from pydantic import BaseModel, Field
@@ -95,6 +96,19 @@
9596

9697
app = FastAPI(description=description, version=__version__, title="Open Quartz Solar Forecast API")
9798

99+
client_id = os.getenv("APITALLY_CLIENT_ID")
100+
101+
if client_id:
102+
app.add_middleware(
103+
ApitallyMiddleware,
104+
client_id=client_id,
105+
environment=os.getenv("APITALLY_ENVIRONMENT", "open_quartz_local"),
106+
enable_request_logging=True,
107+
log_request_headers=True,
108+
log_request_body=True,
109+
log_response_body=True,
110+
capture_logs=True,
111+
)
98112
# CORS middleware setup
99113
origins = [
100114
"*",

pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ dependencies = [
2727
"pydantic_settings",
2828
"httpx",
2929
"sentry_sdk",
30-
"huggingface_hub==0.17.3"
30+
"huggingface_hub==0.17.3",
31+
"apitally[fastapi]>=0.22.3"
3132
]
3233

3334
[project.urls]
@@ -51,6 +52,7 @@ dev = [
5152
"matplotlib",
5253
"zipfile36",
5354
"pytest",
55+
"huggingface_hub==0.17.3"
5456
]
5557
inverters = ["ocf_vrmapi"] # victron
5658
all = [
@@ -60,6 +62,7 @@ all = [
6062
"gdown==5.1.0",
6163
"fastapi",
6264
"pytest",
65+
"huggingface_hub==0.17.3"
6366
]
6467

6568
[tool.mypy]

0 commit comments

Comments
 (0)