forked from mtusi2026/mtusi2026lab6
Реализовать observability для лабораторной 6
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
def _positive_int(name: str, default: str) -> int:
|
||||
try:
|
||||
value = int(os.getenv(name, default))
|
||||
except ValueError as exc:
|
||||
raise RuntimeError(f'{name} должен быть целым числом') from exc
|
||||
if value <= 0:
|
||||
raise RuntimeError(f'{name} должен быть больше нуля')
|
||||
return value
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class ObservabilitySettings:
|
||||
enabled: bool
|
||||
endpoint: str
|
||||
service_name: str
|
||||
service_version: str
|
||||
environment: str
|
||||
export_timeout_seconds: int
|
||||
span_queue_size: int
|
||||
log_queue_size: int
|
||||
metric_export_interval_ms: int
|
||||
|
||||
|
||||
def load_observability_settings(default_service_name: str) -> ObservabilitySettings:
|
||||
disabled = os.getenv('OTEL_SDK_DISABLED', 'false').strip().lower()
|
||||
if disabled not in {'true', 'false'}:
|
||||
raise RuntimeError('OTEL_SDK_DISABLED должен быть true или false')
|
||||
endpoint = os.getenv('OTEL_EXPORTER_OTLP_ENDPOINT', 'http://otel-collector:4318')
|
||||
return ObservabilitySettings(
|
||||
enabled=disabled != 'true',
|
||||
endpoint=endpoint.rstrip('/'),
|
||||
service_name=os.getenv('OTEL_SERVICE_NAME', default_service_name),
|
||||
service_version=os.getenv('OTEL_SERVICE_VERSION', '1.6.0'),
|
||||
environment=os.getenv('OTEL_DEPLOYMENT_ENVIRONMENT', 'lab'),
|
||||
export_timeout_seconds=_positive_int('OTEL_EXPORTER_OTLP_TIMEOUT', '2'),
|
||||
span_queue_size=_positive_int('OTEL_BSP_MAX_QUEUE_SIZE', '2048'),
|
||||
log_queue_size=_positive_int('OTEL_BLRP_MAX_QUEUE_SIZE', '2048'),
|
||||
metric_export_interval_ms=_positive_int('OTEL_METRIC_EXPORT_INTERVAL', '5000'),
|
||||
)
|
||||
Reference in New Issue
Block a user