Mercurial > p > roundup > code
annotate roundup/logcontext.py @ 8487:b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
nanoid is a shorter unique id generator and faster than uuid.
I truncate nanoid id's to 12 chars to make it more readable.
Also added decorator to allow setting the default processName
definition in the logging module. admin.py and wsgi_handler now set
processName. configuration.py knows how to overide the processName if
set to the default MainProcess.
Updated install docs to add nanoid as optional, how to switch to
different trace_id output. pydoc generated docs include logcontext
module and are referenced from admin.py.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Mon, 08 Dec 2025 00:23:14 -0500 |
| parents | df9fc9080e5a |
| children | f80c566f5726 |
| rev | line source |
|---|---|
|
8487
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
1 """Generate and store thread local logging context including unique |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
2 trace id for request, request source etc. to be logged. |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
3 |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
4 Trace id generator can use nanoid or uuid.uuid4 stdlib function. |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
5 Nanoid is preferred if nanoid is installed using pip as nanoid is |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
6 faster and generates a shorter id. If nanoid is installed in the |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
7 tracker's lib subdirectory, it must be enabled using the tracker's |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
8 interfaces.py by adding:: |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
9 |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
10 # if nanoid is installed in tracker's lib directory or |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
11 # if you want to change the length of the nanoid from 12 |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
12 # to 14 chars use: |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
13 from functools import partial |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
14 from nanoid import generate |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
15 import roundup.logcontext |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
16 # change 14 to 12 to get the default nanoid size. |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
17 roundup.logcontext.idgen=partial(generate, size=14) |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
18 |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
19 # to force use of shortened uuid when nanoid is |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
20 # loaded by default |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
21 import roundup.logcontext |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
22 roundup.logcontext.idgen=roundup.logcontext.short_uuid |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
23 |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
24 """ |
|
8446
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
25 import contextvars |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
26 import functools |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
27 import logging |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
28 import os |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
29 import uuid |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
30 |
|
8487
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
31 |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
32 def short_uuid(): |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
33 """Encode a UUID integer in a shorter form for display. |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
34 |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
35 A uuid is long. Make a shorter version that takes less room |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
36 in a log line and is easier to store. |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
37 """ |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
38 alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890" |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
39 result = "" |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
40 alphabet_len = len(alphabet) |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
41 uuid_int = uuid.uuid4().int |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
42 while uuid_int: |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
43 uuid_int, t = divmod(uuid_int, alphabet_len) |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
44 result += alphabet[t] |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
45 return result or "0" |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
46 |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
47 |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
48 try: |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
49 from nanoid import generate |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
50 # With size=12 and the normal alphabet, it take ~4 months |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
51 # with 1000 nanoid's/sec to generate a collision with 1% |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
52 # probability. That's 100 users sec continously. These id's |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
53 # are used to link logging messages/traces that are all done |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
54 # in a few seconds. Collisions ae unlikely to happen in the |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
55 # same time period leading to confusion. |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
56 # |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
57 # nanoid is faster than shortened uuids. |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
58 # 1,000,000 generate(size=12) timeit.timeit at 25.4 seconds |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
59 # 1,000,000 generate(size=21) timeit.timeit at 33.7 seconds |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
60 |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
61 #: Variable used for setting the id generator. |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
62 idgen = functools.partial(generate, size=12) |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
63 except ImportError: |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
64 # 1,000,000 of short_uuid() timeit.timeit at 54.1 seconds |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
65 idgen = short_uuid #: :meta hide-value: |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
66 |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
67 |
|
8446
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
68 logger = logging.getLogger("roundup.logcontext") |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
69 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
70 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
71 class SimpleSentinel: |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
72 """A hack to get a sentinel value where I can define __str__(). |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
73 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
74 I was using sentinel = object(). However some code paths |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
75 resulted in the sentinel object showing up as an argument |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
76 to print or logging.warning|error|debug(...). In this case |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
77 seeing "<class 'object'>" in the output isn't useful. |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
78 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
79 So I created this class (with slots) as a fast method where |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
80 I could control the __str__ representation. |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
81 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
82 """ |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
83 __slots__ = ("name", "str") |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
84 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
85 def __init__(self, name=None, str_value=""): |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
86 self.name = name |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
87 self.str = str_value |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
88 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
89 def __str__(self): |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
90 # Generate a string without whitespace. |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
91 # Used in logging where whitespace could be |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
92 # a field delimiter |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
93 return ("%s%s" % ( |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
94 self.name + "-" if self.name else "", |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
95 self.str)).replace(" ", "_") |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
96 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
97 def __repr__(self): |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
98 return 'SimpleSentinel(name=%s, str_value="%s")' % ( |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
99 self.name, self.str) |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
100 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
101 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
102 # store the context variable names in a dict. Because |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
103 # contactvars.copy_context().items() returns nothing if set has |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
104 # not been called on a context var. I need the contextvar names |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
105 # even if they have not been set. |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
106 ctx_vars = {} |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
107 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
108 # set up sentinel values that will print a suitable error value |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
109 # and the context vars they are associated with. |
|
8487
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
110 _SENTINEL_PROCESSNAME = SimpleSentinel("processName", None) |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
111 ctx_vars['processName'] = contextvars.ContextVar("processName", |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
112 default=_SENTINEL_PROCESSNAME) |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
113 |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
114 |
|
8446
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
115 _SENTINEL_ID = SimpleSentinel("trace_id", "not set") |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
116 ctx_vars['trace_id'] = contextvars.ContextVar("trace_id", default=_SENTINEL_ID) |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
117 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
118 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
119 _SENTINEL_REASON = SimpleSentinel("trace_reason", "missing") |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
120 ctx_vars['trace_reason'] = contextvars.ContextVar("trace_reason", |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
121 default=_SENTINEL_REASON) |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
122 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
123 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
124 def gen_trace_id(): |
|
8487
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
125 """Decorator to generate a trace id (nanoid or encoded uuid4) as contextvar |
|
8446
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
126 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
127 The logging routine uses this to label every log line. All |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
128 logs with the same trace_id should be generated from a |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
129 single request. |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
130 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
131 This decorator is applied to an entry point for a request. |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
132 Different methods of invoking Roundup have different entry |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
133 points. As a result, this decorator can be called multiple |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
134 times as some entry points can traverse another entry point |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
135 used by a different invocation method. It will not set a |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
136 trace_id if one is already assigned. |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
137 |
|
8487
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
138 If a uuid4() is used as the id, the uuid4 integer is encoded |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
139 into a 62 character alphabet (A-Za-z0-9) to shorten |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
140 the log line. |
|
8446
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
141 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
142 This decorator may produce duplicate (colliding) trace_id's |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
143 when used with multiple processes on some platforms where |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
144 uuid.uuid4().is_safe is unknown. Probability of a collision |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
145 is unknown. |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
146 |
|
8487
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
147 If nanoid is used to generate the id, it is 12 chars long and |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
148 uses a 64 char ascii alphabet, the 62 above with '_' and '-'. |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
149 The shorter nanoid has < 1% chance of collision in ~4 months |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
150 when generating 1000 id's per second. |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
151 |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
152 See the help text for the module to change how the id is |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
153 generated. |
|
8446
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
154 """ |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
155 def decorator(func): |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
156 @functools.wraps(func) |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
157 def wrapper(*args, **kwargs): |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
158 prev = None |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
159 trace_id = ctx_vars['trace_id'] |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
160 if trace_id.get() is _SENTINEL_ID: |
|
8487
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
161 prev = trace_id.set(idgen()) |
|
8446
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
162 try: |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
163 r = func(*args, **kwargs) |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
164 finally: |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
165 if prev: |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
166 trace_id.reset(prev) |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
167 return r |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
168 return wrapper |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
169 return decorator |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
170 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
171 |
|
8487
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
172 def set_processName(name): |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
173 """Decorator to set the processName used in the LogRecord |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
174 """ |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
175 def decorator(func): |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
176 @functools.wraps(func) |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
177 def wrapper(*args, **kwargs): |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
178 prev = None |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
179 processName = ctx_vars['processName'] |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
180 if processName.get() is _SENTINEL_PROCESSNAME: |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
181 prev = processName.set(name) |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
182 try: |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
183 r = func(*args, **kwargs) |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
184 finally: |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
185 if prev: |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
186 processName.reset(prev) |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
187 return r |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
188 return wrapper |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
189 return decorator |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
190 |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
191 |
|
8446
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
192 def store_trace_reason(location=None): |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
193 """Decorator finds and stores a reason trace was started in contextvar. |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
194 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
195 Record the url for a regular web triggered request. |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
196 Record the message id for an email triggered request. |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
197 Record a roundup-admin command/action for roundup-admin request. |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
198 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
199 Because the reason can be stored in different locations |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
200 depending on where this is called, it is called with a |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
201 location hint to activate the right extraction method. |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
202 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
203 If the reason has already been stored (and it's not "missing", |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
204 it tries to extract it again and verifies it's the same as the |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
205 stored reason. If it's not the same it logs an error. This |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
206 safety check may be removed in a future version of Roundup. |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
207 """ |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
208 def decorator(func): |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
209 @functools.wraps(func) |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
210 def wrapper(*args, **kwargs): |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
211 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
212 reason = None |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
213 prev_trace_reason = None |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
214 trace_reason = ctx_vars['trace_reason'] |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
215 stored_reason = trace_reason.get() |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
216 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
217 # Fast return path. Not enabled to make sure SANITY |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
218 # CHECK below runs. If the CHECK fails, we have a a |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
219 # bad internal issue: contextvars shared between |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
220 # threads, roundup modifying reason within a request, ... |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
221 # |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
222 # if stored_reason is not _SENTINEL_REASON: |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
223 # return func(*args, **kwargs) |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
224 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
225 # use location to determine how to extract the reason |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
226 if location == "wsgi" and 'REQUEST_URI' in args[1]: |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
227 reason = args[1]['REQUEST_URI'] |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
228 elif location == "client" and 'REQUEST_URI' in args[3]: |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
229 reason = args[3]['REQUEST_URI'] |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
230 elif location == "mailgw": |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
231 reason = args[1].get_header('message-id', "no_message_id") |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
232 elif location == "admin": |
|
8449
756cdf8e34f2
fix: os.getlogin fails with OSError in CI.
John Rouillard <rouilj@ieee.org>
parents:
8448
diff
changeset
|
233 try: |
|
756cdf8e34f2
fix: os.getlogin fails with OSError in CI.
John Rouillard <rouilj@ieee.org>
parents:
8448
diff
changeset
|
234 login = os.getlogin() |
|
756cdf8e34f2
fix: os.getlogin fails with OSError in CI.
John Rouillard <rouilj@ieee.org>
parents:
8448
diff
changeset
|
235 except OSError: |
|
8450
df9fc9080e5a
fix: use getloing value --unknown-- on error.
John Rouillard <rouilj@ieee.org>
parents:
8449
diff
changeset
|
236 login = "--unknown--" |
|
8449
756cdf8e34f2
fix: os.getlogin fails with OSError in CI.
John Rouillard <rouilj@ieee.org>
parents:
8448
diff
changeset
|
237 reason = "roundup-admin(%s): %s" % (login, args[1][:2]) |
|
8446
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
238 elif location.startswith("file://"): |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
239 reason = location |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
240 elif location == "client_main" and 'REQUEST_URI' in args[0].env: |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
241 reason = args[0].env['REQUEST_URI'] |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
242 elif location == "xmlrpc-server": |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
243 reason = args[0].path |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
244 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
245 if reason is None: |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
246 pass |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
247 elif stored_reason is _SENTINEL_REASON: |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
248 # no value stored and reason is not none, update |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
249 prev_trace_reason = trace_reason.set(reason) |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
250 elif reason != stored_reason: # SANITY CHECK |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
251 # Throw an error we have mismatched REASON's which |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
252 # should never happen. |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
253 logger.error("Mismatched REASON's: stored: %s, new: %s at %s", |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
254 stored_reason, reason, location) |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
255 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
256 try: |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
257 r = func(*args, **kwargs) |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
258 finally: |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
259 # reset context var in case thread is reused for |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
260 # another request. |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
261 if prev_trace_reason: |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
262 trace_reason.reset(prev_trace_reason) |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
263 return r |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
264 return wrapper |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
265 return decorator |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
266 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
267 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
268 def get_context_info(): |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
269 """Return list of context var tuples [(var_name, var_value), ...]""" |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
270 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
271 return [(name, ctx.get()) for name, ctx in ctx_vars.items()] |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
272 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
273 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
274 #Is returning a dict for this info more pythonic? |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
275 def get_context_dict(): |
|
8487
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
276 """Return dict of context var tuples {"var_name": "var_value", ...}""" |
|
8446
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
277 return {name: ctx.get() for name, ctx in ctx_vars.items()} |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
278 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
279 # Dummy no=op implementation of this module: |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
280 # |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
281 #def noop_decorator(*args, **kwargs): |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
282 # def decorator(func): |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
283 # return func |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
284 # return decorator |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
285 # |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
286 #def get_context_info(): |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
287 # return [ ("trace_id", "noop_trace_id"), |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
288 # ("trace_reason", "noop_trace_reason") ] |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
289 #gen_trace_id = store_trace_reason = noop_decorator |
