provide-foundation 0.0.0.dev0__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.
Files changed (149) hide show
  1. provide/__init__.py +15 -0
  2. provide/foundation/__init__.py +155 -0
  3. provide/foundation/_version.py +58 -0
  4. provide/foundation/cli/__init__.py +67 -0
  5. provide/foundation/cli/commands/__init__.py +3 -0
  6. provide/foundation/cli/commands/deps.py +71 -0
  7. provide/foundation/cli/commands/logs/__init__.py +63 -0
  8. provide/foundation/cli/commands/logs/generate.py +357 -0
  9. provide/foundation/cli/commands/logs/generate_old.py +569 -0
  10. provide/foundation/cli/commands/logs/query.py +174 -0
  11. provide/foundation/cli/commands/logs/send.py +166 -0
  12. provide/foundation/cli/commands/logs/tail.py +112 -0
  13. provide/foundation/cli/decorators.py +262 -0
  14. provide/foundation/cli/main.py +65 -0
  15. provide/foundation/cli/testing.py +220 -0
  16. provide/foundation/cli/utils.py +210 -0
  17. provide/foundation/config/__init__.py +106 -0
  18. provide/foundation/config/base.py +295 -0
  19. provide/foundation/config/env.py +369 -0
  20. provide/foundation/config/loader.py +311 -0
  21. provide/foundation/config/manager.py +387 -0
  22. provide/foundation/config/schema.py +284 -0
  23. provide/foundation/config/sync.py +281 -0
  24. provide/foundation/config/types.py +78 -0
  25. provide/foundation/config/validators.py +80 -0
  26. provide/foundation/console/__init__.py +29 -0
  27. provide/foundation/console/input.py +364 -0
  28. provide/foundation/console/output.py +178 -0
  29. provide/foundation/context/__init__.py +12 -0
  30. provide/foundation/context/core.py +356 -0
  31. provide/foundation/core.py +20 -0
  32. provide/foundation/crypto/__init__.py +182 -0
  33. provide/foundation/crypto/algorithms.py +111 -0
  34. provide/foundation/crypto/certificates.py +896 -0
  35. provide/foundation/crypto/checksums.py +301 -0
  36. provide/foundation/crypto/constants.py +57 -0
  37. provide/foundation/crypto/hashing.py +265 -0
  38. provide/foundation/crypto/keys.py +188 -0
  39. provide/foundation/crypto/signatures.py +144 -0
  40. provide/foundation/crypto/utils.py +164 -0
  41. provide/foundation/errors/__init__.py +96 -0
  42. provide/foundation/errors/auth.py +73 -0
  43. provide/foundation/errors/base.py +81 -0
  44. provide/foundation/errors/config.py +103 -0
  45. provide/foundation/errors/context.py +299 -0
  46. provide/foundation/errors/decorators.py +484 -0
  47. provide/foundation/errors/handlers.py +360 -0
  48. provide/foundation/errors/integration.py +105 -0
  49. provide/foundation/errors/platform.py +37 -0
  50. provide/foundation/errors/process.py +140 -0
  51. provide/foundation/errors/resources.py +133 -0
  52. provide/foundation/errors/runtime.py +160 -0
  53. provide/foundation/errors/safe_decorators.py +133 -0
  54. provide/foundation/errors/types.py +276 -0
  55. provide/foundation/file/__init__.py +79 -0
  56. provide/foundation/file/atomic.py +157 -0
  57. provide/foundation/file/directory.py +134 -0
  58. provide/foundation/file/formats.py +236 -0
  59. provide/foundation/file/lock.py +175 -0
  60. provide/foundation/file/safe.py +179 -0
  61. provide/foundation/file/utils.py +170 -0
  62. provide/foundation/hub/__init__.py +88 -0
  63. provide/foundation/hub/click_builder.py +310 -0
  64. provide/foundation/hub/commands.py +42 -0
  65. provide/foundation/hub/components.py +640 -0
  66. provide/foundation/hub/decorators.py +244 -0
  67. provide/foundation/hub/info.py +32 -0
  68. provide/foundation/hub/manager.py +446 -0
  69. provide/foundation/hub/registry.py +279 -0
  70. provide/foundation/hub/type_mapping.py +54 -0
  71. provide/foundation/hub/types.py +28 -0
  72. provide/foundation/logger/__init__.py +41 -0
  73. provide/foundation/logger/base.py +22 -0
  74. provide/foundation/logger/config/__init__.py +16 -0
  75. provide/foundation/logger/config/base.py +40 -0
  76. provide/foundation/logger/config/logging.py +394 -0
  77. provide/foundation/logger/config/telemetry.py +188 -0
  78. provide/foundation/logger/core.py +239 -0
  79. provide/foundation/logger/custom_processors.py +172 -0
  80. provide/foundation/logger/emoji/__init__.py +44 -0
  81. provide/foundation/logger/emoji/matrix.py +209 -0
  82. provide/foundation/logger/emoji/sets.py +458 -0
  83. provide/foundation/logger/emoji/types.py +56 -0
  84. provide/foundation/logger/factories.py +56 -0
  85. provide/foundation/logger/processors/__init__.py +13 -0
  86. provide/foundation/logger/processors/main.py +254 -0
  87. provide/foundation/logger/processors/trace.py +113 -0
  88. provide/foundation/logger/ratelimit/__init__.py +31 -0
  89. provide/foundation/logger/ratelimit/limiters.py +294 -0
  90. provide/foundation/logger/ratelimit/processor.py +203 -0
  91. provide/foundation/logger/ratelimit/queue_limiter.py +305 -0
  92. provide/foundation/logger/setup/__init__.py +29 -0
  93. provide/foundation/logger/setup/coordinator.py +138 -0
  94. provide/foundation/logger/setup/emoji_resolver.py +64 -0
  95. provide/foundation/logger/setup/processors.py +85 -0
  96. provide/foundation/logger/setup/testing.py +39 -0
  97. provide/foundation/logger/trace.py +38 -0
  98. provide/foundation/metrics/__init__.py +119 -0
  99. provide/foundation/metrics/otel.py +122 -0
  100. provide/foundation/metrics/simple.py +165 -0
  101. provide/foundation/observability/__init__.py +53 -0
  102. provide/foundation/observability/openobserve/__init__.py +79 -0
  103. provide/foundation/observability/openobserve/auth.py +72 -0
  104. provide/foundation/observability/openobserve/client.py +307 -0
  105. provide/foundation/observability/openobserve/commands.py +357 -0
  106. provide/foundation/observability/openobserve/exceptions.py +41 -0
  107. provide/foundation/observability/openobserve/formatters.py +298 -0
  108. provide/foundation/observability/openobserve/models.py +134 -0
  109. provide/foundation/observability/openobserve/otlp.py +320 -0
  110. provide/foundation/observability/openobserve/search.py +222 -0
  111. provide/foundation/observability/openobserve/streaming.py +235 -0
  112. provide/foundation/platform/__init__.py +44 -0
  113. provide/foundation/platform/detection.py +193 -0
  114. provide/foundation/platform/info.py +157 -0
  115. provide/foundation/process/__init__.py +39 -0
  116. provide/foundation/process/async_runner.py +373 -0
  117. provide/foundation/process/lifecycle.py +406 -0
  118. provide/foundation/process/runner.py +390 -0
  119. provide/foundation/setup/__init__.py +101 -0
  120. provide/foundation/streams/__init__.py +44 -0
  121. provide/foundation/streams/console.py +57 -0
  122. provide/foundation/streams/core.py +65 -0
  123. provide/foundation/streams/file.py +104 -0
  124. provide/foundation/testing/__init__.py +166 -0
  125. provide/foundation/testing/cli.py +227 -0
  126. provide/foundation/testing/crypto.py +163 -0
  127. provide/foundation/testing/fixtures.py +49 -0
  128. provide/foundation/testing/hub.py +23 -0
  129. provide/foundation/testing/logger.py +106 -0
  130. provide/foundation/testing/streams.py +54 -0
  131. provide/foundation/tracer/__init__.py +49 -0
  132. provide/foundation/tracer/context.py +115 -0
  133. provide/foundation/tracer/otel.py +135 -0
  134. provide/foundation/tracer/spans.py +174 -0
  135. provide/foundation/types.py +32 -0
  136. provide/foundation/utils/__init__.py +97 -0
  137. provide/foundation/utils/deps.py +195 -0
  138. provide/foundation/utils/env.py +491 -0
  139. provide/foundation/utils/formatting.py +483 -0
  140. provide/foundation/utils/parsing.py +235 -0
  141. provide/foundation/utils/rate_limiting.py +112 -0
  142. provide/foundation/utils/streams.py +67 -0
  143. provide/foundation/utils/timing.py +93 -0
  144. provide_foundation-0.0.0.dev0.dist-info/METADATA +469 -0
  145. provide_foundation-0.0.0.dev0.dist-info/RECORD +149 -0
  146. provide_foundation-0.0.0.dev0.dist-info/WHEEL +5 -0
  147. provide_foundation-0.0.0.dev0.dist-info/entry_points.txt +2 -0
  148. provide_foundation-0.0.0.dev0.dist-info/licenses/LICENSE +201 -0
  149. provide_foundation-0.0.0.dev0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,394 @@
1
+ #
2
+ # logging.py
3
+ #
4
+ """
5
+ LoggingConfig class for Foundation logger configuration.
6
+ """
7
+
8
+ import json
9
+ import os
10
+ from pathlib import Path
11
+
12
+ from attrs import define
13
+
14
+ from provide.foundation.config import BaseConfig, field
15
+ from provide.foundation.config.types import ConfigSource
16
+ from provide.foundation.logger.config.base import get_config_logger
17
+ from provide.foundation.logger.emoji.types import EmojiSet, EmojiSetConfig
18
+ from provide.foundation.types import (
19
+ _VALID_FORMATTER_TUPLE,
20
+ _VALID_LOG_LEVEL_TUPLE,
21
+ ConsoleFormatterStr,
22
+ LogLevelStr,
23
+ )
24
+
25
+
26
+ @define(slots=True, repr=False)
27
+ class LoggingConfig(BaseConfig):
28
+ """Configuration specific to logging behavior within Foundation Telemetry."""
29
+
30
+ default_level: LogLevelStr = field(
31
+ default="DEBUG",
32
+ env_var="PROVIDE_LOG_LEVEL",
33
+ description="Default logging level",
34
+ )
35
+ module_levels: dict[str, LogLevelStr] = field(
36
+ factory=lambda: {},
37
+ env_var="PROVIDE_LOG_MODULE_LEVELS",
38
+ description="Per-module log levels (format: module1:LEVEL,module2:LEVEL)",
39
+ )
40
+ console_formatter: ConsoleFormatterStr = field(
41
+ default="key_value",
42
+ env_var="PROVIDE_LOG_CONSOLE_FORMATTER",
43
+ description="Console output formatter (key_value or json)",
44
+ )
45
+ logger_name_emoji_prefix_enabled: bool = field(
46
+ default=True,
47
+ env_var="PROVIDE_LOG_LOGGER_NAME_EMOJI_ENABLED",
48
+ description="Enable emoji prefixes based on logger names",
49
+ )
50
+ das_emoji_prefix_enabled: bool = field(
51
+ default=True,
52
+ env_var="PROVIDE_LOG_DAS_EMOJI_ENABLED",
53
+ description="Enable Domain-Action-Status emoji prefixes",
54
+ )
55
+ omit_timestamp: bool = field(
56
+ default=False,
57
+ env_var="PROVIDE_LOG_OMIT_TIMESTAMP",
58
+ description="Omit timestamps from console output",
59
+ )
60
+ enabled_emoji_sets: list[str] = field(
61
+ factory=lambda: [],
62
+ env_var="PROVIDE_LOG_ENABLED_EMOJI_SETS",
63
+ description="Comma-separated list of emoji sets to enable",
64
+ )
65
+ custom_emoji_sets: list[EmojiSetConfig] = field(
66
+ factory=lambda: [],
67
+ env_var="PROVIDE_LOG_CUSTOM_EMOJI_SETS",
68
+ description="JSON array of custom emoji set configurations",
69
+ )
70
+ user_defined_emoji_sets: list[EmojiSet] = field(
71
+ factory=lambda: [],
72
+ env_var="PROVIDE_LOG_USER_DEFINED_EMOJI_SETS",
73
+ description="JSON array of user-defined emoji sets",
74
+ )
75
+ log_file: Path | None = field(
76
+ default=None, env_var="PROVIDE_LOG_FILE", description="Path to log file"
77
+ )
78
+ foundation_setup_log_level: LogLevelStr = field(
79
+ default="INFO",
80
+ env_var="FOUNDATION_LOG_LEVEL",
81
+ description="Log level for Foundation internal setup messages",
82
+ )
83
+ foundation_log_output: str = field(
84
+ default="stderr",
85
+ env_var="FOUNDATION_LOG_OUTPUT",
86
+ description="Output destination for Foundation internal messages (stderr, stdout, main)",
87
+ )
88
+ show_emoji_matrix: bool = field(
89
+ default=False,
90
+ env_var="PROVIDE_SHOW_EMOJI_MATRIX",
91
+ description="Whether to display emoji matrix on startup",
92
+ )
93
+ rate_limit_enabled: bool = field(
94
+ default=False,
95
+ env_var="PROVIDE_LOG_RATE_LIMIT_ENABLED",
96
+ description="Enable rate limiting for log output",
97
+ )
98
+ rate_limit_global: float | None = field(
99
+ default=None,
100
+ env_var="PROVIDE_LOG_RATE_LIMIT_GLOBAL",
101
+ description="Global rate limit (logs per second)",
102
+ )
103
+ rate_limit_global_capacity: float | None = field(
104
+ default=None,
105
+ env_var="PROVIDE_LOG_RATE_LIMIT_GLOBAL_CAPACITY",
106
+ description="Global rate limit burst capacity",
107
+ )
108
+ rate_limit_per_logger: dict[str, tuple[float, float]] = field(
109
+ factory=lambda: {},
110
+ env_var="PROVIDE_LOG_RATE_LIMIT_PER_LOGGER",
111
+ description="Per-logger rate limits (format: logger1:rate:capacity,logger2:rate:capacity)",
112
+ )
113
+ rate_limit_emit_warnings: bool = field(
114
+ default=True,
115
+ env_var="PROVIDE_LOG_RATE_LIMIT_EMIT_WARNINGS",
116
+ description="Emit warnings when logs are rate limited",
117
+ )
118
+ rate_limit_summary_interval: float = field(
119
+ default=5.0,
120
+ env_var="PROVIDE_LOG_RATE_LIMIT_SUMMARY_INTERVAL",
121
+ description="Seconds between rate limit summary reports",
122
+ )
123
+ rate_limit_max_queue_size: int = field(
124
+ default=1000,
125
+ env_var="PROVIDE_LOG_RATE_LIMIT_MAX_QUEUE_SIZE",
126
+ description="Maximum number of logs to queue when rate limited",
127
+ )
128
+ rate_limit_max_memory_mb: float | None = field(
129
+ default=None,
130
+ env_var="PROVIDE_LOG_RATE_LIMIT_MAX_MEMORY_MB",
131
+ description="Maximum memory (MB) for queued logs",
132
+ )
133
+ rate_limit_overflow_policy: str = field(
134
+ default="drop_oldest",
135
+ env_var="PROVIDE_LOG_RATE_LIMIT_OVERFLOW_POLICY",
136
+ description="Policy when queue is full: drop_oldest, drop_newest, or block",
137
+ )
138
+
139
+ @classmethod
140
+ def from_env(cls, strict: bool = True) -> "LoggingConfig":
141
+ """Load LoggingConfig from environment variables."""
142
+ config_dict = {}
143
+
144
+ # Parse standard fields
145
+ if level := os.getenv("PROVIDE_LOG_LEVEL"):
146
+ level = level.upper()
147
+ if level in _VALID_LOG_LEVEL_TUPLE:
148
+ config_dict["default_level"] = level
149
+ elif strict:
150
+ get_config_logger().warning(
151
+ "[Foundation Config Warning] Invalid configuration value, using default",
152
+ config_key="PROVIDE_LOG_LEVEL",
153
+ invalid_value=level,
154
+ valid_options=list(_VALID_LOG_LEVEL_TUPLE),
155
+ default_value="DEBUG",
156
+ )
157
+
158
+ if formatter := os.getenv("PROVIDE_LOG_CONSOLE_FORMATTER"):
159
+ formatter = formatter.lower()
160
+ if formatter in _VALID_FORMATTER_TUPLE:
161
+ config_dict["console_formatter"] = formatter
162
+ elif strict:
163
+ get_config_logger().warning(
164
+ "[Foundation Config Warning] Invalid configuration value, using default",
165
+ config_key="PROVIDE_LOG_CONSOLE_FORMATTER",
166
+ invalid_value=formatter,
167
+ valid_options=list(_VALID_FORMATTER_TUPLE),
168
+ default_value="key_value",
169
+ )
170
+
171
+ if omit_ts := os.getenv("PROVIDE_LOG_OMIT_TIMESTAMP"):
172
+ config_dict["omit_timestamp"] = omit_ts.lower() == "true"
173
+
174
+ if logger_emoji := os.getenv("PROVIDE_LOG_LOGGER_NAME_EMOJI_ENABLED"):
175
+ config_dict["logger_name_emoji_prefix_enabled"] = (
176
+ logger_emoji.lower() == "true"
177
+ )
178
+
179
+ if das_emoji := os.getenv("PROVIDE_LOG_DAS_EMOJI_ENABLED"):
180
+ config_dict["das_emoji_prefix_enabled"] = das_emoji.lower() == "true"
181
+
182
+ if log_file := os.getenv("PROVIDE_LOG_FILE"):
183
+ config_dict["log_file"] = Path(log_file)
184
+
185
+ if foundation_level := os.getenv("FOUNDATION_LOG_LEVEL"):
186
+ foundation_level = foundation_level.upper()
187
+ if foundation_level in _VALID_LOG_LEVEL_TUPLE:
188
+ config_dict["foundation_setup_log_level"] = foundation_level
189
+ elif strict:
190
+ get_config_logger().warning(
191
+ "[Foundation Config Warning] Invalid configuration value, using default",
192
+ config_key="FOUNDATION_LOG_LEVEL",
193
+ invalid_value=foundation_level,
194
+ valid_options=list(_VALID_LOG_LEVEL_TUPLE),
195
+ default_value="INFO",
196
+ )
197
+
198
+ if foundation_output := os.getenv("FOUNDATION_LOG_OUTPUT"):
199
+ foundation_output = foundation_output.lower()
200
+ valid_outputs = ("stderr", "stdout", "main")
201
+ if foundation_output in valid_outputs:
202
+ config_dict["foundation_log_output"] = foundation_output
203
+ elif strict:
204
+ get_config_logger().warning(
205
+ "[Foundation Config Warning] Invalid configuration value, using default",
206
+ config_key="FOUNDATION_LOG_OUTPUT",
207
+ invalid_value=foundation_output,
208
+ valid_options=list(valid_outputs),
209
+ default_value="stderr",
210
+ )
211
+
212
+ if show_matrix := os.getenv("PROVIDE_SHOW_EMOJI_MATRIX"):
213
+ config_dict["show_emoji_matrix"] = show_matrix.strip().lower() in (
214
+ "true",
215
+ "1",
216
+ "yes",
217
+ )
218
+
219
+ # Parse rate limiting configuration
220
+ if rate_limit_enabled := os.getenv("PROVIDE_LOG_RATE_LIMIT_ENABLED"):
221
+ config_dict["rate_limit_enabled"] = rate_limit_enabled.lower() == "true"
222
+
223
+ if rate_limit_global := os.getenv("PROVIDE_LOG_RATE_LIMIT_GLOBAL"):
224
+ try:
225
+ config_dict["rate_limit_global"] = float(rate_limit_global)
226
+ except ValueError:
227
+ if strict:
228
+ get_config_logger().warning(
229
+ "[Foundation Config Warning] Invalid rate limit value",
230
+ config_key="PROVIDE_LOG_RATE_LIMIT_GLOBAL",
231
+ invalid_value=rate_limit_global,
232
+ )
233
+
234
+ if rate_limit_capacity := os.getenv("PROVIDE_LOG_RATE_LIMIT_GLOBAL_CAPACITY"):
235
+ try:
236
+ config_dict["rate_limit_global_capacity"] = float(rate_limit_capacity)
237
+ except ValueError:
238
+ if strict:
239
+ get_config_logger().warning(
240
+ "[Foundation Config Warning] Invalid rate limit capacity",
241
+ config_key="PROVIDE_LOG_RATE_LIMIT_GLOBAL_CAPACITY",
242
+ invalid_value=rate_limit_capacity,
243
+ )
244
+
245
+ if per_logger_limits := os.getenv("PROVIDE_LOG_RATE_LIMIT_PER_LOGGER"):
246
+ limits_dict = {}
247
+ for item in per_logger_limits.split(","):
248
+ parts = item.split(":")
249
+ if len(parts) == 3:
250
+ logger_name, rate, capacity = parts
251
+ try:
252
+ limits_dict[logger_name.strip()] = (
253
+ float(rate.strip()),
254
+ float(capacity.strip()),
255
+ )
256
+ except ValueError:
257
+ if strict:
258
+ get_config_logger().warning(
259
+ "[Foundation Config Warning] Invalid per-logger rate limit",
260
+ config_key="PROVIDE_LOG_RATE_LIMIT_PER_LOGGER",
261
+ invalid_item=item,
262
+ )
263
+ if limits_dict:
264
+ config_dict["rate_limit_per_logger"] = limits_dict
265
+
266
+ if emit_warnings := os.getenv("PROVIDE_LOG_RATE_LIMIT_EMIT_WARNINGS"):
267
+ config_dict["rate_limit_emit_warnings"] = emit_warnings.lower() == "true"
268
+
269
+ if summary_interval := os.getenv("PROVIDE_LOG_RATE_LIMIT_SUMMARY_INTERVAL"):
270
+ try:
271
+ config_dict["rate_limit_summary_interval"] = float(summary_interval)
272
+ except ValueError:
273
+ if strict:
274
+ get_config_logger().warning(
275
+ "[Foundation Config Warning] Invalid summary interval",
276
+ config_key="PROVIDE_LOG_RATE_LIMIT_SUMMARY_INTERVAL",
277
+ invalid_value=summary_interval,
278
+ )
279
+
280
+ if max_queue := os.getenv("PROVIDE_LOG_RATE_LIMIT_MAX_QUEUE_SIZE"):
281
+ try:
282
+ config_dict["rate_limit_max_queue_size"] = int(max_queue)
283
+ except ValueError:
284
+ if strict:
285
+ get_config_logger().warning(
286
+ "[Foundation Config Warning] Invalid max queue size",
287
+ config_key="PROVIDE_LOG_RATE_LIMIT_MAX_QUEUE_SIZE",
288
+ invalid_value=max_queue,
289
+ )
290
+
291
+ if max_memory := os.getenv("PROVIDE_LOG_RATE_LIMIT_MAX_MEMORY_MB"):
292
+ try:
293
+ config_dict["rate_limit_max_memory_mb"] = float(max_memory)
294
+ except ValueError:
295
+ if strict:
296
+ get_config_logger().warning(
297
+ "[Foundation Config Warning] Invalid max memory",
298
+ config_key="PROVIDE_LOG_RATE_LIMIT_MAX_MEMORY_MB",
299
+ invalid_value=max_memory,
300
+ )
301
+
302
+ if overflow_policy := os.getenv("PROVIDE_LOG_RATE_LIMIT_OVERFLOW_POLICY"):
303
+ valid_policies = ("drop_oldest", "drop_newest", "block")
304
+ if overflow_policy in valid_policies:
305
+ config_dict["rate_limit_overflow_policy"] = overflow_policy
306
+ elif strict:
307
+ get_config_logger().warning(
308
+ "[Foundation Config Warning] Invalid overflow policy",
309
+ config_key="PROVIDE_LOG_RATE_LIMIT_OVERFLOW_POLICY",
310
+ invalid_value=overflow_policy,
311
+ valid_options=list(valid_policies),
312
+ )
313
+
314
+ # Parse complex fields
315
+ if module_levels := os.getenv("PROVIDE_LOG_MODULE_LEVELS"):
316
+ levels_dict = {}
317
+ for item in module_levels.split(","):
318
+ if ":" in item:
319
+ module, level = item.split(":", 1)
320
+ module = module.strip()
321
+ level = level.strip().upper()
322
+ if module and level in _VALID_LOG_LEVEL_TUPLE:
323
+ levels_dict[module] = level
324
+ elif strict and module and level not in _VALID_LOG_LEVEL_TUPLE:
325
+ get_config_logger().warning(
326
+ "[Foundation Config Warning] Invalid module log level, skipping",
327
+ config_key="PROVIDE_LOG_MODULE_LEVELS",
328
+ module_name=module,
329
+ invalid_level=level,
330
+ valid_options=list(_VALID_LOG_LEVEL_TUPLE),
331
+ )
332
+ if levels_dict:
333
+ config_dict["module_levels"] = levels_dict
334
+
335
+ if emoji_sets := os.getenv("PROVIDE_LOG_ENABLED_EMOJI_SETS"):
336
+ config_dict["enabled_emoji_sets"] = [
337
+ s.strip() for s in emoji_sets.split(",") if s.strip()
338
+ ]
339
+
340
+ if custom_sets := os.getenv("PROVIDE_LOG_CUSTOM_EMOJI_SETS"):
341
+ try:
342
+ parsed = json.loads(custom_sets)
343
+ if isinstance(parsed, list):
344
+ config_dict["custom_emoji_sets"] = [
345
+ EmojiSetConfig(**item) if isinstance(item, dict) else item
346
+ for item in parsed
347
+ ]
348
+ except json.JSONDecodeError as e:
349
+ if strict:
350
+ get_config_logger().warning(
351
+ "[Foundation Config Warning] Invalid JSON in configuration",
352
+ config_key="PROVIDE_LOG_CUSTOM_EMOJI_SETS",
353
+ error=str(e),
354
+ config_value=custom_sets[:100] + "..."
355
+ if len(custom_sets) > 100
356
+ else custom_sets,
357
+ )
358
+ except (TypeError, ValueError) as e:
359
+ if strict:
360
+ get_config_logger().warning(
361
+ "[Foundation Config Warning] Error parsing custom emoji set configuration",
362
+ config_key="PROVIDE_LOG_CUSTOM_EMOJI_SETS",
363
+ error=str(e),
364
+ error_type=type(e).__name__,
365
+ )
366
+
367
+ if user_sets := os.getenv("PROVIDE_LOG_USER_DEFINED_EMOJI_SETS"):
368
+ try:
369
+ parsed = json.loads(user_sets)
370
+ if isinstance(parsed, list):
371
+ config_dict["user_defined_emoji_sets"] = [
372
+ EmojiSet(**item) if isinstance(item, dict) else item
373
+ for item in parsed
374
+ ]
375
+ except json.JSONDecodeError as e:
376
+ if strict:
377
+ get_config_logger().warning(
378
+ "[Foundation Config Warning] Invalid JSON in configuration",
379
+ config_key="PROVIDE_LOG_USER_DEFINED_EMOJI_SETS",
380
+ error=str(e),
381
+ config_value=user_sets[:100] + "..."
382
+ if len(user_sets) > 100
383
+ else user_sets,
384
+ )
385
+ except (TypeError, ValueError) as e:
386
+ if strict:
387
+ get_config_logger().warning(
388
+ "[Foundation Config Warning] Error parsing user emoji set configuration",
389
+ config_key="PROVIDE_LOG_USER_DEFINED_EMOJI_SETS",
390
+ error=str(e),
391
+ error_type=type(e).__name__,
392
+ )
393
+
394
+ return cls.from_dict(config_dict, source=ConfigSource.ENV)
@@ -0,0 +1,188 @@
1
+ #
2
+ # telemetry.py
3
+ #
4
+ """
5
+ TelemetryConfig class for Foundation telemetry configuration.
6
+ """
7
+
8
+ import os
9
+
10
+ from attrs import define
11
+
12
+ from provide.foundation.config import BaseConfig, field
13
+ from provide.foundation.logger.config.logging import LoggingConfig
14
+
15
+
16
+ @define(slots=True, repr=False)
17
+ class TelemetryConfig(BaseConfig):
18
+ """Main configuration object for the Foundation Telemetry system."""
19
+
20
+ service_name: str | None = field(
21
+ default=None,
22
+ env_var="PROVIDE_SERVICE_NAME",
23
+ description="Service name for telemetry",
24
+ )
25
+ service_version: str | None = field(
26
+ default=None,
27
+ env_var="PROVIDE_SERVICE_VERSION",
28
+ description="Service version for telemetry",
29
+ )
30
+ logging: LoggingConfig = field(
31
+ factory=LoggingConfig, description="Logging configuration"
32
+ )
33
+ globally_disabled: bool = field(
34
+ default=False,
35
+ env_var="PROVIDE_TELEMETRY_DISABLED",
36
+ description="Globally disable telemetry",
37
+ )
38
+
39
+ # OpenTelemetry configuration
40
+ tracing_enabled: bool = field(
41
+ default=True,
42
+ env_var="OTEL_TRACING_ENABLED",
43
+ description="Enable OpenTelemetry tracing",
44
+ )
45
+ metrics_enabled: bool = field(
46
+ default=True,
47
+ env_var="OTEL_METRICS_ENABLED",
48
+ description="Enable OpenTelemetry metrics",
49
+ )
50
+ otlp_endpoint: str | None = field(
51
+ default=None,
52
+ env_var="OTEL_EXPORTER_OTLP_ENDPOINT",
53
+ description="OTLP endpoint for traces and metrics",
54
+ )
55
+ otlp_traces_endpoint: str | None = field(
56
+ default=None,
57
+ env_var="OTEL_EXPORTER_OTLP_TRACES_ENDPOINT",
58
+ description="OTLP endpoint specifically for traces",
59
+ )
60
+ otlp_headers: str | None = field(
61
+ default=None,
62
+ env_var="OTEL_EXPORTER_OTLP_HEADERS",
63
+ description="Headers to send with OTLP requests (key1=value1,key2=value2)",
64
+ )
65
+ otlp_protocol: str = field(
66
+ default="http/protobuf",
67
+ env_var="OTEL_EXPORTER_OTLP_PROTOCOL",
68
+ description="OTLP protocol (grpc, http/protobuf)",
69
+ )
70
+ trace_sample_rate: float = field(
71
+ default=1.0,
72
+ env_var="OTEL_TRACE_SAMPLE_RATE",
73
+ description="Sampling rate for traces (0.0 to 1.0)",
74
+ )
75
+
76
+ # OpenObserve configuration
77
+ openobserve_url: str | None = field(
78
+ default=None,
79
+ env_var="OPENOBSERVE_URL",
80
+ description="OpenObserve API endpoint URL",
81
+ )
82
+ openobserve_org: str = field(
83
+ default="default",
84
+ env_var="OPENOBSERVE_ORG",
85
+ description="OpenObserve organization name",
86
+ )
87
+ openobserve_user: str | None = field(
88
+ default=None,
89
+ env_var="OPENOBSERVE_USER",
90
+ description="OpenObserve username for authentication",
91
+ )
92
+ openobserve_password: str | None = field(
93
+ default=None,
94
+ env_var="OPENOBSERVE_PASSWORD",
95
+ description="OpenObserve password for authentication",
96
+ )
97
+ openobserve_stream: str = field(
98
+ default="default",
99
+ env_var="OPENOBSERVE_STREAM",
100
+ description="Default OpenObserve stream name",
101
+ )
102
+
103
+ @classmethod
104
+ def from_env(cls, strict: bool = True) -> "TelemetryConfig":
105
+ """Creates a TelemetryConfig instance from environment variables."""
106
+ config_dict = {}
107
+
108
+ # Check OTEL_SERVICE_NAME first, then PROVIDE_SERVICE_NAME
109
+ service_name = os.getenv("OTEL_SERVICE_NAME") or os.getenv(
110
+ "PROVIDE_SERVICE_NAME"
111
+ )
112
+ if service_name:
113
+ config_dict["service_name"] = service_name
114
+
115
+ # Service version
116
+ service_version = os.getenv("OTEL_SERVICE_VERSION") or os.getenv(
117
+ "PROVIDE_SERVICE_VERSION"
118
+ )
119
+ if service_version:
120
+ config_dict["service_version"] = service_version
121
+
122
+ # Telemetry disable flag
123
+ if disabled := os.getenv("PROVIDE_TELEMETRY_DISABLED"):
124
+ config_dict["globally_disabled"] = disabled.lower() == "true"
125
+
126
+ # OpenTelemetry specific configuration
127
+ if tracing_enabled := os.getenv("OTEL_TRACING_ENABLED"):
128
+ config_dict["tracing_enabled"] = tracing_enabled.lower() == "true"
129
+
130
+ if metrics_enabled := os.getenv("OTEL_METRICS_ENABLED"):
131
+ config_dict["metrics_enabled"] = metrics_enabled.lower() == "true"
132
+
133
+ if otlp_endpoint := os.getenv("OTEL_EXPORTER_OTLP_ENDPOINT"):
134
+ config_dict["otlp_endpoint"] = otlp_endpoint
135
+
136
+ if otlp_traces_endpoint := os.getenv("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"):
137
+ config_dict["otlp_traces_endpoint"] = otlp_traces_endpoint
138
+
139
+ if otlp_headers := os.getenv("OTEL_EXPORTER_OTLP_HEADERS"):
140
+ config_dict["otlp_headers"] = otlp_headers
141
+
142
+ if otlp_protocol := os.getenv("OTEL_EXPORTER_OTLP_PROTOCOL"):
143
+ config_dict["otlp_protocol"] = otlp_protocol
144
+
145
+ if trace_sample_rate := os.getenv("OTEL_TRACE_SAMPLE_RATE"):
146
+ try:
147
+ config_dict["trace_sample_rate"] = float(trace_sample_rate)
148
+ except ValueError:
149
+ if strict:
150
+ raise
151
+ # Use default value in non-strict mode
152
+
153
+ # OpenObserve configuration
154
+ if openobserve_url := os.getenv("OPENOBSERVE_URL"):
155
+ config_dict["openobserve_url"] = openobserve_url
156
+
157
+ if openobserve_org := os.getenv("OPENOBSERVE_ORG"):
158
+ config_dict["openobserve_org"] = openobserve_org
159
+
160
+ if openobserve_user := os.getenv("OPENOBSERVE_USER"):
161
+ config_dict["openobserve_user"] = openobserve_user
162
+
163
+ if openobserve_password := os.getenv("OPENOBSERVE_PASSWORD"):
164
+ config_dict["openobserve_password"] = openobserve_password
165
+
166
+ if openobserve_stream := os.getenv("OPENOBSERVE_STREAM"):
167
+ config_dict["openobserve_stream"] = openobserve_stream
168
+
169
+ # Load logging config from env
170
+ config_dict["logging"] = LoggingConfig.from_env(strict=strict)
171
+
172
+ return cls(**config_dict)
173
+
174
+ def get_otlp_headers_dict(self) -> dict[str, str]:
175
+ """Parse OTLP headers string into dictionary.
176
+
177
+ Returns:
178
+ Dictionary of header key-value pairs
179
+ """
180
+ if not self.otlp_headers:
181
+ return {}
182
+
183
+ headers = {}
184
+ for pair in self.otlp_headers.split(","):
185
+ if "=" in pair:
186
+ key, value = pair.split("=", 1)
187
+ headers[key.strip()] = value.strip()
188
+ return headers