Skip to content

Commit

Permalink
feat: add basic attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-natan committed Apr 27, 2022
1 parent 9cc3e2c commit fc79f80
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
from opentelemetry.instrumentation.remoulade.package import _instruments
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.propagate import extract, inject
from opentelemetry.semconv.trace import SpanAttributes


_MESSAGE_TAG_KEY = "remoulade.action"
_MESSAGE_SEND = "send"
_MESSAGE_RUN = "run"

_MESSAGE_NAME_KEY = "remoulade.actor_name"


class InstrumentationMiddleware(Middleware):
Expand All @@ -35,6 +43,10 @@ def after_process_message(self, _broker, message, *, result=None, exception=None
return

if span.is_recording():
span.set_attribute(_MESSAGE_TAG_KEY, _MESSAGE_RUN)
# utils.set_attributes_from_context(span, kwargs)
# utils.set_attributes_from_context(span, task.request)
span.set_attribute(_MESSAGE_NAME_KEY, message.actor_name)
pass

activation.__exit__(None, None, None)
Expand All @@ -46,7 +58,10 @@ def before_enqueue(self, _broker, message, delay):
span = self._tracer.start_span(operation_name, kind=trace.SpanKind.PRODUCER)

if span.is_recording():
# span.set_attribute("TEST_ATTRIBUTE", "TEST_VALUE")
span.set_attribute(_MESSAGE_TAG_KEY, _MESSAGE_SEND)
span.set_attribute(SpanAttributes.MESSAGING_MESSAGE_ID, message.message_id)
span.set_attribute(_MESSAGE_NAME_KEY, message.actor_name)
# utils.set_attributes_from_context(span, kwargs)
pass

activation = trace.use_span(span, end_on_exit=True)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import remoulade
from remoulade.brokers.local import LocalBroker
from remoulade.results import Results
from remoulade.results.backends import LocalBackend
from opentelemetry.instrumentation.remoulade import RemouladeInstrumentor
from opentelemetry.test.test_base import TestBase
from opentelemetry.trace import SpanKind

from opentelemetry.semconv.trace import SpanAttributes


@remoulade.actor
Expand All @@ -19,17 +17,13 @@ def setUp(self):

broker = LocalBroker()

# Should a backend be added to wait for the result ?
# result_backend = LocalBackend()
# broker.add_middleware(Results(backend=result_backend))
remoulade.set_broker(broker)
RemouladeInstrumentor().instrument()

broker.declare_actor(actor_multiply)

def test_message(self):
message = actor_multiply.send(1, 2)
# result = message.result.get(block=True)
actor_multiply.send(1, 2)

spans = self.sorted_spans(self.memory_exporter.get_finished_spans())
self.assertEqual(len(spans), 2)
Expand All @@ -38,9 +32,23 @@ def test_message(self):

self.assertEqual(consumer.name, "remoulade/process")
self.assertEqual(consumer.kind, SpanKind.CONSUMER)
self.assertSpanHasAttributes(
consumer,
{
"remoulade.action": "run",
"remoulade.actor_name": "actor_multiply",
},
)

self.assertEqual(producer.name, "remoulade/send")
self.assertEqual(producer.kind, SpanKind.PRODUCER)
self.assertSpanHasAttributes(
producer,
{
"remoulade.action": "send",
"remoulade.actor_name": "actor_multiply",
},
)

self.assertNotEqual(consumer.parent, producer.context)
self.assertEqual(consumer.parent.span_id, producer.context.span_id)
Expand Down

0 comments on commit fc79f80

Please sign in to comment.