class SidecarRecorder(Recorder): """Interface for recording spans to a POSIX message queue. The SidecarRecorder serializes spans to a string representation before adding them to the queue. """ def __init__(self, queue_name: str): self.queue = MessageQueue( "/traces-" + queue_name, max_messages=MAX_SIDECAR_QUEUE_SIZE, max_message_size=MAX_SIDECAR_MESSAGE_SIZE, ) def send(self, span: TraceSpanObserver) -> None: # Don't raise exceptions from here. This is called in the # request/response path and should finish cleanly. serialized_str = json.dumps(span._serialize()).encode("utf8") if len(serialized_str) > MAX_SIDECAR_MESSAGE_SIZE: logger.warning( "Trace too big. Traces published to %s are not allowed to be larger " "than %d bytes. Received trace is %d bytes. This can be caused by " "an excess amount of tags or a large amount of child spans.", self.queue.queue.name, MAX_SIDECAR_MESSAGE_SIZE, len(serialized_str), ) try: self.queue.put(serialized_str, timeout=0) except TimedOutError: logger.warning("Trace queue %s is full. Is trace sidecar healthy?", self.queue.queue.name)
class EventQueue(ContextFactory, config.Parser, Generic[T]): """A queue to transfer events to the publisher. :param name: The name of the event queue to send to. This specifies which publisher should send the events which can be useful for routing to different event pipelines (prod/test/v2 etc.). :param event_serializer: A callable that takes an event object and returns serialized bytes ready to send on the wire. See below for options. """ def __init__(self, name: str, event_serializer: Callable[[T], bytes]): self.queue = MessageQueue("/events-" + name, max_messages=MAX_QUEUE_SIZE, max_message_size=MAX_EVENT_SIZE) self.serialize_event = event_serializer def put(self, event: T) -> None: """Add an event to the queue. The queue is local to the server this code is run on. The event publisher on the server will take these events and send them to the collector. :param event: The event to send. The type of event object passed in depends on the selected ``event_serializer``. :raises: :py:exc:`EventTooLargeError` The serialized event is too large. :raises: :py:exc:`EventQueueFullError` The queue is full. Events are not being published fast enough. """ serialized = self.serialize_event(event) if len(serialized) > MAX_EVENT_SIZE: raise EventTooLargeError(len(serialized)) try: self.queue.put(serialized, timeout=0) except TimedOutError: raise EventQueueFullError def make_object_for_context(self, name: str, span: Span) -> "EventQueue[T]": return self def parse(self, key_path: str, raw_config: config.RawConfig) -> "EventQueue[T]": return self