cledar-sdk 2.0.2__py3-none-any.whl → 2.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cledar/__init__.py +1 -0
- cledar/kafka/README.md +239 -0
- cledar/kafka/__init__.py +42 -0
- cledar/kafka/clients/base.py +117 -0
- cledar/kafka/clients/consumer.py +138 -0
- cledar/kafka/clients/producer.py +97 -0
- cledar/kafka/config/schemas.py +262 -0
- cledar/kafka/exceptions.py +17 -0
- cledar/kafka/handlers/dead_letter.py +88 -0
- cledar/kafka/handlers/parser.py +83 -0
- cledar/kafka/logger.py +5 -0
- cledar/kafka/models/input.py +17 -0
- cledar/kafka/models/message.py +14 -0
- cledar/kafka/models/output.py +12 -0
- cledar/kafka/tests/.env.test.kafka +3 -0
- cledar/kafka/tests/README.md +216 -0
- cledar/kafka/tests/conftest.py +104 -0
- cledar/kafka/tests/integration/__init__.py +1 -0
- cledar/kafka/tests/integration/conftest.py +78 -0
- cledar/kafka/tests/integration/helpers.py +47 -0
- cledar/kafka/tests/integration/test_consumer_integration.py +375 -0
- cledar/kafka/tests/integration/test_integration.py +394 -0
- cledar/kafka/tests/integration/test_producer_consumer_interaction.py +388 -0
- cledar/kafka/tests/integration/test_producer_integration.py +217 -0
- cledar/kafka/tests/unit/__init__.py +1 -0
- cledar/kafka/tests/unit/test_base_kafka_client.py +391 -0
- cledar/kafka/tests/unit/test_config_validation.py +609 -0
- cledar/kafka/tests/unit/test_dead_letter_handler.py +443 -0
- cledar/kafka/tests/unit/test_error_handling.py +674 -0
- cledar/kafka/tests/unit/test_input_parser.py +310 -0
- cledar/kafka/tests/unit/test_input_parser_comprehensive.py +489 -0
- cledar/kafka/tests/unit/test_utils.py +25 -0
- cledar/kafka/tests/unit/test_utils_comprehensive.py +408 -0
- cledar/kafka/utils/callbacks.py +28 -0
- cledar/kafka/utils/messages.py +39 -0
- cledar/kafka/utils/topics.py +15 -0
- cledar/kserve/README.md +352 -0
- cledar/kserve/__init__.py +5 -0
- cledar/kserve/tests/__init__.py +0 -0
- cledar/kserve/tests/test_utils.py +64 -0
- cledar/kserve/utils.py +30 -0
- cledar/logging/README.md +53 -0
- cledar/logging/__init__.py +5 -0
- cledar/logging/tests/test_universal_plaintext_formatter.py +249 -0
- cledar/logging/universal_plaintext_formatter.py +99 -0
- cledar/monitoring/README.md +71 -0
- cledar/monitoring/__init__.py +5 -0
- cledar/monitoring/monitoring_server.py +156 -0
- cledar/monitoring/tests/integration/test_monitoring_server_int.py +162 -0
- cledar/monitoring/tests/test_monitoring_server.py +59 -0
- cledar/nonce/README.md +99 -0
- cledar/nonce/__init__.py +5 -0
- cledar/nonce/nonce_service.py +62 -0
- cledar/nonce/tests/__init__.py +0 -0
- cledar/nonce/tests/test_nonce_service.py +136 -0
- cledar/redis/README.md +536 -0
- cledar/redis/__init__.py +17 -0
- cledar/redis/async_example.py +112 -0
- cledar/redis/example.py +67 -0
- cledar/redis/exceptions.py +25 -0
- cledar/redis/logger.py +5 -0
- cledar/redis/model.py +14 -0
- cledar/redis/redis.py +764 -0
- cledar/redis/redis_config_store.py +333 -0
- cledar/redis/tests/test_async_integration_redis.py +158 -0
- cledar/redis/tests/test_async_redis_service.py +380 -0
- cledar/redis/tests/test_integration_redis.py +119 -0
- cledar/redis/tests/test_redis_service.py +319 -0
- cledar/storage/README.md +529 -0
- cledar/storage/__init__.py +6 -0
- cledar/storage/constants.py +5 -0
- cledar/storage/exceptions.py +79 -0
- cledar/storage/models.py +41 -0
- cledar/storage/object_storage.py +1274 -0
- cledar/storage/tests/conftest.py +18 -0
- cledar/storage/tests/test_abfs.py +164 -0
- cledar/storage/tests/test_integration_filesystem.py +359 -0
- cledar/storage/tests/test_integration_s3.py +453 -0
- cledar/storage/tests/test_local.py +384 -0
- cledar/storage/tests/test_s3.py +521 -0
- {cledar_sdk-2.0.2.dist-info → cledar_sdk-2.1.0.dist-info}/METADATA +1 -1
- cledar_sdk-2.1.0.dist-info/RECORD +84 -0
- cledar_sdk-2.0.2.dist-info/RECORD +0 -4
- {cledar_sdk-2.0.2.dist-info → cledar_sdk-2.1.0.dist-info}/WHEEL +0 -0
- {cledar_sdk-2.0.2.dist-info → cledar_sdk-2.1.0.dist-info}/licenses/LICENSE +0 -0
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
# pylint: disable=unused-argument, protected-access
|
|
2
|
+
import logging
|
|
3
|
+
import os
|
|
4
|
+
import tempfile
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
import pytest
|
|
8
|
+
|
|
9
|
+
from cledar.logging.universal_plaintext_formatter import UniversalPlaintextFormatter
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@pytest.fixture(name="formatter")
|
|
13
|
+
def fixture_formatter() -> UniversalPlaintextFormatter:
|
|
14
|
+
"""Create a basic formatter instance for testing."""
|
|
15
|
+
return UniversalPlaintextFormatter(
|
|
16
|
+
fmt="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@pytest.fixture(name="log_record")
|
|
21
|
+
def fixture_log_record() -> logging.LogRecord:
|
|
22
|
+
"""Create a basic log record for testing."""
|
|
23
|
+
return logging.LogRecord(
|
|
24
|
+
name="test_logger",
|
|
25
|
+
level=logging.INFO,
|
|
26
|
+
pathname="/path/to/file.py",
|
|
27
|
+
lineno=42,
|
|
28
|
+
msg="Test message",
|
|
29
|
+
args=(),
|
|
30
|
+
exc_info=None,
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def test_basic_formatting_without_extras(
|
|
35
|
+
formatter: UniversalPlaintextFormatter, log_record: logging.LogRecord
|
|
36
|
+
) -> None:
|
|
37
|
+
"""Test that basic formatting works without extra attributes."""
|
|
38
|
+
formatted = formatter.format(log_record)
|
|
39
|
+
assert "Test message" in formatted
|
|
40
|
+
assert "test_logger" in formatted
|
|
41
|
+
assert "INFO" in formatted
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def test_standard_attributes_excluded(
|
|
45
|
+
formatter: UniversalPlaintextFormatter, log_record: logging.LogRecord
|
|
46
|
+
) -> None:
|
|
47
|
+
"""Test that standard LogRecord attributes are excluded from extras."""
|
|
48
|
+
formatted = formatter.format(log_record)
|
|
49
|
+
# Standard attributes should not appear as extras
|
|
50
|
+
assert "pathname:" not in formatted
|
|
51
|
+
assert "lineno:" not in formatted
|
|
52
|
+
assert "levelname:" not in formatted
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def test_extra_attributes_included(
|
|
56
|
+
formatter: UniversalPlaintextFormatter, log_record: logging.LogRecord
|
|
57
|
+
) -> None:
|
|
58
|
+
"""Test that extra attributes are included in the formatted output."""
|
|
59
|
+
log_record.user_id = "12345"
|
|
60
|
+
log_record.request_id = "abc-def-ghi"
|
|
61
|
+
|
|
62
|
+
formatted = formatter.format(log_record)
|
|
63
|
+
|
|
64
|
+
assert "user_id: 12345" in formatted
|
|
65
|
+
assert "request_id: abc-def-ghi" in formatted
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def test_default_exclude_keys(
|
|
69
|
+
formatter: UniversalPlaintextFormatter, log_record: logging.LogRecord
|
|
70
|
+
) -> None:
|
|
71
|
+
"""Test that DEFAULT_EXCLUDE_KEYS (message, asctime) are excluded."""
|
|
72
|
+
# Add 'message' and 'asctime' as extra attributes (shouldn't appear in extras)
|
|
73
|
+
log_record.message = "This should be excluded"
|
|
74
|
+
log_record.asctime = "2025-01-01 12:00:00"
|
|
75
|
+
|
|
76
|
+
formatted = formatter.format(log_record)
|
|
77
|
+
|
|
78
|
+
# These should not appear as extras
|
|
79
|
+
lines = formatted.split("\n")
|
|
80
|
+
extra_lines = [
|
|
81
|
+
line for line in lines if line.strip().startswith(("message:", "asctime:"))
|
|
82
|
+
]
|
|
83
|
+
assert len(extra_lines) == 0
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def test_multiple_extras_formatting(
|
|
87
|
+
formatter: UniversalPlaintextFormatter, log_record: logging.LogRecord
|
|
88
|
+
) -> None:
|
|
89
|
+
"""Test formatting with multiple extra attributes."""
|
|
90
|
+
log_record.user_id = "12345"
|
|
91
|
+
log_record.session_id = "session-xyz"
|
|
92
|
+
log_record.ip_address = "192.168.1.1"
|
|
93
|
+
|
|
94
|
+
formatted = formatter.format(log_record)
|
|
95
|
+
|
|
96
|
+
assert "user_id: 12345" in formatted
|
|
97
|
+
assert "session_id: session-xyz" in formatted
|
|
98
|
+
assert "ip_address: 192.168.1.1" in formatted
|
|
99
|
+
|
|
100
|
+
# Check that extras are indented
|
|
101
|
+
lines = formatted.split("\n")
|
|
102
|
+
extra_lines = [
|
|
103
|
+
line
|
|
104
|
+
for line in lines
|
|
105
|
+
if any(key in line for key in ("user_id:", "session_id:", "ip_address:"))
|
|
106
|
+
]
|
|
107
|
+
for line in extra_lines:
|
|
108
|
+
assert line.startswith(" ")
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def test_config_exclude_keys_from_file(log_record: logging.LogRecord) -> None:
|
|
112
|
+
"""Test that exclude_keys from configuration file are properly excluded."""
|
|
113
|
+
with tempfile.TemporaryDirectory() as tmpdir:
|
|
114
|
+
config_path = Path(tmpdir) / "logging.conf"
|
|
115
|
+
config_content = """[formatter_plaintextFormatter]
|
|
116
|
+
exclude_keys = custom_field, another_field
|
|
117
|
+
"""
|
|
118
|
+
config_path.write_text(config_content)
|
|
119
|
+
|
|
120
|
+
# Change to temp directory to read config
|
|
121
|
+
original_dir = os.getcwd()
|
|
122
|
+
try:
|
|
123
|
+
os.chdir(tmpdir)
|
|
124
|
+
formatter = UniversalPlaintextFormatter(fmt="%(message)s")
|
|
125
|
+
|
|
126
|
+
# Add attributes that should be excluded
|
|
127
|
+
log_record.custom_field = "should be excluded"
|
|
128
|
+
log_record.another_field = "also excluded"
|
|
129
|
+
log_record.included_field = "should be included"
|
|
130
|
+
|
|
131
|
+
formatted = formatter.format(log_record)
|
|
132
|
+
|
|
133
|
+
assert "custom_field:" not in formatted
|
|
134
|
+
assert "another_field:" not in formatted
|
|
135
|
+
assert "included_field: should be included" in formatted
|
|
136
|
+
finally:
|
|
137
|
+
os.chdir(original_dir)
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def test_config_exclude_keys_with_whitespace(
|
|
141
|
+
log_record: logging.LogRecord,
|
|
142
|
+
) -> None:
|
|
143
|
+
"""Test that whitespace in exclude_keys configuration is handled correctly."""
|
|
144
|
+
with tempfile.TemporaryDirectory() as tmpdir:
|
|
145
|
+
config_path = Path(tmpdir) / "logging.conf"
|
|
146
|
+
config_content = """[formatter_plaintextFormatter]
|
|
147
|
+
exclude_keys = field1 , field2 , field3
|
|
148
|
+
"""
|
|
149
|
+
config_path.write_text(config_content)
|
|
150
|
+
|
|
151
|
+
original_dir = os.getcwd()
|
|
152
|
+
try:
|
|
153
|
+
os.chdir(tmpdir)
|
|
154
|
+
formatter = UniversalPlaintextFormatter(fmt="%(message)s")
|
|
155
|
+
|
|
156
|
+
log_record.field1 = "excluded"
|
|
157
|
+
log_record.field2 = "excluded"
|
|
158
|
+
log_record.field3 = "excluded"
|
|
159
|
+
|
|
160
|
+
formatted = formatter.format(log_record)
|
|
161
|
+
|
|
162
|
+
assert "field1:" not in formatted
|
|
163
|
+
assert "field2:" not in formatted
|
|
164
|
+
assert "field3:" not in formatted
|
|
165
|
+
finally:
|
|
166
|
+
os.chdir(original_dir)
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def test_no_config_file(
|
|
170
|
+
formatter: UniversalPlaintextFormatter, log_record: logging.LogRecord
|
|
171
|
+
) -> None:
|
|
172
|
+
"""Test that formatter works correctly when config file doesn't exist."""
|
|
173
|
+
log_record.some_extra = "value"
|
|
174
|
+
formatted = formatter.format(log_record)
|
|
175
|
+
|
|
176
|
+
# Should still format correctly
|
|
177
|
+
assert "Test message" in formatted
|
|
178
|
+
assert "some_extra: value" in formatted
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
def test_empty_extras(
|
|
182
|
+
formatter: UniversalPlaintextFormatter, log_record: logging.LogRecord
|
|
183
|
+
) -> None:
|
|
184
|
+
"""Test formatting when there are no extra attributes."""
|
|
185
|
+
formatted = formatter.format(log_record)
|
|
186
|
+
|
|
187
|
+
# Should only contain the base formatted message without extra newlines
|
|
188
|
+
lines = formatted.split("\n")
|
|
189
|
+
assert len([line for line in lines if line.strip()]) == 1
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def test_standard_attrs_caching(
|
|
193
|
+
formatter: UniversalPlaintextFormatter,
|
|
194
|
+
) -> None:
|
|
195
|
+
"""Test that standard attributes are cached after first call."""
|
|
196
|
+
assert formatter._standard_attrs is None
|
|
197
|
+
|
|
198
|
+
# First call should set the cache
|
|
199
|
+
standard_attrs = formatter._get_standard_attrs()
|
|
200
|
+
assert formatter._standard_attrs is not None
|
|
201
|
+
assert formatter._standard_attrs == standard_attrs
|
|
202
|
+
|
|
203
|
+
# Second call should return cached value
|
|
204
|
+
standard_attrs_2 = formatter._get_standard_attrs()
|
|
205
|
+
assert standard_attrs_2 is standard_attrs # Same object
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def test_formatter_with_custom_format_string(
|
|
209
|
+
log_record: logging.LogRecord,
|
|
210
|
+
) -> None:
|
|
211
|
+
"""Test formatter with a custom format string."""
|
|
212
|
+
formatter = UniversalPlaintextFormatter(fmt="[%(levelname)s] %(message)s")
|
|
213
|
+
log_record.extra_data = "test"
|
|
214
|
+
|
|
215
|
+
formatted = formatter.format(log_record)
|
|
216
|
+
|
|
217
|
+
assert "[INFO] Test message" in formatted
|
|
218
|
+
assert "extra_data: test" in formatted
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def test_exclude_keys_combination(log_record: logging.LogRecord) -> None:
|
|
222
|
+
"""Test that all exclusion sources are combined correctly."""
|
|
223
|
+
with tempfile.TemporaryDirectory() as tmpdir:
|
|
224
|
+
config_path = Path(tmpdir) / "logging.conf"
|
|
225
|
+
config_content = """[formatter_plaintextFormatter]
|
|
226
|
+
exclude_keys = config_excluded
|
|
227
|
+
"""
|
|
228
|
+
config_path.write_text(config_content)
|
|
229
|
+
|
|
230
|
+
original_dir = os.getcwd()
|
|
231
|
+
try:
|
|
232
|
+
os.chdir(tmpdir)
|
|
233
|
+
formatter = UniversalPlaintextFormatter(fmt="%(message)s")
|
|
234
|
+
|
|
235
|
+
# Add various attributes
|
|
236
|
+
log_record.pathname = "standard_attr" # Standard LogRecord attribute
|
|
237
|
+
log_record.message = "default_excluded" # DEFAULT_EXCLUDE_KEYS
|
|
238
|
+
log_record.config_excluded = "from_config" # From config file
|
|
239
|
+
log_record.should_appear = "yes" # Should appear
|
|
240
|
+
|
|
241
|
+
formatted = formatter.format(log_record)
|
|
242
|
+
|
|
243
|
+
# Only should_appear should be in extras
|
|
244
|
+
assert "pathname:" not in formatted # Standard attribute
|
|
245
|
+
assert "message:" not in formatted # DEFAULT_EXCLUDE_KEYS
|
|
246
|
+
assert "config_excluded:" not in formatted # Config exclude
|
|
247
|
+
assert "should_appear: yes" in formatted # Should be included
|
|
248
|
+
finally:
|
|
249
|
+
os.chdir(original_dir)
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"""Custom logging formatter for plaintext output with extra attributes.
|
|
2
|
+
|
|
3
|
+
This module provides the UniversalPlaintextFormatter class which allows
|
|
4
|
+
for easy logging of extra attributes in a human-readable format.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import configparser
|
|
8
|
+
import logging
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class UniversalPlaintextFormatter(logging.Formatter):
|
|
13
|
+
"""A custom formatter for logging that extends the standard logging.Formatter.
|
|
14
|
+
|
|
15
|
+
This formatter adds the ability to include extra attributes from log records while
|
|
16
|
+
excluding standard attributes and configurable keys.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
# Predefined exclusions - keys that should always be excluded
|
|
20
|
+
DEFAULT_EXCLUDE_KEYS = {"message", "asctime"}
|
|
21
|
+
|
|
22
|
+
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
|
23
|
+
"""Initialize the formatter with standard formatter parameters.
|
|
24
|
+
|
|
25
|
+
Args:
|
|
26
|
+
*args: Variable length argument list for the parent class.
|
|
27
|
+
**kwargs: Arbitrary keyword arguments for the parent class.
|
|
28
|
+
|
|
29
|
+
"""
|
|
30
|
+
super().__init__(*args, **kwargs)
|
|
31
|
+
self._standard_attrs: set[str] | None = None
|
|
32
|
+
self._config_exclude_keys = self._load_exclude_keys_from_config()
|
|
33
|
+
|
|
34
|
+
def _load_exclude_keys_from_config(self) -> set[str]:
|
|
35
|
+
"""Load additional keys to exclude from the configuration file.
|
|
36
|
+
|
|
37
|
+
Returns:
|
|
38
|
+
set[str]: A set of keys to exclude from log records.
|
|
39
|
+
|
|
40
|
+
"""
|
|
41
|
+
try:
|
|
42
|
+
config = configparser.ConfigParser()
|
|
43
|
+
config.read("logging.conf")
|
|
44
|
+
if config.has_option("formatter_plaintextFormatter", "exclude_keys"):
|
|
45
|
+
exclude_str = config.get("formatter_plaintextFormatter", "exclude_keys")
|
|
46
|
+
return set(key.strip() for key in exclude_str.split(",") if key.strip())
|
|
47
|
+
except (configparser.Error, FileNotFoundError, PermissionError, ValueError):
|
|
48
|
+
pass
|
|
49
|
+
return set()
|
|
50
|
+
|
|
51
|
+
def _get_standard_attrs(self) -> set[str]:
|
|
52
|
+
"""Get the set of standard attributes to exclude from log records.
|
|
53
|
+
|
|
54
|
+
This includes standard LogRecord attributes, predefined exclusions,
|
|
55
|
+
and exclusions from configuration.
|
|
56
|
+
|
|
57
|
+
Returns:
|
|
58
|
+
set[str]: A set of attribute names to exclude.
|
|
59
|
+
|
|
60
|
+
"""
|
|
61
|
+
if self._standard_attrs is None:
|
|
62
|
+
dummy_record = logging.LogRecord(
|
|
63
|
+
name="dummy",
|
|
64
|
+
level=logging.INFO,
|
|
65
|
+
pathname="",
|
|
66
|
+
lineno=0,
|
|
67
|
+
msg="",
|
|
68
|
+
args=(),
|
|
69
|
+
exc_info=None,
|
|
70
|
+
)
|
|
71
|
+
# Combine standard attributes + predefined + from configuration
|
|
72
|
+
all_excludes = (
|
|
73
|
+
set(dummy_record.__dict__.keys())
|
|
74
|
+
| self.DEFAULT_EXCLUDE_KEYS
|
|
75
|
+
| self._config_exclude_keys
|
|
76
|
+
)
|
|
77
|
+
self._standard_attrs = all_excludes
|
|
78
|
+
return self._standard_attrs
|
|
79
|
+
|
|
80
|
+
def format(self, record: logging.LogRecord) -> str:
|
|
81
|
+
"""Format the log record, adding any extra attributes not in the standard set.
|
|
82
|
+
|
|
83
|
+
Args:
|
|
84
|
+
record: The log record to format.
|
|
85
|
+
|
|
86
|
+
Returns:
|
|
87
|
+
str: The formatted log message with extra attributes appended.
|
|
88
|
+
|
|
89
|
+
"""
|
|
90
|
+
base = super().format(record)
|
|
91
|
+
extras = {
|
|
92
|
+
k: v
|
|
93
|
+
for k, v in record.__dict__.items()
|
|
94
|
+
if k not in self._get_standard_attrs()
|
|
95
|
+
}
|
|
96
|
+
if extras:
|
|
97
|
+
extras_str = "\n".join(f" {k}: {v}" for k, v in extras.items())
|
|
98
|
+
return f"{base}\n{extras_str}"
|
|
99
|
+
return base
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# Monitoring Service
|
|
2
|
+
|
|
3
|
+
Monitoring service provides endpoints for healthchecks and Prometheus metrics.
|
|
4
|
+
|
|
5
|
+
This module creates a monitoring server with uvicorn endpoints for managing health of applications.
|
|
6
|
+
|
|
7
|
+
## Endpoints
|
|
8
|
+
|
|
9
|
+
- GET /healthz/liveness ->
|
|
10
|
+
Provides information if the app is alive.
|
|
11
|
+
Example return:
|
|
12
|
+
```json
|
|
13
|
+
{"status": "ok", "checks": {}}
|
|
14
|
+
```
|
|
15
|
+
- GET /healthz/readiness ->
|
|
16
|
+
Provides information if the app is ready and which components are active.
|
|
17
|
+
Example return:
|
|
18
|
+
```json
|
|
19
|
+
{"status": "ok", "checks": {"kafka_alive": true, "model_ready": true, "redis_alive": true}}
|
|
20
|
+
```
|
|
21
|
+
- GET /metrics ->
|
|
22
|
+
Provides metrics collected by Prometheus client, to be used in metrics visualization client f.e. Grafana.
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
In your app you have to define readiness checks - services that need to be running for app to work properly
|
|
27
|
+
f.e. S3, Kafka, model loading, etc.
|
|
28
|
+
This is usually solved by creating MonitoringContext object.
|
|
29
|
+
|
|
30
|
+
```python
|
|
31
|
+
class MonitoringContext:
|
|
32
|
+
def __init__(self) -> None:
|
|
33
|
+
self.kafka_client: Optional[BaseKafkaClient] = None
|
|
34
|
+
self.redis_client: Optional[RedisService] = None
|
|
35
|
+
self._model_ready_flag: bool = False
|
|
36
|
+
|
|
37
|
+
def prepare_readiness_checks(self) -> dict[str, Callable[[], bool]]:
|
|
38
|
+
return {
|
|
39
|
+
"kafka_alive": self._kafka_check,
|
|
40
|
+
"model_ready": lambda: self._model_ready_flag,
|
|
41
|
+
"redis_alive": self._redis_alive,
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
def _redis_alive(self) -> bool:
|
|
45
|
+
if self.redis_client is None:
|
|
46
|
+
return False
|
|
47
|
+
return self.redis_client.is_alive()
|
|
48
|
+
|
|
49
|
+
def _kafka_check(self) -> bool:
|
|
50
|
+
if self.kafka_client is None:
|
|
51
|
+
return False
|
|
52
|
+
return self.kafka_client.is_alive()
|
|
53
|
+
|
|
54
|
+
def set_model_ready_flag(self, flag: bool) -> None:
|
|
55
|
+
self._model_ready_flag = flag
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Now you can prepare your monitoring server by running in __main__:
|
|
59
|
+
|
|
60
|
+
```python
|
|
61
|
+
monitoring_context = MonitoringContext()
|
|
62
|
+
monitoring_config = MonitoringServerConfig(
|
|
63
|
+
monitoring_context.prepare_readiness_checks()
|
|
64
|
+
)
|
|
65
|
+
monitoring_server = MonitoringServer(
|
|
66
|
+
host="0.0.0.0",
|
|
67
|
+
port=8000,
|
|
68
|
+
config=monitoring_config,
|
|
69
|
+
)
|
|
70
|
+
monitoring_server.start_monitoring_server()
|
|
71
|
+
```
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
"""Prometheus monitoring and health checks server implementation."""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import logging
|
|
5
|
+
import threading
|
|
6
|
+
from collections.abc import Callable
|
|
7
|
+
|
|
8
|
+
import prometheus_client
|
|
9
|
+
import uvicorn
|
|
10
|
+
from fastapi import FastAPI, Response
|
|
11
|
+
from fastapi.middleware.cors import CORSMiddleware
|
|
12
|
+
from pydantic.dataclasses import dataclass
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _create_app() -> FastAPI:
|
|
16
|
+
app = FastAPI()
|
|
17
|
+
app.add_middleware(
|
|
18
|
+
CORSMiddleware,
|
|
19
|
+
allow_origins=["*"],
|
|
20
|
+
allow_methods=["*"],
|
|
21
|
+
allow_headers=["*"],
|
|
22
|
+
)
|
|
23
|
+
return app
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _run_monitoring_server(host: str, port: int, app: FastAPI) -> None:
|
|
27
|
+
uvicorn.run(app, host=host, port=port)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@dataclass
|
|
31
|
+
class MonitoringServerConfig:
|
|
32
|
+
"""Configuration for the MonitoringServer.
|
|
33
|
+
|
|
34
|
+
Args:
|
|
35
|
+
readiness_checks: A dictionary of name to callable for readiness checks.
|
|
36
|
+
liveness_checks: An optional dictionary for liveness checks.
|
|
37
|
+
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
readiness_checks: dict[str, Callable[[], bool]]
|
|
41
|
+
liveness_checks: dict[str, Callable[[], bool]] | None = None
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class EndpointFilter(logging.Filter):
|
|
45
|
+
"""Filter for logging that excludes certain paths."""
|
|
46
|
+
|
|
47
|
+
def __init__(self, paths_excluded_for_logging: list[str]):
|
|
48
|
+
"""Initialize the EndpointFilter.
|
|
49
|
+
|
|
50
|
+
Args:
|
|
51
|
+
paths_excluded_for_logging: List of paths to exclude from logs.
|
|
52
|
+
|
|
53
|
+
"""
|
|
54
|
+
super().__init__()
|
|
55
|
+
self.paths_excluded_for_logging = paths_excluded_for_logging
|
|
56
|
+
|
|
57
|
+
def filter(self, record: logging.LogRecord) -> bool:
|
|
58
|
+
"""Filter log records based on path exclusions.
|
|
59
|
+
|
|
60
|
+
Args:
|
|
61
|
+
record: The log record to check.
|
|
62
|
+
|
|
63
|
+
Returns:
|
|
64
|
+
bool: True if record should be logged, False otherwise.
|
|
65
|
+
|
|
66
|
+
"""
|
|
67
|
+
return not any(
|
|
68
|
+
path in record.getMessage() for path in self.paths_excluded_for_logging
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
class MonitoringServer:
|
|
73
|
+
"""A server that exposes Prometheus metrics and health check endpoints."""
|
|
74
|
+
|
|
75
|
+
PATHS_EXCLUDED_FOR_LOGGING = ["/healthz/readiness", "/healthz/liveness"]
|
|
76
|
+
|
|
77
|
+
def __init__(
|
|
78
|
+
self,
|
|
79
|
+
host: str,
|
|
80
|
+
port: int,
|
|
81
|
+
config: MonitoringServerConfig,
|
|
82
|
+
):
|
|
83
|
+
"""Initialize the MonitoringServer.
|
|
84
|
+
|
|
85
|
+
Args:
|
|
86
|
+
host: The host to bind the server to.
|
|
87
|
+
port: The port to bind the server to.
|
|
88
|
+
config: The server configuration.
|
|
89
|
+
|
|
90
|
+
"""
|
|
91
|
+
self.config = config
|
|
92
|
+
self.host = host
|
|
93
|
+
self.port = port
|
|
94
|
+
logging.getLogger("uvicorn.access").addFilter(
|
|
95
|
+
EndpointFilter(self.PATHS_EXCLUDED_FOR_LOGGING)
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
def add_paths(self, app: FastAPI) -> None:
|
|
99
|
+
"""Add monitoring and health check endpoints to the FastAPI application.
|
|
100
|
+
|
|
101
|
+
Args:
|
|
102
|
+
app: The FastAPI application to add routes to.
|
|
103
|
+
|
|
104
|
+
"""
|
|
105
|
+
|
|
106
|
+
@app.get("/metrics")
|
|
107
|
+
async def get_metrics() -> Response:
|
|
108
|
+
return Response(
|
|
109
|
+
content=prometheus_client.generate_latest(),
|
|
110
|
+
media_type=prometheus_client.CONTENT_TYPE_LATEST,
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
@app.get("/healthz/liveness")
|
|
114
|
+
async def get_healthz_liveness() -> Response:
|
|
115
|
+
return await self._get_healthz_response(self.config.liveness_checks)
|
|
116
|
+
|
|
117
|
+
@app.get("/healthz/readiness")
|
|
118
|
+
async def get_healthz_readiness() -> Response:
|
|
119
|
+
return await self._get_healthz_response(self.config.readiness_checks)
|
|
120
|
+
|
|
121
|
+
async def _get_healthz_response(
|
|
122
|
+
self, checks: dict[str, Callable[[], bool]] | None
|
|
123
|
+
) -> Response:
|
|
124
|
+
try:
|
|
125
|
+
results = (
|
|
126
|
+
{check_name: check_fn() for check_name, check_fn in checks.items()}
|
|
127
|
+
if checks
|
|
128
|
+
else {}
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
status = "error"
|
|
132
|
+
status_code = 503
|
|
133
|
+
if all(results.values()):
|
|
134
|
+
status = "ok"
|
|
135
|
+
status_code = 200
|
|
136
|
+
|
|
137
|
+
data = {"status": status, "checks": results}
|
|
138
|
+
data_json = json.dumps(data)
|
|
139
|
+
return Response(content=data_json, status_code=status_code)
|
|
140
|
+
|
|
141
|
+
except Exception as e:
|
|
142
|
+
data = {"status": "error", "message": str(e)}
|
|
143
|
+
data_json = json.dumps(data)
|
|
144
|
+
return Response(content=data_json, status_code=503)
|
|
145
|
+
|
|
146
|
+
def start_monitoring_server(self) -> None:
|
|
147
|
+
"""Start the monitoring server in a background thread."""
|
|
148
|
+
local_app = _create_app()
|
|
149
|
+
self.add_paths(local_app)
|
|
150
|
+
server_thread = threading.Thread(
|
|
151
|
+
target=_run_monitoring_server,
|
|
152
|
+
args=(self.host, self.port, local_app),
|
|
153
|
+
)
|
|
154
|
+
server_thread.daemon = True # to ensure it dies with the main thread
|
|
155
|
+
server_thread.start()
|
|
156
|
+
logging.info("Monitoring server listening at %s:%s.", self.host, self.port)
|