Skip to content

Commit

Permalink
log usage of package
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdudfield committed Dec 9, 2024
1 parent f39fef9 commit 8ce3f8f
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ conda install -c conda-forge pyresample

This can solve the [bug: \_\_\_kmpc_for_static_fini](https://github.com/openclimatefix/Open-Source-Quartz-Solar-Forecast/issues/32).

### Logging

The package logs when `run_forecast` is used. This is useful for OCF to determine how the package is being used
and how we can make improvements in the future.
Note that any latitude and longitude is rounded to 2 decimals places for in order to anonymize the data.
If you would like to disable this logging, you can do so by setting the environment variable `QUARTZ_SOLAR_FORECAST_LOGGING` to `False`.


## Model

Two models are currently available to make predictions.
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dependencies = [
"uvicorn",
"pydantic_settings",
"httpx",
"sentry_sdk"
]

[project.urls]
Expand Down
6 changes: 6 additions & 0 deletions quartz_solar_forecast/forecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from quartz_solar_forecast.data import get_nwp, make_pv_data
from quartz_solar_forecast.forecasts import forecast_v1_tilt_orientation, TryolabsSolarPowerPredictor
from quartz_solar_forecast.pydantic_models import PVSite
from quartz_solar_forecast.utils.sentry_logging import write_sentry

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -124,6 +125,11 @@ def run_forecast(
:return: The PV forecast of the site for time (ts) for 48 hours
"""

# log usage to sentry, if you dont want to log usage to sentry, you can
# 1. set environmental variable QUARTZ_SOLAR_FORECAST_LOGGING='false', or
# 2. comment out this line
write_sentry({"site": site, "model": model, "ts": ts, "nwp_source": nwp_source})

if model == "gb":
return predict_ocf(site, None, ts, nwp_source)

Expand Down
42 changes: 42 additions & 0 deletions quartz_solar_forecast/utils/sentry_logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
""" Log usage of this package to Sentry """

import sentry_sdk
from quartz_solar_forecast.pydantic_models import PVSite

import os

quartz_solar_forecast_logging = bool(os.getenv("QUARTZ_SOLAR_FORECAST_LOGGING", True))

SENTRY_DSN = 'https://[email protected]/4508439933157376'
sentry_sdk.init(
dsn=SENTRY_DSN,
)


def write_sentry(params):
"""
Log usage of this package to Sentry
"""

if not quartz_solar_forecast_logging:
return

try:
for key, value in params.items():

# we want to make sure we don't store the exact location of the site
if isinstance(value, PVSite):
value.round_latitude_and_longitude()

# set sentry tag
sentry_sdk.set_tag(key, value)

message = "quartz_solar_forecast is being used"

if os.getenv("PYTEST_CURRENT_TEST") is not None:
message += ": in CI tests"

sentry_sdk.capture_message(message)

except Exception as _: # noqa
pass

0 comments on commit 8ce3f8f

Please sign in to comment.