-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathconfig.py
More file actions
209 lines (171 loc) · 6.76 KB
/
config.py
File metadata and controls
209 lines (171 loc) · 6.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# ----------------------------------------------------------------------------
# Copyright (C) 2021-2022 Deepchecks (https://www.deepchecks.com)
#
# This file is part of Deepchecks.
# Deepchecks is distributed under the terms of the GNU Affero General
# Public License (version 3 or later).
# You should have received a copy of the GNU Affero General Public License
# along with Deepchecks. If not, see <http://www.gnu.org/licenses/>.
# ----------------------------------------------------------------------------
"""Module defining the configuration for the deepchecks_monitoring package."""
import json
import os
import pathlib
import secrets
import typing as t
from enum import Enum
import boto3
from aiokafka.helpers import create_ssl_context
from pydantic import BaseSettings, PostgresDsn, RedisDsn, validator
from pydantic.networks import AnyHttpUrl
__all__ = [
'Settings',
'tags_metadata',
'Tags',
'DatabaseSettings',
'RedisSettings',
'KafkaSettings',
'BaseDeepchecksSettings',
'EmailSettings'
]
PROJECT_DIR = pathlib.Path(__file__).parent.parent.absolute()
class BaseDeepchecksSettings(BaseSettings):
"""Base class for all config classes."""
def __init__(self, *args, **kwargs): # pylint: disable=useless-super-delegation
super().__init__(*args, **kwargs)
class Config:
"""Settings configuration."""
env_file = '.env'
env_file_encoding = 'utf-8'
class EmailSettings(BaseDeepchecksSettings):
"""Settings for mail service."""
deepchecks_email: str = 'app@deepchecks.com'
email_smtp_host: t.Optional[str]
email_smtp_port: int = 25
email_smtp_username: t.Optional[str]
email_smtp_password: t.Optional[str]
class KafkaSettings(BaseDeepchecksSettings):
"""Settings for kafka usage for data ingestion."""
kafka_host: t.Optional[str] = None
kafka_security_protocol: t.Optional[str] = None
kafka_sasl_mechanism: t.Optional[str] = None
kafka_username: t.Optional[str] = None
kafka_password: t.Optional[str] = None
kafka_replication_factor: int = 1
kafka_max_metadata_age: int = 300000
@property
def kafka_params(self):
"""Get connection parameters for kafka."""
return {
'bootstrap_servers': self.kafka_host,
'security_protocol': self.kafka_security_protocol,
'sasl_mechanism': self.kafka_sasl_mechanism,
'sasl_plain_username': self.kafka_username,
'sasl_plain_password': self.kafka_password,
'ssl_context': create_ssl_context(),
'metadata_max_age_ms': self.kafka_max_metadata_age
}
def get_postgres_uri(postgres_secret_name, amazon_region_name) -> str:
"""Get postgres uri from AWS secrets manager."""
session = boto3.session.Session()
client = session.client(
service_name='secretsmanager',
region_name=amazon_region_name
)
get_secret_value_response = client.get_secret_value(SecretId=postgres_secret_name)
secret_dict = json.loads(get_secret_value_response['SecretString'])
database_uri = f'postgresql://{secret_dict["username"]}:{secret_dict["password"]}@{secret_dict["host"]}:' \
f'{secret_dict["port"]}/{secret_dict["dbname"]}'
return database_uri
class DatabaseSettings(BaseSettings):
"""Database settings."""
# The following two fields are used to get the database_uri from AWS secrets manager.
postgres_secret_name: str = None
amazon_region_name: str = None
database_uri: PostgresDsn
echo_sql: bool = False
@property
def async_database_uri(self) -> PostgresDsn:
"""Return async postgres connection string."""
return t.cast(PostgresDsn, self.database_uri.replace(
'postgresql',
'postgresql+asyncpg'
))
@validator('database_uri', pre=True)
def validate_database_uri(cls, v, values): # pylint: disable=no-self-argument
"""
Validate allows us to try to get the database_uri from AWS secrets manager.
The validator allows us to check if postgres_secret_name and amazon_region_name are set in current environment
if they are, we should be able to get the database_uri from AWS secrets manager,
otherwise, we should get the database_uri from the environment variables.
"""
postgres_secret_name = values.get('postgres_secret_name')
amazon_region_name = values.get('amazon_region_name')
if postgres_secret_name is not None and amazon_region_name is not None:
return get_postgres_uri(postgres_secret_name, amazon_region_name)
else:
return v
class RedisSettings(BaseDeepchecksSettings):
"""Redis settings."""
redis_uri: t.Optional[RedisDsn] = None
socket_connect_timeout: int = 5
socket_timeout: int = 5
socket_keepalive: bool = True
retry_attempts: int = 6
cluster_error_retry_attempts: int = 2
class Settings(
DatabaseSettings,
EmailSettings,
KafkaSettings,
RedisSettings
):
"""Settings for the deepchecks_monitoring package."""
assets_folder: pathlib.Path = PROJECT_DIR / 'assets'
is_cloud: bool = False
is_on_prem: bool = False
deployment_url: AnyHttpUrl = 'http://localhost:8000'
auth_jwt_secret: t.Optional[str] = secrets.token_hex(20)
oauth_url: AnyHttpUrl
oauth_client_id: str
oauth_client_secret: str
mixpanel_id: str | None
enable_analytics: bool = True
parallel_check_executor_flag: bool = True
init_local_ray_instance: str | None = None
total_number_of_check_executor_actors: int = os.cpu_count() or 8
class Tags(Enum):
"""Tags for the deepchecks_monitoring package."""
USERS = 'Users'
MODELS = 'Models'
CHECKS = 'Checks'
MONITORS = 'Monitors'
ALERTS = 'Alerts'
DATA_ALERTS = 'Data Alerts'
DATA = 'Data'
CONFIG = 'Configuration'
DATA_SOURCES = 'Data Sources'
tags_metadata = [
{
'name': Tags.MODELS.value,
'description': 'APIs for interacting with model entities.'
},
{
'name': Tags.CHECKS.value,
'description': 'APIs for interacting with check entities. Includes adding/updating checks, '
'and retrieving check results.',
},
{
'name': Tags.MONITORS.value,
'description': 'APIs for interacting with monitor entities. Includes adding/updating monitors within a '
'dashboard, getting dasbboard data, and retrieving monitor results.',
},
{
'name': Tags.ALERTS.value,
'description': 'APIs for interacting with alert/alert-rule entities. Includes adding/updating alert-rules, '
'and retrieving/counting active alerts results.'
},
{
'name': Tags.DATA.value,
'description': 'APIs for sending data to the deepchecks_monitoring service.'
}
]