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