openclimatefix / ocf-infrastructure

Infrastructure code for OCF's cloud environments
3 stars 6 forks source link

Add Sentry logs to all backend components #540

Open peterdudfield opened 5 months ago

peterdudfield commented 5 months ago

Detailed Description

It would be great to have sentry logs in all back end components

Context

Possible Implementation

SImilar to the api

https://github.com/openclimatefix/pv-site-api/blob/main/pv_site_api/main.py#L89C1-L93C2


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":
        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
        return 0.05

sentry_sdk.init(
    dsn=os.getenv("SENTRY_DSN"),
    environment=os.getenv("ENVIRONMENT", "local"),
    traces_sampler=traces_sampler,
)

this bit of code needs to sit at the start of the main app, and sentry_sdk be added to the requirements file

component code ready dev pro
GSP Consumer yes 1.1.19 yes
NWP Consumer 90% needs this yes
Satellite Consumer yes 2.12.8
[uk-pvnet-app]() yes yes 2.4.1 Not working
pvnet app DA yes yes 2.4.1 Not working
National xg yes 1.0.38 yes yes
forecast sites yes 1.0.10 yes
blend yes yes 1.0.2 yes
Metrics yes 1.2.21 yes yes
UK GPS api yes yes yes
UK sites api yes yes yes
India api yes 1.0.41 yes
india ruvnl consumer 0.1.12 yes yes
india forecast yes TODO add version and name yes yes
india satellite yes 2.12.8 yes yes
india nwp yes 0.5.25 yes
PVConsumer yes
peterdudfield commented 5 months ago

@braddf I've put this here, as other people might be able to contribute to this

peterdudfield commented 4 months ago

Ive tested the above code in the india-forecast-app, so it can be rolled out to other repos now

Plomo-02 commented 4 months ago

hello, can I be assigned to this issue?

Plomo-02 commented 4 months ago

@peterdudfield can I have more info about the sentry_sdk version repos need?

peterdudfield commented 4 months ago

what do you mean version?

https://github.com/openclimatefix/india-forecast-app/pull/75/files this is a good example of what would be great to do to the other repos

peterdudfield commented 3 months ago

for GSPConumer - https://github.com/openclimatefix/GSPConsumer/pull/76

peterdudfield commented 3 months ago

https://github.com/openclimatefix/Satip/pull/280

peterdudfield commented 1 month ago

Here's a good example - https://github.com/openclimatefix/uk-pvnet-app/pull/125

peterdudfield commented 1 month ago

A nice simple way to do this is (this should be put in the main app)

import sentry_sdk
import os

sentry_sdk.init(
    dsn=os.getenv("SENTRY_DSN", ""),
    environment=f'{os.getenv("ENVIRONMENT", "local")}',
    traces_sample_rate=1.0,
    profiles_sample_rate=1.0,
)
sentry_sdk.set_tag("app_name", APP_NAME)
sentry_sdk.set_tag("version", VERSION)
peterdudfield commented 1 week ago

Currently deployed on uk-pvnet-app on development, but errors not showing up in sentry. Weird