Mercurial > p > roundup > code
annotate roundup/logcontext.py @ 8564:13732c1d8392
bug: fix typing for pre 3.9 python.
when I added basic typing to logcontext.py I used a spec unsupported in
3.8and earlier.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Thu, 09 Apr 2026 00:09:29 -0400 |
| parents | f80c566f5726 |
| children |
| 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. |
|
8557
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
5 Nanoid is preferred if nanoid is installed using pip. Nanoid is |
|
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
6 faster and generates a shorter id. |
|
8487
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
7 |
|
8557
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
8 If nanoid is installed in the tracker's lib subdirectory, it must be |
|
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
9 enabled using the tracker's interfaces.py by adding:: |
|
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
10 |
|
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
11 # if nanoid is installed in the tracker's lib directory and |
|
8487
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
12 # 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
|
13 # 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
|
14 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
|
15 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
|
16 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
|
17 # 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
|
18 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
|
19 |
|
8557
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
20 # If nanoid is instanned and you want to use use the shortened uuid |
|
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
21 # add this to interfaces.py:: |
|
8487
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
22 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
|
23 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
|
24 |
|
8557
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
25 If you are wrapping a staticmethod, you need to include staticmethod |
|
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
26 before the calls to gen_trace_id or store_trace_reason when wrapping a |
|
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
27 static method. If you don't the static method gets the self argument |
|
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
28 prepended which breaks the call. For example to wrap the 'serve' |
|
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
29 staticmethod from WhiteNoise:: |
|
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
30 |
|
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
31 WhiteNoise.serve = staticmethod(store_trace_reason( |
|
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
32 extract="'whitenoise ' + args[1]['REQUEST_URI']")( |
|
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
33 gen_trace_id()(WhiteNoise.serve))) |
|
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
34 |
|
8487
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
35 """ |
|
8446
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
36 import contextvars |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
37 import functools |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
38 import logging |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
39 import os |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
40 import uuid |
|
8564
13732c1d8392
bug: fix typing for pre 3.9 python.
John Rouillard <rouilj@ieee.org>
parents:
8557
diff
changeset
|
41 from typing import Callable, Dict |
|
8446
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
42 |
|
8487
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
43 |
|
8557
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
44 def short_uuid() -> str: |
|
8487
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
45 """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
|
46 |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
47 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
|
48 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
|
49 """ |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
50 alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890" |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
51 result = "" |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
52 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
|
53 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
|
54 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
|
55 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
|
56 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
|
57 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
|
58 |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
59 |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
60 try: |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
61 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
|
62 # 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
|
63 # 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
|
64 # 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
|
65 # are used to link logging messages/traces that are all done |
|
8557
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
66 # in a few seconds. Collisions are unlikely to happen in the |
|
8487
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
67 # 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
|
68 # |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
69 # 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
|
70 # 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
|
71 # 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
|
72 |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
73 #: Variable used for setting the id generator. |
|
8557
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
74 idgen: Callable = functools.partial(generate, size=12) |
|
8487
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
75 except ImportError: |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
76 # 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
|
77 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
|
78 |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
79 |
|
8446
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
80 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
|
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 class SimpleSentinel: |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
84 """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
|
85 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
86 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
|
87 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
|
88 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
|
89 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
|
90 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
91 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
|
92 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
|
93 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
94 """ |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
95 __slots__ = ("name", "str") |
|
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 __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
|
98 self.name = name |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
99 self.str = str_value |
|
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 def __str__(self): |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
102 # 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
|
103 # 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
|
104 # a field delimiter |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
105 return ("%s%s" % ( |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
106 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
|
107 self.str)).replace(" ", "_") |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
108 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
109 def __repr__(self): |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
110 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
|
111 self.name, self.str) |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
112 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
113 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
114 # 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
|
115 # 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
|
116 # 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
|
117 # even if they have not been set. |
|
8564
13732c1d8392
bug: fix typing for pre 3.9 python.
John Rouillard <rouilj@ieee.org>
parents:
8557
diff
changeset
|
118 ctx_vars: Dict[str, contextvars.ContextVar] = {} |
|
8446
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
119 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
120 # 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
|
121 # 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
|
122 _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
|
123 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
|
124 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
|
125 |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
126 |
|
8446
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
127 _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
|
128 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
|
129 |
|
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 _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
|
132 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
|
133 default=_SENTINEL_REASON) |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
134 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
135 |
|
8557
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
136 def gen_trace_id() -> Callable: |
|
8487
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
137 """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
|
138 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
139 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
|
140 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
|
141 single request. |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
142 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
143 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
|
144 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
|
145 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
|
146 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
|
147 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
|
148 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
|
149 |
|
8487
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
150 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
|
151 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
|
152 the log line. |
|
8446
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
153 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
154 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
|
155 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
|
156 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
|
157 is unknown. |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
158 |
|
8487
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
159 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
|
160 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
|
161 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
|
162 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
|
163 |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
164 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
|
165 generated. |
|
8446
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
166 """ |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
167 def decorator(func): |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
168 @functools.wraps(func) |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
169 def wrapper(*args, **kwargs): |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
170 prev = None |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
171 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
|
172 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
|
173 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
|
174 try: |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
175 r = func(*args, **kwargs) |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
176 finally: |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
177 if prev: |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
178 trace_id.reset(prev) |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
179 return r |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
180 return wrapper |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
181 return decorator |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
182 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
183 |
|
8487
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
184 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
|
185 """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
|
186 """ |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
187 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
|
188 @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
|
189 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
|
190 prev = None |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
191 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
|
192 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
|
193 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
|
194 try: |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
195 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
|
196 finally: |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
197 if prev: |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
198 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
|
199 return r |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
200 return wrapper |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
201 return decorator |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
202 |
|
b09ef85f0da6
feat: add nanoid pkg trace_id gen and decorator for setting processName
John Rouillard <rouilj@ieee.org>
parents:
8450
diff
changeset
|
203 |
|
8557
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
204 def store_trace_reason(location="unset", extract=None): |
|
8446
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
205 """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
|
206 |
|
8557
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
207 Record the url path for a regular web triggered request. |
|
8446
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
208 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
|
209 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
|
210 |
|
8557
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
211 (*) There are multiple entry points to the code. Some entry |
|
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
212 points call through other entry points. As a result this can be |
|
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
213 called multiple times in one request. Because the reason can |
|
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
214 be stored from multiple locations depending on where this is |
|
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
215 called, it is called with a location hint to identify the |
|
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
216 caller (faster than looking up the stack). |
|
8446
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
217 |
|
8557
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
218 If a reason has already been stored (and it's not "missing", it |
|
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
219 tries to extract it again and verifies it's the same as the |
|
8446
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
220 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
|
221 safety check may be removed in a future version of Roundup. |
|
8557
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
222 |
|
8446
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
223 """ |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
224 def decorator(func): |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
225 @functools.wraps(func) |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
226 def wrapper(*args, **kwargs): |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
227 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
228 reason = None |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
229 prev_trace_reason = None |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
230 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
|
231 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
|
232 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
233 # 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
|
234 # 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
|
235 # 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
|
236 # 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
|
237 # |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
238 # 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
|
239 # return func(*args, **kwargs) |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
240 |
|
8557
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
241 if extract: |
|
8449
756cdf8e34f2
fix: os.getlogin fails with OSError in CI.
John Rouillard <rouilj@ieee.org>
parents:
8448
diff
changeset
|
242 try: |
|
8557
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
243 reason = eval(extract, |
|
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
244 {"__builtin__": {}}, |
|
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
245 {"args": args, "kwargs": kwargs}) |
|
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
246 |
|
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
247 except (IndexError, KeyError, TypeError) as e: |
|
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
248 # extract usually looks something like: |
|
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
249 # "args[1]['PATH_INFO']" |
|
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
250 # report bad index/key/type |
|
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
251 logger.error( |
|
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
252 "Eval of extract('%(extract)s')@%(loc)s caused %(e)s" % |
|
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
253 {"e": repr(e), "extract": extract, "loc": location} |
|
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
254 ) |
|
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
255 |
|
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
256 # provide fallback hinting that it is an error |
|
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
257 reason = "error@call_location:%s" % location |
|
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
258 |
|
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
259 else: |
|
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
260 # if no extract, use location as reason |
|
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
261 # e.g. file://some_roundup-script |
|
8446
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
262 reason = location |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
263 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
264 if reason is None: |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
265 pass |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
266 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
|
267 # 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
|
268 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
|
269 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
|
270 # Throw an error we have mismatched REASON's which |
|
8557
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
271 # should never happen. It can happen if multiple |
|
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
272 # functions are decorated and they don't agree on |
|
f80c566f5726
feat: improve store_trace_reason with extract parameter
John Rouillard <rouilj@ieee.org>
parents:
8487
diff
changeset
|
273 # what the reason should be. see (*) above. |
|
8446
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
274 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
|
275 stored_reason, reason, location) |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
276 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
277 try: |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
278 r = func(*args, **kwargs) |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
279 finally: |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
280 # 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
|
281 # another request. |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
282 if prev_trace_reason: |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
283 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
|
284 return r |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
285 return wrapper |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
286 return decorator |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
287 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
288 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
289 def get_context_info(): |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
290 """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
|
291 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
292 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
|
293 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
294 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
295 #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
|
296 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
|
297 """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
|
298 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
|
299 |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
300 # 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
|
301 # |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
302 #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
|
303 # def decorator(func): |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
304 # return func |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
305 # return decorator |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
306 # |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
307 #def get_context_info(): |
|
14c7c07b32d8
feature: add thread local trace_id and trace_reason to logging.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
308 # 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
|
309 # ("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
|
310 #gen_trace_id = store_trace_reason = noop_decorator |
