Skip to content

Commit 8c3eea2

Browse files
authored
Merge pull request #70 from stat-kwon/master
Add thread_id in transaction
2 parents d1112f9 + ebfb59e commit 8c3eea2

1 file changed

Lines changed: 21 additions & 7 deletions

File tree

src/spaceone/core/transaction.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import threading
12
import traceback
23
import logging
34
from threading import local
45

56
from spaceone.core import utils, config
6-
from spaceone.core.error import *
77
from opentelemetry import trace
88
from opentelemetry.trace import format_trace_id
99
from opentelemetry.trace.span import TraceFlags
@@ -18,6 +18,8 @@
1818
class Transaction(object):
1919

2020
def __init__(self, resource: str = None, verb: str = None, trace_id: str = None, meta=None):
21+
self._id = None
22+
self._thread_id = str(threading.current_thread().ident)
2123
self._service = config.get_service()
2224
self._resource = resource
2325
self._verb = verb
@@ -45,6 +47,10 @@ def _set_meta(self, meta: dict = None):
4547
def id(self) -> str:
4648
return self._id
4749

50+
@property
51+
def thread_id(self) -> str:
52+
return self._thread_id
53+
4854
@property
4955
def service(self) -> str:
5056
return self._service
@@ -97,28 +103,36 @@ def notify_event(self, message):
97103
handler.notify(self, 'IN_PROGRESS', message)
98104

99105

100-
def get_transaction(trace_id: str = None, is_create: bool = True) -> [Transaction, None]:
106+
def get_transaction(is_create: bool = True) -> [Transaction, None]:
101107
current_span_context = trace.get_current_span().get_span_context()
108+
thread_id = str(threading.current_thread().ident)
102109

103110
if current_span_context.trace_flags == TraceFlags.SAMPLED:
104111
trace_id_from_current_span = format_trace_id(current_span_context.trace_id)
105112
return getattr(LOCAL_STORAGE, trace_id_from_current_span, None)
106-
elif trace_id:
107-
return getattr(LOCAL_STORAGE, trace_id, None)
113+
elif hasattr(LOCAL_STORAGE, thread_id):
114+
return getattr(LOCAL_STORAGE, thread_id, None)
108115
elif is_create:
109-
return create_transaction()
116+
return create_transaction(thread_id=thread_id)
110117
else:
111118
return None
112119

113120

114121
def create_transaction(resource: str = None, verb: str = None, trace_id: str = None,
115-
meta: dict = None) -> Transaction:
122+
meta: dict = None, thread_id: str = None) -> Transaction:
116123
transaction = Transaction(resource, verb, trace_id, meta)
117-
setattr(LOCAL_STORAGE, transaction.id, transaction)
124+
125+
if thread_id:
126+
setattr(LOCAL_STORAGE, thread_id, transaction)
127+
else:
128+
setattr(LOCAL_STORAGE, transaction.id, transaction)
129+
118130
return transaction
119131

120132

121133
def delete_transaction() -> None:
122134
if transaction := get_transaction(is_create=False):
123135
if hasattr(LOCAL_STORAGE, transaction.id):
124136
delattr(LOCAL_STORAGE, transaction.id)
137+
elif hasattr(LOCAL_STORAGE, transaction.thread_id):
138+
delattr(LOCAL_STORAGE, transaction.thread_id)

0 commit comments

Comments
 (0)