def end_subsegment(end_time: nil)
      entity = current_entity
      return unless entity.is_a?(Subsegment)
      entity.close end_time: end_time
      # update current context
      if entity.parent.closed?
        context.clear!
      else
        context.store_entity entity: entity.parent
      end
      # check if the entire segment can be send.
      # If not, stream subsegments when threshold is reached.
      segment = entity.segment
      if segment.ready_to_send?
        emitter.send_entity entity: segment
      elsif streamer.eligible? segment: segment
        streamer.stream_subsegments root: segment, emitter: emitter
      end
    end
def end_subsegment(end_time: nil)
      entity = current_entity
      return unless entity.is_a?(Subsegment)
      entity.close end_time: end_time
      # update current context
      if entity.parent.closed?
        context.clear!
      else
        context.store_entity entity: entity.parent
      end
      # check if the entire segment can be send.
      # If not, stream subsegments when threshold is reached.
      segment = entity.segment
      if segment.ready_to_send?
        emitter.send_entity entity: segment
      elsif streamer.eligible? segment: segment
        streamer.stream_subsegments root: segment, emitter: emitter
      end
    end
End the current active subsegment. It also send the entire segment if
 this subsegment is the last one open or stream out subsegments of its
 parent segment if the stream threshold is breached.