Skip to content

Commit 53698aa

Browse files
committed
feat: add retries and retries tests
1 parent 8eab917 commit 53698aa

File tree

2 files changed

+54
-11
lines changed

2 files changed

+54
-11
lines changed

instrumentation/opentelemetry-instrumentation-remoulade/src/opentelemetry/instrumentation/remoulade/__init__.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,15 @@ def __init__(self, _tracer):
2626

2727
def before_process_message(self, _broker, message):
2828
trace_ctx = extract(message.options["trace_ctx"])
29-
operation_name = "remoulade/process"
29+
retry_count = message.options.get("retries")
30+
31+
operation_name = "remoulade/process" if retry_count is None else f"remoulade/process(retry-{retry_count})"
3032

3133
span = self._tracer.start_span(operation_name, kind=trace.SpanKind.CONSUMER, context=trace_ctx)
3234

35+
if retry_count is not None:
36+
span.set_attribute("retry_count", retry_count)
37+
3338
activation = trace.use_span(span, end_on_exit=True)
3439
activation.__enter__()
3540

@@ -44,24 +49,26 @@ def after_process_message(self, _broker, message, *, result=None, exception=None
4449

4550
if span.is_recording():
4651
span.set_attribute(_MESSAGE_TAG_KEY, _MESSAGE_RUN)
47-
# utils.set_attributes_from_context(span, kwargs)
48-
# utils.set_attributes_from_context(span, task.request)
4952
span.set_attribute(_MESSAGE_NAME_KEY, message.actor_name)
5053
pass
5154

5255
activation.__exit__(None, None, None)
5356
utils.detach_span(self._span_registry, message.message_id)
5457

5558
def before_enqueue(self, _broker, message, delay):
56-
operation_name = "remoulade/send"
59+
retry_count = message.options.get("retries")
60+
61+
operation_name = "remoulade/send" if retry_count is None else f"remoulade/send(retry-{retry_count})"
5762

5863
span = self._tracer.start_span(operation_name, kind=trace.SpanKind.PRODUCER)
5964

65+
if retry_count is not None:
66+
span.set_attribute("retry_count", retry_count)
67+
6068
if span.is_recording():
6169
span.set_attribute(_MESSAGE_TAG_KEY, _MESSAGE_SEND)
6270
span.set_attribute(SpanAttributes.MESSAGING_MESSAGE_ID, message.message_id)
6371
span.set_attribute(_MESSAGE_NAME_KEY, message.actor_name)
64-
# utils.set_attributes_from_context(span, kwargs)
6572
pass
6673

6774
activation = trace.use_span(span, end_on_exit=True)

instrumentation/opentelemetry-instrumentation-remoulade/tests/test_messages.py

+42-6
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
from opentelemetry.semconv.trace import SpanAttributes
77

88

9-
@remoulade.actor
10-
def actor_multiply(x, y):
11-
return x * y
9+
@remoulade.actor(max_retries=3)
10+
def actor_div(x, y):
11+
return x / y
1212

1313

1414
class TestRemouladeInstrumentation(TestBase):
@@ -20,10 +20,10 @@ def setUp(self):
2020
remoulade.set_broker(broker)
2121
RemouladeInstrumentor().instrument()
2222

23-
broker.declare_actor(actor_multiply)
23+
broker.declare_actor(actor_div)
2424

2525
def test_message(self):
26-
actor_multiply.send(1, 2)
26+
actor_div.send(2, 3)
2727

2828
spans = self.sorted_spans(self.memory_exporter.get_finished_spans())
2929
self.assertEqual(len(spans), 2)
@@ -52,4 +52,40 @@ def test_message(self):
5252

5353
self.assertNotEqual(consumer.parent, producer.context)
5454
self.assertEqual(consumer.parent.span_id, producer.context.span_id)
55-
self.assertEqual(consumer.context.trace_id, producer.context.trace_id)
55+
self.assertEqual(consumer.context.trace_id, producer.context.trace_id)
56+
57+
def test_retries(self):
58+
try:
59+
actor_div.send(1, 0)
60+
except ZeroDivisionError:
61+
pass
62+
63+
spans = self.sorted_spans(self.memory_exporter.get_finished_spans())
64+
self.assertEqual(len(spans), 8)
65+
66+
consumer_spans = spans[::2]
67+
producer_spans = spans[1::2]
68+
69+
self.assertEqual(consumer_spans[0].name, "remoulade/process(retry-3)")
70+
self.assertSpanHasAttributes(
71+
consumer_spans[0],
72+
{ "retry_count": 3 }
73+
)
74+
self.assertEqual(consumer_spans[1].name, "remoulade/process(retry-2)")
75+
self.assertSpanHasAttributes(
76+
consumer_spans[1],
77+
{"retry_count": 2}
78+
)
79+
self.assertEqual(consumer_spans[3].name, "remoulade/process")
80+
81+
self.assertEqual(producer_spans[0].name, "remoulade/send(retry-3)")
82+
self.assertSpanHasAttributes(
83+
producer_spans[0],
84+
{"retry_count": 3}
85+
)
86+
self.assertEqual(producer_spans[1].name, "remoulade/send(retry-2)")
87+
self.assertSpanHasAttributes(
88+
producer_spans[1],
89+
{"retry_count": 2}
90+
)
91+
self.assertEqual(producer_spans[3].name, "remoulade/send")

0 commit comments

Comments
 (0)