|
| 1 | +import threading |
1 | 2 | import traceback |
2 | 3 | import logging |
3 | 4 | from threading import local |
4 | 5 |
|
5 | 6 | from spaceone.core import utils, config |
6 | | -from spaceone.core.error import * |
7 | 7 | from opentelemetry import trace |
8 | 8 | from opentelemetry.trace import format_trace_id |
9 | 9 | from opentelemetry.trace.span import TraceFlags |
|
18 | 18 | class Transaction(object): |
19 | 19 |
|
20 | 20 | 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) |
21 | 23 | self._service = config.get_service() |
22 | 24 | self._resource = resource |
23 | 25 | self._verb = verb |
@@ -45,6 +47,10 @@ def _set_meta(self, meta: dict = None): |
45 | 47 | def id(self) -> str: |
46 | 48 | return self._id |
47 | 49 |
|
| 50 | + @property |
| 51 | + def thread_id(self) -> str: |
| 52 | + return self._thread_id |
| 53 | + |
48 | 54 | @property |
49 | 55 | def service(self) -> str: |
50 | 56 | return self._service |
@@ -97,28 +103,36 @@ def notify_event(self, message): |
97 | 103 | handler.notify(self, 'IN_PROGRESS', message) |
98 | 104 |
|
99 | 105 |
|
100 | | -def get_transaction(trace_id: str = None, is_create: bool = True) -> [Transaction, None]: |
| 106 | +def get_transaction(is_create: bool = True) -> [Transaction, None]: |
101 | 107 | current_span_context = trace.get_current_span().get_span_context() |
| 108 | + thread_id = str(threading.current_thread().ident) |
102 | 109 |
|
103 | 110 | if current_span_context.trace_flags == TraceFlags.SAMPLED: |
104 | 111 | trace_id_from_current_span = format_trace_id(current_span_context.trace_id) |
105 | 112 | 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) |
108 | 115 | elif is_create: |
109 | | - return create_transaction() |
| 116 | + return create_transaction(thread_id=thread_id) |
110 | 117 | else: |
111 | 118 | return None |
112 | 119 |
|
113 | 120 |
|
114 | 121 | 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: |
116 | 123 | 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 | + |
118 | 130 | return transaction |
119 | 131 |
|
120 | 132 |
|
121 | 133 | def delete_transaction() -> None: |
122 | 134 | if transaction := get_transaction(is_create=False): |
123 | 135 | if hasattr(LOCAL_STORAGE, transaction.id): |
124 | 136 | delattr(LOCAL_STORAGE, transaction.id) |
| 137 | + elif hasattr(LOCAL_STORAGE, transaction.thread_id): |
| 138 | + delattr(LOCAL_STORAGE, transaction.thread_id) |
0 commit comments