Wei-Yu Chen | 49950b9 | 2021-11-08 19:19:18 +0800 | [diff] [blame^] | 1 | """ |
| 2 | Copyright 2020 The Magma Authors. |
| 3 | |
| 4 | This source code is licensed under the BSD-style license found in the |
| 5 | LICENSE file in the root directory of this source tree. |
| 6 | |
| 7 | Unless required by applicable law or agreed to in writing, software |
| 8 | distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | See the License for the specific language governing permissions and |
| 11 | limitations under the License. |
| 12 | """ |
| 13 | |
| 14 | import os |
| 15 | |
| 16 | import sentry_sdk |
| 17 | import snowflake |
| 18 | from configuration.service_configs import get_service_config_value |
| 19 | |
| 20 | CONTROL_PROXY = 'control_proxy' |
| 21 | SENTRY_URL = 'sentry_url_python' |
| 22 | SENTRY_SAMPLE_RATE = 'sentry_sample_rate' |
| 23 | COMMIT_HASH = 'COMMIT_HASH' |
| 24 | HWID = 'hwid' |
| 25 | SERVICE_NAME = 'service_name' |
| 26 | |
| 27 | |
| 28 | def sentry_init(service_name: str): |
| 29 | """Initialize connection and start piping errors to sentry.io.""" |
| 30 | sentry_url = get_service_config_value( |
| 31 | CONTROL_PROXY, |
| 32 | SENTRY_URL, |
| 33 | default='', |
| 34 | ) |
| 35 | if not sentry_url: |
| 36 | return |
| 37 | |
| 38 | sentry_sample_rate = get_service_config_value( |
| 39 | CONTROL_PROXY, |
| 40 | SENTRY_SAMPLE_RATE, |
| 41 | default=1.0, |
| 42 | ) |
| 43 | sentry_sdk.init( |
| 44 | dsn=sentry_url, |
| 45 | release=os.getenv(COMMIT_HASH), |
| 46 | traces_sample_rate=sentry_sample_rate, |
| 47 | ) |
| 48 | sentry_sdk.set_tag(HWID, snowflake.snowflake()) |
| 49 | sentry_sdk.set_tag(SERVICE_NAME, service_name) |