Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
ska-logging
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Jira
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
LOFAR2.0
ska-logging
Commits
03a598dd
Unverified
Commit
03a598dd
authored
4 years ago
by
SKAJohanVenter
Browse files
Options
Downloads
Patches
Plain Diff
SAR-150
Updated the use of thread_local
parent
ee5b7175
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/ska/logging/transactions.py
+15
-27
15 additions, 27 deletions
src/ska/logging/transactions.py
with
15 additions
and
27 deletions
src/ska/logging/transactions.py
+
15
−
27
View file @
03a598dd
...
...
@@ -21,15 +21,13 @@ class TransactionIDTagsFilter(logging.Filter):
"""
def
get_transaction_id
(
self
):
if
hasattr
(
thread_local_data
,
"
transaction_ids
"
):
thread_id
=
threading
.
get_ident
()
return
thread_local_data
.
transaction_ids
.
get
(
thread_id
,
None
)
if
hasattr
(
thread_local_data
,
"
transaction_id
"
):
return
thread_local_data
.
transaction_id
return
None
def
get_frame
(
self
):
if
hasattr
(
thread_local_data
,
"
frames
"
):
thread_id
=
threading
.
get_ident
()
return
thread_local_data
.
frames
.
get
(
thread_id
,
None
)
def
get_caller_frame
(
self
):
if
hasattr
(
thread_local_data
,
"
caller_frame
"
):
return
thread_local_data
.
caller_frame
return
None
def
filter
(
self
,
record
):
...
...
@@ -47,15 +45,15 @@ class TransactionIDTagsFilter(logging.Filter):
# difficult to debug where the `Enter` and `Exit` of a transaction log message
# originated.
# From Python 3.8 we should rather use `stacklevel`
frame
=
self
.
get_frame
()
if
frame
:
caller_
frame
=
self
.
get_
caller_
frame
()
if
caller_
frame
:
if
record
.
pathname
==
__file__
and
record
.
funcName
in
[
"
__enter__
"
,
"
__exit__
"
,
]:
record
.
filename
=
os
.
path
.
basename
(
frame
.
filename
)
record
.
lineno
=
frame
.
lineno
record
.
funcName
=
frame
.
function
record
.
filename
=
os
.
path
.
basename
(
caller_
frame
.
filename
)
record
.
lineno
=
caller_
frame
.
lineno
record
.
funcName
=
caller_
frame
.
function
return
True
...
...
@@ -163,7 +161,7 @@ class Transaction:
self
.
_transaction_id_key
=
transaction_id_key
self
.
_transaction_id
=
self
.
_get_id_from_params_or_generate_new_id
(
transaction_id
)
self
.
_frame
=
inspect
.
stack
()[
1
]
self
.
_
caller_
frame
=
inspect
.
stack
()[
1
]
if
transaction_id
and
params
.
get
(
self
.
_transaction_id_key
):
self
.
logger
.
info
(
...
...
@@ -177,22 +175,12 @@ class Transaction:
self
.
_transaction_filter
=
TransactionIDTagsFilter
()
def
store_thread_data
(
self
):
thread_id
=
threading
.
get_ident
()
if
not
hasattr
(
thread_local_data
,
"
transaction_ids
"
):
thread_local_data
.
transaction_ids
=
{}
if
not
hasattr
(
thread_local_data
,
"
frames
"
):
thread_local_data
.
frames
=
{}
thread_local_data
.
transaction_ids
[
thread_id
]
=
self
.
_transaction_id
thread_local_data
.
frames
[
thread_id
]
=
self
.
_frame
thread_local_data
.
transaction_id
=
self
.
_transaction_id
thread_local_data
.
caller_frame
=
self
.
_caller_frame
def
clear_thread_data
(
self
):
thread_id
=
threading
.
get_ident
()
if
hasattr
(
thread_local_data
,
"
transaction_ids
"
):
if
thread_id
in
thread_local_data
.
transaction_ids
:
del
thread_local_data
.
transaction_ids
[
thread_id
]
if
hasattr
(
thread_local_data
,
"
frames
"
):
if
thread_id
in
thread_local_data
.
frames
:
del
thread_local_data
.
frames
[
thread_id
]
thread_local_data
.
transaction_id
=
None
thread_local_data
.
caller_frame
=
None
def
__enter__
(
self
):
self
.
store_thread_data
()
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment