blob: eb433ed9a24ae29507bcda143ad493f0f024ae15 [file] [log] [blame]
Wei-Yu Chen49950b92021-11-08 19:19:18 +08001"""
2Copyright 2020 The Magma Authors.
3
4This source code is licensed under the BSD-style license found in the
5LICENSE file in the root directory of this source tree.
6
7Unless required by applicable law or agreed to in writing, software
8distributed under the License is distributed on an "AS IS" BASIS,
9WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10See the License for the specific language governing permissions and
11limitations under the License.
12"""
13
14import os
15
16import sentry_sdk
17import snowflake
18from configuration.service_configs import get_service_config_value
19
20CONTROL_PROXY = 'control_proxy'
21SENTRY_URL = 'sentry_url_python'
22SENTRY_SAMPLE_RATE = 'sentry_sample_rate'
23COMMIT_HASH = 'COMMIT_HASH'
24HWID = 'hwid'
25SERVICE_NAME = 'service_name'
26
27
28def 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)