django-log-formatter-asim 0.0.6__tar.gz → 1.0.0__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.
@@ -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.6
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
@@ -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
 
@@ -3,7 +3,7 @@ line-length = 100
3
3
 
4
4
  [tool.poetry]
5
5
  name = "django-log-formatter-asim"
6
- version = "0.0.6"
6
+ version = "1.0.0"
7
7
  description = "Formats Django logs in ASIM format."
8
8
  authors = ["Department for Business and Trade Platform Team <sre-team@digital.trade.gov.uk>"]
9
9
  license = "MIT"
@@ -19,6 +19,7 @@ django = [
19
19
  { version = ">=3,<5", python = ">=3.9,<3.10" },
20
20
  { version = ">=3,<6", python = ">=3.10,<4" },
21
21
  ]
22
+ ddtrace = "^3.2.1"
22
23
 
23
24
  [tool.poetry.group.dev.dependencies]
24
25
  pre-commit = "^3.5.0"