From 41247ac8de0a38edecf296f390d1be56f24ba764 Mon Sep 17 00:00:00 2001 From: peterdudfield Date: Mon, 2 Sep 2024 13:58:58 +0100 Subject: [PATCH 1/8] add sentry app --- pvnet_app/app.py | 23 +++++++++++++++++++++-- requirements.txt | 1 + 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/pvnet_app/app.py b/pvnet_app/app.py index f27f3db..e37348d 100644 --- a/pvnet_app/app.py +++ b/pvnet_app/app.py @@ -36,6 +36,7 @@ import pvnet from pvnet.models.base_model import BaseModel as PVNetBaseModel from pvnet.utils import GSPLocationLookup +import sentry_sdk import pvnet_app from pvnet_app.utils import ( @@ -55,6 +56,16 @@ preprocess_nwp_data, ) from pvnet_app.forecast_compiler import ForecastCompiler +from pvnet_app.sentry import traces_sampler + +# SENTRY + +sentry_sdk.init( + dsn=os.getenv("SENTRY_DSN", ""), + environment=os.getenv("ENVIRONMENT", "local"), + traces_sampler=traces_sampler, + server_name=f'uk-pvnet-app--{pvnet_app.__version__}' +) # --------------------------------------------------------------------------- # GLOBAL SETTINGS @@ -215,14 +226,22 @@ def app( ): """Inference function for production - This app expects these evironmental variables to be available: + This app expects these environmental variables to be available: - DB_URL - NWP_UKV_ZARR_PATH - NWP_ECMWF_ZARR_PATH - SATELLITE_ZARR_PATH + The following are options - PVNET_V2_VERSION, pvnet version, default is a version above - PVNET_V2_SUMMATION_VERSION, the pvnet version, default is above - - DOWNLOAD_SATELLITE, option to get satelite data. defaults to true + - USE_SATELLITE, option to get satellite data. defaults to true + - USE_ADJUSTER, option to use adjuster, defaults to true + - SAVE_GSP_SUM, option to save gsp sum, defaults to false + - RUN_EXTRA_MODELS, option to run extra models, defaults to false + - DAY_AHEAD_MODEL, option to use day ahead model, defaults to false + - SENTRY_DSN, optional link to sentry + - ENVIRONMENT, the environment this is running in, defaults to local + Args: t0 (datetime): Datetime at which forecast is made gsp_ids (array_like): List of gsp_ids to make predictions for. This list of GSPs are summed diff --git a/requirements.txt b/requirements.txt index 5a3c550..b9c0ef7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -18,3 +18,4 @@ typer testcontainers xesmf==0.8.7 huggingface-hub==0.24.1 +sentry-sdk = "^2.1.1" From 73e2bd8a2d21225fe652a51eebacc5c7767dedde Mon Sep 17 00:00:00 2001 From: peterdudfield Date: Mon, 2 Sep 2024 14:02:43 +0100 Subject: [PATCH 2/8] pin sentry --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index b9c0ef7..29f8b52 100644 --- a/requirements.txt +++ b/requirements.txt @@ -18,4 +18,4 @@ typer testcontainers xesmf==0.8.7 huggingface-hub==0.24.1 -sentry-sdk = "^2.1.1" +sentry-sdk==2.13.0 From c2ea1b23cef11625baf2e094f777f8480bb4d9f2 Mon Sep 17 00:00:00 2001 From: peterdudfield Date: Mon, 2 Sep 2024 14:39:30 +0100 Subject: [PATCH 3/8] add sentry function --- pvnet_app/sentry.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 pvnet_app/sentry.py diff --git a/pvnet_app/sentry.py b/pvnet_app/sentry.py new file mode 100644 index 0000000..6ead73c --- /dev/null +++ b/pvnet_app/sentry.py @@ -0,0 +1,24 @@ +""" Sentry tracer function """ +import os + + +def traces_sampler(sampling_context): + """ + Filter tracing for sentry logs. + + Examine provided context data (including parent decision, if any) + along with anything in the global namespace to compute the sample rate + or sampling decision for this transaction + """ + + if os.getenv("ENVIRONMENT", "local") == "local": + return 0.0 + elif "error" in sampling_context["transaction_context"]["name"]: + # These are important - take a big sample + return 1.0 + elif sampling_context["parent_sampled"] is True: + # These aren't something worth tracking - drop all transactions like this + return 0.0 + else: + # Default sample rate, report all errors + return 1.0 From f5d6c8d2c52beb23368e784a271cd7966b0ac3bc Mon Sep 17 00:00:00 2001 From: peterdudfield Date: Mon, 2 Sep 2024 14:49:10 +0100 Subject: [PATCH 4/8] update --- pvnet_app/app.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pvnet_app/app.py b/pvnet_app/app.py index e37348d..3c4a989 100644 --- a/pvnet_app/app.py +++ b/pvnet_app/app.py @@ -58,13 +58,11 @@ from pvnet_app.forecast_compiler import ForecastCompiler from pvnet_app.sentry import traces_sampler -# SENTRY - +# sentry sentry_sdk.init( dsn=os.getenv("SENTRY_DSN", ""), - environment=os.getenv("ENVIRONMENT", "local"), + environment=f'{os.getenv("ENVIRONMENT", "development")}__uk-pvnet-app--{pvnet_app.__version__}', traces_sampler=traces_sampler, - server_name=f'uk-pvnet-app--{pvnet_app.__version__}' ) # --------------------------------------------------------------------------- From db570cfd2b8e1629850925c3af3c393c1cbbfe8a Mon Sep 17 00:00:00 2001 From: peterdudfield Date: Mon, 2 Sep 2024 15:21:05 +0100 Subject: [PATCH 5/8] remove version --- pvnet_app/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pvnet_app/app.py b/pvnet_app/app.py index 3c4a989..3c1c4cb 100644 --- a/pvnet_app/app.py +++ b/pvnet_app/app.py @@ -61,7 +61,7 @@ # sentry sentry_sdk.init( dsn=os.getenv("SENTRY_DSN", ""), - environment=f'{os.getenv("ENVIRONMENT", "development")}__uk-pvnet-app--{pvnet_app.__version__}', + environment=f'{os.getenv("ENVIRONMENT", "development")}__uk-pvnet-app', traces_sampler=traces_sampler, ) From d34b39257d25441def420d85c1431325e2b1f64f Mon Sep 17 00:00:00 2001 From: peterdudfield Date: Mon, 2 Sep 2024 15:44:30 +0100 Subject: [PATCH 6/8] change default to local --- pvnet_app/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pvnet_app/app.py b/pvnet_app/app.py index 3c1c4cb..0ddb329 100644 --- a/pvnet_app/app.py +++ b/pvnet_app/app.py @@ -61,7 +61,7 @@ # sentry sentry_sdk.init( dsn=os.getenv("SENTRY_DSN", ""), - environment=f'{os.getenv("ENVIRONMENT", "development")}__uk-pvnet-app', + environment=f'{os.getenv("ENVIRONMENT", "local")}__uk-pvnet-app', traces_sampler=traces_sampler, ) From b500d8fc5e34dc0d1e8951e92048985310b4fd37 Mon Sep 17 00:00:00 2001 From: peterdudfield Date: Tue, 3 Sep 2024 09:01:41 +0100 Subject: [PATCH 7/8] set tag --- pvnet_app/app.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pvnet_app/app.py b/pvnet_app/app.py index 0ddb329..3490d92 100644 --- a/pvnet_app/app.py +++ b/pvnet_app/app.py @@ -61,9 +61,10 @@ # sentry sentry_sdk.init( dsn=os.getenv("SENTRY_DSN", ""), - environment=f'{os.getenv("ENVIRONMENT", "local")}__uk-pvnet-app', + environment=f'{os.getenv("ENVIRONMENT", "local")}', traces_sampler=traces_sampler, ) +sentry_sdk.set_tag("app_name", "pvnet_app") # --------------------------------------------------------------------------- # GLOBAL SETTINGS From 7548f915f476dc404dd387656b6d5bc6a59c2886 Mon Sep 17 00:00:00 2001 From: peterdudfield Date: Tue, 3 Sep 2024 17:20:37 +0100 Subject: [PATCH 8/8] simplify trace_sampler --- pvnet_app/app.py | 4 ++-- pvnet_app/sentry.py | 24 ------------------------ 2 files changed, 2 insertions(+), 26 deletions(-) delete mode 100644 pvnet_app/sentry.py diff --git a/pvnet_app/app.py b/pvnet_app/app.py index 3490d92..768fdbc 100644 --- a/pvnet_app/app.py +++ b/pvnet_app/app.py @@ -56,13 +56,13 @@ preprocess_nwp_data, ) from pvnet_app.forecast_compiler import ForecastCompiler -from pvnet_app.sentry import traces_sampler # sentry sentry_sdk.init( dsn=os.getenv("SENTRY_DSN", ""), environment=f'{os.getenv("ENVIRONMENT", "local")}', - traces_sampler=traces_sampler, + traces_sample_rate=1.0, + profiles_sample_rate=1.0, ) sentry_sdk.set_tag("app_name", "pvnet_app") diff --git a/pvnet_app/sentry.py b/pvnet_app/sentry.py deleted file mode 100644 index 6ead73c..0000000 --- a/pvnet_app/sentry.py +++ /dev/null @@ -1,24 +0,0 @@ -""" Sentry tracer function """ -import os - - -def traces_sampler(sampling_context): - """ - Filter tracing for sentry logs. - - Examine provided context data (including parent decision, if any) - along with anything in the global namespace to compute the sample rate - or sampling decision for this transaction - """ - - if os.getenv("ENVIRONMENT", "local") == "local": - return 0.0 - elif "error" in sampling_context["transaction_context"]["name"]: - # These are important - take a big sample - return 1.0 - elif sampling_context["parent_sampled"] is True: - # These aren't something worth tracking - drop all transactions like this - return 0.0 - else: - # Default sample rate, report all errors - return 1.0