Skip to content

Commit 33fbce2

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 786bd00 commit 33fbce2

File tree

5 files changed

+64
-34
lines changed

5 files changed

+64
-34
lines changed

src/workflows/recipe/__init__.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from __future__ import annotations
2-
from contextlib import ExitStack
2+
33
import functools
44
import logging
55
from collections.abc import Callable
6+
from contextlib import ExitStack
67
from typing import Any
78

89
from opentelemetry import trace
@@ -80,8 +81,6 @@ def unwrap_recipe(header, message):
8081
if recipe_id:
8182
span.set_attribute("recipe_id", recipe_id)
8283

83-
84-
8584
# Extract span_id and trace_id for logging
8685
span_context = span.get_span_context()
8786
if span_context and span_context.is_valid:
@@ -92,21 +91,29 @@ def unwrap_recipe(header, message):
9291
"span_id": span_id,
9392
"trace_id": trace_id,
9493
}
95-
94+
9695
if recipe_id:
9796
otel_logs["recipe_id"] = recipe_id
98-
97+
9998
with ExitStack() as stack:
10099
# Configure the context depending on if service is emitting spans
101-
if otel_logs and log_extender and rw.environment and rw.environment.get("ID"):
102-
stack.enter_context(log_extender('recipe_ID', rw.environment.get("ID")))
103-
stack.enter_context(log_extender('otel_logs', otel_logs))
100+
if (
101+
otel_logs
102+
and log_extender
103+
and rw.environment
104+
and rw.environment.get("ID")
105+
):
106+
stack.enter_context(
107+
log_extender("recipe_ID", rw.environment.get("ID"))
108+
)
109+
stack.enter_context(log_extender("otel_logs", otel_logs))
104110
elif log_extender and rw.environment and rw.environment.get("ID"):
105-
stack.enter_context(log_extender('recipe_ID', rw.environment.get("ID")))
111+
stack.enter_context(
112+
log_extender("recipe_ID", rw.environment.get("ID"))
113+
)
106114

107115
return callback(rw, header, message.get("payload"))
108-
109-
116+
110117
if allow_non_recipe_messages:
111118
return callback(None, header, message)
112119
# self.log.warning('Discarding non-recipe message:\n' + \

src/workflows/services/common_service.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,12 @@ def start_transport(self):
192192
self.transport.subscription_callback_set_intercept(
193193
self._transport_interceptor
194194
)
195-
195+
196196
# Configure OTELTracing if configuration is available
197197
otel_config = (
198-
self.config._opentelemetry if self.config and hasattr(self.config, "opentelemetry") else None
198+
self.config._opentelemetry
199+
if self.config and hasattr(self.config, "opentelemetry")
200+
else None
199201
)
200202
if otel_config:
201203
# Configure OTELTracing

src/workflows/transport/common_transport.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
MessageCallback = Callable[[Mapping[str, Any], Any], None]
1212

13+
1314
class TemporarySubscription(NamedTuple):
1415
subscription_id: int
1516
queue_name: str

src/workflows/transport/middleware/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ def wrap(f: Callable):
236236
# debugging
237237
if f.__name__ == "send":
238238
print("we are wrapping send now")
239-
239+
240240
@functools.wraps(f)
241241
def wrapper(self, *args, **kwargs):
242242
return functools.reduce(
@@ -248,4 +248,4 @@ def wrapper(self, *args, **kwargs):
248248
)(*args, **kwargs)
249249

250250
print(wrapper.__wrapped__)
251-
return wrapper
251+
return wrapper

src/workflows/transport/middleware/otel_tracing.py

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
from __future__ import annotations
22

33
import functools
4+
import json
45
from collections.abc import Callable
56

67
from opentelemetry import trace
7-
from opentelemetry.propagate import extract, inject
88
from opentelemetry.context import Context
9+
from opentelemetry.propagate import extract, inject
10+
11+
from workflows.transport.common_transport import MessageCallback, TemporarySubscription
912

10-
from workflows.transport.middleware import BaseTransportMiddleware
11-
from workflows.transport.common_transport import TemporarySubscription, MessageCallback
12-
import json
1313

1414
class OTELTracingMiddleware:
1515
def __init__(self, tracer: trace.Tracer, service_name: str):
@@ -19,7 +19,9 @@ def __init__(self, tracer: trace.Tracer, service_name: str):
1919
def send(self, call_next: Callable, destination: str, message: Any, **kwargs):
2020
# Get current span context (may be None if this is the root span)
2121
current_span = trace.get_current_span()
22-
parent_context = trace.set_span_in_context(current_span) if current_span else None
22+
parent_context = (
23+
trace.set_span_in_context(current_span) if current_span else None
24+
)
2325

2426
with self.tracer.start_as_current_span(
2527
"transport.send",
@@ -29,8 +31,7 @@ def send(self, call_next: Callable, destination: str, message: Any, **kwargs):
2931

3032
span.set_attribute("message", json.dumps(message))
3133
span.set_attribute("destination", destination)
32-
print("parent_context is...",parent_context)
33-
34+
print("parent_context is...", parent_context)
3435

3536
# Inject the current trace context into the message headers
3637
headers = kwargs.get("headers", {})
@@ -41,7 +42,9 @@ def send(self, call_next: Callable, destination: str, message: Any, **kwargs):
4142

4243
return call_next(destination, message, **kwargs)
4344

44-
def subscribe(self, call_next: Callable, channel: str, callback: Callable, **kwargs) -> int:
45+
def subscribe(
46+
self, call_next: Callable, channel: str, callback: Callable, **kwargs
47+
) -> int:
4548
@functools.wraps(callback)
4649
def wrapped_callback(header, message):
4750
# Extract trace context from message headers
@@ -63,13 +66,15 @@ def wrapped_callback(header, message):
6366

6467
return call_next(channel, wrapped_callback, **kwargs)
6568

66-
def subscribe_broadcast(self, call_next: Callable, channel: str, callback: Callable, **kwargs) -> int:
69+
def subscribe_broadcast(
70+
self, call_next: Callable, channel: str, callback: Callable, **kwargs
71+
) -> int:
6772
@functools.wraps(callback)
6873
def wrapped_callback(header, message):
6974
# Extract trace context from message headers
7075
ctx = extract(header) if header else Context()
7176

72-
# # Start a new span with the extracted context
77+
# # Start a new span with the extracted context
7378
with self.tracer.start_as_current_span(
7479
"transport.subscribe_broadcast",
7580
context=ctx,
@@ -119,7 +124,9 @@ def unsubscribe(
119124
):
120125
# Get current span context
121126
current_span = trace.get_current_span()
122-
current_context = trace.set_span_in_context(current_span) if current_span else Context()
127+
current_context = (
128+
trace.set_span_in_context(current_span) if current_span else Context()
129+
)
123130

124131
with self.tracer.start_as_current_span(
125132
"transport.unsubscribe",
@@ -141,7 +148,9 @@ def ack(
141148
):
142149
# Get current span context
143150
current_span = trace.get_current_span()
144-
current_context = trace.set_span_in_context(current_span) if current_span else Context()
151+
current_context = (
152+
trace.set_span_in_context(current_span) if current_span else Context()
153+
)
145154

146155
with self.tracer.start_as_current_span(
147156
"transport.ack",
@@ -163,7 +172,9 @@ def nack(
163172
):
164173
# Get current span context
165174
current_span = trace.get_current_span()
166-
current_context = trace.set_span_in_context(current_span) if current_span else Context()
175+
current_context = (
176+
trace.set_span_in_context(current_span) if current_span else Context()
177+
)
167178

168179
with self.tracer.start_as_current_span(
169180
"transport.nack",
@@ -183,7 +194,9 @@ def transaction_begin(
183194
"""Start a new transaction span"""
184195
# Get current span context (may be None if this is the root span)
185196
current_span = trace.get_current_span()
186-
current_context = trace.set_span_in_context(current_span) if current_span else Context()
197+
current_context = (
198+
trace.set_span_in_context(current_span) if current_span else Context()
199+
)
187200

188201
with self.tracer.start_as_current_span(
189202
"transaction.begin",
@@ -196,11 +209,15 @@ def transaction_begin(
196209

197210
return call_next(subscription_id=subscription_id, **kwargs)
198211

199-
def transaction_abort(self, call_next: Callable, transaction_id: int | None = None, **kwargs):
212+
def transaction_abort(
213+
self, call_next: Callable, transaction_id: int | None = None, **kwargs
214+
):
200215
"""Abort a transaction span"""
201216
# Get current span context
202217
current_span = trace.get_current_span()
203-
current_context = trace.set_span_in_context(current_span) if current_span else Context()
218+
current_context = (
219+
trace.set_span_in_context(current_span) if current_span else Context()
220+
)
204221

205222
with self.tracer.start_as_current_span(
206223
"transaction.abort",
@@ -213,11 +230,15 @@ def transaction_abort(self, call_next: Callable, transaction_id: int | None = No
213230

214231
call_next(transaction_id=transaction_id, **kwargs)
215232

216-
def transaction_commit(self, call_next: Callable, transaction_id: int | None = None, **kwargs):
233+
def transaction_commit(
234+
self, call_next: Callable, transaction_id: int | None = None, **kwargs
235+
):
217236
"""Commit a transaction span"""
218237
# Get current span context
219238
current_span = trace.get_current_span()
220-
current_context = trace.set_span_in_context(current_span) if current_span else Context()
239+
current_context = (
240+
trace.set_span_in_context(current_span) if current_span else Context()
241+
)
221242

222243
with self.tracer.start_as_current_span(
223244
"transaction.commit",
@@ -228,4 +249,3 @@ def transaction_commit(self, call_next: Callable, transaction_id: int | None = N
228249
span.set_attribute("transaction_id", transaction_id)
229250

230251
call_next(transaction_id=transaction_id, **kwargs)
231-

0 commit comments

Comments
 (0)