-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathotel_tracing.py
More file actions
227 lines (187 loc) · 7.74 KB
/
otel_tracing.py
File metadata and controls
227 lines (187 loc) · 7.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
from __future__ import annotations
import functools
from collections.abc import Callable
from opentelemetry import trace
from opentelemetry.context import Context
from opentelemetry.propagate import extract, inject
from workflows.transport.common_transport import MessageCallback, TemporarySubscription
class OTELTracingMiddleware:
def __init__(self, tracer: trace.Tracer, service_name: str):
self.tracer = tracer
self.service_name = service_name
def _set_span_attributes(self, span, **attributes):
"""Helper method to set common span attributes"""
span.set_attribute("service_name", self.service_name)
for key, value in attributes.items():
if value is not None:
span.set_attribute(key, value)
def send(self, call_next: Callable, destination: str, message, **kwargs):
# Get current span context (may be None if this is the root span)
current_span = trace.get_current_span()
parent_context = (
trace.set_span_in_context(current_span) if current_span else None
)
with self.tracer.start_as_current_span(
"transport.send",
context=parent_context,
) as span:
self._set_span_attributes(span, destination=destination)
# Inject the current trace context into the message headers
headers = kwargs.get("headers", {})
if headers is None:
headers = {}
inject(headers) # This modifies headers in-place
kwargs["headers"] = headers
return call_next(destination, message, **kwargs)
def subscribe(
self, call_next: Callable, channel: str, callback: Callable, **kwargs
) -> int:
@functools.wraps(callback)
def wrapped_callback(header, message):
# Extract trace context from message headers
ctx = extract(header) if header else Context()
# Start a new span with the extracted context
with self.tracer.start_as_current_span(
"transport.subscribe",
context=ctx,
) as span:
self._set_span_attributes(span, channel=channel)
# Call the original callback - this will process the message
# and potentially call send() which will pick up this context
return callback(header, message)
return call_next(channel, wrapped_callback, **kwargs)
def subscribe_broadcast(
self, call_next: Callable, channel: str, callback: Callable, **kwargs
) -> int:
@functools.wraps(callback)
def wrapped_callback(header, message):
# Extract trace context from message headers
ctx = extract(header) if header else Context()
# Start a new span with the extracted context
with self.tracer.start_as_current_span(
"transport.subscribe_broadcast",
context=ctx,
) as span:
self._set_span_attributes(span, channel=channel)
return callback(header, message)
return call_next(channel, wrapped_callback, **kwargs)
def subscribe_temporary(
self,
call_next: Callable,
channel_hint: str | None,
callback: MessageCallback,
**kwargs,
) -> TemporarySubscription:
@functools.wraps(callback)
def wrapped_callback(header, message):
# Extract trace context from message headers
ctx = extract(header) if header else Context()
# Start a new span with the extracted context
with self.tracer.start_as_current_span(
"transport.subscribe_temporary",
context=ctx,
) as span:
self._set_span_attributes(span, channel_hint=channel_hint)
return callback(header, message)
return call_next(channel_hint, wrapped_callback, **kwargs)
def unsubscribe(
self,
call_next: Callable,
subscription: int,
drop_callback_reference=False,
**kwargs,
):
# Get current span context
current_span = trace.get_current_span()
current_context = (
trace.set_span_in_context(current_span) if current_span else Context()
)
with self.tracer.start_as_current_span(
"transport.unsubscribe",
context=current_context,
) as span:
self._set_span_attributes(span, subscription_id=subscription)
call_next(
subscription, drop_callback_reference=drop_callback_reference, **kwargs
)
def ack(
self,
call_next: Callable,
message,
subscription_id: int | None = None,
**kwargs,
):
# Get current span context
current_span = trace.get_current_span()
current_context = (
trace.set_span_in_context(current_span) if current_span else Context()
)
with self.tracer.start_as_current_span(
"transport.ack",
context=current_context,
) as span:
self._set_span_attributes(span, subscription_id=subscription_id)
call_next(message, subscription_id=subscription_id, **kwargs)
def nack(
self,
call_next: Callable,
message,
subscription_id: int | None = None,
**kwargs,
):
# Get current span context
current_span = trace.get_current_span()
current_context = (
trace.set_span_in_context(current_span) if current_span else Context()
)
with self.tracer.start_as_current_span(
"transport.nack",
context=current_context,
) as span:
self._set_span_attributes(span, subscription_id=subscription_id)
call_next(message, subscription_id=subscription_id, **kwargs)
def transaction_begin(
self, call_next: Callable, subscription_id: int | None = None, **kwargs
) -> int:
"""Start a new transaction span"""
# Get current span context (may be None if this is the root span)
current_span = trace.get_current_span()
current_context = (
trace.set_span_in_context(current_span) if current_span else Context()
)
with self.tracer.start_as_current_span(
"transaction.begin",
context=current_context,
) as span:
self._set_span_attributes(span, subscription_id=subscription_id)
return call_next(subscription_id=subscription_id, **kwargs)
def transaction_abort(
self, call_next: Callable, transaction_id: int | None = None, **kwargs
):
"""Abort a transaction span"""
# Get current span context
current_span = trace.get_current_span()
current_context = (
trace.set_span_in_context(current_span) if current_span else Context()
)
with self.tracer.start_as_current_span(
"transaction.abort",
context=current_context,
) as span:
self._set_span_attributes(span, transaction_id=transaction_id)
call_next(transaction_id=transaction_id, **kwargs)
def transaction_commit(
self, call_next: Callable, transaction_id: int | None = None, **kwargs
):
"""Commit a transaction span"""
# Get current span context
current_span = trace.get_current_span()
current_context = (
trace.set_span_in_context(current_span) if current_span else Context()
)
with self.tracer.start_as_current_span(
"transaction.commit",
context=current_context,
) as span:
self._set_span_attributes(span, transaction_id=transaction_id)
call_next(transaction_id=transaction_id, **kwargs)