django-log-formatter-asim 0.0.5__py3-none-any.whl → 1.0.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.
@@ -2,7 +2,10 @@ import json
2
2
  import logging
3
3
  from datetime import datetime
4
4
  from importlib.metadata import distribution
5
+ import os
5
6
 
7
+ import ddtrace
8
+ from ddtrace.trace import tracer
6
9
  from django.conf import settings
7
10
 
8
11
 
@@ -25,6 +28,46 @@ class ASIMRootFormatter:
25
28
 
26
29
  return copied_dict
27
30
 
31
+ def _get_container_id(self):
32
+ """
33
+ The dockerId (container Id) is available via the metadata endpoint. However, it looks like it is embedded in the
34
+ metadata URL e.g.:
35
+ ECS_CONTAINER_METADATA_URI=http://169.254.170.2/v3/709d1c10779d47b2a84db9eef2ebd041-0265927825
36
+ See: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-metadata-endpoint-v4-response.html
37
+ """
38
+
39
+ try:
40
+ return os.environ["ECS_CONTAINER_METADATA_URI"].split("/")[-1]
41
+ except (KeyError, IndexError):
42
+ return ""
43
+
44
+ def _get_first_64_bits_of(self, trace_id):
45
+ # See https://docs.datadoghq.com/tracing/other_telemetry/connect_logs_and_traces/python/#no-standard-library-logging
46
+ return str((1 << 64) - 1 & trace_id)
47
+
48
+ def _datadog_trace_dict(self):
49
+ event_dict = {}
50
+
51
+ span = tracer.current_span()
52
+ trace_id, span_id = (
53
+ (self._get_first_64_bits_of(span.trace_id), span.span_id)
54
+ if span
55
+ else (None, None)
56
+ )
57
+
58
+ # add ids to structlog event dictionary
59
+ event_dict["dd.trace_id"] = str(trace_id or 0)
60
+ event_dict["dd.span_id"] = str(span_id or 0)
61
+
62
+ # add the env, service, and version configured for the tracer
63
+ event_dict["env"] = ddtrace.config.env or ""
64
+ event_dict["service"] = ddtrace.config.service or ""
65
+ event_dict["version"] = ddtrace.config.version or ""
66
+
67
+ event_dict["container_id"] = self._get_container_id()
68
+
69
+ return event_dict
70
+
28
71
  def get_log_dict(self):
29
72
  record = self.record
30
73
  log_time = datetime.utcfromtimestamp(record.created).isoformat()
@@ -48,6 +91,8 @@ class ASIMRootFormatter:
48
91
  },
49
92
  }
50
93
 
94
+ log_dict.update(self._datadog_trace_dict())
95
+
51
96
  if getattr(settings, "DLFA_INCLUDE_RAW_LOG", False):
52
97
  return self.get_log_dict_with_raw(log_dict)
53
98
 
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.3
2
2
  Name: django-log-formatter-asim
3
- Version: 0.0.5
3
+ Version: 1.0.0
4
4
  Summary: Formats Django logs in ASIM format.
5
5
  License: MIT
6
6
  Author: Department for Business and Trade Platform Team
@@ -12,7 +12,9 @@ Classifier: Programming Language :: Python :: 3.9
12
12
  Classifier: Programming Language :: Python :: 3.10
13
13
  Classifier: Programming Language :: Python :: 3.11
14
14
  Classifier: Programming Language :: Python :: 3.12
15
- Requires-Dist: django (>=3,<5) ; python_version >= "3.9" and python_version < "3.10"
15
+ Classifier: Programming Language :: Python :: 3.13
16
+ Requires-Dist: ddtrace (>=3.2.1,<4.0.0)
17
+ Requires-Dist: django (>=3,<5) ; python_version == "3.9"
16
18
  Requires-Dist: django (>=3,<6) ; python_version >= "3.10" and python_version < "4"
17
19
  Requires-Dist: pre-commit (>=3.5.0,<4.0.0)
18
20
  Description-Content-Type: text/markdown
@@ -140,3 +142,35 @@ Run `poetry run pytest` in the root directory to run all tests.
140
142
 
141
143
  Or, run `poetry run tox` in the root directory to run all tests for multiple Python versions. See the [`tox` configuration file](tox.ini).
142
144
 
145
+ ### Publishing
146
+
147
+ 1. Acquire API token from [Passman](https://passman.ci.uktrade.digital/secret/cc82a3f7-ddfa-4312-ab56-1ff8528dadc8/).
148
+ - Request access from the SRE team.
149
+ - _Note: You will need access to the `platform` group in Passman._
150
+ 2. Run `poetry config pypi-token.pypi <token>` to add the token to your Poetry configuration.
151
+
152
+ Update the version, as the same version cannot be published to PyPI.
153
+
154
+ ```
155
+ poetry version patch
156
+ ```
157
+
158
+ More options for the `version` command can be found in the [Poetry documentation](https://python-poetry.org/docs/cli/#version). For example, for a minor version bump: `poetry version minor`.
159
+
160
+ Build the Python package.
161
+
162
+ ```
163
+ poetry build
164
+ ```
165
+
166
+ Publish the Python package.
167
+
168
+ _Note: Make sure your Pull Request (PR) is approved and contains the version upgrade in `pyproject.toml` before publishing the package._
169
+
170
+ ```
171
+ poetry publish
172
+ ```
173
+
174
+ Check the [PyPI Release history](https://pypi.org/project/django-log-formatter-asim/#history) to make sure the package has been updated.
175
+
176
+ For an optional manual check, install the package locally and test everything works as expected.
@@ -0,0 +1,5 @@
1
+ django_log_formatter_asim/__init__.py,sha256=i-7HqYE5s3hjmHLh4TTzLpEdgr5N22msFEQw16Pe_EI,7867
2
+ django_log_formatter_asim-1.0.0.dist-info/LICENSE,sha256=dP79lN73--7LMApnankTGLqDbImXg8iYFqWgnExGkGk,1090
3
+ django_log_formatter_asim-1.0.0.dist-info/METADATA,sha256=Gmn92QLejMsh63aivEaeqLfOKfnGl-9Hw5MPO-YC5KQ,5576
4
+ django_log_formatter_asim-1.0.0.dist-info/WHEEL,sha256=XbeZDeTWKc1w7CSIyre5aMDU_-PohRwTQceYnisIYYY,88
5
+ django_log_formatter_asim-1.0.0.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: poetry-core 1.9.0
2
+ Generator: poetry-core 2.1.1
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
@@ -1,5 +0,0 @@
1
- django_log_formatter_asim/__init__.py,sha256=xB7ESmQuHJ1XZJpXOdRIpdB9qra9b_Uc6es3FsgDlEc,6215
2
- django_log_formatter_asim-0.0.5.dist-info/LICENSE,sha256=dP79lN73--7LMApnankTGLqDbImXg8iYFqWgnExGkGk,1090
3
- django_log_formatter_asim-0.0.5.dist-info/METADATA,sha256=iwI4EBJpc7lQApvTjQ-y-B2tIK3kWo-a_WVPzhjEL18,4408
4
- django_log_formatter_asim-0.0.5.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
5
- django_log_formatter_asim-0.0.5.dist-info/RECORD,,