Wei-Yu Chen | ad55cb8 | 2022-02-15 20:07:01 +0800 | [diff] [blame] | 1 | # SPDX-FileCopyrightText: 2020 The Magma Authors. |
| 2 | # SPDX-FileCopyrightText: 2022 Open Networking Foundation <support@opennetworking.org> |
| 3 | # |
| 4 | # SPDX-License-Identifier: BSD-3-Clause |
Wei-Yu Chen | 49950b9 | 2021-11-08 19:19:18 +0800 | [diff] [blame] | 5 | |
| 6 | import os |
| 7 | |
| 8 | import sentry_sdk |
| 9 | import snowflake |
| 10 | from configuration.service_configs import get_service_config_value |
| 11 | |
| 12 | CONTROL_PROXY = 'control_proxy' |
| 13 | SENTRY_URL = 'sentry_url_python' |
| 14 | SENTRY_SAMPLE_RATE = 'sentry_sample_rate' |
| 15 | COMMIT_HASH = 'COMMIT_HASH' |
| 16 | HWID = 'hwid' |
| 17 | SERVICE_NAME = 'service_name' |
| 18 | |
| 19 | |
| 20 | def sentry_init(service_name: str): |
| 21 | """Initialize connection and start piping errors to sentry.io.""" |
| 22 | sentry_url = get_service_config_value( |
| 23 | CONTROL_PROXY, |
| 24 | SENTRY_URL, |
| 25 | default='', |
| 26 | ) |
| 27 | if not sentry_url: |
| 28 | return |
| 29 | |
| 30 | sentry_sample_rate = get_service_config_value( |
| 31 | CONTROL_PROXY, |
| 32 | SENTRY_SAMPLE_RATE, |
| 33 | default=1.0, |
| 34 | ) |
| 35 | sentry_sdk.init( |
| 36 | dsn=sentry_url, |
| 37 | release=os.getenv(COMMIT_HASH), |
| 38 | traces_sample_rate=sentry_sample_rate, |
| 39 | ) |
| 40 | sentry_sdk.set_tag(HWID, snowflake.snowflake()) |
| 41 | sentry_sdk.set_tag(SERVICE_NAME, service_name) |