base-api-utils 1.1.124__tar.gz → 1.1.126__tar.gz

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 (140) hide show
  1. {base_api_utils-1.1.124/src/base_api_utils.egg-info → base_api_utils-1.1.126}/PKG-INFO +30 -1
  2. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/README.md +15 -1
  3. base_api_utils-1.1.126/pyproject.toml +65 -0
  4. base_api_utils-1.1.126/src/base_api_utils/log_handlers.py +22 -0
  5. {base_api_utils-1.1.124/src/base_api_utils/utils → base_api_utils-1.1.126/src/base_api_utils}/log_redaction.py +4 -0
  6. base_api_utils-1.1.126/src/base_api_utils/logging_config.py +140 -0
  7. base_api_utils-1.1.126/src/base_api_utils/otel_celery.py +80 -0
  8. base_api_utils-1.1.126/src/base_api_utils/otel_instrumentation.py +527 -0
  9. base_api_utils-1.1.126/src/base_api_utils/otel_logging.py +264 -0
  10. base_api_utils-1.1.126/src/base_api_utils/tracing.py +65 -0
  11. base_api_utils-1.1.126/src/base_api_utils/utils/log_redaction.py +15 -0
  12. {base_api_utils-1.1.124 → base_api_utils-1.1.126/src/base_api_utils.egg-info}/PKG-INFO +30 -1
  13. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils.egg-info/SOURCES.txt +12 -0
  14. base_api_utils-1.1.126/src/base_api_utils.egg-info/requires.txt +32 -0
  15. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/tests/test_log_redaction.py +1 -1
  16. base_api_utils-1.1.126/tests/test_logging_config.py +339 -0
  17. base_api_utils-1.1.126/tests/test_otel_celery.py +224 -0
  18. base_api_utils-1.1.126/tests/test_otel_instrumentation.py +790 -0
  19. base_api_utils-1.1.126/tests/test_otel_logging.py +507 -0
  20. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/tests/test_retryable_event_propagation.py +1 -1
  21. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/tests/test_token_logging.py +1 -1
  22. base_api_utils-1.1.126/tests/test_tracing.py +134 -0
  23. base_api_utils-1.1.124/pyproject.toml +0 -45
  24. base_api_utils-1.1.124/src/base_api_utils.egg-info/requires.txt +0 -16
  25. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/LICENSE +0 -0
  26. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/setup.cfg +0 -0
  27. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/__init__.py +0 -0
  28. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/commands/__init__.py +0 -0
  29. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/commands/failed_task_management_mixin.py +0 -0
  30. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/domain/__init__.py +0 -0
  31. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/domain/event_message.py +0 -0
  32. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/domain/event_message_factory.py +0 -0
  33. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/domain/events.py +0 -0
  34. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/domain/exceptions.py +0 -0
  35. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/dtos/__init__.py +0 -0
  36. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/dtos/authz_user.py +0 -0
  37. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/fields/__init__.py +0 -0
  38. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/fields/email_list_field.py +0 -0
  39. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/filters/__init__.py +0 -0
  40. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/filters/base_filter.py +0 -0
  41. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/filters/custom_ordering_filter.py +0 -0
  42. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/filters/filter_utils_mixin.py +0 -0
  43. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/handlers/__init__.py +0 -0
  44. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/handlers/base_event_handler.py +0 -0
  45. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/handlers/sponsor_event_handler.py +0 -0
  46. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/handlers/sponsorship_addon_event_handler.py +0 -0
  47. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/handlers/sponsorship_event_handler.py +0 -0
  48. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/handlers/summit_event_handler.py +0 -0
  49. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/ioc/__init__.py +0 -0
  50. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/ioc/container.py +0 -0
  51. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/models/__init__.py +0 -0
  52. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/models/item_image.py +0 -0
  53. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/security/__init__.py +0 -0
  54. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/security/abstract_access_token_service.py +0 -0
  55. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/security/abstract_oauth2_api_client.py +0 -0
  56. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/security/access_token_service.py +0 -0
  57. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/security/check_sponsor_user_permissions.py +0 -0
  58. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/security/group_required.py +0 -0
  59. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/security/oauth2_authentication.py +0 -0
  60. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/security/oauth2_client_factory.py +0 -0
  61. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/security/oauth2_scope_required.py +0 -0
  62. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/security/permission_event_handler.py +0 -0
  63. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/security/shared.py +0 -0
  64. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/security/user_permissions_service.py +0 -0
  65. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/serializers/__init__.py +0 -0
  66. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/serializers/base_model_serializer.py +0 -0
  67. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/serializers/email_list_char_field.py +0 -0
  68. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/serializers/nullable_slug_related_field.py +0 -0
  69. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/serializers/serializers_registry.py +0 -0
  70. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/serializers/task_result_serializer.py +0 -0
  71. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/serializers/timestamp_field.py +0 -0
  72. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/serializers/v2/__init__.py +0 -0
  73. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/serializers/v2/abstract_serializer.py +0 -0
  74. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/serializers/v2/base_model_serializer.py +0 -0
  75. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/serializers/v2/expands.py +0 -0
  76. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/serializers/v2/query_params.py +0 -0
  77. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/serializers/v2/spectacular.py +0 -0
  78. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/services/__init__.py +0 -0
  79. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/services/abstract_domain_events_service.py +0 -0
  80. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/services/abstract_event_dispatcher.py +0 -0
  81. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/services/abstract_images_service.py +0 -0
  82. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/services/abstract_sponsor_metadata_service.py +0 -0
  83. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/services/abstract_storage_service.py +0 -0
  84. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/services/abstract_summit_metadata_service.py +0 -0
  85. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/services/amqp_service.py +0 -0
  86. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/services/celery_failed.py +0 -0
  87. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/services/event_dispatcher.py +0 -0
  88. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/services/images_service.py +0 -0
  89. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/services/s3_service.py +0 -0
  90. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/services/strategies/__init__.py +0 -0
  91. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/services/strategies/abstract_task_runner_strategy.py +0 -0
  92. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/services/strategies/publish_domain_event_strategy.py +0 -0
  93. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/tests/__init__.py +0 -0
  94. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/tests/test_failed_celery_task_management.py +0 -0
  95. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/tests/test_task_status_endpoints.py +0 -0
  96. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/urls.py +0 -0
  97. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/utils/__init__.py +0 -0
  98. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/utils/admin_groups.py +0 -0
  99. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/utils/async_helpers.py +0 -0
  100. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/utils/circuit_breaker.py +0 -0
  101. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/utils/config.py +0 -0
  102. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/utils/custom_auto_schema.py +0 -0
  103. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/utils/db_connection_helpers.py +0 -0
  104. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/utils/debounce_helpers.py +0 -0
  105. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/utils/enums.py +0 -0
  106. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/utils/env_var_eval.py +0 -0
  107. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/utils/exceptions.py +0 -0
  108. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/utils/expand_queryset_optimization_mixin.py +0 -0
  109. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/utils/external_errors.py +0 -0
  110. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/utils/file_lock.py +0 -0
  111. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/utils/fingerprint_helpers.py +0 -0
  112. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/utils/http_helpers.py +0 -0
  113. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/utils/inject_job.py +0 -0
  114. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/utils/look_up_value_mixin.py +0 -0
  115. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/utils/model_helpers.py +0 -0
  116. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/utils/oauth2_custom_schema_base.py +0 -0
  117. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/utils/pagination.py +0 -0
  118. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/utils/rate_utils.py +0 -0
  119. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/utils/read_write_serializer_mixin.py +0 -0
  120. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/utils/serializers.py +0 -0
  121. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/utils/service_utils.py +0 -0
  122. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/utils/string.py +0 -0
  123. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/utils/tests.py +0 -0
  124. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/views/__init__.py +0 -0
  125. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/views/base_view.py +0 -0
  126. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/views/parent_children_crud.py +0 -0
  127. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/views/root_entity_crud.py +0 -0
  128. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils/views/tasks_result_view.py +0 -0
  129. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils.egg-info/dependency_links.txt +0 -0
  130. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/src/base_api_utils.egg-info/top_level.txt +0 -0
  131. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/tests/test_amqp_service.py +0 -0
  132. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/tests/test_check_sponsor_user_permissions.py +0 -0
  133. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/tests/test_circuit_breaker.py +0 -0
  134. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/tests/test_db_connection_helpers.py +0 -0
  135. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/tests/test_file_lock.py +0 -0
  136. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/tests/test_is_admin.py +0 -0
  137. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/tests/test_lazy_imports.py +0 -0
  138. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/tests/test_oauth2_scope_required.py +0 -0
  139. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/tests/test_timestamp_field.py +0 -0
  140. {base_api_utils-1.1.124 → base_api_utils-1.1.126}/tests/test_v2_abstract_serializer.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: base-api-utils
3
- Version: 1.1.124
3
+ Version: 1.1.126
4
4
  Summary: Django Rest Framework micro services common utilities
5
5
  Author-email: Román Gutierrez <roman@tipit.net>, Sebastian Marcet <sebastian@tipit.net>
6
6
  Project-URL: Homepage, https://github.com/fntechgit/base-api-utils
@@ -13,6 +13,7 @@ Requires-Dist: boto3
13
13
  Requires-Dist: celery
14
14
  Requires-Dist: Django>=3.2
15
15
  Requires-Dist: django-celery-results
16
+ Requires-Dist: django-model-utils
16
17
  Requires-Dist: django_filter
17
18
  Requires-Dist: djangorestframework>=3.15.2
18
19
  Requires-Dist: drf-spectacular
@@ -22,8 +23,22 @@ Requires-Dist: pika
22
23
  Requires-Dist: pybreaker
23
24
  Requires-Dist: requests
24
25
  Requires-Dist: requests-oauthlib
26
+ Provides-Extra: otel
27
+ Requires-Dist: opentelemetry-sdk>=1.38.0; extra == "otel"
28
+ Requires-Dist: opentelemetry-exporter-otlp-proto-http>=1.38.0; extra == "otel"
29
+ Requires-Dist: opentelemetry-instrumentation-logging; extra == "otel"
30
+ Requires-Dist: opentelemetry-instrumentation-django; extra == "otel"
31
+ Requires-Dist: opentelemetry-instrumentation-mysqlclient; extra == "otel"
32
+ Requires-Dist: opentelemetry-instrumentation-redis; extra == "otel"
33
+ Requires-Dist: opentelemetry-instrumentation-requests; extra == "otel"
34
+ Requires-Dist: opentelemetry-instrumentation-celery; extra == "otel"
25
35
  Provides-Extra: dev
26
36
  Requires-Dist: pytest==9.0.2; extra == "dev"
37
+ Requires-Dist: opentelemetry-sdk==1.44.0; extra == "dev"
38
+ Requires-Dist: opentelemetry-exporter-otlp-proto-http==1.44.0; extra == "dev"
39
+ Requires-Dist: opentelemetry-instrumentation-logging==0.65b0; extra == "dev"
40
+ Requires-Dist: opentelemetry-instrumentation-requests==0.65b0; extra == "dev"
41
+ Requires-Dist: opentelemetry-instrumentation-celery==0.65b0; extra == "dev"
27
42
  Dynamic: license-file
28
43
 
29
44
  # base-api-utils
@@ -109,3 +124,17 @@ pip install git+https://github.com/fntechgit/base-api-utils
109
124
  For a deep dive into the technical implementation, CLI usage, and troubleshooting steps, please refer to the dedicated management guide:
110
125
 
111
126
  **[Management & Recovery Deep Dive: /docs/celery-management.md](./docs/celery-management.md)**
127
+
128
+ # Logging and the OTLP log bridge
129
+
130
+ `base_api_utils.logging_config.build_logging` builds a service's `settings.LOGGING`, and
131
+ `base_api_utils.otel_logging.configure_otel_logging` ships its log records over OTLP:
132
+
133
+ **[Logging and the OTLP log bridge: /docs/logging.md](./docs/logging.md)**
134
+
135
+ # Traces
136
+
137
+ `base_api_utils.otel_instrumentation`, `base_api_utils.tracing` and `base_api_utils.otel_celery`
138
+ give a service its traces, a root span for jobs and consumers, and the Celery worker hooks:
139
+
140
+ **[Traces, root spans and the Celery worker hooks: /docs/tracing.md](./docs/tracing.md)**
@@ -80,4 +80,18 @@ pip install git+https://github.com/fntechgit/base-api-utils
80
80
 
81
81
  For a deep dive into the technical implementation, CLI usage, and troubleshooting steps, please refer to the dedicated management guide:
82
82
 
83
- **[Management & Recovery Deep Dive: /docs/celery-management.md](./docs/celery-management.md)**
83
+ **[Management & Recovery Deep Dive: /docs/celery-management.md](./docs/celery-management.md)**
84
+
85
+ # Logging and the OTLP log bridge
86
+
87
+ `base_api_utils.logging_config.build_logging` builds a service's `settings.LOGGING`, and
88
+ `base_api_utils.otel_logging.configure_otel_logging` ships its log records over OTLP:
89
+
90
+ **[Logging and the OTLP log bridge: /docs/logging.md](./docs/logging.md)**
91
+
92
+ # Traces
93
+
94
+ `base_api_utils.otel_instrumentation`, `base_api_utils.tracing` and `base_api_utils.otel_celery`
95
+ give a service its traces, a root span for jobs and consumers, and the Celery worker hooks:
96
+
97
+ **[Traces, root spans and the Celery worker hooks: /docs/tracing.md](./docs/tracing.md)**
@@ -0,0 +1,65 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61.0"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [tool.setuptools.packages.find]
6
+ where = ["src"]
7
+
8
+ [project]
9
+ name = "base-api-utils"
10
+ version = "1.1.126"
11
+ dependencies = [
12
+ "boto3",
13
+ "celery",
14
+ "Django>=3.2",
15
+ "django-celery-results",
16
+ "django-model-utils",
17
+ "django_filter",
18
+ "djangorestframework>=3.15.2",
19
+ "drf-spectacular",
20
+ "injector",
21
+ "oauthlib",
22
+ "pika",
23
+ "pybreaker",
24
+ "requests",
25
+ "requests-oauthlib"
26
+ ]
27
+ authors = [
28
+ { name="Román Gutierrez", email="roman@tipit.net" },
29
+ { name="Sebastian Marcet", email="sebastian@tipit.net" },
30
+ ]
31
+ description = "Django Rest Framework micro services common utilities"
32
+ readme = "README.md"
33
+ requires-python = ">=3.10"
34
+ classifiers = [
35
+ "Programming Language :: Python :: 3",
36
+ "Operating System :: OS Independent",
37
+ ]
38
+
39
+ [project.optional-dependencies]
40
+ # The OpenTelemetry scaffold (otel_logging, otel_instrumentation, tracing, otel_celery).
41
+ # A service pins its own SDK line.
42
+ otel = [
43
+ "opentelemetry-sdk>=1.38.0",
44
+ "opentelemetry-exporter-otlp-proto-http>=1.38.0",
45
+ "opentelemetry-instrumentation-logging",
46
+ "opentelemetry-instrumentation-django",
47
+ "opentelemetry-instrumentation-mysqlclient",
48
+ "opentelemetry-instrumentation-redis",
49
+ "opentelemetry-instrumentation-requests",
50
+ "opentelemetry-instrumentation-celery"
51
+ ]
52
+ # The instrumentors of Django, mysqlclient and Redis are stubbed in the tests: each imports the
53
+ # library it instruments.
54
+ dev = [
55
+ "pytest==9.0.2",
56
+ "opentelemetry-sdk==1.44.0",
57
+ "opentelemetry-exporter-otlp-proto-http==1.44.0",
58
+ "opentelemetry-instrumentation-logging==0.65b0",
59
+ "opentelemetry-instrumentation-requests==0.65b0",
60
+ "opentelemetry-instrumentation-celery==0.65b0"
61
+ ]
62
+
63
+ [project.urls]
64
+ Homepage = "https://github.com/fntechgit/base-api-utils"
65
+
@@ -0,0 +1,22 @@
1
+ """The local sinks ``logging_config.build_logging`` declares.
2
+
3
+ Each writes a redacted copy of the record, as the OTLP handler exports it: urllib3 logs the
4
+ introspection request line at DEBUG, and base_api_utils.security sends the token in its query
5
+ string.
6
+
7
+ dictConfig loads this module by its dotted path when Django configures logging, which is before it
8
+ loads the apps. So it imports nothing but the standard library and log_redaction, which does the
9
+ same: base_api_utils.utils imports Django REST framework's views, and those resolve the service's
10
+ default authentication classes, which may import a model.
11
+ """
12
+ import logging
13
+
14
+ from base_api_utils.log_redaction import RedactingHandlerMixin
15
+
16
+
17
+ class RedactingStreamHandler(RedactingHandlerMixin, logging.StreamHandler):
18
+ """The console sink."""
19
+
20
+
21
+ class RedactingFileHandler(RedactingHandlerMixin, logging.FileHandler):
22
+ """The file sink."""
@@ -15,6 +15,10 @@ more people, and kept for longer, than a token is valid.
15
15
  alone in a sentence it finds only a long value after the word "token". Where the value is known,
16
16
  log its fingerprint instead of passing the text through it. When in doubt it redacts: a hidden
17
17
  word is the cheaper mistake.
18
+
19
+ This module imports nothing but the standard library: the logging handlers load it while Django
20
+ configures logging, before the app registry is ready, where ``base_api_utils.utils`` cannot be
21
+ imported. ``base_api_utils.utils.log_redaction``, its path until now, re-exports it.
18
22
  """
19
23
  import copy
20
24
  import hashlib
@@ -0,0 +1,140 @@
1
+ """The LOGGING dict of an FN service, built in one place.
2
+
3
+ # settings.py
4
+ from base_api_utils.logging_config import build_logging
5
+
6
+ LOGGING = build_logging(
7
+ log_file=os.path.join(BASE_DIR, 'logs/api.log'),
8
+ loggers={
9
+ 'django': os.getenv('DJANGO_LOG_LEVEL', 'DEBUG'),
10
+ 'api': os.getenv('API_LOG_LEVEL', 'DEBUG'),
11
+ },
12
+ )
13
+
14
+ The service names its loggers and their levels. The rest is the same in every service:
15
+
16
+ - ``LOG_HANDLERS`` picks the local sinks, ``file`` and ``console``, of the root logger and of every
17
+ logger the service names. Both by default, for development. A deployed service keeps ``console``
18
+ until its OTLP bridge is on, then sets the variable empty and logs only through OTLP.
19
+ - The loggers the service names do not propagate: root has the same sinks, and in a Celery worker
20
+ it also carries Celery's own stderr handler, so a record that travelled up would be written twice.
21
+ - The OTLP bridge's own loggers keep the console whatever ``LOG_HANDLERS`` says.
22
+
23
+ settings.py imports this module, so it imports nothing but the standard library:
24
+ ``base_api_utils.utils`` reads REST_FRAMEWORK when it is imported, which fails before the settings
25
+ exist. The handler classes are named by their dotted path and loaded when logging is configured.
26
+ """
27
+ import os
28
+
29
+ LOCAL_SINKS = ('file', 'console')
30
+
31
+ DEFAULT_FORMAT = '%(asctime)s %(name)-12s %(levelname)-8s %(message)s'
32
+
33
+ # The OTLP bridge's own loggers, and the level each is held at: audit.otlp is where
34
+ # base_api_utils.otel_logging reports (enabled, flush timed out) and opentelemetry is the SDK's
35
+ # (failed export, retry). They are never bridged, since a pipeline cannot report its own failure
36
+ # through itself, so they keep the console: with no sink left, a collector that rejects the exports
37
+ # would otherwise leave no trace anywhere. WARNING keeps the SDK's routine lines out.
38
+ DIAGNOSTIC_LOGGERS = {'audit.otlp': 'INFO', 'opentelemetry': 'WARNING'}
39
+
40
+ # Bridged in every service: ftn-audit and base_api_utils.security log on the first two, Django on
41
+ # the others.
42
+ PLATFORM_BRIDGE_LOGGERS = ('audit', 'oauth2', 'django.request', 'django.server')
43
+
44
+ # django.request and django.server propagate to django: a handler on the parent as well would
45
+ # export each of their records twice.
46
+ _NEVER_BRIDGED = ('django', *DIAGNOSTIC_LOGGERS)
47
+
48
+
49
+ def selected_sinks(environ=None):
50
+ """The handlers ``LOG_HANDLERS`` leaves on a logger.
51
+
52
+ A name other than ``file`` or ``console`` is ignored, so a stale value cannot stop the service
53
+ booting: dictConfig refuses a handler it does not know. With no sink left the logger gets a
54
+ NullHandler, because a logger with no handler at all hands WARNING and above to
55
+ ``logging.lastResort``, which writes them to stderr.
56
+ """
57
+ environ = os.environ if environ is None else environ
58
+ names = [name.strip() for name in environ.get('LOG_HANDLERS', ','.join(LOCAL_SINKS)).split(',')]
59
+ return list(dict.fromkeys(name for name in names if name in LOCAL_SINKS)) or ['null']
60
+
61
+
62
+ def build_logging(*, log_file, loggers, root_level='WARNING', fmt=DEFAULT_FORMAT, environ=None):
63
+ """The dict for ``settings.LOGGING``.
64
+
65
+ ``loggers`` maps each logger the service logs to onto its level. ``log_file`` is where the
66
+ ``file`` sink writes; its directory is created when ``LOG_HANDLERS`` selects that sink.
67
+ ``root_level`` is the level of the root logger, which receives the
68
+ loggers the service does not name (third-party libraries). ``fmt`` is the line format of both
69
+ sinks. ``environ`` is read for ``LOG_HANDLERS`` and defaults to ``os.environ``.
70
+
71
+ The result is a plain dict: a service that needs something of its own (a filter, one logger
72
+ with other handlers) changes it after the call.
73
+ """
74
+ taken = sorted(set(loggers) & ({''} | set(DIAGNOSTIC_LOGGERS)))
75
+ if taken:
76
+ raise ValueError(f'build_logging declares these loggers itself: {taken!r}')
77
+
78
+ sinks = selected_sinks(environ)
79
+ if 'file' in sinks:
80
+ # The file opens on the first line written. Without its directory that logging call
81
+ # raises, in the code that logs. Created here, a directory that cannot be created stops
82
+ # the boot instead.
83
+ directory = os.path.dirname(os.fspath(log_file))
84
+ if directory:
85
+ os.makedirs(directory, exist_ok=True)
86
+ # A list of its own for each logger: a service may change one after the call.
87
+ declared = {'': {'handlers': list(sinks), 'level': root_level}}
88
+ for name, level in loggers.items():
89
+ declared[name] = {'handlers': list(sinks), 'level': level, 'propagate': False}
90
+ for name, level in DIAGNOSTIC_LOGGERS.items():
91
+ declared[name] = {'handlers': ['console'], 'level': level, 'propagate': False}
92
+
93
+ return {
94
+ 'version': 1,
95
+ 'disable_existing_loggers': False,
96
+ 'formatters': {
97
+ 'console': {'format': fmt},
98
+ 'file': {'format': fmt},
99
+ },
100
+ 'handlers': {
101
+ 'null': {'class': 'logging.NullHandler'},
102
+ # No require_debug_true: it would silence the console wherever DEBUG is off.
103
+ 'console': {
104
+ 'class': 'base_api_utils.log_handlers.RedactingStreamHandler',
105
+ 'formatter': 'console',
106
+ },
107
+ 'file': {
108
+ 'class': 'base_api_utils.log_handlers.RedactingFileHandler',
109
+ 'formatter': 'file',
110
+ 'filename': os.fspath(log_file),
111
+ # dictConfig builds every handler declared here, selected or not, and a
112
+ # FileHandler opens its file when it is built.
113
+ 'delay': True,
114
+ },
115
+ },
116
+ 'loggers': declared,
117
+ }
118
+
119
+
120
+ def bridge_logger_names(logging_config):
121
+ """The loggers that get the OTLP handler when ``OTEL_BRIDGE_LOGGERS`` names none.
122
+
123
+ The platform loggers plus every logger ``logging_config`` declares, so a logger added to
124
+ ``settings.LOGGING`` is exported without a second list to keep in step. Left out: root,
125
+ ``django``, the bridge's own diagnostics, and a logger that propagates to one already in the
126
+ list, whose records reach that handler anyway.
127
+ """
128
+ declared = (logging_config or {}).get('loggers', {})
129
+ candidates = list(PLATFORM_BRIDGE_LOGGERS)
130
+ for name in declared:
131
+ if name and name not in _NEVER_BRIDGED and name not in candidates:
132
+ candidates.append(name)
133
+
134
+ def reaches_a_bridged_ancestor(name):
135
+ if not declared.get(name, {}).get('propagate', True):
136
+ return False
137
+ parts = name.split('.')
138
+ return any('.'.join(parts[:depth]) in candidates for depth in range(1, len(parts)))
139
+
140
+ return [name for name in candidates if not reaches_a_bridged_ancestor(name)]
@@ -0,0 +1,80 @@
1
+ """What a Celery worker needs for its telemetry to survive the prefork pool.
2
+
3
+ # backend/celery.py
4
+ from base_api_utils.otel_celery import connect_worker_hooks
5
+
6
+ connect_worker_hooks()
7
+
8
+ - A pool child is forked after the parent imported Django, so the Celery instrumentation is
9
+ installed again in each child, from ``worker_process_init``.
10
+ - A child leaves through ``os._exit``, which skips the ``atexit`` hooks that flush the batch
11
+ processors: the spans and log records still queued when the worker stops, or when a child is
12
+ recycled, would be lost. Celery sends ``worker_process_shutdown`` in the child just before that
13
+ exit, and both providers are flushed from there.
14
+
15
+ ``backend/celery.py`` is imported with the ``backend`` package, before the settings exist, so this
16
+ module imports nothing but Celery's signals until a signal fires.
17
+ """
18
+ import logging
19
+ import sys
20
+ import threading
21
+ import time
22
+
23
+ from celery.signals import worker_process_init, worker_process_shutdown
24
+
25
+ # Overall budget for flushing both providers when a worker process exits. The SDK does not bound
26
+ # that flush on its own: force_flush ignores its timeout and shutdown waits for the export thread,
27
+ # so a collector that accepts the connection but never answers would hold the child well past it.
28
+ WORKER_EXIT_FLUSH_DEADLINE_SECONDS = 5
29
+
30
+ _TRACES_MODULE = 'base_api_utils.otel_instrumentation'
31
+
32
+
33
+ def connect_worker_hooks():
34
+ """Connect both receivers. Calling it again changes nothing."""
35
+ worker_process_init.connect(
36
+ _instrument_celery_worker, weak=False, dispatch_uid='base_api_utils.otel_celery.init')
37
+ worker_process_shutdown.connect(
38
+ _flush_otel_on_worker_process_exit, weak=False, dispatch_uid='base_api_utils.otel_celery.shutdown')
39
+
40
+
41
+ def _instrument_celery_worker(**kwargs):
42
+ # The tracer provider itself is set up by settings.py (OTEL_INSTRUMENTATION_ENABLED).
43
+ from django.conf import settings
44
+
45
+ if getattr(settings, 'OTEL_INSTRUMENTATION_ENABLED', False):
46
+ from base_api_utils.otel_instrumentation import DjangoTelemetry
47
+
48
+ DjangoTelemetry.instrument_celery()
49
+
50
+
51
+ def _flush_otel_on_worker_process_exit(**kwargs):
52
+ # Each flush runs in a daemon thread, and the receiver waits for both until the deadline; an
53
+ # export still blocked after that ends with the os._exit. Spans go first, since their shutdown
54
+ # logs a line, but get only half the budget before the log flush starts anyway: a stalled span
55
+ # export must not cost the log records.
56
+ from base_api_utils.otel_logging import shutdown_otel_logging
57
+
58
+ deadline = time.monotonic() + WORKER_EXIT_FLUSH_DEADLINE_SECONDS
59
+ flushers = {}
60
+ # Only when tracing was set up in this process: a service that exports logs alone need not
61
+ # have the instrumentation packages installed.
62
+ traces = sys.modules.get(_TRACES_MODULE)
63
+ if traces is not None:
64
+ flushers['spans'] = _start_exit_flush(traces.DjangoTelemetry.shutdown, 'spans')
65
+ flushers['spans'].join(WORKER_EXIT_FLUSH_DEADLINE_SECONDS / 2)
66
+ flushers['logs'] = _start_exit_flush(shutdown_otel_logging, 'logs')
67
+ for signal, flusher in flushers.items():
68
+ flusher.join(max(0.0, deadline - time.monotonic()))
69
+ if flusher.is_alive():
70
+ logging.getLogger('audit.otlp').warning(
71
+ 'OTLP: %s flush did not finish within %s s; dropping what is still queued.',
72
+ signal,
73
+ WORKER_EXIT_FLUSH_DEADLINE_SECONDS,
74
+ )
75
+
76
+
77
+ def _start_exit_flush(target, signal):
78
+ flusher = threading.Thread(target=target, name=f'otel-worker-exit-flush-{signal}', daemon=True)
79
+ flusher.start()
80
+ return flusher