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