-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.py
More file actions
294 lines (241 loc) · 9.61 KB
/
config.py
File metadata and controls
294 lines (241 loc) · 9.61 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# SPDX-FileCopyrightText: 2026 The Botanu Authors
# SPDX-License-Identifier: Apache-2.0
"""Configuration for Botanu SDK.
The SDK is intentionally minimal on the hot path. Heavy processing happens in
the OpenTelemetry Collector, not in the application:
- **SDK responsibility**: Generate run_id, propagate minimal context (run_id, use_case)
- **Collector responsibility**: PII redaction, vendor detection, attribute enrichment
Configuration precedence (highest to lowest):
1. Code arguments (explicit values passed to BotanuConfig)
2. Environment variables (BOTANU_*, OTEL_*)
3. YAML config file (botanu.yaml or specified path)
4. Built-in defaults
"""
from __future__ import annotations
import logging
import os
import re
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, Dict, List, Optional
logger = logging.getLogger(__name__)
@dataclass
class BotanuConfig:
"""Configuration for Botanu SDK and OpenTelemetry.
The SDK is a thin wrapper on OpenTelemetry. PII redaction, cardinality
limits, and vendor enrichment are handled by the OTel Collector — not here.
Example::
>>> config = BotanuConfig(
... service_name="my-service",
... otlp_endpoint="http://collector:4318/v1/traces",
... )
>>> # Or load from YAML
>>> config = BotanuConfig.from_yaml("config/botanu.yaml")
"""
# Service identification
service_name: Optional[str] = None
service_version: Optional[str] = None
service_namespace: Optional[str] = None
deployment_environment: Optional[str] = None
# Resource detection
auto_detect_resources: bool = True
# OTLP exporter configuration
otlp_endpoint: Optional[str] = None
otlp_headers: Optional[Dict[str, str]] = None
# Span export configuration
max_export_batch_size: int = 512
max_queue_size: int = 2048
schedule_delay_millis: int = 5000
# Propagation mode: "lean" (run_id + use_case only) or "full" (all context)
propagation_mode: str = "lean"
# Auto-instrumentation packages to enable
auto_instrument_packages: List[str] = field(
default_factory=lambda: [
# HTTP clients
"requests",
"httpx",
"urllib3",
"aiohttp_client",
# Web frameworks
"fastapi",
"flask",
"django",
"starlette",
# Databases
"sqlalchemy",
"psycopg2",
"asyncpg",
"pymongo",
"redis",
# Messaging
"celery",
"kafka_python",
# gRPC
"grpc",
# GenAI / AI
"openai_v2",
"anthropic",
"vertexai",
"google_genai",
"langchain",
# Runtime
"logging",
]
)
# Config file path (for tracking where config was loaded from)
_config_file: Optional[str] = field(default=None, repr=False)
def __post_init__(self) -> None:
"""Apply environment variable defaults."""
if self.service_name is None:
self.service_name = os.getenv("OTEL_SERVICE_NAME", "unknown_service")
if self.service_version is None:
self.service_version = os.getenv("OTEL_SERVICE_VERSION")
if self.service_namespace is None:
self.service_namespace = os.getenv("OTEL_SERVICE_NAMESPACE")
env_auto_detect = os.getenv("BOTANU_AUTO_DETECT_RESOURCES")
if env_auto_detect is not None:
self.auto_detect_resources = env_auto_detect.lower() in ("true", "1", "yes")
if self.deployment_environment is None:
self.deployment_environment = os.getenv(
"OTEL_DEPLOYMENT_ENVIRONMENT",
os.getenv("BOTANU_ENVIRONMENT", "production"),
)
if self.otlp_endpoint is None:
env_endpoint = os.getenv("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT")
if env_endpoint:
self.otlp_endpoint = env_endpoint
else:
base = os.getenv("OTEL_EXPORTER_OTLP_ENDPOINT", "http://localhost:4318")
self.otlp_endpoint = f"{base}/v1/traces"
env_propagation_mode = os.getenv("BOTANU_PROPAGATION_MODE")
if env_propagation_mode and env_propagation_mode in ("lean", "full"):
self.propagation_mode = env_propagation_mode
# ------------------------------------------------------------------
# YAML loading
# ------------------------------------------------------------------
@classmethod
def from_yaml(cls, path: Optional[str] = None) -> BotanuConfig:
"""Load configuration from a YAML file.
Supports environment variable interpolation using ``${VAR_NAME}`` syntax.
Args:
path: Path to YAML config file.
Raises:
FileNotFoundError: If config file doesn't exist.
ValueError: If YAML is malformed.
"""
if path is None:
raise FileNotFoundError("No config file path provided")
resolved = Path(path)
if not resolved.exists():
raise FileNotFoundError(f"Config file not found: {resolved}")
try:
import yaml # type: ignore[import-untyped]
except ImportError as err:
raise ImportError("PyYAML required for YAML config. Install with: pip install pyyaml") from err
with open(resolved) as fh:
raw_content = fh.read()
content = _interpolate_env_vars(raw_content)
try:
data = yaml.safe_load(content)
except yaml.YAMLError as exc:
raise ValueError(f"Invalid YAML in {resolved}: {exc}") from exc
if data is None:
data = {}
return cls._from_dict(data, config_file=str(resolved))
@classmethod
def from_file_or_env(cls, path: Optional[str] = None) -> BotanuConfig:
"""Load config from file if exists, otherwise use environment variables.
Search order:
1. Explicit *path* argument
2. ``BOTANU_CONFIG_FILE`` env var
3. ``./botanu.yaml``
4. ``./config/botanu.yaml``
5. Falls back to env-only config
"""
search_paths: List[Path] = []
if path:
search_paths.append(Path(path))
env_path = os.getenv("BOTANU_CONFIG_FILE")
if env_path:
search_paths.append(Path(env_path))
search_paths.extend(
[
Path("botanu.yaml"),
Path("botanu.yml"),
Path("config/botanu.yaml"),
Path("config/botanu.yml"),
]
)
for candidate in search_paths:
if candidate.exists():
logger.info("Loading config from: %s", candidate)
return cls.from_yaml(str(candidate))
logger.debug("No config file found, using environment variables only")
return cls()
@classmethod
def _from_dict(
cls,
data: Dict[str, Any],
config_file: Optional[str] = None,
) -> BotanuConfig:
"""Create config from dictionary (parsed YAML)."""
service = data.get("service", {})
otlp = data.get("otlp", {})
export = data.get("export", {})
propagation = data.get("propagation", {})
resource = data.get("resource", {})
auto_packages = data.get("auto_instrument_packages")
return cls(
service_name=service.get("name"),
service_version=service.get("version"),
service_namespace=service.get("namespace"),
deployment_environment=service.get("environment"),
auto_detect_resources=resource.get("auto_detect", True),
otlp_endpoint=otlp.get("endpoint"),
otlp_headers=otlp.get("headers"),
max_export_batch_size=export.get("batch_size", 512),
max_queue_size=export.get("queue_size", 2048),
schedule_delay_millis=export.get("delay_ms", 5000),
propagation_mode=propagation.get("mode", "lean"),
auto_instrument_packages=(auto_packages if auto_packages else BotanuConfig().auto_instrument_packages),
_config_file=config_file,
)
def to_dict(self) -> Dict[str, Any]:
"""Export configuration as dictionary."""
return {
"service": {
"name": self.service_name,
"version": self.service_version,
"namespace": self.service_namespace,
"environment": self.deployment_environment,
},
"resource": {
"auto_detect": self.auto_detect_resources,
},
"otlp": {
"endpoint": self.otlp_endpoint,
"headers": self.otlp_headers,
},
"export": {
"batch_size": self.max_export_batch_size,
"queue_size": self.max_queue_size,
"delay_ms": self.schedule_delay_millis,
},
"propagation": {
"mode": self.propagation_mode,
},
"auto_instrument_packages": self.auto_instrument_packages,
}
def _interpolate_env_vars(content: str) -> str:
"""Interpolate ``${VAR_NAME}`` and ``${VAR_NAME:-default}`` in *content*."""
pattern = re.compile(r"\$\{([A-Za-z_][A-Za-z0-9_]*)(?::-([^}]*))?\}")
def _replace(match: re.Match) -> str: # type: ignore[type-arg]
var_name = match.group(1)
default = match.group(2)
value = os.getenv(var_name)
if value is not None:
return value
if default is not None:
return default
return match.group(0)
return pattern.sub(_replace, content)