# Loop in Logstash with ruby code

**URL:** https://discuss.elastic.co/t/loop-in-logstash-with-ruby-code/55404
**Category:** Logstash
**Created:** [July 13, 2016, 12:40pm UTC](https://discuss.elastic.co/t/loop-in-logstash-with-ruby-code/55404 "2016-07-13T12:40:17Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Jeff\_Contour](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@Jeff\_Contour](https://discuss.elastic.co/u/Jeff_Contour)
#### Post date: [July 13, 2016, 12:40pm UTC](https://discuss.elastic.co/t/loop-in-logstash-with-ruby-code/55404/1 "2016-07-13T12:40:17Z")

</div>

Hi there  
I hope someone can give me some advice for this.

I have a portion of ruby code that makes Logstash looping (CPU = 100 %, no more indexing in Elasticsearch).  
I try to index about 400 000 events and I see the indexing throuput gradually getting down (about half events eventually indexed)  
I thought the loop was inside the code so I added a new field "elapsed\_time\_in\_Ruby". And It appears the loop is not inside Ruby itself

Any idea would be very much appreciated  
Logstash 2.3.3

---

<div class="post-metadata">

### Author: ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)
#### Post date: [July 13, 2016, 1:20pm UTC](https://discuss.elastic.co/t/loop-in-logstash-with-ruby-code/55404/2 "2016-07-13T13:20:54Z")

</div>

Without seeing your configuration it's impossible to give advice.

---

<div class="post-metadata">

### Author: ![Jeff\_Contour](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@Jeff\_Contour](https://discuss.elastic.co/u/Jeff_Contour)
#### Post date: [July 14, 2016, 8:50pm UTC](https://discuss.elastic.co/t/loop-in-logstash-with-ruby-code/55404/3 "2016-07-14T20:50:09Z")

</div>

I guess you mean the ruby code (and the .conf file) I'll give it to you  
next Monday (Bastille day here.)

---

<div class="post-metadata">

### Author: ![Jeff\_Contour](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@Jeff\_Contour](https://discuss.elastic.co/u/Jeff_Contour)
#### Post date: [July 18, 2016, 9:41am UTC](https://discuss.elastic.co/t/loop-in-logstash-with-ruby-code/55404/5 "2016-07-18T09:41:49Z")

</div>

`The loop appears when I add the following lines of code :  
(trying to catch couples of (key, value) with a separator (🙂 between key and value and at least to white spaces between couples. For example :  
my key1 : value 1 my key 2 : value 2 my key 3 : value 3  
expected result : (my\_key1, value 1), (my\_key\_2, value 2), (my\_key\_3, value 3)

When I remove this code it's fine : [full conf.file](https://docs.google.com/document/d/1jI9eU1jN1pcXLgA_MNM368g4AxME-t_mlvQAF9z7pt0/edit?usp=drive_web)

```
# Fonction permettant de récupérer un nom de clé nettoyé et d'autre part la string d'entrée amputée de sa première partie (la clé et le ':" remplacés par |) 
def getonekey(s)
# la clé est constituée de chaînes de caractères (\S+) séparées par au plus un espace et 'à gauche' du séparateur (:). Voir http://rubular.com/ pour les regexp Ruby 
  pattern = '\S+(\s?\S+)+(\s)*:'
# le match renvoie la clé dans le premier élément
  key = s.match(pattern)[0]
# remplacement de la clé complète par un pipe   
s = s.gsub(key,'|')
# cleansing de la clé    
  key = key.gsub(':','').strip.tr(' ','_').tr('.','_').downcase
#
  return[s,key]
end

.... (ruby code before (ok))

if (nbre_separateurs > 1)
            patternko1 = ':\s*:'
            if (v.match(patternko1).to_s.empty?)
              s = v
              keylist = Array.new
              begin
                for i in (1..nbre_separateurs) do
                  res = getonekey(s)
                  s = res[0]
                  keylist.push(res[1])
                end
                vallist = s.split('|')
                for i in (1..nbre_separateurs) do
                  if (event.to_hash.include?(keylist[i-1]))
                    keylist[i-1].concat('_').concat(column_number.to_s)
                  end
                  if (!vallist[i].to_s.empty?) 
                     event[keylist[i-1]] = vallist[i].strip
                  end
                end
              end
            else
               (event['tags'] ||= []) << 'pb parsing patternko1 ligne '.concat(column_number.to_s)
            end
`
```

---

<div class="post-metadata">

### Author: ![Jeff\_Contour](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@Jeff\_Contour](https://discuss.elastic.co/u/Jeff_Contour)
#### Post date: [July 18, 2016, 9:42am UTC](https://discuss.elastic.co/t/loop-in-logstash-with-ruby-code/55404/6 "2016-07-18T09:42:35Z")

</div>

separator = ":"

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [July 6, 2017, 4:47am UTC](https://discuss.elastic.co/t/loop-in-logstash-with-ruby-code/55404/7 "2017-07-06T04:47:36Z")

</div>


